Re: Beginner - Help Needed!

"Rangarajan Krishnamoorthy" <[email protected]> Wed, 2 Jul 2008 04:45:54 +0530
Newsgroups gmane.comp.ai.powerloom
Message-ID <000301c8dbd0$6b4b5a00$41e20e00$@com>
Hans,
Thanks to you and Tom for the generous help. I understand PLI quite well
now.

Regards,
Rangarajan

-----Original Message-----
From: Hans Chalupsky [mailto:[email protected]] 
Sent: Wednesday, July 02, 2008 4:20 AM
To: Rangarajan Krishnamoorthy
Cc: [email protected]
Subject: Re: [PowerLoom Forum] Beginner - Help Needed!

Rangarajan,

Tom gave a very nice introduction on how to officially interact with
PowerLoom in Lisp using the PLI functions.  For a quick and dirty way
to access the result of a retrieve in Lisp, you can use the STELLA
functions consify and stringify, for your example:

CL-USER(13): (setf matches (STELLA::retrieve all (person ?x)))
There is 1 solution:
  #1: ?X=JOHN
CL-USER(14): (stella::consify matches)
(|i|JOHN)  ;; this still contains a complex logic object representing John
CL-USER(15): (stella::stringify (first *)) ;; converts it into a string
"JOHN"     
CL-USER(16): (stella::stringify matches)   ;; converts the query iterator
into a string
"(JOHN)
"
CL-USER(17): 

One caveat with this approach is that it will always exhaust the query
iterator (i.e., retrieve all solutions).

Hans

>>>>> Thomas Russ <[email protected]> writes:

> On Jul 1, 2008, at 2:00 AM, Rangarajan Krishnamoorthy wrote:

>> Russ,
>> Thanks a lot for the quick reply. I noticed Chapter 6 lists the PLI.  
>> Did you mean Chapter 6 or 7 in your reply (1)?

> Well, I just checked and it seems that the numbering of the on-line  
> HTML manual and the PDF manual are different.  So, in the PDF manual  
> the Chapter is number 6.  It is number 7 in the on-line manual.  This  
> will be fixed when we next generate the documentation.

>> Consider the following scenario. I have loaded PowerLoom into my  
>> Lisp. Instead of saying (in-package "STELLA"), I do the following:

> OK, that works.

>> CL-USER 1 > (STELLA::defconcept person)
>> |c|PERSON

>> CL-USER 2 > (STELLA::assert (person john))
>> |P|(PERSON JOHN)

>> CL-USER 3 > (STELLA::ask (person john))
>> TRUE

>> CL-USER 4 > (STELLA::ask (person peter))
>> UNKNOWN
>> 
>> CL-USER 5 > (STELLA::retrieve all (person ?x))
>> There is 1 solution:
>> 
>> #1: ?X=JOHN
>> 
>> CL-USER 6 > (setf matches (STELLA::retrieve all (person ?x)))
>> There is 1 solution:
>> #1: ?X=JOHN
>> 
>> CL-USER 7 > matches
>> There is 1 solution:
>> #1: ?X=JOHN
>> 
>> Notice that when I retrieve the matches (as in CL-USER 6 prompt), I  
>> expect to get a "Lisp" form. That would allow me to use the matches  
>> in my Lisp code. But that is not what I get (see CL-USER 7).

> Well, you actually do get a Lisp object back.  It is just that it has  
> a custom print function that formats it to look pretty to an  
> interactive user.  The actual object type is a QUERY-ITERATOR, and you  
> can access the elements using the Stella iterator interface.

> But it would, at this point, be more convenient to use the PLI  
> (PowerLoom interface) functions that are defined in the PLI package  
> instead.  They are designed to be called from code.

>> So, to restate my original question, how do I programmatically  
>> interact with PowerLoom so that I get the results of queries/ 
>> computations into my Lisp environment? I will most probably define  
>> the domain knowledge (concepts, relations, functions, rules, etc.)  
>> in a Powerloom file, load it into my Lisp environment (using the "in- 
>> package" format you indicated), but issue queries from Lisp code.  
>> Hope I have conveyed my usage scenario.

> Yes, defining the domain content in a PowerLoom file is the best way  
> to do it.  You would then do things like the following with the PLI  
> functions.  A lot of the interaction functions come in an object  
> version and a string version. In Lisp, the string version names start  
> with "s-".  These are the functions described in Chapter 6(or 7).  The  
> string version allows you to use string inputs rather than having to  
> construct Stella or PowerLoom objects to pass to the other functions.

> I'll illustrate this using the business.plm file that has the  
> annotated example knowledge base in it.

> (pli:load "kbs/business.plm" stella::null)

> (setq matches
>       (pli:s-retrieve "all (company ?c)" "BUSINESS" stella::null))
>    ;; This returns an object of type PLI-ITERATOR.
>    ;; There are accessor functions for getting the elements out of the  
> iteration.
>    ;;    pli:next? will advance the iterator to the next element and  
> return
>    ;;              stella::TRUE if there is such an element.
>    ;;              PowerLoom's stella::FALSE in conveniently cl:nill  
> in Common Lisp
>    ;;    Then there are a large number of pli:get-nth-... accessors  
> that can be used
>    ;;         to get elements of the tuple.  Note that the elements  
> will be some
>    ;;         type of Stella object, including wrapped literals like  
> numbers or strings.

> (loop with module = (pli:get-module "BUSINESS" stella::null)
>        while (stella::next? matches)
>        do (print (pli:get-nth-value matches 0 module stella::null)))

>    ;; Since this particular retrieval returns a simple list of elements,
>    ;; each tuple of the answer has only one item in it, so the index is
>    ;; always 0.  What follows is an example that shows a more  
> complicated
>    ;; query.

> (setq matches2
>       (pli:s-retrieve "all (and (company ?c) (company-name ?c ?name))"  
> "BUSINESS" stella::null))

> (loop with module = (pli:get-module "BUSINESS" stella::null)
>        while (stella::next? matches2)
>        do (format t "~A is called ~A~%"
>                   (pli:get-nth-value matches2 0 module stella::null)
>                   (pli:get-nth-string matches2 1 module stella::null)))
>     ;; In this case, each value of the PL-Iterator is a tuple with 2  
> elements, and
>     ;;   the second one is a string type.  We get them using 0-based  
> indexing into
>     ;;   the tuple values.

> The general super-type of all returned items is STELLA::OBJECT, and  
> those things which are not wrapped literals are subclasses of  
> STELLA::LOGIC-OBJECT.

> The mismatch in package names is a bit unfortunate.  I plan to remedy  
> that shortly.  It will be in the next incremental release.

> Also note that stella::NULL and stella::NULL-STRING are different  
> objects, as are stella::NULL-INTEGER and stella::NULL-FLOAT.  This is  
> necessary because of type restrictions, particular with Common Lisp  
> implementations like CMUCL and SBCL that are very strict about  
> datatypes.

>> 
>> 
>> Regards,
>> Rangarajan
>> 
>> ----- Original Message ----- From: "Thomas Russ" <[email protected]>
>> To: "Rangarajan Krishnamoorthy" <[email protected]>
>> Cc: <[email protected]>
>> Sent: Tuesday, July 01, 2008 9:46 AM
>> Subject: Re: [PowerLoom Forum] Beginner - Help Needed!
>> 
>> 
>> 
>> On Jun 30, 2008, at 7:42 PM, Rangarajan Krishnamoorthy wrote:
>> 
>>> Hi,
>>> I have just downloaded PowerLoom on Windows. I am using LispWorks  
>>> 5.1. Although the documentation of PowerLoom talks about the  
>>> language and API, I do not find examples of how to call the API  
>>> from Lisp. I could not even get started with simple examples using  
>>> the API from Lisp.
>> 
>> First off, I apologize because we have not yet had the opportunity to
>> finish the lisp-specific section of the manual.
>> 
>> Fortunately, the Lisp interface is straight-forward, since the
>> documentation of the functions are all in the Lisp syntax.  The key
>> items that are missing from the documentation are:
>> 
>> 1.  The PowerLoom Interface functions from section 7 are all in the  
>> PLI
>> package.  Those API functions are exported and are intended for use
>> from your own Lisp program.
>> 
>> 2.  All other PowerLoom functions are in the STELLA package, but are
>> not exported.  This includes all of the normal PowerLoom functions.   
>> So
>> it is often convenient to just operate in the STELLA package.  This
>> works well for interactive development using the Lisp environment.
>> 
>> So, to use PowerLoom in Lisp one would need to do the following:
>> 
>> (load "load-powerloom.lisp")
>> (in-package "STELLA")
>> ;; At this point you can just type PowerLoom commands,
>> ;; or you could go through the annotated example in the manual.
>> 
>> (defconcept Person)
>> (deffunction age ((?p person) (?age integer)))
>> (defrelation friend ((?p1 person) (?p2 person)))
>> 
>> (assert (person fred))
>> (assert (person rajiv))
>> (assert (and (age fred 20) (age rajiv 19) (friend fred rajiv)))
>> 
>> (ask (age fred 20))
>> (ask (age fred 30))
>> (retrieve all (friend ?x rajiv))
>> 
>> (defconcept teenager ((?p person))
>> :<=> (and (person ?p)
>> (< (age ?p) 20)
>> (> (age ?p) 12)))
>> 
>> (retrieve all (teenager ?p))
>> (retrieve all (and (teenager ?p) (age ?p ?age)))
>> 
>> 
>> 
>> 
>>> 
>>> There is some mention of "translation" to C++, etc. Does it mean  
>>> that PowerLoom code can be translated to Lisp (or C++) and then  
>>> used in that mode? How do I invoke the translator?
>> 
>> PowerLoom itself has already been translated to Lisp, Java and C++.
>> This is done by using our underlying Stella language, which has such a
>> translator.  More information and an incomplete manual can be found at
>> http://www.isi.edu/isd/LOOM/Stella/index.html
>> 
>> When using Stella to write programs, one would normally build a
>> "system" which has a description of the files and Stella modules
>> (namespaces) involved.  Then the translator is invoked with
>> 
>> (make-system "system-name" :target-language)
>> 
>> where the :target-language is one of :common-lisp :java or :cpp
>> and "system-name" is the name of your system.
>> 
>>> 
>>> Thanks for your help.
>> 
>> I hope this helps to get you started.  If this isn't quite enough,
>> please ask further questions and we'll help you get started.
>> 
>> 
>> 
>>> 
>>> Regards,
>>> Rangarajan_______________________________________________
>>> powerloom-forum mailing list
>>> [email protected]
>>> http://mailman.isi.edu/mailman/listinfo/powerloom-forum
>> 

> _______________________________________________
> powerloom-forum mailing list
> [email protected]
> http://mailman.isi.edu/mailman/listinfo/powerloom-forum