-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrules.py
170 lines (136 loc) · 4.14 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
##########################################
class RuleEffect:
"""The effects of a rule, subclassed by other classes below"""
def apply(self,kb,trace):
pass
##########################################
class ExecuteAction(RuleEffect):
def __init__(self,action):
super().__init__()
self.action=action
def apply(self,kb,public_trace):
print (kb.name() + " does/expects "+self.action)
s = kb.name() + ":"+self.action
public_trace.append(s)
def effect_equals(self, effect):
if (isinstance(effect, ExecuteAction)):
return self.action == effect.action
return False
def __repr__(self):
return "."+self.action
##########################################
class AddBelief(RuleEffect):
def __init__(self,belief):
super().__init__()
self.belief=belief
def apply(self,kb,public_trace):
kb.add_belief(self.belief)
def effect_equals(self, effect):
if (isinstance(effect, AddBelief)):
return self.belief == effect.belief
return False
def __repr__(self):
return "+"+self.belief
##########################################
class RemoveBelief(RuleEffect):
def __init__(self,belief):
super().__init__()
self.belief=belief
def apply(self,kb,public_trace):
kb.remove_belief(self.belief)
def effect_equals(self, effect):
if (isinstance(effect, RemoveBelief)):
return self.belief == effect.belief
return False
def __repr__(self):
return "-"+self.belief
# GOAL Based rules not mentioned in paper.
##########################################
class AddGoal(RuleEffect):
def __init__(self,goal):
super().__init__()
self.goal=goal
def effect_equals(self, effect):
if (isinstance(effect, AddGoal)):
return self.goal == effect.goal
return False
def apply(self,kb,public_trace):
kb.add_goal(self.goal)
def __repr__(self):
return "+!"+self.goal
##########################################
class RemoveGoal(RuleEffect):
def __init__(self,goal):
super().__init__()
self.goal=goal
def effect_equals(self, effect):
if (isinstance(effect, RemoveGoal)):
return self.goal == effect.goal
return False
def apply(self,kb,public_trace):
kb.remove_goal(self.goal)
def __repr__(self):
return "-!"+self.goal
##########################################
#######END RULE EFFECT CLASSES############
##########################################
class Rule:
def __init__(self,beliefs=set(),goals=set(),effects=set()):
self.beliefs=beliefs
self.goals=goals
self.effects=effects
def rule_equals(self, rule):
for b in self.beliefs:
if (not b in rule.beliefs):
return False
for g in self.goals:
if (not g in rule.goals):
return False
for e in self.effects:
not_present = True
for e2 in rule.effects:
if (e.effect_equals(e2)):
not_present = False
break;
if (not_present):
return False
for b in rule.beliefs:
if (not b in self.beliefs):
return False
for g in rule.goals:
if (not g in self.goals):
return False
for e in rule.effects:
not_present = True
for e2 in self.effects:
if (e.effect_equals(e2)):
not_present = False
break;
if (not_present):
return False
return True
def in_rule_list(self, rule_list):
for rule in rule_list:
if (self.rule_equals(rule)):
return True
return False
def __repr__(self):
s=""
for b in self.beliefs:
s+=b+","
for g in self.goals:
s+="!"+g+","
s+=" -> "
for e in self.effects:
s+=str(e)+","
return s
class Perception(Rule):
def __init__(self):
Rule.__init__(self)
def rule_equals(self, rule):
return False
def in_rule_list(self, rule_list):
return False
def __repr__(self):
return "perception"
##########################################