Python: Fix URL in Variant parsing and serialization; Notification
[smartapi.git] / Common / Python / SmartAPI / model / ValueObject.py
1 from SmartAPI.common.PROPERTY import PROPERTY
2 from SmartAPI.common.RESOURCE import RESOURCE
3 from SmartAPI.model.Obj import Obj
4 from SmartAPI.rdf.Variant import Variant
5 from SmartAPI.common.Tools import Tools
6 from SmartAPI.common.NS import NS
7 from rdflib import URIRef
8 import traceback
9
10
11 class ValueObject(Obj):
12
13         def __init__(self, uri = None, quantity = None, unit = None, value = None, secondaryQuantity=None, secondaryUnit=None,
14                                 dataType = None, description = None, types = None):
15                 '''
16                 @param dataType, description, uri, quantity, unit: string 
17                 @param types: a list of string, or one single string representing Type
18                 '''
19                 Obj.__init__(self, uri)
20                 
21                 self.init_property(PROPERTY.QUANTITYKIND, 'quantity', self.hasQuantity, self.getQuantity)
22                 self.init_property(PROPERTY.UNIT, 'unit', self.hasUnit, self.getUnit)
23                 self.init_property(PROPERTY.SECONDARYQUANTITYKIND, 'secondaryQuantity', self.hasSecondaryQuantity, self.getSecondaryQuantity)
24                 self.init_property(PROPERTY.SECONDARYUNIT, 'secondaryUnit', self.hasSecondaryUnit, self.getSecondaryUnit)
25                 self.init_property(PROPERTY.DATATYPE, 'dataType', self.hasDataType, self.getDataType)
26                 self.init_property(PROPERTY.RDF_VALUE, 'value', self.hasValue, self.getValue)
27                 self.init_property(PROPERTY.MAXIMUM, 'maximum', self.hasMaximum, self.getMaximum)
28                 self.init_property(PROPERTY.MINIMUM, 'minimum', self.hasMinimum, self.getMinimum)
29                 self.init_property(PROPERTY.INSTANT, 'instant', self.hasInstant, self.getInstant)
30                 self.init_property(PROPERTY.TEMPORALCONTEXT, 'temporalContext', self.hasTemporalContext, self.getTemporalContext)
31                 
32                 self.setType(RESOURCE.VALUEOBJECT)
33                 self.setQuantity(quantity)
34                 self.setUnit(unit)
35                 self.setSecondaryQuantity(secondaryQuantity)
36                 self.setSecondaryUnit(secondaryUnit)
37                 
38                 if value is not None:
39                         if not isinstance(value, Variant):
40                                 value = Variant(value)
41                         self.setValue(value)
42                 
43                 if description is not None:
44                         self.setDescription(description)
45                 
46                 if types is not None:
47                         if isinstance(types, list):
48                                 for ty in types:
49                                         self.addType(ty)
50                         else:
51                                 self.addType(types)
52
53         def hasQuantity(self):
54                 return self.quantity is not None
55
56         def getQuantity(self):
57                 return self.quantity
58         
59         def setQuantity(self, u):
60                 '''
61                 @type quantity: string. It can look like "asema:SomeCustomType" or an URI
62                 '''
63                 if u is not None:
64                         self.quantity = self.convert(u)
65
66         def hasUnit(self):
67                 return self.unit is not None
68
69         def getUnit(self):
70                 return self.unit
71         
72         def setUnit(self, u):
73                 '''
74                 @type unit: string. It can look like "asema:SomeCustomType" or an URI
75                 '''
76                 if u is not None:
77                         self.unit = self.convert(u)
78                         
79         def hasSecondaryQuantity(self):
80                 return self.secondaryQuantity is not None
81
82         def getSecondaryQuantity(self):
83                 return self.secondaryQuantity
84         
85         def setSecondaryQuantity(self, u):
86                 '''
87                 @type secondaryQuantity: string. It can look like "asema:SomeCustomType" or an URI
88                 '''
89                 if u is not None:
90                         self.secondaryQuantity = self.convert(u)
91                         
92         def hasSecondaryUnit(self):
93                 return self.secondaryUnit is not None
94
95         def getSecondaryUnit(self):
96                 return self.secondaryUnit
97         
98         def setSecondaryUnit(self, u):
99                 '''
100                 @type unit: string. It can look like "asema:SomeCustomType" or an URI
101                 '''
102                 if u is not None:
103                         self.secondaryUnit = self.convert(u)
104
105         def hasDataType(self):
106                 return self.dataType is not None
107         
108         def getDataType(self):
109                 return self.dataType
110         
111         def setDataType(self, u):
112                 '''
113                 @type dataType: string. It can look like "asema:SomeCustomType" or an URI
114                 '''
115                 if u is not None:
116                         self.dataType = self.convert(u)
117
118         def hasValue(self):
119                 return self.value is not None
120         
121         def getValue(self):
122                 return self.value
123
124         def setValue(self, value):
125                 if value is not None:
126                         if not isinstance(value, Variant):
127                                 value = Variant(value)
128                         self.value = value
129                 
130         def hasMaximum(self):
131                 return self.maximum is not None
132         
133         def getMaximum(self):
134                 return self.maximum
135         
136         def setMaximum(self, max):
137                 if max is not None:
138                         if not isinstance(max, Variant):
139                                 max = Variant(max)
140                         self.maximum = max
141                 
142         def hasMinimum(self):
143                 return self.minimum is not None
144         
145         def getMinimum(self):
146                 return self.minimum
147         
148         def setMinimum(self, min):
149                 if min is not None:
150                         if not isinstance(min, Variant):
151                                 min = Variant(min)
152                         self.minimum = min
153
154         def getTemporalContext(self):
155                 return self.temporalContext
156         
157         def hasTemporalContext(self):
158                 return self.temporalContext is not None
159         
160         def setTemporalContext(self, temporalContext = None, start = None, end = None):
161                 from SmartAPI.model.TemporalContext import TemporalContext
162                 if temporalContext is not None:
163                         self.temporalContext = temporalContext
164                 elif start is not None:
165                         t = TemporalContext(start = start, end = end)
166                         self.temporalContext = t
167                         
168         def hasInstant(self):
169                 return self.instant is not None
170         
171         def getInstant(self):
172                 return self.instant
173
174         def setInstant(self, instant):
175                 if not isinstance(instant, Variant):
176                         instant = Variant(instant)
177                 self.instant = instant
178
179         def convert(self, v):
180                 if isinstance(v, Obj):
181                         return Variant(URIRef(u.getIdentifierUri()))
182                 elif isinstance(v, Variant):
183                         return v
184                 else:
185                         try:
186                                 return Variant(URIRef(v))
187                         except:
188                                 return None
189
190         def _parseStatement(self, statement, custom_classes = None):
191                 from SmartAPI.rdf.Resource import Resource
192                 from SmartAPI.model.TemporalContext import TemporalContext
193                 
194                 self.parse_property(statement, PROPERTY.QUANTITYKIND, self.setQuantity, Variant, custom_classes = custom_classes)
195                 self.parse_property(statement, PROPERTY.UNIT, self.setUnit, Variant, custom_classes = custom_classes)
196                 self.parse_property(statement, PROPERTY.SECONDARYQUANTITYKIND, self.setSecondaryQuantity, Variant, custom_classes = custom_classes)
197                 self.parse_property(statement, PROPERTY.SECONDARYUNIT, self.setSecondaryUnit, Variant, custom_classes = custom_classes)
198                 self.parse_property(statement, PROPERTY.DATATYPE, self.setDataType, Variant, custom_classes = custom_classes)
199                 self.parse_property(statement, PROPERTY.RDF_VALUE, self.setValue, Variant, custom_classes = custom_classes)
200                 self.parse_property(statement, PROPERTY.MAXIMUM, self.setMaximum, Variant, custom_classes = custom_classes)
201                 self.parse_property(statement, PROPERTY.MINIMUM, self.setMinimum, Variant, custom_classes = custom_classes)
202                 self.parse_property(statement, PROPERTY.INSTANT, self.setInstant, Variant, custom_classes = custom_classes)
203                 self.parse_property(statement, PROPERTY.TEMPORALCONTEXT, self.setTemporalContext, TemporalContext, custom_classes = custom_classes)
204                 super(ValueObject, self)._parseStatement(statement, custom_classes = custom_classes)