-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHornClause.py
55 lines (44 loc) · 1.69 KB
/
HornClause.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
NoGood = '⊥'
Negation = '¬'
def isAssumption(value):
check = 0
# Lines 8 and 9 should be commented because the paper says negative assumptions are not assumptions
if value[check] is Negation:
check+=1
return value[check].isupper()
def negation(variable):
"""Denies the variable. It adds the negation symbol.
"""
if variable[0] == Negation:
return variable[1:]
return Negation + variable
class HornClause:
"""Class that represent a HornClause. Is a classical definition see
https://en.wikipedia.org/wiki/Horn_clause
"""
def __init__(self, antecedents, consequent = None):
self.antecedents = antecedents
self.consequent = consequent
if isinstance(antecedents, str) and consequent is None:
self.consequent = antecedents
self.antecedents = set()
if not HornClause._isFact(self.antecedents, self.consequent):
self.antecedents = None
def __repr__(self):
if self.isFact(): return "Fact"
type = "Goal" if self.consequent == NoGood else "Definite"
return type + " clause: " + str(self.antecedents) + " -> " + self.consequent
def __eq__(self, other):
if not isinstance(other, HornClause):
return False
if self.consequent == other.consequent:
if self.antecedents == other.antecedents:
return True
return False
def __hash__(self):
return hash(str(self))
def isFact(self):
return HornClause._isFact(self.antecedents, self.consequent)
@staticmethod
def _isFact(antecedents, consequent):
return antecedents == set() and isAssumption(consequent)