e1d691a776191f620253706780bd456a0448225b
[smartapi.git] / Common / Python / SmartAPI / model / Entity.py
1 from SmartAPI.common.PROPERTY import PROPERTY
2 from SmartAPI.common.RESOURCE import RESOURCE
3 from SmartAPI.rdf.Resource import Resource
4 from SmartAPI.model.Coordinates import Coordinates
5 from SmartAPI.model.Obj import Obj
6
7 from SmartAPI.common.Tools import Tools
8
9 import sys
10 import traceback
11 from rdflib.term import URIRef
12
13 class Entity(Obj):
14
15         def __init__(self, uri = None):
16                 Obj.__init__(self, uri)
17
18                 self.init_property(PROPERTY.ZONE, 'zone', self.hasZone, self.getZone)
19                 self.init_property(PROPERTY.HASADDRESS, 'address', self.hasAddress, self.getAddress)
20                 self.init_property(PROPERTY.ISMANAGEDBY, 'managedBy', self.hasManagedBy, self.getManagedBy)
21                 self.init_property(PROPERTY.LOCATION, 'coordinates', self.hasCoordinates, self.getCoordinates)
22                 self.init_property(PROPERTY.ROUTE, 'route', self.hasRoute, self.getRoute)
23                 self.init_property(PROPERTY.DC_CREATOR, 'creators', self.hasCreator, self.getCreators, islist = True)
24                 self.init_property(PROPERTY.OWNER, 'owners', self.hasOwner, self.getOwners, islist = True)
25                 self.init_property(PROPERTY.HASEVALUATION, 'controllabilities', self.hasControllability, self.getControllabilities, islist = True)
26                 self.init_property(PROPERTY.HASAVAILABILITY, 'availabilities', self.hasAvailability, self.getAvailabilities, islist = True)
27                 self.init_property(PROPERTY.HASDATAAVAILABILITY, 'dataAvailabilities', self.hasDataAvailability, self.getDataAvailabilities, islist = True)
28                 self.init_property(PROPERTY.HASCAPABILITY, 'capabilities', self.hasCapability, self.getCapabilities, islist = True)
29                 self.init_property(PROPERTY.CAPACITY, 'capacities', self.hasCapacities, self.getCapacities, islist = True)
30                 self.init_property(PROPERTY.MANAGES, 'managedEntities', self.hasManagedEntity, self.getManagedEntities, islist = True)
31                 self.init_property(PROPERTY.INTERFACE, 'interfaces', self.hasInterface, self.getInterfaces, islist = True)
32                 self.init_property(PROPERTY.VALUEOBJECT, 'valueObjects', self.hasValueObject, self.getValueObjects, islist = True)
33                 self.init_property(PROPERTY.HASURL, 'websites', self.hasWebsite, self.getWebsites, islist = True)
34                 self.init_property(PROPERTY.HASLOGO, 'logos', self.hasLogo, self.getLogos, islist = True)
35                 self.init_property(PROPERTY.HASPHOTO, 'photos', self.hasPhoto, self.getPhotos, islist = True)
36                 self.init_property(PROPERTY.HASSOUND, 'sounds', self.hasSound, self.getSounds, islist = True)
37                 
38                 self.setType(RESOURCE.ENTITY)
39
40         def _parseStatement(self, statement, custom_classes = None):
41                 from SmartAPI.model.Address import Address
42                 from SmartAPI.model.Coordinates import Coordinates
43                 from SmartAPI.model.Zone import Zone
44                 from SmartAPI.model.Controllability import Controllability
45                 from SmartAPI.model.Availability import Availability
46                 from SmartAPI.model.Activity import Activity
47                 from SmartAPI.model.Evaluation import Evaluation
48                 from SmartAPI.model.InterfaceAddress import InterfaceAddress
49                 from SmartAPI.model.Route import Route
50                 from SmartAPI.model.ValueObject import ValueObject
51                 from SmartAPI.rdf.Variant import Variant
52                 
53                 self.parse_property(statement, PROPERTY.ZONE, self.setZone, Zone, custom_classes = custom_classes)
54                 self.parse_property(statement, PROPERTY.LOCATION, self.setCoordinates, Coordinates, custom_classes = custom_classes)
55                 self.parse_property(statement, PROPERTY.HASADDRESS, self.setAddress, Address, custom_classes = custom_classes)
56                 self.parse_property(statement, PROPERTY.ISMANAGEDBY, self.setManagedBy, Variant, custom_classes = custom_classes)
57                 self.parse_property(statement, PROPERTY.HASEVALUATION, self.addControllability, Controllability, custom_classes = custom_classes)
58                 self.parse_property(statement, PROPERTY.HASAVAILABILITY, self.addAvailability, Availability, custom_classes = custom_classes)
59                 self.parse_property(statement, PROPERTY.HASDATAAVAILABILITY, self.addDataAvailability, Availability, custom_classes = custom_classes)
60                 self.parse_property(statement, PROPERTY.HASCAPABILITY, self.addCapability, Activity, custom_classes = custom_classes)
61                 self.parse_property(statement, PROPERTY.MANAGES, self.addManagedEntity, Variant, custom_classes = custom_classes)
62                 self.parse_property(statement, PROPERTY.CAPACITY, self.addCapacity, Evaluation, custom_classes = custom_classes)
63                 self.parse_property(statement, PROPERTY.OWNER, self.addOwner, Entity, custom_classes = custom_classes)
64                 self.parse_property(statement, PROPERTY.HASURL, self.addWebsite, Variant, custom_classes = custom_classes)
65                 self.parse_property(statement, PROPERTY.DC_CREATOR, self.addCreator, Entity, custom_classes = custom_classes)
66                 self.parse_property(statement, PROPERTY.HASLOGO, self.addLogo, Variant, custom_classes = custom_classes)
67                 self.parse_property(statement, PROPERTY.HASPHOTO, self.addPhoto, Variant, custom_classes = custom_classes)
68                 self.parse_property(statement, PROPERTY.HASSOUND, self.addSound, Variant, custom_classes = custom_classes)
69                 self.parse_property(statement, PROPERTY.VALUEOBJECT, self.addValueObject, ValueObject, custom_classes = custom_classes)
70                 self.parse_property(statement, PROPERTY.INTERFACE, self.addInterface, InterfaceAddress, custom_classes = custom_classes)
71                 self.parse_property(statement, PROPERTY.ROUTE, self.setRoute, Route, custom_classes = custom_classes)
72                 super(Entity, self)._parseStatement(statement, custom_classes = custom_classes)
73         
74         def hasZone(self):
75                 return self.zone is not None
76         
77         def getZone(self):
78                 return self.zone
79
80         def setZone(self, zone):
81                 self.zone = zone
82
83         def hasCoordinates(self):
84                 return (self.coordinates is not None)
85         
86         def getCoordinates(self):
87                 return self.coordinates
88
89 #       def setCoordinates(self, c):
90 #               self.coordinates = c
91                 
92         def setCoordinates(self, coordinates = None, uri = None, latitude = None, longitude = None, altitude = None):
93                 if ( coordinates != None ):
94                         self.coordinates = coordinates
95                 else:
96                         self.coordinates = Coordinates(uri = uri, latitude = latitude, longitude = longitude, altitude = altitude)
97         
98         def hasAddress(self):
99                 return (self.address is not None)
100         
101         def getAddress(self):
102                 return self.address
103
104         def setAddress(self, a):
105                 self.address = a
106
107         def hasControllability(self):
108                 return len(self.controllabilities) > 0
109         
110         def getControllabilities(self):
111                 return self.controllabilities
112         
113         def addControllability(self, controllability):
114                 self.controllabilities.append(controllability)
115
116         def hasAvailability(self):
117                 return len(self.availabilities) > 0
118                 
119         def getAvailabilities(self):
120                 return self.availabilities
121         
122         def addAvailability(self, availability):
123                 self.availabilities.append(availability)
124
125         def hasDataAvailability(self):
126                 return len(self.dataAvailabilities) > 0
127         
128         def getDataAvailabilities(self):
129                 return self.dataAvailabilities
130         
131         def addDataAvailability(self, availability):
132                 self.dataAvailabilities.append(availability)
133
134         def hasCapability(self):
135                 return len(self.capabilities) > 0
136
137         def setCapabilities(self, capabilities):
138                 self.capabilities = capabilities
139
140         def addCapability(self, capability):
141                 '''
142                 @param capability: either Activity object, or URI string 
143                 '''
144                 from SmartAPI.model.Activity import Activity
145                 
146                 if isinstance(capability, Activity):
147                         self.capabilities.append(capability)
148                 elif isinstance(capability, str):
149                         activity = Activity(capability)
150                         #activity.clearTypes()
151                         self.capabilities.append(activity)
152
153         def getCapabilities(self):
154                 return self.capabilities
155
156         def hasCapacities(self):
157                 return len(self.capacities) > 0
158
159         def getCapacities(self):
160                 return self.capacities
161         
162         def addCapacity(self, cap):
163                 self.capacities.append(cap)
164         
165         def hasManagedEntity(self):
166                 return len(self.managedEntities) > 0
167         
168         def getManagedEntities(self):
169                 return self.managedEntities
170
171         def setManagedEntity(self, e):
172                 self.managedEntities = []
173                 self.addManagedEntity(e)
174                 
175         def addManagedEntity(self, e):
176                 from SmartAPI.rdf.Variant import Variant
177                 if isinstance(e, Obj):
178                         e = e.getIdentifierUri()
179                 if isinstance(e, str):
180                         e = URIRef(e)
181                 if isinstance(e, URIRef):
182                         e = Variant(e)
183                 
184                 self.managedEntities.append(e)
185         
186         def hasManagedBy(self):
187                 return self.managedBy is not None
188         
189         def setManagedBy(self, obj):
190                 from SmartAPI.rdf.Variant import Variant
191                 if isinstance(obj, Obj):
192                         self.managedBy = Variant(URIRef(obj.getIdentifierUri()))
193                 elif isinstance(obj, str):
194                         self.managedBy = Variant(URIRef(obj))
195                 elif isinstance(obj, URIRef):
196                         self.managedBy = Variant(obj)
197                 else:
198                         self.managedBy = obj
199                 
200         def getManagedBy(self):
201                 return self.managedBy
202         
203         def hasInterface(self):
204                 return len(self.interfaces) > 0
205                 
206         def getInterfaces(self):
207                 return self.interfaces
208
209         def setInterface(self, interfaceAddress):
210                 self.interfaces = [interfaceAddress]
211
212         def addInterface(self, interfaceAddress):
213                 self.interfaces.append(interfaceAddress)
214                 
215         def hasOwner(self):
216                 return len(self.owners) > 0
217                 
218         def getOwners(self):
219                 return self.owners
220
221         def setOwner(self, o):
222                 self.owners = [o]
223
224         def addOwner(self, o):
225                 self.owners.append(o)
226         
227         def hasCreator(self):
228                 return len(self.creators) > 0
229                 
230         def getCreators(self):
231                 return self.creators
232
233         def setCreator(self, c):
234                 self.creators = [c]
235
236         def addCreator(self, c):
237                 self.creators.append(c)
238                 
239         def hasPhoto(self):
240                 return len(self.photos) > 0
241         
242         def getPhotos(self):
243                 return self.photos
244                 
245         def addPhoto(self, url):
246                 from SmartAPI.rdf.Variant import Variant
247                 if not isinstance(url, Variant):
248                         url = Variant(url)
249                 self.photos.append(url)
250                 
251         def hasLogo(self):
252                 return len(self.logos) > 0
253         
254         def getLogos(self):
255                 return self.logos
256         
257         def addLogo(self, logo):
258                 from SmartAPI.rdf.Variant import Variant
259                 if not isinstance(logo, Variant):
260                         logo = Variant(logo)
261                 self.logos.append(logo)
262                 
263         def hasWebsite(self):
264                 return len(self.websites) > 0
265         
266         def getWebsites(self):
267                 return self.websites
268         
269         def addWebsite(self, url):
270                 from SmartAPI.rdf.Variant import Variant
271                 if not isinstance(url, Variant):
272                         url = Variant(url)
273                 self.websites.append(url)
274         
275         def hasSound(self):
276                 return len(self.sounds) > 0
277         
278         def getSounds(self):
279                 return self.sounds
280         
281         def addSound(self, url):
282                 from SmartAPI.rdf.Variant import Variant
283                 if not isinstance(url, Variant):
284                         url = Variant(url)
285                 self.sounds.append(url)
286         
287         def hasRoute(self):
288                 return (self.route is not None)
289         
290         def getRoute(self):
291                 return self.route
292
293         def setRoute(self, r):
294                 self.route = r
295
296         def hasValueObject(self):
297                 return len(self.valueObjects) > 0
298         
299         def setValueObjects(self, valueObjects):
300                 self.valueObjects = valueObjects
301
302         def addValueObject(self, valueObject):
303                 self.valueObjects.append(valueObject)
304
305         def getValueObjects(self):
306                 return self.valueObjects
307         
308         def newValueObject(self, quantity, unit, dataType, description, types):
309                 '''
310                 @param types: a list of string, or one single string representing Type; 
311                 all the others are string types  
312                 '''
313                 from SmartAPI.model.ValueObject import ValueObject
314                 
315                 valueObject = ValueObject(quantity=quantity, unit=unit, dataType=dataType, 
316                                                                 description=description, types=types)
317                 self.addValueObject(valueObject)
318                 return valueObject
319         
320         def getValueObjectByQuantity(self, quantity):
321                 '''
322                 @type quantity: string 
323                 '''
324                 if quantity is not None:
325                         for vo in self.getValueObjects():
326                                 if vo.hasQuantity() and vo.getQuantity() == quantity:
327                                         return vo
328                 else:
329                         for v in self.getValueObjects():
330                                 if not v.hasQuantity():
331                                         return v
332                                 
333         def getValueObjectByIdentifier(self, identifier):
334                 '''
335                 @type identifier: URI string 
336                 '''
337                 if identifier is None or identifier=='':
338                         return None
339                 else:
340                         for vo in self.getValueObjects():
341                                 if vo.hasIdentifierUri(identifier):
342                                         return vo
343                         return None