Fact I/O¶
In the basic example we used Python code to insert input facts into the expert system’s working memory. We also used Python code to process the output facts directly. It is often useful to import/export facts from/to other formats for integration with other data processing tools (or simply for storage on disk).
CLIPS format¶
The native CLIPS format can be used for storing facts in files:
from rulu.engine import RuleEngine
engine = RuleEngine()
engine.load_module('family')
engine.load('family-in.clp')
engine.run()
engine.save('family-out.clp')
family-in.clp
(IsFatherOf (father "Adam") (son "Seth"))
(IsFatherOf (father "Seth") (son "Enos"))
(IsFatherOf (father "Enos") (son "Kenan"))
(IsFatherOf (father "Kenan") (son "Mahalalel"))
family-out.clp
(initial-fact)
(IsFatherOf (father "Adam") (son "Seth"))
(IsFatherOf (father "Seth") (son "Enos"))
(IsFatherOf (father "Enos") (son "Kenan"))
(IsFatherOf (father "Kenan") (son "Mahalalel"))
(IsGrandfatherOf (greatness 0) (grandson "Mahalalel") (grandfather "Enos"))
(IsGrandfatherOf (greatness 0) (grandson "Kenan") (grandfather "Seth"))
(IsGrandfatherOf (greatness 0) (grandson "Enos") (grandfather "Adam"))
(IsGrandfatherOf (greatness 1) (grandson "Kenan") (grandfather "Adam"))
(IsGrandfatherOf (greatness 1) (grandson "Mahalalel") (grandfather "Seth"))
(IsGrandfatherOf (greatness 2) (grandson "Mahalalel") (grandfather "Adam"))
Pandas dataframes¶
The dataframe is a very convenient structure for holding fact data:
>>> from rulu.rulu_io import facts_to_df
>>> facts_to_df(engine, 'IsFatherOf')
father son
0 Adam Seth
1 Seth Enos
2 Enos Kenan
3 Kenan Mahalalel
[4 rows x 2 columns]
Export to Excel¶
Database integration¶
TBD :)