|
1
|
|
|
package it.cnr.istc.pst.cognition.koala.reasoner.owl.jena; |
|
2
|
|
|
|
|
3
|
|
|
import java.util.HashMap; |
|
4
|
|
|
import java.util.List; |
|
5
|
|
|
import java.util.Map; |
|
6
|
|
|
|
|
7
|
|
|
import org.apache.jena.rdf.model.Model; |
|
8
|
|
|
import org.apache.jena.rdf.model.Resource; |
|
9
|
|
|
|
|
10
|
|
|
import it.cnr.istc.pst.cognition.koala.reasoner.environment.EnvironmentReasoner; |
|
11
|
|
|
import it.cnr.istc.pst.cognition.koala.reasoner.environment.parser.xml.Element; |
|
12
|
|
|
import it.cnr.istc.pst.cognition.koala.reasoner.environment.parser.xml.Sensor; |
|
13
|
|
|
import it.cnr.istc.pst.cognition.koala.reasoner.environment.parser.xml.XMLEnvironmentConfigurationParser; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* @author anacleto |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
public class OWLKoalaEnvironmentReasoner extends EnvironmentReasoner |
|
21
|
|
|
{ |
|
22
|
|
|
public OWLKoalaModel kb; // the knowledge base |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* @param ontologyFilePath |
|
27
|
|
|
* @param ruleFilePath |
|
28
|
|
|
*/ |
|
29
|
|
|
public OWLKoalaEnvironmentReasoner(String ontologyFilePath, String ruleFilePath) { |
|
30
|
|
|
// create a knowledge-base instance |
|
31
|
|
|
this.kb = new OWLKoalaModel(ontologyFilePath, ruleFilePath); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* @return |
|
37
|
|
|
*/ |
|
38
|
|
|
@Override |
|
39
|
|
|
public Model getModel() { |
|
40
|
|
|
// get current knowledge base |
|
41
|
|
|
return this.kb.getModel(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Setup the knowledge-based by reading the XML configuration file of the environment |
|
46
|
|
|
*/ |
|
47
|
|
|
public void init(String envConfigFilePath) |
|
48
|
|
|
{ |
|
49
|
|
|
try |
|
50
|
|
|
{ |
|
51
|
|
|
// resource index |
|
52
|
|
|
Map<Element, Resource> index = new HashMap<Element, Resource>(); |
|
53
|
|
|
|
|
54
|
|
|
// get XML configuration parser |
|
55
|
|
|
XMLEnvironmentConfigurationParser parser = new XMLEnvironmentConfigurationParser(envConfigFilePath); |
|
56
|
|
|
// get the list of elements |
|
57
|
|
|
List<Element> elements = parser.getElements(); |
|
58
|
|
|
// check room elements |
|
59
|
|
|
for (Element element : elements) |
|
60
|
|
|
{ |
|
61
|
|
|
// create room individual |
|
62
|
|
|
Resource resource = this.doCreateElementIndividual(element); |
|
63
|
|
|
// index element |
|
64
|
|
|
index.put(element, resource); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// assert structure information |
|
68
|
|
|
for (Element element : elements) |
|
69
|
|
|
{ |
|
70
|
|
|
// check part of |
|
71
|
|
|
if (element.getPartOf() != null) |
|
72
|
|
|
{ |
|
73
|
|
|
// get parent resource |
|
74
|
|
|
Resource rParent = index.get(element.getPartOf()); |
|
75
|
|
|
Resource rChild = index.get(element); |
|
76
|
|
|
|
|
77
|
|
|
// locate object into the room |
|
78
|
|
|
this.doLocateObjectIntoRoom(rChild, rParent); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
// get list of sensors |
|
84
|
|
|
List<Sensor> sensors = parser.getSensors(); |
|
85
|
|
|
for (Sensor sensor : sensors) |
|
86
|
|
|
{ |
|
87
|
|
|
// create sensor individual |
|
88
|
|
|
Resource resource = this.doCreateSensorIndividual(sensor); |
|
89
|
|
|
// get sensor target |
|
90
|
|
|
Resource target = index.get(sensor.getTarget()); |
|
91
|
|
|
// install sensor on object |
|
92
|
|
|
this.doInstallSensorOnElement(resource, target); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
catch (Exception ex) { |
|
96
|
|
|
throw new RuntimeException("Knowledge initialization error:\n- message: " + ex.getMessage() + "\n"); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* |
|
102
|
|
|
* @param room |
|
103
|
|
|
* @return |
|
104
|
|
|
* @throws Exception |
|
105
|
|
|
*/ |
|
106
|
|
|
private Resource doCreateElementIndividual(Element element) |
|
107
|
|
|
throws Exception |
|
108
|
|
|
{ |
|
109
|
|
|
// get room classification |
|
110
|
|
|
String classURI = element.getType(); |
|
111
|
|
|
// create individual |
|
112
|
|
|
Resource resource = this.kb.createIndividual(classURI); |
|
113
|
|
|
// assert data property |
|
114
|
|
|
this.kb.assertDataProperty(resource.getURI(), OWLKoalaNameSpace.KOALA + "hasId", new Long(element.getId())); |
|
115
|
|
|
// create space region to model spatial information of the room |
|
116
|
|
|
Resource region = this.kb.createIndividual(OWLKoalaNameSpace.DUL + "SpaceRegion"); |
|
117
|
|
|
// assert property |
|
118
|
|
|
this.kb.assertProperty( |
|
119
|
|
|
resource.getURI(), |
|
120
|
|
|
OWLKoalaNameSpace.DUL + "hasRegion", |
|
121
|
|
|
region.getURI()); |
|
122
|
|
|
// get created individual |
|
123
|
|
|
return resource; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* |
|
128
|
|
|
* @param sensor |
|
129
|
|
|
* @return |
|
130
|
|
|
* @throws Exception |
|
131
|
|
|
*/ |
|
132
|
|
|
private Resource doCreateSensorIndividual(Sensor sensor) |
|
133
|
|
|
throws Exception |
|
134
|
|
|
{ |
|
135
|
|
|
// get sensor classification |
|
136
|
|
|
String classURI = sensor.getType(); |
|
137
|
|
|
// create individual into the knowledge base |
|
138
|
|
|
Resource resource = this.kb.createIndividual(classURI); |
|
139
|
|
|
// assert data property |
|
140
|
|
|
this.kb.assertDataProperty(resource.getURI(), OWLKoalaNameSpace.KOALA + "hasId", new Long(sensor.getId())); |
|
141
|
|
|
// check sensor state |
|
142
|
|
|
switch (sensor.getState()) |
|
143
|
|
|
{ |
|
144
|
|
|
// check failure state |
|
145
|
|
|
case FAILURE: { |
|
146
|
|
|
// assert failure state |
|
147
|
|
|
this.kb.assertProperty(resource.getURI(), OWLKoalaNameSpace.KOALA.getNs() + "hasDiagnosis", OWLKoalaNameSpace.KOALA_FAILURE_STATE_INDIVIDUAL.getNs()); |
|
148
|
|
|
} |
|
149
|
|
|
break; |
|
150
|
|
|
|
|
151
|
|
|
// check maintenance state |
|
152
|
|
|
case MAINENTANCE: { |
|
153
|
|
|
// assert maintenance state |
|
154
|
|
|
this.kb.assertProperty(resource.getURI(), OWLKoalaNameSpace.KOALA.getNs() + "hasDiagnosis", OWLKoalaNameSpace.KOALA_MAINTENANCE_STATE_INDIVIDUAL.getNs()); |
|
155
|
|
|
} |
|
156
|
|
|
break; |
|
157
|
|
|
|
|
158
|
|
|
// check off state |
|
159
|
|
|
case OFF: { |
|
160
|
|
|
// assert off state |
|
161
|
|
|
this.kb.assertProperty(resource.getURI(), OWLKoalaNameSpace.KOALA.getNs() + "hasDiagnosis", OWLKoalaNameSpace.KOALA_OFF_STATE_INDIVIDUAL.getNs()); |
|
162
|
|
|
} |
|
163
|
|
|
break; |
|
164
|
|
|
|
|
165
|
|
|
// check on state |
|
166
|
|
|
case ON: { |
|
167
|
|
|
// assert on state |
|
168
|
|
|
this.kb.assertProperty(resource.getURI(), OWLKoalaNameSpace.KOALA.getNs() + "hasDiagnosis", OWLKoalaNameSpace.KOALA_ON_STATE_INDIVIDUAL.getNs()); |
|
169
|
|
|
} |
|
170
|
|
|
break; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
// get created individual |
|
174
|
|
|
return resource; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* |
|
179
|
|
|
* @param object |
|
180
|
|
|
* @param room |
|
181
|
|
|
* @throws Exception |
|
182
|
|
|
*/ |
|
183
|
|
|
private void doLocateObjectIntoRoom(Resource object, Resource room) |
|
184
|
|
|
throws Exception |
|
185
|
|
|
{ |
|
186
|
|
|
// simply assert property - use DUL:hasPart for transitivity, DUL:hasComponent otherwise |
|
187
|
|
|
this.kb.assertProperty(room.getURI(), OWLKoalaNameSpace.DUL + "hasComponent", object.getURI()); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* |
|
192
|
|
|
* @param sensor |
|
193
|
|
|
* @param element |
|
194
|
|
|
* @throws Exception |
|
195
|
|
|
*/ |
|
196
|
|
|
private void doInstallSensorOnElement(Resource sensor, Resource element) |
|
197
|
|
|
throws Exception |
|
198
|
|
|
{ |
|
199
|
|
|
// create a platform form the object |
|
200
|
|
|
Resource platform = this.kb.createIndividual(OWLKoalaNameSpace.SSN + "Platform"); |
|
201
|
|
|
// associate the platform to the holding element/object of the environment |
|
202
|
|
|
this.kb.assertProperty(element.getURI(), OWLKoalaNameSpace.DUL + "hasLocation", platform.getURI()); |
|
203
|
|
|
|
|
204
|
|
|
// deploy sensor on platform |
|
205
|
|
|
Resource deployment = this.kb.createIndividual(OWLKoalaNameSpace.SSN + "Deployment"); |
|
206
|
|
|
// assert related statements |
|
207
|
|
|
this.kb.assertProperty(sensor.getURI(), OWLKoalaNameSpace.SSN + "hasDeployment", deployment.getURI()); |
|
208
|
|
|
this.kb.assertProperty(deployment.getURI(), OWLKoalaNameSpace.SSN + "deployedOnPlatform", platform.getURI()); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// /** |
|
212
|
|
|
// * |
|
213
|
|
|
// * @param args |
|
214
|
|
|
// */ |
|
215
|
|
|
// public static void main(String[] args) |
|
216
|
|
|
// { |
|
217
|
|
|
// try |
|
218
|
|
|
// { |
|
219
|
|
|
// // create knowledge manager |
|
220
|
|
|
// OWLEnvironmentReasoner km = new OWLEnvironmentReasoner( |
|
221
|
|
|
// "etc/ontology/koala_v1.0.owl", |
|
222
|
|
|
// "etc/ontology/feature_extraction_v1.0.rules"); |
|
223
|
|
|
// |
|
224
|
|
|
// // initialize reasoner |
|
225
|
|
|
// km.init("etc/environment/house_config.xml"); |
|
226
|
|
|
// |
|
227
|
|
|
// |
|
228
|
|
|
// |
|
229
|
|
|
// System.out.println("-----------------------------------------------------------------------------------------"); |
|
230
|
|
|
// km.kb.listStatements(OWLNameSpace.SSN + "deployedOnPlatform"); |
|
231
|
|
|
// System.out.println("-----------------------------------------------------------------------------------------"); |
|
232
|
|
|
// km.kb.listStatements(OWLNameSpace.DUL + "hasComponent"); |
|
233
|
|
|
// System.out.println("-----------------------------------------------------------------------------------------"); |
|
234
|
|
|
// km.kb.listStatements(OWLNameSpace.KOALA + "hasDiagnosis"); |
|
235
|
|
|
// // check detected features of interest |
|
236
|
|
|
// System.out.println("\n-------------------------------------------------------------------------------------------------\n" |
|
237
|
|
|
// + "\tList of detected observable features of the environment\n" |
|
238
|
|
|
// + "-------------------------------------------------------------------------------------------------\n"); |
|
239
|
|
|
// |
|
240
|
|
|
// // list detected observable features |
|
241
|
|
|
// km.kb.listIndividualsOfClass(OWLNameSpace.KOALA + "ObservableFeature"); |
|
242
|
|
|
// System.out.println("|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"); |
|
243
|
|
|
// km.kb.listStatements(OWLNameSpace.KOALA + "hasObservableFeature"); |
|
244
|
|
|
// |
|
245
|
|
|
// System.out.println("#######################################################################################################"); |
|
246
|
|
|
// // list properties that can be observed through features |
|
247
|
|
|
// km.kb.listStatements(OWLNameSpace.KOALA + "hasObservableProperty"); |
|
248
|
|
|
// } |
|
249
|
|
|
// catch (Exception ex) { |
|
250
|
|
|
// System.err.println(ex.getMessage()); |
|
251
|
|
|
// } |
|
252
|
|
|
// } |
|
253
|
|
|
} |
|
254
|
|
|
|