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

Source Code for Module spade.Flora2KB

 1  from logic import KB 
 2  from pyxf import flora2 
 3   
4 -class Flora2KB( KB ):
5 '''Flora2 knowledge base'''
6 - def __init__( self, sentence=None, path='runflora' ):
7 '''Constructor method 8 Usage: Flora2KB( sentence, path ) 9 sentence - F-logic sentence to be added to the KB (default: None) 10 path - path to Flora2 executable (default: 'runflora')''' 11 self.flora2 = flora2( path ) 12 if sentence: 13 self.tell( sentence )
14
15 - def tell( self, sentence, type='insert' ):
16 '''Adds sentence to KB 17 Usage: instance.tell( sentence, type ) 18 sentence - frame logic sentence to be added to KB 19 type - insertion type (one of insert, insertall, t_insert, t_insertall, insertrule, newmodule; default: 'insert')''' 20 sentence = sentence.strip() 21 if sentence[ -1 ] == '.': 22 sentence = sentence[ :-1 ] 23 return self.flora2.query( type + '{' + sentence + '}' )
24
25 - def ask( self, query ):
26 '''Queries the KB''' 27 return self.flora2.query( query )
28
29 - def retract( self, sentence, type='delete' ):
30 '''Deletes sentence from KB 31 Usage: instance.retract( sentence, type ) 32 sentence - frame logic sentence to be deleted from KB 33 type - deletion type (one of delete, deleteall, erase, eraseall, t_delete, t_deleteall, t_erase, t_eraseall, deletetrule, erasemodule; default: 'delete')''' 34 sentence = sentence.strip() 35 if sentence[ -1 ] == '.': 36 sentence = sentence[ :-1 ] 37 return self.flora2.query( type + '{' + sentence + '}' )
38
39 - def loadModule( self, module ):
40 '''Loads module to KB 41 Usage: instance.loadModule( path ) 42 path - path to module''' 43 self.flora2.load( module )
44 45 if __name__ == '__main__': 46 kb = Flora2KB() 47 kb.tell( 'a[ b->c ]' ) 48 kb.tell( '( ?x[ c->?y ] :- ?x[ b->?y ] )', 'insertrule' ) 49 for result in kb.ask( '?x[ ?y->?z ]' ): 50 print result 51 kb.retract( 'a[ b->c ]' ) 52