Python: added Conditions to price specs
[smartapi.git] / Common / Python / SmartAPI / model / Condition.py
1 from SmartAPI.common.PROPERTY import PROPERTY
2 from SmartAPI.common.RESOURCE import RESOURCE
3 from SmartAPI.common.Tools import Tools
4
5 #from SmartAPI.model.Controllability import Controllability
6
7 from SmartAPI.model.Obj import Obj
8 from SmartAPI.rdf.Resource import Resource
9 from SmartAPI.rdf.Variant import Variant
10 from SmartAPI.common.Tools import Tools
11 import traceback
12
13
14 class Condition(Obj):
15
16         def __init__(self, uri = None):
17                 Obj.__init__(self, uri)         
18                 self.setType(RESOURCE.CONDITION)
19                 self.init_property(PROPERTY.TEMPORALCONTEXT, 'temporalContext', self.hasTemporalContext, self.getTemporalContext)
20                 self.init_property(PROPERTY.REGEX, 'regexes', self.hasRegex, self.getRegexes, True)
21                 self.init_property(PROPERTY.OR, 'ors', self.hasOr, self.getOrs, True)
22                 self.init_property(PROPERTY.AND, 'ands', self.hasAnd, self.getAnds, True)
23                 self.init_property(PROPERTY.XOR, 'xors', self.hasXor, self.getXors, True)
24                 self.init_property(PROPERTY.ACTION, 'actions', self.hasAction, self.getActions, True)
25                 self.init_property(PROPERTY.EQUAL, 'equals', self.hasEqual, self.getEquals, True)
26                 self.init_property(PROPERTY.NONEQUAL, 'nonEquals', self.hasNonEqual, self.getNonEquals, True)
27                 self.init_property(PROPERTY.LESSER, 'lessers', self.hasLesser, self.getLessers, True)
28                 self.init_property(PROPERTY.LESSEROREQUAL, 'lesserOrEquals', self.hasLesserOrEqual, self.getLesserOrEquals, True)
29                 self.init_property(PROPERTY.GREATER, 'greaters', self.hasGreater, self.getGreaters, True)
30                 self.init_property(PROPERTY.GREATEROREQUAL, 'greaterOrEquals', self.hasGreaterOrEqual, self.getGreaterOrEquals, True)
31         
32         def _parseStatement(self, statement, custom_classes = None):
33                 from SmartAPI.model.TemporalContext import TemporalContext
34                 
35                 self.parse_property(statement, PROPERTY.REGEX, self.addRegex, Variant, custom_classes = custom_classes)
36                 self.parse_property(statement, PROPERTY.OR, self.addOr, Variant, custom_classes = custom_classes)
37                 self.parse_property(statement, PROPERTY.AND, self.addAnd, Variant, custom_classes = custom_classes)
38                 self.parse_property(statement, PROPERTY.XOR, self.addXor, Variant, custom_classes = custom_classes)
39                 self.parse_property(statement, PROPERTY.TEMPORALCONTEXT, self.setTemporalContext, TemporalContext, custom_classes = custom_classes)
40                 self.parse_property(statement, PROPERTY.ACTION, self.addAction, Variant, custom_classes = custom_classes)
41                 self.parse_property(statement, PROPERTY.EQUAL, self.addEqual, Variant, custom_classes = custom_classes)
42                 self.parse_property(statement, PROPERTY.NONEQUAL, self.addNonEqual, Variant, custom_classes = custom_classes)
43                 self.parse_property(statement, PROPERTY.LESSER, self.addLesser, Variant, custom_classes = custom_classes)
44                 self.parse_property(statement, PROPERTY.LESSEROREQUAL, self.addLesserOrEqual, Variant, custom_classes = custom_classes)
45                 self.parse_property(statement, PROPERTY.GREATER, self.addGreater, Variant, custom_classes = custom_classes)
46                 self.parse_property(statement, PROPERTY.GREATEROREQUAL, self.addGreaterOrEqual, Variant, custom_classes = custom_classes)
47                 
48                 super(Condition, self)._parseStatement(statement, custom_classes = custom_classes)
49
50         def hasAction(self):
51                 return len(self.actions) > 0
52
53         def getActions(self):
54                 return self.actions
55
56         def addAction(self, a):
57                 if not isinstance(a, Variant):
58                         a = Variant(a)
59                 self.actions.append(a)
60                 
61         def hasOr(self):
62                 return len(self.ors) > 0
63
64         def getOrs(self):
65                 return self.ors
66
67         def addOr(self, o):
68                 if not isinstance(o, Variant):
69                         o = Variant(o)
70                 self.ors.append(o)
71                 
72         def hasXor(self):
73                 return len(self.xors) > 0
74
75         def getXors(self):
76                 return self.xors
77
78         def addXor(self, o):
79                 if not isinstance(o, Variant):
80                         o = Variant(o)
81                 self.xors.append(o)
82
83         def hasAnd(self):
84                 return len(self.ands) > 0
85
86         def getAnds(self):
87                 return self.ands
88
89         def addAnd(self, a):
90                 if not isinstance(a, Variant):
91                         a = Variant(a)
92                 self.ands.append(a)
93
94         def hasRegex(self):
95                 return len(self.regexes) > 0
96
97         def getRegexes(self):
98                 return self.regexes
99
100         def addRegex(self, regex):
101                 if not isinstance(regex, Variant):
102                         regex = Variant(regex)
103                 self.regexes.append(regex)
104                 
105         def hasEqual(self):
106                 return len(self.equals) > 0
107
108         def getEquals(self):
109                 return self.equals
110
111         def addEqual(self, e):
112                 if not isinstance(e, Variant):
113                         e = Variant(e)
114                 self.equals.append(e)
115         
116         def hasNonEqual(self):
117                 return len(self.nonEquals) > 0
118
119         def getNonEquals(self):
120                 return self.nonEquals
121
122         def addNonEqual(self, e):
123                 if not isinstance(e, Variant):
124                         e = Variant(e)
125                 self.nonEquals.append(e)
126
127         def hasLesser(self):
128                 return len(self.lessers) > 0
129
130         def getLessers(self):
131                 return self.lessers
132
133         def addLesser(self, l):
134                 if not isinstance(l, Variant):
135                         l = Variant(l)
136                 self.lessers.append(l)
137                 
138         def hasLesserOrEqual(self):
139                 return len(self.lesserOrEquals) > 0
140
141         def getLesserOrEquals(self):
142                 return self.lesserOrEquals
143
144         def addLesserOrEqual(self, l):
145                 if not isinstance(l, Variant):
146                         l = Variant(l)
147                 self.lesserOrEquals.append(l)
148                 
149         def hasGreater(self):
150                 return len(self.greaters) > 0
151
152         def getGreaters(self):
153                 return self.greaters
154
155         def addGreater(self, g):
156                 if not isinstance(g, Variant):
157                         g = Variant(g)
158                 self.greaters.append(g)
159                 
160         def hasGreaterOrEqual(self):
161                 return len(self.greaterOrEquals) > 0
162
163         def getGreaterOrEquals(self):
164                 return self.greaterOrEquals
165
166         def addGreaterOrEqual(self, g):
167                 if not isinstance(g, Variant):
168                         g = Variant(g)
169                 self.greaterOrEquals.append(g)
170                 
171         def hasTemporalContext(self):
172                 return self.temporalContext is not None
173
174         def getTemporalContext(self):
175                 return self.temporalContext
176
177         def setTemporalContext(self, t):
178                 self.temporalContext = t
179