/uos_core-3.1.0/src/org/unbiquitous/uos/core/ontologyEngine/OntologyChangeManager.java

https://github.com/dedebf/DetetUbi · Java · 178 lines · 122 code · 17 blank · 39 comment · 25 complexity · 4d10183eb43ae40759ceb241d56d3e14 MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package org.unbiquitous.uos.core.ontologyEngine;
  6. import java.util.HashSet;
  7. import java.util.List;
  8. import java.util.Set;
  9. import org.semanticweb.owlapi.model.AddAxiom;
  10. import org.semanticweb.owlapi.model.IRI;
  11. import org.semanticweb.owlapi.model.OWLAnnotation;
  12. import org.semanticweb.owlapi.model.OWLAxiom;
  13. import org.semanticweb.owlapi.model.OWLClass;
  14. import org.semanticweb.owlapi.model.OWLDataFactory;
  15. import org.semanticweb.owlapi.model.OWLDataProperty;
  16. import org.semanticweb.owlapi.model.OWLObjectProperty;
  17. import org.semanticweb.owlapi.model.OWLOntology;
  18. import org.semanticweb.owlapi.model.OWLOntologyManager;
  19. import org.unbiquitous.uos.core.applicationManager.UosApplication;
  20. import org.unbiquitous.uos.core.ontologyEngine.exception.CycleException;
  21. import org.unbiquitous.uos.core.ontologyEngine.exception.DeclarationException;
  22. import org.unbiquitous.uos.core.ontologyEngine.exception.DisjunctionException;
  23. import org.unbiquitous.uos.core.ontologyEngine.exception.RedundancyException;
  24. import org.unbiquitous.uos.core.ontologyEngine.exception.RemovalException;
  25. /**
  26. *
  27. * @author anaozaki
  28. */
  29. public class OntologyChangeManager {
  30. private OWLOntology localContext;
  31. private OWLOntologyManager manager;
  32. private OntologyReasoner ontologyReasoner;
  33. private String prefix;
  34. OntologyChangeManager(OWLOntologyManager manager, OWLOntology localContext, Ontology ontology) {
  35. this.manager = manager;
  36. this.localContext = localContext;
  37. this.ontologyReasoner = (OntologyReasoner) ontology.getOntologyReasoner();
  38. prefix = localContext.getOntologyID().getOntologyIRI() + "#";
  39. }
  40. public void validateAddChange(OWLAxiom axiom) throws DeclarationException{
  41. //Constraint 1
  42. if(localContext.containsAxiom(axiom)){
  43. throw new DeclarationException("Entity already defined.");
  44. }
  45. }
  46. public void validateAddSubClassChange(OWLAxiom axiom, OWLClass owlClass, OWLClass owlClass2) throws RedundancyException, CycleException, DeclarationException {
  47. //Constraint 2 - Prevents cycles
  48. List<String> strList = ontologyReasoner.getSuperClassesFromClass(owlClass2.getIRI().getFragment(), false);
  49. if(strList.contains(owlClass.getIRI().getFragment()))
  50. throw new CycleException("Cycle detected by inserion of subClass axiom. ");
  51. List<String> strList2 = ontologyReasoner.getSuperClassesFromClass(owlClass.getIRI().getFragment(), true);
  52. //Constraint 4 - Prevents redundancy of subclass
  53. for(String str2 : strList2)
  54. for(String str : strList){
  55. if(str2.compareTo(str) == 0){
  56. if(str.compareTo("Thing") != 0){
  57. throw new RedundancyException("SubClass redundancy detected between classes "+owlClass.getIRI().getFragment()+" and " +str,owlClass.getIRI().getFragment(),str);
  58. }
  59. }
  60. }
  61. //Constraint 1
  62. if(localContext.containsAxiomIgnoreAnnotations(axiom)){
  63. throw new DeclarationException("SubClass already defined.");
  64. }
  65. }
  66. public void validateDisjointChange(AddAxiom addAxiom, OWLClass owlClass, OWLClass owlClass2) throws DisjunctionException{
  67. //Constraint 3
  68. if(owlClass.getIRI().getFragment().compareTo(owlClass2.getIRI().getFragment()) == 0)
  69. throw new DisjunctionException("Same class disjunction exception. ");
  70. }
  71. public void validateAddSubDataPropertyChange(OWLAxiom axiom, OWLDataProperty owlDataProperty, OWLDataProperty owlDataProperty2) throws RedundancyException, CycleException, DeclarationException {
  72. //Constraint 1
  73. if(localContext.containsAxiomIgnoreAnnotations(axiom)){
  74. throw new DeclarationException("SubDataProperty already defined.");
  75. }
  76. //Constraint 2 - Prevents cycles
  77. List<String> strList = ontologyReasoner.getSuperDataPropertiesFromDataProperty(owlDataProperty2.getIRI().getFragment(), false);
  78. if(strList.contains(owlDataProperty.getIRI().getFragment()))
  79. throw new CycleException("Cycle detected by inserion of subDataProperty axiom. ");
  80. List<String> strList2 = ontologyReasoner.getSuperDataPropertiesFromDataProperty(owlDataProperty.getIRI().getFragment(), true);
  81. //Constraint 4 - Prevents redundancy of subclass
  82. for(String str2 : strList2)
  83. for(String str : strList)
  84. if(str2.compareTo(str) == 0){
  85. //if(str.compareTo("Thing") != 0)
  86. throw new RedundancyException("SubDataProperty redundancy detected between data properties "+owlDataProperty.getIRI().getFragment()+" and " +str,owlDataProperty.getIRI().getFragment(),str);
  87. }
  88. }
  89. public void validateAddSubObjectPropertyChange(OWLAxiom axiom, OWLObjectProperty owlObjectProperty, OWLObjectProperty owlObjectProperty2) throws RedundancyException, CycleException, DeclarationException {
  90. //Constraint 1
  91. if(localContext.containsAxiomIgnoreAnnotations(axiom)){
  92. throw new DeclarationException("SubObjectProperty already defined.");
  93. }
  94. //Constraint 2 - Prevents cycles
  95. List<String> strList = ontologyReasoner.getSuperObjectPropertiesFromObjectProperty(owlObjectProperty2.getIRI().getFragment(), false);
  96. if(strList.contains(owlObjectProperty.getIRI().getFragment()))
  97. throw new CycleException("Cycle detected by inserion of subObjectProperty axiom. ");
  98. List<String> strList2 = ontologyReasoner.getSuperObjectPropertiesFromObjectProperty(owlObjectProperty.getIRI().getFragment(), true);
  99. //Constraint 4 - Prevents redundancy of subclass
  100. for(String str2 : strList2)
  101. for(String str : strList)
  102. if(str2.compareTo(str) == 0){
  103. //if(str.compareTo("Thing") != 0)
  104. throw new RedundancyException("SubObjectProperty redundancy detected between object properties "+owlObjectProperty.getIRI().getFragment()+" and " +str,owlObjectProperty.getIRI().getFragment(),str);
  105. }
  106. }
  107. public void validateRemovalChange(OWLAxiom axiom) throws RemovalException{
  108. //Constraint 5
  109. if(!localContext.containsAxiom(axiom))
  110. throw new RemovalException("Ontology "+prefix+" does not contain the axiom.");
  111. }
  112. @SuppressWarnings({ "unchecked", "rawtypes", "restriction" })
  113. protected String getApplicationName() {
  114. // Stack Trace Method
  115. // StackTraceElement[] cause = Thread.currentThread().getStackTrace();
  116. // int i;
  117. // for(i = 0; i < cause.length; i++){
  118. // Class cls;
  119. // try {
  120. // cls = Class.forName(cause[i].getClassName());
  121. // cls.asSubclass(UosApplication.class);
  122. // return cause[i].getClassName();
  123. // } catch (ClassCastException ex){
  124. //
  125. // } catch (ClassNotFoundException ex) {
  126. // return "uOS";
  127. // }
  128. // }
  129. // return "uOS";
  130. // Reflection is faster than stack trace
  131. Class cls;
  132. int i = 2;
  133. cls = sun.reflect.Reflection.getCallerClass(1);
  134. while(cls != null){
  135. try{
  136. cls.asSubclass(UosApplication.class);
  137. return cls.getName();
  138. } catch (ClassCastException ex){
  139. cls = sun.reflect.Reflection.getCallerClass(i);
  140. i++;
  141. }
  142. }
  143. return "uOS";
  144. }
  145. public OWLAxiom getAnnotatedAxiom(OWLAxiom axiom) {
  146. OWLDataFactory factory = manager.getOWLDataFactory();
  147. OWLAnnotation annotation = factory.getOWLAnnotation(
  148. factory.getOWLAnnotationProperty(IRI.create(prefix + Ontology.ANNOTATION_PROPERTY)),
  149. factory.getOWLLiteral(this.getApplicationName()));
  150. Set<OWLAnnotation> annotations = new HashSet<OWLAnnotation>();
  151. annotations.add(annotation);
  152. return axiom.getAnnotatedAxiom(annotations);
  153. }
  154. public OWLAnnotation getAxiomAnnotation(OWLAxiom axiom) {
  155. OWLDataFactory factory = manager.getOWLDataFactory();
  156. OWLAnnotation annotation = factory.getOWLAnnotation(
  157. factory.getOWLAnnotationProperty(IRI.create(prefix + Ontology.ANNOTATION_PROPERTY)),
  158. factory.getOWLLiteral(this.getApplicationName()));
  159. return annotation;
  160. }
  161. }