Package spade :: Module ECLiPSeKB
[hide private]
[frames] | no frames]

Source Code for Module spade.ECLiPSeKB

 1  from logic import KB 
 2  from pyxf import eclipse 
 3   
4 -class ECLiPSeKB( KB ):
5 '''ECLiPSe Prolog knowledge base'''
6 - def __init__( self, sentence=None, path='eclipse' ):
7 '''Constructor method 8 Usage: ECLiPSeKB( sentence, path ) 9 sentence - Prolog sentence to be added to the KB (default: None) 10 path - path to ECLiPSe Prolog executable (default: 'eclipse')''' 11 self.eclipse = eclipse( path ) 12 if sentence: 13 self.tell( sentence )
14
15 - def tell( self, sentence ):
16 '''Adds sentence to KB''' 17 sentence = sentence.strip() 18 if sentence[ -1 ] == '.': 19 sentence = sentence[ :-1 ] 20 return self.eclipse.query( 'assert(' + sentence + ')' )
21
22 - def ask( self, query ):
23 '''Queries the KB''' 24 return self.eclipse.query( query )
25
26 - def retract( self, sentence ):
27 '''Deletes sentence from KB''' 28 sentence = sentence.strip() 29 if sentence[ -1 ] == '.': 30 sentence = sentence[ :-1 ] 31 return self.eclipse.query( 'retract(' + sentence + ')' )
32
33 - def loadModule( self, module ):
34 '''Loads module to KB 35 Usage: instance.loadModule( path ) 36 path - path to module''' 37 self.eclipse.load( module )
38 39 if __name__ == '__main__': 40 kb = ECLiPSeKB() 41 kb.tell( 'a(b,c)' ) 42 kb.tell( 'a(c,d)' ) 43 kb.tell( '( p(_X,_Y) :- a(_X,_Y) )' ) 44 kb.tell( '( p(_X,_Y) :- a(_X,_Z), p(_Z,_Y) )' ) 45 for result in kb.ask( 'p(X,Y)' ): 46 print result 47 kb.retract( 'a(b,c)' ) 48