| Conditions | 16 |
| Total Lines | 405 |
| Code Lines | 248 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like it.cnr.istc.pst.cognition.koala.reasoner.owl.jena.OWLKoalaObservationReasoner.doHandleObservation(String,Object,String) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package it.cnr.istc.pst.cognition.koala.reasoner.owl.jena; |
||
| 107 | @Override |
||
| 108 | public void doHandleObservation(String sensorId, Object observationValue, String propertyUri) |
||
| 109 | throws Exception |
||
| 110 | { |
||
| 111 | // get current time |
||
| 112 | long now = System.currentTimeMillis(); |
||
| 113 | try |
||
| 114 | { |
||
| 115 | // retrieve sensor individual according to the ID |
||
| 116 | Resource sensor = this.kb.getResourceById(new Long(sensorId)); |
||
| 117 | // create observation individual |
||
| 118 | Resource observation = this.kb.createIndividual(OWLKoalaNameSpace.SSN.getNs() + "Observation"); |
||
| 119 | // create sensor output individual |
||
| 120 | Resource output = this.kb.createIndividual(OWLKoalaNameSpace.SSN.getNs() + "SensorOutput"); |
||
| 121 | |||
| 122 | // assert properties |
||
| 123 | this.kb.assertProperty(observation.getURI(), OWLKoalaNameSpace.SSN.getNs() + "hasOutput", output.getURI()); |
||
| 124 | this.kb.assertProperty(observation.getURI(), OWLKoalaNameSpace.SSN.getNs() + "observedBy", sensor.getURI()); |
||
| 125 | |||
| 126 | // check property in order to create observation value accordingly |
||
| 127 | switch (KoalaPropertyDictionary.getPropertyByURI(propertyUri)) |
||
| 128 | { |
||
| 129 | case KOALA_LUMINOSITY : |
||
| 130 | { |
||
| 131 | // assert observation property |
||
| 132 | this.kb.assertProperty( |
||
| 133 | observation.getURI(), |
||
| 134 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 135 | OWLKoalaNameSpace.KOALA.getNs() + "Luminosity"); |
||
| 136 | |||
| 137 | |||
| 138 | // create observation value |
||
| 139 | Resource value = this.kb.createIndividual( |
||
| 140 | OWLKoalaNameSpace.KOALA.getNs() + "LuminosityObservationValue"); |
||
| 141 | |||
| 142 | // assert data property |
||
| 143 | this.kb.assertDataProperty( |
||
| 144 | value.getURI(), |
||
| 145 | OWLKoalaNameSpace.KOALA.getNs() + "hasLuminosityValue", |
||
| 146 | (Double) observationValue); |
||
| 147 | |||
| 148 | // assert property |
||
| 149 | this.kb.assertProperty( |
||
| 150 | output.getURI(), |
||
| 151 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 152 | value.getURI()); |
||
| 153 | } |
||
| 154 | break; |
||
| 155 | |||
| 156 | case KOALA_TEMPERATURE : |
||
| 157 | { |
||
| 158 | // assert observation property |
||
| 159 | this.kb.assertProperty( |
||
| 160 | observation.getURI(), |
||
| 161 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 162 | OWLKoalaNameSpace.KOALA.getNs() + "Temperature"); |
||
| 163 | |||
| 164 | |||
| 165 | // create observation value |
||
| 166 | Resource value = this.kb.createIndividual( |
||
| 167 | OWLKoalaNameSpace.KOALA.getNs() + "TemperatureObservationValue"); |
||
| 168 | |||
| 169 | // assert data property |
||
| 170 | this.kb.assertDataProperty( |
||
| 171 | value.getURI(), |
||
| 172 | OWLKoalaNameSpace.KOALA.getNs() + "hasTemperatureValue", |
||
| 173 | (Double) observationValue); |
||
| 174 | |||
| 175 | // assert property |
||
| 176 | this.kb.assertProperty( |
||
| 177 | output.getURI(), |
||
| 178 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 179 | value.getURI()); |
||
| 180 | } |
||
| 181 | break; |
||
| 182 | |||
| 183 | |||
| 184 | case KOALA_ENERGY : |
||
| 185 | { |
||
| 186 | // assert observation property |
||
| 187 | this.kb.assertProperty( |
||
| 188 | observation.getURI(), |
||
| 189 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 190 | OWLKoalaNameSpace.KOALA.getNs() + "Energy"); |
||
| 191 | |||
| 192 | |||
| 193 | // create observation value |
||
| 194 | Resource value = this.kb.createIndividual( |
||
| 195 | OWLKoalaNameSpace.KOALA.getNs() + "EnergyObservationValue"); |
||
| 196 | |||
| 197 | // assert data property |
||
| 198 | this.kb.assertDataProperty( |
||
| 199 | value.getURI(), |
||
| 200 | OWLKoalaNameSpace.KOALA.getNs() + "hasEnergyValue", |
||
| 201 | (Double) observationValue); |
||
| 202 | |||
| 203 | // assert property |
||
| 204 | this.kb.assertProperty( |
||
| 205 | output.getURI(), |
||
| 206 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 207 | value.getURI()); |
||
| 208 | } |
||
| 209 | break; |
||
| 210 | |||
| 211 | case KOALA_PRESENCE : |
||
| 212 | { |
||
| 213 | // assert observation property |
||
| 214 | this.kb.assertProperty( |
||
| 215 | observation.getURI(), |
||
| 216 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 217 | OWLKoalaNameSpace.KOALA.getNs() + "Presence"); |
||
| 218 | |||
| 219 | |||
| 220 | // create observation value |
||
| 221 | Resource value = this.kb.createIndividual( |
||
| 222 | OWLKoalaNameSpace.KOALA.getNs() + "PresenceObservationValue"); |
||
| 223 | |||
| 224 | // assert data property |
||
| 225 | this.kb.assertDataProperty( |
||
| 226 | value.getURI(), |
||
| 227 | OWLKoalaNameSpace.KOALA.getNs() + "hasPresenceValue", |
||
| 228 | (Boolean) observationValue); |
||
| 229 | |||
| 230 | // assert property |
||
| 231 | this.kb.assertProperty( |
||
| 232 | output.getURI(), |
||
| 233 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 234 | value.getURI()); |
||
| 235 | |||
| 236 | } |
||
| 237 | break; |
||
| 238 | |||
| 239 | case KOALA_BATTERY_LEVEL: |
||
| 240 | { |
||
| 241 | // assert observation property |
||
| 242 | this.kb.assertProperty( |
||
| 243 | observation.getURI(), |
||
| 244 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 245 | OWLKoalaNameSpace.KOALA.getNs() + "BatteryLevel"); |
||
| 246 | |||
| 247 | |||
| 248 | // create observation value |
||
| 249 | Resource value = this.kb.createIndividual( |
||
| 250 | OWLKoalaNameSpace.KOALA.getNs() + "BatteryLevelObservationValue"); |
||
| 251 | |||
| 252 | // assert data property |
||
| 253 | this.kb.assertDataProperty( |
||
| 254 | value.getURI(), |
||
| 255 | OWLKoalaNameSpace.KOALA.getNs() + "hasBatteryLevelValue", |
||
| 256 | (Double) observationValue); |
||
| 257 | |||
| 258 | // assert property |
||
| 259 | this.kb.assertProperty( |
||
| 260 | output.getURI(), |
||
| 261 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 262 | value.getURI()); |
||
| 263 | } |
||
| 264 | break; |
||
| 265 | |||
| 266 | |||
| 267 | case KOALA_BLOOD_PRESSURE : |
||
| 268 | { |
||
| 269 | // get observed ranges |
||
| 270 | Integer[] ranges = (Integer[]) observationValue; |
||
| 271 | int min = ranges[0]; |
||
| 272 | int max = ranges[1]; |
||
| 273 | |||
| 274 | // assert observation property |
||
| 275 | this.kb.assertProperty( |
||
| 276 | observation.getURI(), |
||
| 277 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 278 | OWLKoalaNameSpace.KOALA.getNs() + "BloodPressure"); |
||
| 279 | |||
| 280 | |||
| 281 | // create observation value |
||
| 282 | Resource value = this.kb.createIndividual( |
||
| 283 | OWLKoalaNameSpace.KOALA.getNs() + "BloodPressureObservationValue"); |
||
| 284 | |||
| 285 | // assert data property |
||
| 286 | this.kb.assertDataProperty( |
||
| 287 | value.getURI(), |
||
| 288 | OWLKoalaNameSpace.KOALA.getNs() + "hasMinBloodPressureValue", |
||
| 289 | min); |
||
| 290 | |||
| 291 | this.kb.assertDataProperty( |
||
| 292 | value.getURI(), |
||
| 293 | OWLKoalaNameSpace.KOALA.getNs() + "hasMaxBloodPressureValue", |
||
| 294 | max); |
||
| 295 | |||
| 296 | // assert property |
||
| 297 | this.kb.assertProperty( |
||
| 298 | output.getURI(), |
||
| 299 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 300 | value.getURI()); |
||
| 301 | } |
||
| 302 | break; |
||
| 303 | |||
| 304 | |||
| 305 | case KOALA_BLOOD_SUGAR : |
||
| 306 | { |
||
| 307 | // assert observation property |
||
| 308 | this.kb.assertProperty( |
||
| 309 | observation.getURI(), |
||
| 310 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 311 | OWLKoalaNameSpace.KOALA.getNs() + "BloodSugar"); |
||
| 312 | |||
| 313 | |||
| 314 | // create observation value |
||
| 315 | Resource value = this.kb.createIndividual( |
||
| 316 | OWLKoalaNameSpace.KOALA.getNs() + "BloodSugarObservationValue"); |
||
| 317 | |||
| 318 | // assert data property |
||
| 319 | this.kb.assertDataProperty( |
||
| 320 | value.getURI(), |
||
| 321 | OWLKoalaNameSpace.KOALA.getNs() + "hasBloodSugarValue", |
||
| 322 | (Double) observationValue); |
||
| 323 | |||
| 324 | // assert property |
||
| 325 | this.kb.assertProperty( |
||
| 326 | output.getURI(), |
||
| 327 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 328 | value.getURI()); |
||
| 329 | } |
||
| 330 | break; |
||
| 331 | |||
| 332 | case KOALA_BODY_TEMPERATURE : |
||
| 333 | { |
||
| 334 | // assert observation property |
||
| 335 | this.kb.assertProperty( |
||
| 336 | observation.getURI(), |
||
| 337 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 338 | OWLKoalaNameSpace.KOALA.getNs() + "BodyTemperature"); |
||
| 339 | |||
| 340 | |||
| 341 | // create observation value |
||
| 342 | Resource value = this.kb.createIndividual( |
||
| 343 | OWLKoalaNameSpace.KOALA.getNs() + "BodyTemperatureObservationValue"); |
||
| 344 | |||
| 345 | // assert data property |
||
| 346 | this.kb.assertDataProperty( |
||
| 347 | value.getURI(), |
||
| 348 | OWLKoalaNameSpace.KOALA.getNs() + "hasBodyTemperatureValue", |
||
| 349 | (Double) observationValue); |
||
| 350 | |||
| 351 | // assert property |
||
| 352 | this.kb.assertProperty( |
||
| 353 | output.getURI(), |
||
| 354 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 355 | value.getURI()); |
||
| 356 | } |
||
| 357 | break; |
||
| 358 | |||
| 359 | |||
| 360 | case KOALA_BODY_WEIGHT : |
||
| 361 | { |
||
| 362 | // assert observation property |
||
| 363 | this.kb.assertProperty( |
||
| 364 | observation.getURI(), |
||
| 365 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 366 | OWLKoalaNameSpace.KOALA.getNs() + "BodyWeight"); |
||
| 367 | |||
| 368 | |||
| 369 | // create observation value |
||
| 370 | Resource value = this.kb.createIndividual( |
||
| 371 | OWLKoalaNameSpace.KOALA.getNs() + "BodyWeightObservationValue"); |
||
| 372 | |||
| 373 | // assert data property |
||
| 374 | this.kb.assertDataProperty( |
||
| 375 | value.getURI(), |
||
| 376 | OWLKoalaNameSpace.KOALA.getNs() + "hasBodyWeightValue", |
||
| 377 | (Double) observationValue); |
||
| 378 | |||
| 379 | // assert property |
||
| 380 | this.kb.assertProperty( |
||
| 381 | output.getURI(), |
||
| 382 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 383 | value.getURI()); |
||
| 384 | } |
||
| 385 | break; |
||
| 386 | |||
| 387 | |||
| 388 | case KOALA_CONTACT : |
||
| 389 | { |
||
| 390 | // assert observation property |
||
| 391 | this.kb.assertProperty( |
||
| 392 | observation.getURI(), |
||
| 393 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 394 | OWLKoalaNameSpace.KOALA.getNs() + "Contact"); |
||
| 395 | |||
| 396 | |||
| 397 | // create observation value |
||
| 398 | Resource value = this.kb.createIndividual( |
||
| 399 | OWLKoalaNameSpace.KOALA.getNs() + "ContactObservationValue"); |
||
| 400 | |||
| 401 | // assert data property |
||
| 402 | this.kb.assertDataProperty( |
||
| 403 | value.getURI(), |
||
| 404 | OWLKoalaNameSpace.KOALA.getNs() + "hasContactValue", |
||
| 405 | (Boolean) observationValue); |
||
| 406 | |||
| 407 | // assert property |
||
| 408 | this.kb.assertProperty( |
||
| 409 | output.getURI(), |
||
| 410 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 411 | value.getURI()); |
||
| 412 | } |
||
| 413 | break; |
||
| 414 | |||
| 415 | |||
| 416 | case KOALA_HEART_RATE : |
||
| 417 | { |
||
| 418 | // assert observation property |
||
| 419 | this.kb.assertProperty( |
||
| 420 | observation.getURI(), |
||
| 421 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 422 | OWLKoalaNameSpace.KOALA.getNs() + "HeartRate"); |
||
| 423 | |||
| 424 | |||
| 425 | // create observation value |
||
| 426 | Resource value = this.kb.createIndividual( |
||
| 427 | OWLKoalaNameSpace.KOALA.getNs() + "HeartRateObservationValue"); |
||
| 428 | |||
| 429 | // assert data property |
||
| 430 | this.kb.assertDataProperty( |
||
| 431 | value.getURI(), |
||
| 432 | OWLKoalaNameSpace.KOALA.getNs() + "hasHeartRateValue", |
||
| 433 | (Double) observationValue); |
||
| 434 | |||
| 435 | // assert property |
||
| 436 | this.kb.assertProperty( |
||
| 437 | output.getURI(), |
||
| 438 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 439 | value.getURI()); |
||
| 440 | } |
||
| 441 | break; |
||
| 442 | |||
| 443 | |||
| 444 | case KOALA_OXIMETRY : |
||
| 445 | { |
||
| 446 | // assert observation property |
||
| 447 | this.kb.assertProperty( |
||
| 448 | observation.getURI(), |
||
| 449 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 450 | OWLKoalaNameSpace.KOALA.getNs() + "Oximetry"); |
||
| 451 | |||
| 452 | |||
| 453 | // create observation value |
||
| 454 | Resource value = this.kb.createIndividual( |
||
| 455 | OWLKoalaNameSpace.KOALA.getNs() + "OximetryObservationValue"); |
||
| 456 | |||
| 457 | // assert data property |
||
| 458 | this.kb.assertDataProperty( |
||
| 459 | value.getURI(), |
||
| 460 | OWLKoalaNameSpace.KOALA.getNs() + "hasOximetryValue", |
||
| 461 | (Double) observationValue); |
||
| 462 | |||
| 463 | // assert property |
||
| 464 | this.kb.assertProperty( |
||
| 465 | output.getURI(), |
||
| 466 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 467 | value.getURI()); |
||
| 468 | } |
||
| 469 | break; |
||
| 470 | |||
| 471 | case KOALA_VOICE_COMMAND : |
||
| 472 | { |
||
| 473 | // assert observation property |
||
| 474 | this.kb.assertProperty( |
||
| 475 | observation.getURI(), |
||
| 476 | OWLKoalaNameSpace.SSN.getNs() + "observedProperty", |
||
| 477 | OWLKoalaNameSpace.KOALA.getNs() + "VoiceCommand"); |
||
| 478 | |||
| 479 | |||
| 480 | // create observation value |
||
| 481 | Resource value = this.kb.createIndividual( |
||
| 482 | OWLKoalaNameSpace.KOALA.getNs() + "VoiceCommandObservationValue"); |
||
| 483 | |||
| 484 | // assert data property |
||
| 485 | this.kb.assertDataProperty( |
||
| 486 | value.getURI(), |
||
| 487 | OWLKoalaNameSpace.KOALA.getNs() + "hasVoiceCommandValue", |
||
| 488 | (String) observationValue); |
||
| 489 | |||
| 490 | // assert property |
||
| 491 | this.kb.assertProperty( |
||
| 492 | output.getURI(), |
||
| 493 | OWLKoalaNameSpace.SSN.getNs() + "hasValue", |
||
| 494 | value.getURI()); |
||
| 495 | } |
||
| 496 | break; |
||
| 497 | |||
| 498 | |||
| 499 | default : |
||
| 500 | throw new RuntimeException("[ObservationReasoner] Unknown KOaLa property \"" + propertyUri + "\""); |
||
| 501 | } |
||
| 502 | } |
||
| 503 | catch (Exception ex) { |
||
| 504 | // forward exception |
||
| 505 | throw new Exception(ex.getMessage()); |
||
| 506 | } |
||
| 507 | finally { |
||
| 508 | // compute inference time |
||
| 509 | long time = System.currentTimeMillis() - now; |
||
| 510 | // update total inference time |
||
| 511 | this.inferenceTime += time; |
||
| 512 | } |
||
| 620 | } |