How to get the name of the all class instances at once

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get the name of the all class instances at once

Eunice Palmeira
Hello,
   I use JessTab but I didn't found a function to return the name of the all
class instances at once.
   I would like to get the name of the all class instances, called Test for
example.
  Somebody help me, plese?

   []'s Eunice

-------------------------------------------------------------------------
To unsubscribe go to http://protege.stanford.edu/community/subscribe.html

Reply | Threaded
Open this post in threaded view
|

Re: How to get the name of the all class instances at once

Dona Mommsen

Hi Eunice,

(usually you have better chances to get answers to Jess-specific  
questions on the Jess mailing list
http://www.jessrules.com/jess/mailing_list.shtml)

  The following function returns a list of all instances of class Test:

(find-all-instances ((?class Test)) TRUE)

Note that the result is a list of Protege instances
(<Java-Object:edu.stanford.smi.protege.model.DefaultSimpleInstance>)

To access slots of the result use JessTab function (slot-get object  
slot), where the slot would be :NAME

If you just want to print the names, you can directly use:
(do-for-all-instances ((?cl TEST)) TRUE (printout t (slot-get ?cl  
:NAME) crlf))

For instance-set queries such as find-all-instances or  
do-for-all-instances it is worth to download the CLIPS manual (section  
9.7 is only a few pages)

http://www.ghg.net/clips/download/documentation/bpg.pdf


There is a second option to write your own query and function
(This is modified from a very old post by Samson Tu on the Jess mailing  
list, can't find the link at the moment...):

(defquery findObj-by-class
     (declare (variables ?class))
     (object (is-a ?class)))

This query returns an Iterator  
<Java-Object:java.util.AbstractList$Itr>, and NOT a list:
(run-query findObj-by-class Test)

The following function calls the query and transforms the resulting  
Iterator into a list:

(deffunction findObj (?class)
     "This function returns a list, but the items are NOT Protege  
Instances, but mapped facts.
     Thus slot-get does not work for these facts, but fact-slot-value,  
see JiA p. 129"
     (bind ?list nil)
     (bind ?it (run-query findObj-by-class ?class))
     (while (?it hasNext)
         (bind ?token (call ?it next))
         (bind ?fact (call ?token fact 1))
         (bind ?list (create$ ?list ?fact)))
     (return (rest$ ?list)))

Note: This returns a list with mapped facts:  
(<Java-Object:JessTab.DefaultMappedFact>)
To access the slots of the result use Jess function (fact-slot-value  
object slot).
Again, the JessTab function (slot-get object slot) does not work for  
<Java-Object:JessTab.DefaultMappedFact>

HTH

Dona

On Feb 1, 2006, at 7:25 AM, Eunice Palmeira wrote:

> Hello,
>    I use JessTab but I didn't found a function to return the name of  
> the all
> class instances at once.
>    I would like to get the name of the all class instances, called  
> Test for
> example.
>   Somebody help me, plese?
>
>    []'s Eunice
>
> -----------------------------------------------------------------------
> --
> To unsubscribe go to  
> http://protege.stanford.edu/community/subscribe.html
>

-------------------------------------------------------------------------
To unsubscribe go to http://protege.stanford.edu/community/subscribe.html

Reply | Threaded
Open this post in threaded view
|

Re: How to get the name of the all class instances at once

samsontu


Note that find-all-instances, do-for-all-instances are JessTab
functions, not Jess. "defquery" is Jess. (I think Jess7.0 has additional
query functions, but I don't remember the details.)

sAmson

Dona Mommsen wrote:

> Hi Eunice,
>
> (usually you have better chances to get answers to Jess-specific  
> questions on the Jess mailing list
> http://www.jessrules.com/jess/mailing_list.shtml)
>
>   The following function returns a list of all instances of class Test:
>
> (find-all-instances ((?class Test)) TRUE)
>
> Note that the result is a list of Protege instances
> (<Java-Object:edu.stanford.smi.protege.model.DefaultSimpleInstance>)
>
> To access slots of the result use JessTab function (slot-get object  
> slot), where the slot would be :NAME
>
> If you just want to print the names, you can directly use:
> (do-for-all-instances ((?cl TEST)) TRUE (printout t (slot-get ?cl  
> :NAME) crlf))
>
> For instance-set queries such as find-all-instances or  
> do-for-all-instances it is worth to download the CLIPS manual (section  
> 9.7 is only a few pages)
>
> http://www.ghg.net/clips/download/documentation/bpg.pdf
>
>
> There is a second option to write your own query and function
> (This is modified from a very old post by Samson Tu on the Jess mailing  
> list, can't find the link at the moment...):
>
> (defquery findObj-by-class
>      (declare (variables ?class))
>      (object (is-a ?class)))
>
> This query returns an Iterator  
> <Java-Object:java.util.AbstractList$Itr>, and NOT a list:
> (run-query findObj-by-class Test)
>
> The following function calls the query and transforms the resulting  
> Iterator into a list:
>
> (deffunction findObj (?class)
>      "This function returns a list, but the items are NOT Protege  
> Instances, but mapped facts.
>      Thus slot-get does not work for these facts, but fact-slot-value,  
> see JiA p. 129"
>      (bind ?list nil)
>      (bind ?it (run-query findObj-by-class ?class))
>      (while (?it hasNext)
>          (bind ?token (call ?it next))
>          (bind ?fact (call ?token fact 1))
>          (bind ?list (create$ ?list ?fact)))
>      (return (rest$ ?list)))
>
> Note: This returns a list with mapped facts:  
> (<Java-Object:JessTab.DefaultMappedFact>)
> To access the slots of the result use Jess function (fact-slot-value  
> object slot).
> Again, the JessTab function (slot-get object slot) does not work for  
> <Java-Object:JessTab.DefaultMappedFact>
>
> HTH
>
> Dona
>
> On Feb 1, 2006, at 7:25 AM, Eunice Palmeira wrote:
>
>
>>Hello,
>>   I use JessTab but I didn't found a function to return the name of  
>>the all
>>class instances at once.
>>   I would like to get the name of the all class instances, called  
>>Test for
>>example.
>>  Somebody help me, plese?
>>
>>   []'s Eunice
>>
>>-----------------------------------------------------------------------
>>--
>>To unsubscribe go to  
>>http://protege.stanford.edu/community/subscribe.html
>>
>
>
> -------------------------------------------------------------------------
> To unsubscribe go to http://protege.stanford.edu/community/subscribe.html
>
>


--
Samson Tu                    email: [hidden email]
Senior Research Scientist    web: www.stanford.edu/~swt/
Stanford Medical Informatics phone: 1-650-725-3391
Stanford University          fax: 1-650-725-7944

-------------------------------------------------------------------------
To unsubscribe go to http://protege.stanford.edu/community/subscribe.html