from pyDatalog import pyDatalog, pyEngine
from pyDatalog.pyDatalog import assert_fact, load, ask
import logging
pyEngine.Logging = True
logging.basicConfig(level=logging.INFO)
pyDatalog.create_terms('causes_fault, faulty, find_fault, raw_input, check_empty, check_open, X, Y, Z')
# Fault hierarchy, e.g. fault in 'gas' causes fault in 'bbq'
+causes_fault('gas', 'bbq')
+causes_fault('valve', 'gas')
+causes_fault('cylinder', 'gas')
#+faulty('valve')
# Rules that say faults flow up the 'causes_fault' network
find_fault(X,Z) <= causes_fault(X,Z) & faulty(X)
find_fault(X,Z) <= causes_fault(Y,Z) & find_fault(X,Y)
# Rules to prompt user for leaf faults
check_empty(X) <= (raw_input('Is '+X+' empty ? (Y/N) ')=='Y')
check_open(X) <= (raw_input('Is '+X+' open ? (Y/N) ')=='Y')
faulty(X) <= (X == 'cylinder') & check_empty(X)
faulty(X) <= (X == 'valve') & ~check_open(X)
# Run the query
print(find_fault(X, 'bbq'))
from pyDatalog import pyDatalog, pyEngine
from pyDatalog.pyDatalog import assert_fact, load, ask
import logging
pyEngine.Logging = True
logging.basicConfig(level=logging.INFO)
pyDatalog.create_terms('container_of, is_faulty, cause_of, input, check_empty, check_open, X, Y, Z')
# Fault hierarchy, e.g. fault in 'gas' causes fault in 'bbq'
+(container_of['gas']=='bbq')
+(container_of['valve']=='gas')
+(container_of['cylinder']=='gas')
#+(is_faulty['valve']==True)
# Rules that say faults flow up the 'container_of' network
(cause_of[Z]==X) <= (container_of[X]==Z) & (is_faulty[X]==True)
(cause_of[Z]==X) <= (container_of[Y]==Z) & (cause_of[Y]==X)
# Rules to prompt user for leaf faults
check_empty(X) <= (input('Is '+X+' empty ? (Y/N) ')=='Y')
check_open(X) <= (input('Is '+X+' open ? (Y/N) ')=='Y')
# Leaf rules
(cause_of['gas']==X) <= ((X == 'cylinder') & check_empty(X))
(cause_of['gas']==X) <= ((X == 'valve') & ~check_open(X))
# Run the query
print(cause_of['bbq']==X)
Some hints :
use : container_of['gas']='bbq'
instead of : +(container_of['gas']=='bbq')
use : is_faulty(X)
instead of : (is_faulty[X]==True)
PC