Fixed Find server URLs, AdaptDataService demo, support for new
[smartapi.git] / Common / Python / SmartAPI / model / Activity.py
1 from SmartAPI.common.RESOURCE import RESOURCE
2 from SmartAPI.common.PROPERTY import PROPERTY
3 from SmartAPI.common.Tools import Tools
4 from SmartAPI.rdf.Resource import Resource
5
6 from SmartAPI.model.Entity import Entity
7 from SmartAPI.model.Evaluation import Evaluation
8 from SmartAPI.model.Obj import Obj
9 from SmartAPI.model.Input import Input
10 from SmartAPI.rdf.Variant import Variant
11
12 import traceback
13 from SmartAPI.model.TemporalContext import TemporalContext
14 from rdflib.term import URIRef
15
16
17 class Activity(Evaluation):
18
19         def __init__(self, uri = None):
20                 Evaluation.__init__(self, uri)
21                 self.setType(RESOURCE.ACTIVITY)
22                 
23                 self.init_property(PROPERTY.HASINPUT, 'inputs', self.hasInput, self.getInputs, True)
24                 self.init_property(PROPERTY.HASREFINPUT, 'refInputs', self.hasRefInput, self.getRefInputs, True)
25                 self.init_property(PROPERTY.HASOUTPUT, 'outputs', self.hasOutput, self.getOutputs, True)
26                 self.init_property(PROPERTY.HASREFOUTPUT, 'refOutputs', self.hasRefOutput, self.getRefOutputs, True)
27                 self.init_property(PROPERTY.HASAVAILABILITY, 'availabilities', self.hasAvailability, self.getAvailabilities, True)
28                 self.init_property(PROPERTY.AUTHORIZATION, 'authorization', self.hasAuthorization, self.getAuthorization)
29                 self.init_property(PROPERTY.HASDATAAVAILABILITY, 'dataAvailabilities', self.hasDataAvailability, self.getDataAvailabilities, True)
30                 self.init_property(PROPERTY.INTERFACE, 'interfaces', self.hasInterface, self.getInterfaces, True)
31                 self.init_property(PROPERTY.ENTITY, 'entities', self.hasEntity, self.getEntities, True)
32                 
33                 
34         def _parseStatement(self, statement, custom_classes = None):
35                 from SmartAPI.model.Availability import Availability
36                 from SmartAPI.model.Authorization import Authorization
37                 from SmartAPI.model.Input import Input
38                 from SmartAPI.model.Output import Output
39                 from SmartAPI.model.InterfaceAddress import InterfaceAddress
40                 from SmartAPI.model.Entity import Entity
41                 
42                 self.parse_property(statement, PROPERTY.HASINPUT, self.addInput, Input, custom_classes = custom_classes)
43                 self.parse_property(statement, PROPERTY.HASREFINPUT, self.addRefInput, Input, custom_classes = custom_classes)
44                 self.parse_property(statement, PROPERTY.HASOUTPUT, self.addOutput, Output, custom_classes = custom_classes)
45                 self.parse_property(statement, PROPERTY.HASREFOUTPUT, self.addRefOutput, Output, custom_classes = custom_classes)
46                 self.parse_property(statement, PROPERTY.HASAVAILABILITY, self.addAvailability, Availability, custom_classes = custom_classes)
47                 self.parse_property(statement, PROPERTY.HASDATAAVAILABILITY, self.addDataAvailability, Availability, custom_classes = custom_classes)
48                 self.parse_property(statement, PROPERTY.INTERFACE, self.addInterface, InterfaceAddress, custom_classes = custom_classes)
49                 self.parse_property(statement, PROPERTY.ENTITY, self.addEntity, Entity, custom_classes = custom_classes)
50                 self.parse_property(statement, PROPERTY.AUTHORIZATION, self.setAuthorization, Authorization, custom_classes = custom_classes)
51                 
52                 super(Activity, self)._parseStatement(statement, custom_classes = custom_classes)
53
54         def hasInput(self):
55                 return len(self.inputs) > 0
56
57         def getInputs(self):
58                 return self.inputs
59         
60         def setInput(self, input):
61                 self.inputs = [input]
62
63         def addInput(self, input):
64                 self.inputs.append(input)
65                 
66         def hasRefInput(self):
67                 return len(self.refInputs) > 0
68         
69         def getRefInputs(self):
70                 return self.refInputs
71         
72         def addRefInput(self, refInput):
73                 self.refInputs.append(refInput)
74
75         def hasOutput(self):
76                 return len(self.outputs) > 0
77
78         def getOutputs(self):
79                 return self.outputs
80         
81         def setOutput(self, output):
82                 self.outputs = [output]
83
84         def addOutput(self, output):
85                 self.outputs.append(output)
86                 
87         def hasRefOutput(self):
88                 return len(self.refOutputs) > 0
89         
90         def getRefOutputs(self):
91                 return self.refOutputs
92         
93         def addRefOutput(self, refOutput):
94                 self.refOutputs.append(refOutput)
95
96         def hasAuthorization(self):
97                 return self.authorization is not None
98         
99         def getAuthorization(self):
100                 return self.authorization
101         
102         def setAuthorization(self, authorization):
103                 self.authorization = authorization
104         
105         def hasAvailability(self):
106                 return len(self.availabilities) > 0
107         
108         def getAvailabilities(self):
109                 return self.availabilities
110         
111         def addAvailability(self, availability):
112                 self.availabilities.append(availability)
113
114         def hasDataAvailability(self):
115                 return len(self.dataAvailabilities) > 0
116         
117         def getDataAvailabilities(self):
118                 return self.dataAvailabilities
119         
120         def addDataAvailability(self, availability):
121                 self.dataAvailabilities.append(availability)
122
123         def getInterfaces(self):
124                 return self.interfaces
125         
126         def hasInterface(self):
127                 return len(self.interfaces) > 0
128         
129         def getHTTPInterface(self):
130                 for ia in self.getInterfaces():
131                         if ia.hasScheme() and (ia.getScheme().lower() == 'http'):
132                                 return ia
133         
134         def setInterface(self, interfaceAddress):
135                 self.interfaces = [interfaceAddress]
136
137         def addInterface(self, interfaceAddress):
138                 self.interfaces.append(interfaceAddress)
139                 
140         def hasEntity(self):
141                 return len(self.entities) > 0
142
143         def setEntities(self, entities):
144                 '''
145                 @param entities: a list of Entity objects 
146                 '''
147                 self.entities = entities
148
149         def addEntity(self, entity):
150                 self.entities.append(entity)
151
152         def getEntities(self):
153                 return self.entities
154         
155         def getEntity(self, identifierUri):
156                 for entity in self.entities:
157                         if identifierUri == entity.getIdentifierUri():
158                                 return entity
159                 return None
160
161         def newEntity(self, identifier=None):
162                 if identifier:
163                         entity = Entity(identifier)
164                         self.addEntity(entity)
165                         return entity
166                 else:
167                         entity = Entity()
168                         self.addEntity(entity)
169                         return entity
170
171         def firstEntity(self):
172                 return getFirstEntity()
173         
174         def getFirstEntity(self):
175                 try:
176                         return self.entities[0]
177                 except:
178                         return None
179
180         def firstInput(self):
181                 return getFirstInput()
182         
183         def getFirstInput(self):
184                 try:
185                         return self.inputs[0]
186                 except:
187                         return None
188    
189         def firstOutput(self):
190                 return getFirstOutput()
191
192         def getFirstOutput(self):
193                 try:
194                         return self.outputs[0]
195                 except:
196                         return None
197