Basic Ideas and Use
Updated 14 Oct 2002
Upper levels:
Amzi Python Interface 0.2...

 2. Basic Ideas and Use

 [ Prev - Up - Next ] 

 Index


 Ideas

  [ Top ] 

The AmziProlog module provides several new types to Python:

AmziEngine:
a Python-subclassable type encapsulating an Amzi prolog engine (subclassable means you can add your own methods, defined in Python).
AmziTerm:
a non-subclassable type encapsulating an Amzi prolog term (since terms are normally produced by an Amzi engine, subclassing them in Python would not be very useful, afaics).
There is also an 'iterator' class, AmziCall, used for looping over multiple answers, this isn't really meant for direct use.

The package itself doesn't provide any generators, but they can certainly be useful in managing access to the results of nondeterministic predicates.


 Use

  [ Top ] 

To begin with, import the module, and create an engine:

  import AmziProlog
  eng = AmziProlog(engine('<xplfile>'))
where <xplfile>.xpl is the name of the Amzi Prolog compiled .xpl file you want to load, and <xplfile>.cfg is the name of any associated cfg file. For now, these files must have the same name. You can make as many engines as you like.

For a single-answer, deterministic call (equivalent to 'Exec' in the other lsapi's), use the engine .run() method:

  answer = eng.run('query(X,Y)')
answer will now be an AmziTerm instantiating the query if it succeeded, or None if it failed. You can print 'answer' as is, or access its functors and arguments with indexing:
  print "The answer is: %s"%answer
  print "The functor is: %s"answer[0]
  print "The second argument term is: %s"%answer[2]
Terms to be run can also be provided as tuples, with 0-th member the functor, and None occupying unbound variable positions:
  answer = eng.run(('query', None, None))
Note the double parentheses, since the argument is a tuple (a bit verbose, but it means that it will be possible to add extra arguments to the .run() method if wanted.

And there are also engine methods for putting together terms, which can then be submitted to .run() (see 'Building').

For going through multiple answers, one possibility is the engine methods .call() and .redo(), corresponding to regular lsapi 'call' and 'redo' methods. .call() takes the same kinds of arguments that .run() does, and returns an AmziTerm as value; .redo() takes no arguments and returns the the next instantiation of a previous call. But in a for-loop, the best method to use is .calls():

for answer in eng.calls('father(X,Y)'):
    print "%s is the father of %s"%(answer[2], answer[1])
Technically, .calls() produces an iterator, with the for-loop construction accesses and utilizes.



 [ Prev - Top - Next ]