| Total Complexity | 49 |
| Total Lines | 484 |
| Duplicated Lines | 17.56 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like it.cnr.istc.pst.cognition.semantic.owl.jena.OWLModel 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.semantic.owl.jena; |
||
| 28 | public abstract class OWLModel |
||
| 29 | { |
||
| 30 | protected final AtomicLong idCounter; |
||
| 31 | protected String ontologyFile; |
||
| 32 | protected String inferenceRuleFile; |
||
| 33 | protected OntModel model; |
||
| 34 | protected InfModel infModel; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * |
||
| 38 | * @param ontologyFile |
||
| 39 | * @param domainNameSpace |
||
| 40 | */ |
||
| 41 | protected OWLModel(String ontologyFile, String domainNameSpace) |
||
| 42 | { |
||
| 43 | // set ID counter |
||
| 44 | this.idCounter = new AtomicLong(0); |
||
| 45 | // set onto file |
||
| 46 | this.ontologyFile = ontologyFile; |
||
| 47 | // create the model schema from the onto |
||
| 48 | this.model = ModelFactory.createOntologyModel( |
||
| 49 | OntModelSpec.RDFS_MEM_TRANS_INF); // create in-memory onto model with RDFS language and transitive rule-based reasoner with RDFS rules |
||
| 50 | // use DocumentManager API to specify that onto is replicated locally on disk |
||
| 51 | this.model.getDocumentManager().addAltEntry(domainNameSpace, "file:" + this.ontologyFile); |
||
| 52 | // read onto file |
||
| 53 | this.model.read(domainNameSpace); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * |
||
| 58 | * @param ontologyFile |
||
| 59 | * @param inferenceRuleFile |
||
| 60 | * @param domainNameSpace |
||
| 61 | */ |
||
| 62 | protected OWLModel(String ontologyFile, String inferenceRuleFile, String domainNameSpace) |
||
| 63 | { |
||
| 64 | this(ontologyFile, domainNameSpace); |
||
| 65 | // set inference rule file |
||
| 66 | this.inferenceRuleFile = inferenceRuleFile; |
||
| 67 | // parse the list of inference rules for feature extraction |
||
| 68 | List<Rule> rules = Rule.rulesFromURL("file:" + inferenceRuleFile); |
||
| 69 | // create a generic rule-based reasoner |
||
| 70 | GenericRuleReasoner reasoner = new GenericRuleReasoner(rules); |
||
| 71 | // configure reasoner - user forward chaining RETE rule engine |
||
| 72 | reasoner.setParameter(ReasonerVocabulary.PROPruleMode, "forwardRETE"); |
||
| 73 | // create an inference model on the raw data model |
||
| 74 | this.infModel = ModelFactory.createInfModel(reasoner, this.model); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * |
||
| 79 | * @return |
||
| 80 | */ |
||
| 81 | public String getOntologyFile() { |
||
| 82 | return ontologyFile; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * |
||
| 87 | * @return |
||
| 88 | */ |
||
| 89 | public String getInferenceRuleFile() { |
||
| 90 | return inferenceRuleFile; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * |
||
| 95 | * @param model |
||
| 96 | * @param domainNameSpace |
||
| 97 | */ |
||
| 98 | protected void setup(Model model, String domainNameSpace) |
||
| 99 | { |
||
| 100 | // setup a new model |
||
| 101 | this.model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF, model); |
||
| 102 | // use DocumentManager API to specify that ontology is replicated locally on disk |
||
| 103 | this.model.getDocumentManager().addAltEntry(domainNameSpace, "file:" + this.ontologyFile); |
||
| 104 | // read ontology file |
||
| 105 | this.model.read(domainNameSpace); |
||
| 106 | |||
| 107 | // parse the list of inference rules for feature extraction |
||
| 108 | List<Rule> rules = Rule.rulesFromURL("file:" + inferenceRuleFile); |
||
| 109 | // create a generic rule-based reasoner |
||
| 110 | GenericRuleReasoner reasoner = new GenericRuleReasoner(rules); |
||
| 111 | // configure reasoner - user forward chaining RETE rule engine |
||
| 112 | reasoner.setParameter(ReasonerVocabulary.PROPruleMode, "forwardRETE"); |
||
| 113 | // create an inference model on the raw data model |
||
| 114 | this.infModel = ModelFactory.createInfModel(reasoner, this.model); |
||
| 115 | } |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * |
||
| 120 | * @param classURI |
||
| 121 | * @return |
||
| 122 | */ |
||
| 123 | public List<Resource> getIndividuals(String classURI) |
||
| 124 | { |
||
| 125 | // list of found individuals |
||
| 126 | List<Resource> list = new ArrayList<Resource>(); |
||
| 127 | // get resource class |
||
| 128 | Resource c = this.getResource(classURI); |
||
| 129 | if (c != null) { |
||
| 130 | // iterate over individuals |
||
| 131 | Iterator<Individual> it = this.model.listIndividuals(c); |
||
| 132 | while (it.hasNext()) { |
||
| 133 | list.add(it.next()); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | // get the list |
||
| 138 | return list; |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * |
||
| 144 | * @return |
||
| 145 | */ |
||
| 146 | public void rebind() { |
||
| 147 | // re-bind the inference model to the underlying data model |
||
| 148 | this.infModel.rebind(); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * |
||
| 153 | * @return |
||
| 154 | */ |
||
| 155 | public boolean validate() { |
||
| 156 | // re-bind the inference model to the underlying dat model |
||
| 157 | this.infModel.rebind(); |
||
| 158 | // validate the inference model of the knowledge base |
||
| 159 | ValidityReport report = this.infModel.validate(); |
||
| 160 | // check if the model is valid |
||
| 161 | return report.isValid(); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * |
||
| 166 | * @return |
||
| 167 | */ |
||
| 168 | public Iterator<Statement> iterator() { |
||
| 169 | // get an iterator over the statements of the model |
||
| 170 | return this.infModel.listStatements(); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * |
||
| 175 | * @return |
||
| 176 | */ |
||
| 177 | protected Model getModel(String domainNameSpace) |
||
| 178 | { |
||
| 179 | // create a new model |
||
| 180 | OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF, this.model); |
||
|
|
|||
| 181 | // use DocumentManager API to specify that ontology is replicated locally on disk |
||
| 182 | model.getDocumentManager().addAltEntry(domainNameSpace, "file:" + this.ontologyFile); |
||
| 183 | // read ontology file |
||
| 184 | model.read(domainNameSpace); |
||
| 185 | |||
| 186 | // add inferred statements |
||
| 187 | Iterator<Statement> it = this.infModel.getDeductionsModel().listStatements(); |
||
| 188 | while (it.hasNext()) { |
||
| 189 | // add inferred statement |
||
| 190 | model.add(it.next()); |
||
| 191 | } |
||
| 192 | |||
| 193 | // get model |
||
| 194 | return model; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * |
||
| 199 | * @param classURI |
||
| 200 | * @return |
||
| 201 | * @throws Exception |
||
| 202 | */ |
||
| 203 | public Resource createIndividual(String classURI) |
||
| 204 | throws Exception |
||
| 205 | { |
||
| 206 | // check whether a resource with "classURI" actually exists |
||
| 207 | Resource res = this.infModel.getResource(classURI); |
||
| 208 | if (res == null) { |
||
| 209 | throw new Exception("No resource found with URI= \"" + classURI + "\" in the knowledge-base"); |
||
| 210 | } |
||
| 211 | |||
| 212 | // create an individual of the class |
||
| 213 | Resource resource = this.infModel.createResource(res.getLocalName().toLowerCase() + "_" + this.idCounter.getAndIncrement(), res); |
||
| 214 | // get created individual |
||
| 215 | return resource; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * |
||
| 220 | * @param referenceURI |
||
| 221 | * @param propertyURI |
||
| 222 | * @param targetURI |
||
| 223 | * @throws Exception |
||
| 224 | */ |
||
| 225 | public void assertProperty(String referenceURI, String propertyURI, String targetURI) |
||
| 226 | throws Exception |
||
| 227 | { |
||
| 228 | // check whether a resource with "referenceURI" actually exists |
||
| 229 | Resource reference = this.infModel.getResource(referenceURI); |
||
| 230 | if (reference == null) { |
||
| 231 | throw new Exception("Resource with URI=\"" + referenceURI + "\" not found in the knowledge-base"); |
||
| 232 | } |
||
| 233 | |||
| 234 | // get target |
||
| 235 | Resource target = this.infModel.getResource(targetURI); |
||
| 236 | if (target == null) { |
||
| 237 | throw new Exception("Resource with URI=\"" + targetURI + "\" not found in the knowledge-base"); |
||
| 238 | } |
||
| 239 | |||
| 240 | // check whether a resource with "propertyURI" actually exists |
||
| 241 | Property property = this.infModel.getProperty(propertyURI); |
||
| 242 | if (property == null) { |
||
| 243 | throw new Exception("Property with URI=\"" + propertyURI + "\" not found in the knowledge-base"); |
||
| 244 | } |
||
| 245 | |||
| 246 | // add statement to the knowledge base |
||
| 247 | this.infModel.add(reference, property, target); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * |
||
| 252 | * @param referenceURI |
||
| 253 | * @param propertyURI |
||
| 254 | * @param value |
||
| 255 | * @throws Exception |
||
| 256 | */ |
||
| 257 | View Code Duplication | public void assertDataProperty(String referenceURI, String propertyURI, long value) |
|
| 258 | throws Exception |
||
| 259 | { |
||
| 260 | // check whether a resource with "referenceURI" actually exists |
||
| 261 | Resource reference = this.infModel.getResource(referenceURI); |
||
| 262 | if (reference == null) { |
||
| 263 | throw new Exception("Resource with URI=\"" + referenceURI + "\" not found in the knowledge-base"); |
||
| 264 | } |
||
| 265 | |||
| 266 | // check whether a resource with "propertyURI" actually exists |
||
| 267 | Property property = this.infModel.getProperty(propertyURI); |
||
| 268 | if (property == null) { |
||
| 269 | throw new Exception("Property with URI=\"" + propertyURI + "\" not found in the knowledge-base"); |
||
| 270 | } |
||
| 271 | |||
| 272 | // add literal and related property |
||
| 273 | this.infModel.addLiteral(reference, property, value); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * |
||
| 278 | * @param referenceURI |
||
| 279 | * @param propertyURI |
||
| 280 | * @param value |
||
| 281 | * @throws Exception |
||
| 282 | */ |
||
| 283 | View Code Duplication | public void assertDataProperty(String referenceURI, String propertyURI, String value) |
|
| 284 | throws Exception |
||
| 285 | { |
||
| 286 | // check whether a resource with "referenceURI" actually exists |
||
| 287 | Resource reference = this.infModel.getResource(referenceURI); |
||
| 288 | if (reference == null) { |
||
| 289 | throw new Exception("Resource with URI=\"" + referenceURI + "\" not found in the knowledge-base"); |
||
| 290 | } |
||
| 291 | |||
| 292 | // check whether a resource with "propertyURI" actually exists |
||
| 293 | Property property = this.infModel.getProperty(propertyURI); |
||
| 294 | if (property == null) { |
||
| 295 | throw new Exception("Property with URI=\"" + propertyURI + "\" not found in the knowledge-base"); |
||
| 296 | } |
||
| 297 | |||
| 298 | // add literal and related property |
||
| 299 | this.infModel.add(reference, property, value); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * |
||
| 304 | * @param referenceURI |
||
| 305 | * @param propertyURI |
||
| 306 | * @param value |
||
| 307 | * @throws Exception |
||
| 308 | */ |
||
| 309 | View Code Duplication | public void assertDataProperty(String referenceURI, String propertyURI, double value) |
|
| 310 | throws Exception |
||
| 311 | { |
||
| 312 | // check whether a resource with "referenceURI" actually exists |
||
| 313 | Resource reference = this.infModel.getResource(referenceURI); |
||
| 314 | if (reference == null) { |
||
| 315 | throw new Exception("Resource with URI=\"" + referenceURI + "\" not found in the knowledge-base"); |
||
| 316 | } |
||
| 317 | |||
| 318 | // check whether a resource with "propertyURI" actually exists |
||
| 319 | Property property = this.infModel.getProperty(propertyURI); |
||
| 320 | if (property == null) { |
||
| 321 | throw new Exception("Property with URI=\"" + propertyURI + "\" not found in the knowledge-base"); |
||
| 322 | } |
||
| 323 | |||
| 324 | // add literal and related property |
||
| 325 | this.infModel.addLiteral(reference, property, value); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * |
||
| 330 | * @param referenceURI |
||
| 331 | * @param propertyURI |
||
| 332 | * @param value |
||
| 333 | * @throws Exception |
||
| 334 | */ |
||
| 335 | View Code Duplication | public void assertDataProperty(String referenceURI, String propertyURI, boolean value) |
|
| 336 | throws Exception |
||
| 337 | { |
||
| 338 | // check whether a resource with "referenceURI" actually exists |
||
| 339 | Resource reference = this.infModel.getResource(referenceURI); |
||
| 340 | if (reference == null) { |
||
| 341 | throw new Exception("Resource with URI=\"" + referenceURI + "\" not found in the knowledge-base"); |
||
| 342 | } |
||
| 343 | |||
| 344 | // check whether a resource with "propertyURI" actually exists |
||
| 345 | Property property = this.infModel.getProperty(propertyURI); |
||
| 346 | if (property == null) { |
||
| 347 | throw new Exception("Property with URI=\"" + propertyURI + "\" not found in the knowledge-base"); |
||
| 348 | } |
||
| 349 | |||
| 350 | // add literal and related property |
||
| 351 | this.infModel.addLiteral(reference, property, value); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * |
||
| 356 | * @param referenceURI |
||
| 357 | * @param propertyURI |
||
| 358 | * @param value |
||
| 359 | * @throws Exception |
||
| 360 | */ |
||
| 361 | View Code Duplication | public void assertDataProperty(String referenceURI, String propertyURI, int value) |
|
| 362 | throws Exception |
||
| 363 | { |
||
| 364 | // check whether a resource with "referenceURI" actually exists |
||
| 365 | Resource reference = this.infModel.getResource(referenceURI); |
||
| 366 | if (reference == null) { |
||
| 367 | throw new Exception("Resource with URI=\"" + referenceURI + "\" not found in the knowledge-base"); |
||
| 368 | } |
||
| 369 | |||
| 370 | // check whether a resource with "propertyURI" actually exists |
||
| 371 | Property property = this.infModel.getProperty(propertyURI); |
||
| 372 | if (property == null) { |
||
| 373 | throw new Exception("Property with URI=\"" + propertyURI + "\" not found in the knowledge-base"); |
||
| 374 | } |
||
| 375 | |||
| 376 | // add literal and related property |
||
| 377 | this.infModel.addLiteral(reference, property, value); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * |
||
| 382 | * @param id |
||
| 383 | * @param domainNameSpace |
||
| 384 | * @return |
||
| 385 | * @throws Exception |
||
| 386 | */ |
||
| 387 | protected Resource getResourceById(Long id, String domainNameSpace) |
||
| 388 | throws Exception |
||
| 389 | { |
||
| 390 | // get property |
||
| 391 | Property property = this.infModel.getProperty(domainNameSpace + "hasId"); |
||
| 392 | // check statements |
||
| 393 | Iterator<Statement> it = this.infModel.listLiteralStatements(null, property, id); |
||
| 394 | if (!it.hasNext()) { |
||
| 395 | throw new Exception("Resource with ID " + id + " not found into the knowledge base"); |
||
| 396 | } |
||
| 397 | |||
| 398 | // get statement |
||
| 399 | Statement s = it.next(); |
||
| 400 | // get resource |
||
| 401 | return s.getSubject(); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * |
||
| 406 | * @param resURI |
||
| 407 | * @return |
||
| 408 | */ |
||
| 409 | public Resource getResource(String resURI) { |
||
| 410 | return this.infModel.getResource(resURI); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * |
||
| 415 | * @param propertyId |
||
| 416 | * @return |
||
| 417 | * @throws Exception |
||
| 418 | */ |
||
| 419 | public List<Statement> getStatements(String propertyId) |
||
| 420 | throws Exception { |
||
| 421 | // list of statements |
||
| 422 | return this.getStatements(null, propertyId, null); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * |
||
| 427 | * @param propertyId |
||
| 428 | * @param targetId |
||
| 429 | * @return |
||
| 430 | * @throws Exception |
||
| 431 | */ |
||
| 432 | public List<Statement> getStatements(String propertyId, String targetId) |
||
| 433 | throws Exception { |
||
| 434 | // get statements |
||
| 435 | return this.getStatements(null, propertyId, targetId); |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * |
||
| 440 | * @param subjectId |
||
| 441 | * @param propertyId |
||
| 442 | * @param targetId |
||
| 443 | * @return |
||
| 444 | * @throws Exception |
||
| 445 | */ |
||
| 446 | public List<Statement> getStatements(String subjectId, String propertyId, String targetId) |
||
| 447 | throws Exception |
||
| 448 | { |
||
| 449 | // list of statements |
||
| 450 | List<Statement> list = new ArrayList<Statement>(); |
||
| 451 | |||
| 452 | // check resource parameter |
||
| 453 | Resource subject = null; |
||
| 454 | if (subjectId != null) { |
||
| 455 | // get resource |
||
| 456 | subject = this.infModel.getResource(subjectId); |
||
| 457 | if (subject == null) { |
||
| 458 | throw new Exception("Resource with URI=\"" + subjectId + "\" not found in the knowledge-base"); |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | // check property parameter |
||
| 463 | Property prop = null; |
||
| 464 | if (propertyId != null) { |
||
| 465 | // get property |
||
| 466 | prop = this.infModel.getProperty(propertyId); |
||
| 467 | if (prop == null) { |
||
| 468 | throw new Exception("Property with URI=\"" + propertyId + "\" not found in the knowledge-base"); |
||
| 469 | } |
||
| 470 | } |
||
| 471 | |||
| 472 | // check target parameter |
||
| 473 | RDFNode target = null; |
||
| 474 | if (targetId != null) { |
||
| 475 | // get target |
||
| 476 | target = (RDFNode) this.infModel.getResource(targetId); |
||
| 477 | if (target == null) { |
||
| 478 | throw new Exception("Target with URI=\"" + targetId + "\" not found in the knowledge-base"); |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | |||
| 483 | // iterate over statements |
||
| 484 | Iterator<Statement> it = this.infModel.listStatements(subject, prop, target); |
||
| 485 | while (it.hasNext()) { |
||
| 486 | // add statement to list |
||
| 487 | list.add(it.next()); |
||
| 488 | } |
||
| 489 | |||
| 490 | |||
| 491 | // get list |
||
| 492 | return list; |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * |
||
| 497 | * @param propertyId |
||
| 498 | * @return |
||
| 499 | * @throws Exception |
||
| 500 | */ |
||
| 501 | public Property getProperty(String propertyId) |
||
| 502 | throws Exception |
||
| 503 | { |
||
| 504 | // get property |
||
| 505 | Property prop = this.infModel.getProperty(propertyId); |
||
| 506 | if (prop == null) { |
||
| 507 | throw new Exception("Property with URI=\"" + propertyId + "\" not found in the knowledge-base"); |
||
| 508 | } |
||
| 509 | |||
| 510 | // get property |
||
| 511 | return prop; |
||
| 512 | } |
||
| 514 |