-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpret.py
More file actions
56 lines (47 loc) · 1.72 KB
/
interpret.py
File metadata and controls
56 lines (47 loc) · 1.72 KB
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
"""
interpreter pattern with Python Code
"""
from abc import abstractmethod,ABCMeta
class Expression(metaclass=ABCMeta):
@abstractmethod
def interpret(self, inContext):
pass
class TerminalExpression(Expression):
_data = ""
def __int__(self, inData):
self._data = inData
def interpret(self, inContext):
if inContext.find(self._data) >= 0:
return True
return False
class OrExpression(Expression):
_expr1 = None
_expr2 = None
def __init__(self, inExpr1, inExpr2):
self._expr1 = inExpr1
self._expr2 = inExpr2
def interpret(self, inContext):
return self._expr1.interpret(inContext) or self._expr2.interpret(inContext)
class AndExpression(Expression):
_expr1 = None
_expr2 = None
def __init__(self, inExpr1, inExpr2):
self._expr1 = inExpr1
self._expr2 = inExpr2
def interpret(self, inContext):
return self._expr1.interpret(inContext) and self._expr2.interpret(inContext)
if __name__ == '__main__':
# 规则:Robert和John是男性
def getMaleExpression():
robert = TerminalExpression("Robert")
john = TerminalExpression("John")
return OrExpression(robert,john)
# 规则:Julie是一个已婚的女性
def getMarriedWomanExpression():
julie = TerminalExpression("Julie")
married = TerminalExpression("Married")
return AndExpression(julie,married)
isMale = getMaleExpression()
isMarriedWoman = getMarriedWomanExpression()
print("John is male? " + str(isMale.interpret("John")))
print("Julie is a married women? " + str(isMarriedWoman.interpret("Married Julie")))