saving asserted facts to the file

199 views
Skip to first unread message

Eugene

unread,
Oct 19, 2012, 2:09:18 AM10/19/12
to py...@googlegroups.com
Hey, guys!
I would like to use facts asserted by fc rules in another application (not Python). The input for another application is simple text file which structure reminds fact base (.kfb). in Pyke. Therefore I'm looking for the way how can I save asserted facts in the file, in other words I wanna write result of command engine.get_kb(__name__).dump_specific_facts() to the file. So far I only come up with solution when I add python statements in my fc rules, smth like this

foreach 
eventlog.timelength($id, $time)
eventlog.averagetime($averagetime)
check $time > $averagetime 
assert
eventlog.late($id,$time,$averagetime)
python import string
python s = 'late('+$id+','+ str($time) + ','+ str($averagetime)+')'
python f = open( "derivedfacts.pkl", "r+" )
python old = f.read()
python f.seek(0)
python f.write(s +"\n"+ old)
python f.close()

Are there any other ways? In current approach I write to file many excess facts.
Thanks. 

Lilo

unread,
Aug 21, 2013, 5:50:02 PM8/21/13
to py...@googlegroups.com
Hi,
I see that this thread is a bit old but it might still be helpful. I added a function that builds and return a string instead of printing it.

1/ Add these two functions to pyke/fact_base.py

class fact_base(knowledge_base.knowledge_base):
[...]
        def retrieve_specific_facts(self):
        result = ""
        for fl_name in sorted(self.entity_lists.iterkeys()):
            s = self.entity_lists[fl_name].retrieve_specific_facts()
            result = result + '\n' + s
        return result    

class fact_list(knowledge_base.knowledge_entity_list):
[...]
    def retrieve_specific_facts(self):
        result = ""
        for args in self.case_specific_facts:
            result = result + '%s%s\n' % (self.name, args)
        return result

Then use it that way:
content = engine.get_kb('mykb').retrieve_specific_facts()
#print the retrieved facts is equivalent to engine.get_kb('mykb').dump_specific_facts()
print content
#write that to a file
with open(logfilename,'w') as f:
    f.write(content)

Nicola Vitucci

unread,
Sep 21, 2013, 6:58:47 AM9/21/13
to py...@googlegroups.com
Hi,

I'm doing more or less like Lilo suggested, but without changing anything in the original code:

facts = engine.knowledge_bases['kb'].entity_lists['el'].case_specific_facts
for f in facts:
    out_file.write(f)

where "engine" is created with knowledge_engine.engine() and "out_file" is a file open for writing. Some days ago anyway, before I subscribed to this group, I issued a feature request asking for such kind of function in PyKE, i.e. to retrieve a list of facts rather than just dumping them :-)

Cheers,

Nicola
Reply all
Reply to author
Forward
0 new messages