1 from logic import KB
2 from pyxf import xsb
3
5 '''XSB Prolog knowledge base'''
6 - def __init__( self, sentence=None, path='xsb' ):
7 '''Constructor method
8 Usage: XSBKB( sentence, path )
9 sentence - Prolog sentence to be added to the KB (default: None)
10 path - path to XSB executable (default: 'xsb')'''
11 self.xsb = xsb( 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.xsb.query( 'assert(' + sentence + ')' )
21
22 - def ask( self, query ):
25
27 '''Deletes sentence from KB'''
28 sentence = sentence.strip()
29 if sentence[ -1 ] == '.':
30 sentence = sentence[ :-1 ]
31 return self.xsb.query( 'retract(' + sentence + ')' )
32
34 '''Loads module to KB
35 Usage: instance.loadModule( path )
36 path - path to module'''
37 self.xsb.load( module )
38
39 if __name__ == '__main__':
40 kb = XSBKB()
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