-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.py
108 lines (84 loc) · 3.05 KB
/
rules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Classes for creating facts and rules
# Matthew Stone
# CS 503 Fall 2013
# File organization -- two parts.
# *** FACTS ***
# *** RULES ***
# Find the part that you want by search.
import sys
import unify
# *** FACTS ***
class FACT(object) :
# A fact just has a single term as its body
def __init__(self, body) :
self.term = body
# Apply a method to the a fact
# by operating on the term body
def app(self, fn) :
fn(self.term)
# Transform the representation of a
# fact by doing some operation to the body
def xform(self, fn) :
return FACT(fn(self.term))
# Print out a fact to a terminal,
# in a form that can be read in again
# by the parser
def prettyprint(self, n) :
sys.stdout.write(n * " ")
sys.stdout.write(unify.prettyprintformulaterm(self.term))
sys.stdout.write(".\n")
# Give a one-line description of a fact as a string
def __str__(self) :
return ("FACT " + unify.prettyprintformulaterm(self.term))
# Compare facts: requires exact identity
# (that is, no renaming of variables)
def __eq__(self, other) :
return isinstance(other, FACT) and self.term == other.term
def __ne__(self, other) :
return not isinstance(other, FACT) or self.term != other.term
# *** RULES ***
class RULE(object) :
# A fact has a term as an antecedent
# and either a fact or rule as its consequent.
def __init__(self, antecedent, consequent) :
self.cond = antecedent
self.result = consequent
# Apply a method to a rule
# by operating on the term antecedent
# and applying recursively to the consequent
def app(self, fn) :
fn(self.cond)
self.result.app(fn)
# Transform the representation of a
# rule by doing some operation to the
# antecedent and applying the transformation
# recursively to the consequent
def xform(self, fn) :
return RULE(fn(self.cond), self.result.xform(fn))
# Print out a fact to a terminal,
# in a form that can be read in again
# by the parser
def prettyprint(self, n) :
sys.stdout.write(n * " ")
sys.stdout.write(unify.prettyprintformulaterm(self.cond))
sys.stdout.write(" =>\n")
self.result.prettyprint(n + 3)
# Give a one-line description of a fact as a string
def __str__(self) :
return ("RULE " + unify.prettyprintformulaterm(self.cond) + " => " \
+ str(self.result))
# Compare facts: requires exact identity
# (that is, no renaming of variables)
def __eq__(self, other) :
return isinstance(other, RULE) and \
self.cond == other.cond and self.result == other.result
def __ne__(self, other) :
return not isinstance(other, RULE) or \
self.cond != other.cond or self.result != other.result
# Takes the abstract syntax tree representing
# a knowledge-base program and prints it to
# the terminal, in a form that can be read
# in again by the parser.
def prettyprintkb(d) :
for f in d :
f.prettyprint(0)