Sei sulla pagina 1di 2

[29] import re

class ExpSysRule:
def __init__(self, antecedent, consequent):
self.antecedent = antecedent
self.consequent = consequent

def matchExp(self, anteExp, asrtExp):


match = False
x = re.compile('x')
tailExp = x.sub('', anteExp)
tregexp = re.compile(tailExp)
ktail = tregexp.findall(asrtExp)
xname = ''

if not len(ktail)==0:
match = True
xname = ktail[0]

return match, xname

def matchRule(self, listOfAssertions):


fire = True
if len(self.antecedent) > 1:
for i in range(1, len(self.antecedent)):
anteMatch = False
for j in range(len(listOfAssertions)):
if self.matchExp(self.antecedent[i], listOfAssertions
anteMatch = True
break
fire = fire and anteMatch
return fire
else:
anteMatch = False
for j in range(len(listOfAssertions)):
if self.matchExp(self.antecedent[0], listOfAssertions[j]):
anteMatch = True
break
fire = fire and anteMatch
return fire

[30] za1 = ['x has hair']


zc1 = ['x is a mammal']
z1 = ExpSysRule(za1, zc1)

[43] aList = ['Fido has hair']


z1.matchRule(aList)

True
[36] listOfAssertions = ['Sparky has tawny color', 'Bella gives milk', 'Sparky give

[42] listOfRules = [
ExpSysRule(['x has hair'], ['x is a mammal']),
ExpSysRule(['x gives milk'], ['x is a mammal']),
ExpSysRule(['x has feathers'], ['x is a bird']),
ExpSysRule(['x flies', 'x lays eggs'], ['x is a bird']),
ExpSysRule(['x is a mammal', 'x eats meat'], ['x is a carnivore']),
ExpSysRule(['x is a mammal', 'x has pointed teeth', 'x has claws', 'x has
ExpSysRule(['x is a mammal', 'x has hoofs'], ['x is an ungulate']),
ExpSysRule(['x is a mammal', 'x chews cud'], ['x is an ungulate']),
ExpSysRule(['x is a carnivore', 'x has tawny color', 'x has dark spots'
ExpSysRule(['x is a carnivore', 'x has tawny color', 'x has black stripes
ExpSysRule(['x is an ungulate', 'x has long legs', 'x has long neck'
ExpSysRule(['x is an ungulate', 'x has white color', 'x has black stripes
ExpSysRule(['x is a bird', 'x does not fly', 'x has long legs', 'x has lo
ExpSysRule(['x is a bird', 'x does not fly', 'x swins', 'x has black and
ExpSysRule(['x is a bird', 'x is a good flyer'], ['x is an albatross'

[ ] rulesFired = True


while rulesFired:
matchedRules = []
for rule in listOfRules:
# We don't understand the next algorithm explained cause exist

Potrebbero piacerti anche