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