dfa3594b104bb2642c1d34d3e425a7c8adc94298
[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                 if value is not None:
38                         if not isinstance(value, Variant):
39                                 value = Variant(value)
40                         self.setValue(value)
41                 
42                 if description is not None:
43                         self.setDescription(description)
44                 
45                 if types is not None:
46                         if isinstance(types, list):
47                                 for ty in types:
48                                         self.addType(ty)
49                         else:
50                                 self.addType(types)
51
52         def hasQuantity(self):
53                 return self.quantity is not None
54
55         def getQuantity(self):
56                 return self.quantity
57         
58         def setQuantity(self, u):
59                 '''
60                 @type quantity: string. It can look like "asema:SomeCustomType" or an URI
61                 '''
62                 if u is not None:
63                         if isinstance(u, Obj):
64                                 self.quantity = URIRef(u.getIdentifierUri())
65                                 return
66                         if not isinstance(u, Variant):
67                                 self.quantity = Variant(URIRef(NS.toAbsoluteUri(u)))
68                         else:
69                                 self.quantity = URIRef(u)
70
71         def hasUnit(self):
72                 return self.unit is not None
73
74         def getUnit(self):
75                 return self.unit
76         
77         def setUnit(self, u):
78                 '''
79                 @type unit: string. It can look like "asema:SomeCustomType" or an URI
80                 '''
81                 if u is not None:
82                         if isinstance(u, Obj):
83                                 self.unit = URIRef(u.getIdentifierUri())
84                                 return
85                         if not isinstance(u, Variant):
86                                 self.unit = Variant(URIRef(NS.toAbsoluteUri(u)))
87                         else:
88                                 self.unit = URIRef(u)
89                         
90         def hasSecondaryQuantity(self):
91                 return self.secondaryQuantity is not None
92
93         def getSecondaryQuantity(self):
94                 return self.secondaryQuantity
95         
96         def setSecondaryQuantity(self, u):
97                 '''
98                 @type secondaryQuantity: string. It can look like "asema:SomeCustomType" or an URI
99                 '''
100                 if u is not None:
101                         if not isinstance(u, Variant):
102                                 self.secondaryQuantity = Variant(URIRef(NS.toAbsoluteUri(u)))
103                         else:
104                                 self.secondaryQuantity = URIRef(u)
105                         
106         def hasSecondaryUnit(self):
107                 return self.secondaryUnit is not None
108
109         def getSecondaryUnit(self):
110                 return self.secondaryUnit
111         
112         def setSecondaryUnit(self, u):
113                 '''
114                 @type unit: string. It can look like "asema:SomeCustomType" or an URI
115                 '''
116                 if u is not None:
117                         if not isinstance(u, Variant):
118                                 self.secondaryUnit = Variant(URIRef(NS.toAbsoluteUri(u)))
119                         else:
120                                 self.secondaryUnit = URIRef(u)
121
122         def hasDataType(self):
123                 return self.dataType is not None
124         
125         def getDataType(self):
126                 return self.dataType
127         
128         def setDataType(self, u):
129                 '''
130                 @type dataType: string. It can look like "asema:SomeCustomType" or an URI
131                 '''
132                 if u is not None:
133                         self.dataType = Variant(NS.toAbsoluteUri(u))
134
135         def hasValue(self):
136                 return self.value is not None
137         
138         def getValue(self):
139                 return self.value
140
141         def setValue(self, value):
142                 if value is not None:
143                         if not isinstance(value, Variant):
144                                 value = Variant(value)
145                         self.value = value
146                 
147         def hasMaximum(self):
148                 return self.maximum is not None
149         
150         def getMaximum(self):
151                 return self.maximum
152         
153         def setMaximum(self, max):
154                 if max is not None:
155                         if not isinstance(max, Variant):
156                                 max = Variant(max)
157                         self.maximum = max
158                 
159         def hasMinimum(self):
160                 return self.minimum is not None
161         
162         def getMinimum(self):
163                 return self.minimum
164         
165         def setMinimum(self, min):
166                 if min is not None:
167                         if not isinstance(min, Variant):
168                                 min = Variant(min)
169                         self.minimum = min
170
171         def getTemporalContext(self):
172                 return self.temporalContext
173         
174         def hasTemporalContext(self):
175                 return self.temporalContext is not None
176         
177         def setTemporalContext(self, temporalContext = None, start = None, end = None):         
178                 from SmartAPI.model.TemporalContext import TemporalContext
179                 if temporalContext is not None:
180                         self.temporalContext = temporalContext
181                 elif start is not None:
182                         t = TemporalContext(start = start, end = end)
183                         self.temporalContext = t
184                         
185         def hasInstant(self):
186                 return self.instant is not None
187         
188         def getInstant(self):
189                 return self.instant
190
191         def setInstant(self, instant):
192                 if not isinstance(instant, Variant):
193                         instant = Variant(instant)
194                 self.instant = instant
195         
196         def _parseStatement(self, statement, custom_classes = None):
197                 from SmartAPI.rdf.Resource import Resource
198                 from SmartAPI.model.TemporalContext import TemporalContext
199                 
200                 self.parse_property(statement, PROPERTY.QUANTITYKIND, self.setQuantity, Variant, custom_classes = custom_classes)
201                 self.parse_property(statement, PROPERTY.UNIT, self.setUnit, Variant, custom_classes = custom_classes)
202                 self.parse_property(statement, PROPERTY.SECONDARYQUANTITYKIND, self.setSecondaryQuantity, Variant, custom_classes = custom_classes)
203                 self.parse_property(statement, PROPERTY.SECONDARYUNIT, self.setSecondaryUnit, Variant, custom_classes = custom_classes)
204                 self.parse_property(statement, PROPERTY.DATATYPE, self.setDataType, Variant, custom_classes = custom_classes)
205                 self.parse_property(statement, PROPERTY.RDF_VALUE, self.setValue, Variant, custom_classes = custom_classes)
206                 self.parse_property(statement, PROPERTY.MAXIMUM, self.setMaximum, Variant, custom_classes = custom_classes)
207                 self.parse_property(statement, PROPERTY.MINIMUM, self.setMinimum, Variant, custom_classes = custom_classes)
208                 self.parse_property(statement, PROPERTY.INSTANT, self.setInstant, Variant, custom_classes = custom_classes)
209                 self.parse_property(statement, PROPERTY.TEMPORALCONTEXT, self.setTemporalContext, TemporalContext, custom_classes = custom_classes)
210                 super(ValueObject, self)._parseStatement(statement, custom_classes = custom_classes)