Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.05 KB | None | 0 0
  1. package de.uniwue.mk.kall.ie.terminology.util;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Set;
  13.  
  14. import org.semanticweb.owlapi.apibinding.OWLManager;
  15. import org.semanticweb.owlapi.io.OWLXMLOntologyFormat;
  16. import org.semanticweb.owlapi.model.AddAxiom;
  17. import org.semanticweb.owlapi.model.IRI;
  18. import org.semanticweb.owlapi.model.OWLAnnotation;
  19. import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
  20. import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom;
  21. import org.semanticweb.owlapi.model.OWLAxiom;
  22. import org.semanticweb.owlapi.model.OWLClass;
  23. import org.semanticweb.owlapi.model.OWLClassExpression;
  24. import org.semanticweb.owlapi.model.OWLDataFactory;
  25. import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
  26. import org.semanticweb.owlapi.model.OWLEntity;
  27. import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom;
  28. import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom;
  29. import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom;
  30. import org.semanticweb.owlapi.model.OWLObjectProperty;
  31. import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
  32. import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
  33. import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
  34. import org.semanticweb.owlapi.model.OWLOntology;
  35. import org.semanticweb.owlapi.model.OWLOntologyChange;
  36. import org.semanticweb.owlapi.model.OWLOntologyCreationException;
  37. import org.semanticweb.owlapi.model.OWLOntologyManager;
  38. import org.semanticweb.owlapi.model.OWLOntologyStorageException;
  39. import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom;
  40. import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
  41. import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom;
  42. import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
  43. import org.semanticweb.owlapi.model.RemoveAxiom;
  44. import org.semanticweb.owlapi.util.AutoIRIMapper;
  45. import org.semanticweb.owlapi.util.OWLEntityRemover;
  46. import org.semanticweb.owlapi.util.OWLEntityRenamer;
  47.  
  48. import de.uniwue.mk.kall.ie.terminology.struct.IEOntology;
  49. import de.uniwue.mk.kall.ie.terminology.struct.OWLOntologyClassWrapper;
  50. import de.uniwue.mk.kall.ie.terminology.struct.OWLOntologyPropertyWrapper;
  51.  
  52. public class OWLUtil {
  53.  
  54. /*
  55. * helper class for accessing all the required Elements of an OWL Ontology
  56. * used for this annotator project
  57. */
  58.  
  59. public static final String ANNOTATION_CONSTRUCTION_PROPERTY = "CONSTRUCTION_PROPERTY";
  60.  
  61. public static OWLOntology loadOntologyFromFile(File ontologyFile) throws OWLOntologyCreationException {
  62.  
  63. OWLOntologyManager m = OWLManager.createOWLOntologyManager();
  64.  
  65. m.addIRIMapper(new AutoIRIMapper(new File("materializedOntologies"), true));
  66.  
  67. IRI create = IRI.create(ontologyFile);
  68.  
  69. OWLOntology o = m.loadOntologyFromOntologyDocument(create);
  70.  
  71. return o;
  72.  
  73. }
  74.  
  75. public static void saveOntologyToFile(String path, OWLOntology ont) {
  76.  
  77. OWLOntologyManager m = OWLManager.createOWLOntologyManager();
  78. File output = new File(path);
  79. IRI docIri = IRI.create(output);
  80.  
  81. try {
  82. m.saveOntology(ont, new OWLXMLOntologyFormat(), docIri);
  83. } catch (OWLOntologyStorageException e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87.  
  88. }
  89.  
  90. public String getAnnotationCategory(OWLAnnotation anno) {
  91.  
  92. String fragment = anno.getProperty().getIRI().getFragment();
  93.  
  94. return fragment;
  95. }
  96.  
  97. public static Set<OWLObjectProperty> getSubsumingPropertiesOf(OWLObjectProperty prop, OWLOntology o) {
  98.  
  99. Set<OWLObjectPropertyExpression> subsuming = prop.getSuperProperties(o);
  100.  
  101. Set<OWLObjectProperty> subsumingProbs = new HashSet<OWLObjectProperty>();
  102.  
  103. for (OWLObjectPropertyExpression superProp : subsuming) {
  104. subsumingProbs.addAll(superProp.getObjectPropertiesInSignature());
  105. }
  106. return subsumingProbs;
  107.  
  108. }
  109.  
  110. public static Set<OWLObjectProperty> getInversePropertiesOf(OWLObjectProperty prop, OWLOntology o) {
  111.  
  112. Set<OWLObjectPropertyExpression> inverses = prop.getInverses(o);
  113.  
  114. Set<OWLObjectProperty> inverseProbs = new HashSet<OWLObjectProperty>();
  115.  
  116. for (OWLObjectPropertyExpression invProp : inverses) {
  117. inverseProbs.addAll(invProp.getObjectPropertiesInSignature());
  118. }
  119. return inverseProbs;
  120. }
  121.  
  122. public static List<OWLObjectProperty> getAllPropertiesAsList(OWLOntology ont) {
  123. Set<OWLObjectProperty> annotationPropertiesInSignature = ont.getObjectPropertiesInSignature();
  124.  
  125. List<OWLObjectProperty> properties = new ArrayList<OWLObjectProperty>(annotationPropertiesInSignature);
  126.  
  127. return properties;
  128. }
  129.  
  130. public String getPropertyName(OWLObjectProperty prop) {
  131. String[] propS = prop.toString().split("#");
  132.  
  133. return propS[propS.length - 1].substring(0, propS[propS.length - 1].length() - 1);
  134. }
  135.  
  136. public Set<OWLObjectProperty> getIdenticalProperties(OWLObjectProperty prop, OWLOntology o) {
  137.  
  138. Set<OWLObjectPropertyExpression> identical = prop.getEquivalentProperties(o);
  139.  
  140. Set<OWLObjectProperty> identicalProbs = new HashSet<OWLObjectProperty>();
  141.  
  142. for (OWLObjectPropertyExpression invProp : identical) {
  143. identicalProbs.addAll(invProp.getObjectPropertiesInSignature());
  144. }
  145. return identicalProbs;
  146. }
  147.  
  148. public OWLClass getClassForName(String className, OWLOntology ont) {
  149.  
  150. for (OWLClass c : ont.getClassesInSignature()) {
  151.  
  152. if (c.getIRI().getFragment().equals(className)) {
  153. return c;
  154. }
  155. }
  156. return null;
  157.  
  158. }
  159.  
  160. public static List<OWLObjectProperty> getAllSubsumedProperties(OWLObjectProperty owlProperty, OWLOntology ont) {
  161.  
  162. List<OWLObjectProperty> subsumedProperties = new ArrayList<OWLObjectProperty>();
  163.  
  164. List<OWLObjectProperty> propertiesToTest = new ArrayList<OWLObjectProperty>();
  165. propertiesToTest.add(owlProperty);
  166.  
  167. while (!propertiesToTest.isEmpty()) {
  168.  
  169. OWLObjectProperty actual = propertiesToTest.remove(0);
  170.  
  171. // get all subsumed classes and add them
  172. Set<OWLObjectProperty> propertiesInSignature = actual.getObjectPropertiesInSignature();
  173.  
  174. for (OWLObjectProperty oc : propertiesInSignature) {
  175. List<OWLObjectProperty> accordingClasses = getObjectPropertiesFromExpressions(
  176. oc.getSubProperties(ont).toArray(new OWLObjectPropertyExpression[0]));
  177. propertiesToTest.addAll(accordingClasses);
  178. subsumedProperties.addAll(accordingClasses);
  179. }
  180.  
  181. }
  182.  
  183. return subsumedProperties;
  184. }
  185.  
  186. private static List<OWLObjectProperty> getObjectPropertiesFromExpressions(OWLObjectPropertyExpression... array) {
  187. List<OWLObjectProperty> classes = new LinkedList<>();
  188. for (OWLObjectPropertyExpression ce : array) {
  189. classes.addAll(ce.getObjectPropertiesInSignature());
  190. }
  191. return classes;
  192. }
  193.  
  194. public static List<OWLClass> getAllSubsumedClasses(OWLClass owlClass, OWLOntology ont) {
  195.  
  196. List<OWLClass> subsumedClasses = new ArrayList<OWLClass>();
  197.  
  198. List<OWLClass> classesToTest = new ArrayList<OWLClass>();
  199. classesToTest.add(owlClass);
  200.  
  201. while (!classesToTest.isEmpty()) {
  202.  
  203. OWLClass actual = classesToTest.remove(0);
  204.  
  205. // get all subsumed classes and add them
  206. Set<OWLClass> classesInSignature = actual.getClassesInSignature();
  207.  
  208. for (OWLClass oc : classesInSignature) {
  209. List<OWLClass> accordingClasses = getClassesFromExpressions(
  210. oc.getSubClasses(ont).toArray(new OWLClassExpression[0]));
  211. classesToTest.addAll(accordingClasses);
  212. subsumedClasses.addAll(accordingClasses);
  213. }
  214.  
  215. }
  216.  
  217. return subsumedClasses;
  218. }
  219.  
  220. private static List<OWLClass> getClassesFromExpressions(OWLClassExpression... array) {
  221.  
  222. List<OWLClass> classes = new LinkedList<>();
  223. for (OWLClassExpression ce : array) {
  224. classes.addAll(ce.getClassesInSignature());
  225. }
  226. return classes;
  227. }
  228.  
  229. public static OWLOntologyClassWrapper getRootClass(IEOntology ontology) {
  230.  
  231. OWLOntologyClassWrapper root = new OWLOntologyClassWrapper(ontology.getOntology(), null);
  232. root.setWrappedClass(OWLUtil.getOWLThing(ontology.getOntology()));
  233.  
  234. List<OWLOntologyClassWrapper> wrappedClasses = new LinkedList<>();
  235. Map<OWLClass, OWLOntologyClassWrapper> map = new HashMap<>();
  236. for (OWLClass c : ontology.getOntology().getClassesInSignature()) {
  237. if (c.isOWLThing())
  238. continue;
  239. OWLOntologyClassWrapper owlOntologyClassWrapper = new OWLOntologyClassWrapper(ontology.getOntology(), c);
  240. wrappedClasses.add(owlOntologyClassWrapper);
  241. map.put(c, owlOntologyClassWrapper);
  242. }
  243.  
  244. // assign children (parent gets assigned inherently;
  245. for (OWLOntologyClassWrapper wrappedClass : wrappedClasses) {
  246. Set<OWLClassExpression> subClasses = wrappedClass.getWrappedClass().getSubClasses(ontology.getOntology());
  247. for (OWLClassExpression ce : subClasses) {
  248.  
  249. OWLClass next = ce.getClassesInSignature().iterator().next();
  250. wrappedClass.addChild(map.get(next));
  251.  
  252. }
  253.  
  254. }
  255.  
  256. // add wrapper to root that do not have a parent assigned
  257. for (OWLOntologyClassWrapper wrapper : wrappedClasses) {
  258. if (wrapper.getParent() == null) {
  259. root.addChild(wrapper);
  260. }
  261. }
  262.  
  263. return root;
  264. }
  265.  
  266. public static OWLClass getOWLThing(OWLOntology ontology) {
  267. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  268. OWLDataFactory factory = manager.getOWLDataFactory();
  269. return factory.getOWLThing();
  270. }
  271.  
  272. public static OWLObjectProperty getOWLTopObjectProperty(OWLOntology ontology) {
  273. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  274. OWLDataFactory factory = manager.getOWLDataFactory();
  275. return factory.getOWLTopObjectProperty();
  276. }
  277.  
  278. public static OWLOntologyPropertyWrapper getRootProperty(IEOntology ontology) {
  279. OWLOntologyPropertyWrapper root = new OWLOntologyPropertyWrapper(ontology.getOntology(),
  280. getOWLTopObjectProperty(ontology.getOntology()));
  281.  
  282. List<OWLOntologyPropertyWrapper> wrappedProperties = new LinkedList<>();
  283. Map<OWLObjectProperty, OWLOntologyPropertyWrapper> map = new HashMap<>();
  284.  
  285. for (OWLObjectProperty prop : ontology.getOntology().getObjectPropertiesInSignature()) {
  286. OWLOntologyPropertyWrapper owlOntologyPropertyWrapper = new OWLOntologyPropertyWrapper(
  287. ontology.getOntology(), prop);
  288. wrappedProperties.add(owlOntologyPropertyWrapper);
  289. map.put(prop, owlOntologyPropertyWrapper);
  290. }
  291.  
  292. // assign children (parent gets assigned inherently;
  293. for (OWLOntologyPropertyWrapper wrappedProperty : wrappedProperties) {
  294. Set<OWLObjectPropertyExpression> subProperties = wrappedProperty.getWrappedProperty()
  295. .getSubProperties(ontology.getOntology());
  296. for (OWLObjectPropertyExpression ce : subProperties) {
  297.  
  298. OWLObjectProperty next = ce.getObjectPropertiesInSignature().iterator().next();
  299. wrappedProperty.addChild(map.get(next));
  300.  
  301. }
  302.  
  303. }
  304.  
  305. // add wrapper to root that do not have a parent assigned
  306. for (OWLOntologyPropertyWrapper wrapper : wrappedProperties) {
  307. if (wrapper.getParent() == null) {
  308. root.addChild(wrapper);
  309. }
  310. }
  311.  
  312. return root;
  313. }
  314.  
  315. public static IRI getOntologyIRI(OWLOntology ontology) {
  316. return ontology.getOntologyID().getOntologyIRI();
  317. }
  318.  
  319. public static OWLClass createNewClass(IEOntology ontology, String value) {
  320. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  321. OWLDataFactory factory = manager.getOWLDataFactory();
  322.  
  323. IRI classIri = IRI.create(getOntologyIRI(ontology.getOntology()).toString() + "#" + value);
  324.  
  325. OWLClass owlClass = factory.getOWLClass(classIri);
  326.  
  327. return owlClass;
  328. }
  329.  
  330. public static void addEntityToOntology(OWLEntity newClass, IEOntology ontology) {
  331.  
  332. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  333. OWLDataFactory factory = manager.getOWLDataFactory();
  334.  
  335. OWLDeclarationAxiom declarationAxiom = factory.getOWLDeclarationAxiom(newClass);
  336. manager.addAxiom(ontology.getOntology(), declarationAxiom);
  337. }
  338.  
  339. public static void assignAsSubclass(OWLClass parent, OWLClass child, IEOntology ontology) {
  340. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  341. OWLDataFactory factory = manager.getOWLDataFactory();
  342.  
  343. if (parent != null && !parent.equals(OWLUtil.getOWLThing(ontology.getOntology()))) {
  344. OWLAxiom axiom = factory.getOWLSubClassOfAxiom(child, parent);
  345. AddAxiom addAxiom = new AddAxiom(ontology.getOntology(), axiom);
  346. manager.applyChange(addAxiom);
  347. }
  348.  
  349. }
  350.  
  351. public static void removeChildClass(OWLClass parent, OWLClass currentChild, OWLOntology ontology) {
  352.  
  353. OWLAxiom originalAxiom = getFactory().getOWLSubClassOfAxiom(currentChild, parent);
  354. OWLOntologyChange chg = new RemoveAxiom(ontology, originalAxiom);
  355. getManager().applyChange(chg);
  356. }
  357.  
  358. public static void assignAsSubObjectProperty(OWLObjectProperty parent, OWLObjectProperty child,
  359. IEOntology ontology) {
  360. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  361. OWLDataFactory factory = manager.getOWLDataFactory();
  362.  
  363. if (parent != null && !parent.equals(OWLUtil.getOWLTopObjectProperty(ontology.getOntology()))) {
  364. OWLAxiom axiom = factory.getOWLSubObjectPropertyOfAxiom(child, parent);
  365. AddAxiom addAxiom = new AddAxiom(ontology.getOntology(), axiom);
  366. manager.applyChange(addAxiom);
  367. }
  368.  
  369. }
  370.  
  371. public static void deleteEntitiyFromOntology(OWLEntity entity, IEOntology ontology) {
  372. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  373. OWLEntityRemover remover = new OWLEntityRemover(manager, Collections.singleton(ontology.getOntology()));
  374. entity.accept(remover);
  375. manager.applyChanges(remover.getChanges());
  376.  
  377. }
  378.  
  379. public static OWLObjectProperty createNewProperty(OWLOntology ontology, String value) {
  380. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  381. OWLDataFactory factory = manager.getOWLDataFactory();
  382.  
  383. IRI classIri = IRI.create(getOntologyIRI(ontology).toString() + "#" + value);
  384.  
  385. OWLObjectProperty owlProperty = factory.getOWLObjectProperty(classIri);
  386. return owlProperty;
  387. }
  388.  
  389. public static OWLOntology createOntology(String ontIri) {
  390. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  391.  
  392. OWLOntology ontology = null;
  393. try {
  394. ontology = manager.createOntology(IRI.create(ontIri));
  395. } catch (OWLOntologyCreationException e) {
  396. // TODO Auto-generated catch block
  397. e.printStackTrace();
  398. }
  399.  
  400. return ontology;
  401.  
  402. }
  403.  
  404. public static List<OWLAnnotation> getAnnotationsOnEntity(OWLClass classWithAnnos, OWLOntology ontology) {
  405.  
  406. return new LinkedList<OWLAnnotation>(classWithAnnos.getAnnotations(ontology));
  407. }
  408.  
  409. public static OWLAnnotation createAnnotation(String label, String value, OWLOntology ontology) {
  410. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  411. OWLDataFactory factory = manager.getOWLDataFactory();
  412.  
  413. OWLAnnotation owlAnnotation = factory.getOWLAnnotation(
  414. factory.getOWLAnnotationProperty(IRI.create(getOntologyIRI(ontology).toString() + "#" + label)),
  415. factory.getOWLLiteral(value));
  416. return owlAnnotation;
  417.  
  418. }
  419.  
  420. public static void addAnnotationToEntity(OWLAnnotation entity, OWLEntity classWithAnnos, OWLOntology ontology) {
  421. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  422. OWLDataFactory factory = manager.getOWLDataFactory();
  423. OWLAxiom ax = factory.getOWLAnnotationAssertionAxiom(classWithAnnos.getIRI(), entity);
  424. // Add the axiom to the ontology
  425. manager.applyChange(new AddAxiom(ontology, ax));
  426. }
  427.  
  428. public static List<OWLClass> getClassesInDomain(OWLObjectProperty property, OWLOntology ontology) {
  429. Set<OWLClassExpression> ranges = property.getDomains(ontology);
  430.  
  431. return getClassesFromExpressions(ranges.toArray(new OWLClassExpression[0]));
  432.  
  433. }
  434.  
  435. public static List<OWLClass> getClassesInRange(OWLObjectProperty property, OWLOntology ontology) {
  436. Set<OWLClassExpression> ranges = property.getRanges(ontology);
  437.  
  438. return getClassesFromExpressions(ranges.toArray(new OWLClassExpression[0]));
  439.  
  440. }
  441.  
  442. public static void addClassToDomain(OWLObjectProperty property, OWLOntology ontology, OWLClass cl) {
  443.  
  444. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  445. OWLDataFactory factory = manager.getOWLDataFactory();
  446. OWLObjectPropertyDomainAxiom owlObjectPropertyDomainAxiom = factory.getOWLObjectPropertyDomainAxiom(property,
  447. cl);
  448. manager.addAxiom(ontology, owlObjectPropertyDomainAxiom);
  449.  
  450. }
  451.  
  452. public static void addClassToRange(OWLObjectProperty property, OWLOntology ontology, OWLClass cl) {
  453.  
  454. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  455. OWLDataFactory factory = manager.getOWLDataFactory();
  456. OWLObjectPropertyRangeAxiom owlObjectPropertyDomainAxiom = factory.getOWLObjectPropertyRangeAxiom(property, cl);
  457. manager.addAxiom(ontology, owlObjectPropertyDomainAxiom);
  458.  
  459. }
  460.  
  461. private static OWLDataFactory getFactory() {
  462. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  463. OWLDataFactory factory = manager.getOWLDataFactory();
  464. return factory;
  465. }
  466.  
  467. private static OWLOntologyManager getManager() {
  468. return OWLManager.createOWLOntologyManager();
  469. }
  470.  
  471. public static void renameEntity(OWLEntity entity, OWLOntology ontology, String newIRI) {
  472. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  473. OWLEntityRenamer renamer = new OWLEntityRenamer(manager, Collections.singleton(ontology));
  474.  
  475. List<OWLOntologyChange> changeIRI = renamer.changeIRI(entity, IRI.create(newIRI));
  476. manager.applyChanges(changeIRI);
  477.  
  478. }
  479.  
  480. public static List<OWLObjectProperty> getConstructionProperties(OWLClass classWithAnnos, IEOntology ontology) {
  481.  
  482. List<OWLObjectProperty> constructionProperties = new LinkedList<>();
  483.  
  484. List<OWLObjectProperty> properties = OWLUtil.getAllObjectPropertiesInOntology(ontology);
  485.  
  486. for (OWLObjectProperty prop : properties) {
  487.  
  488. // get the domain of prop
  489. List<OWLClass> classesInDomain = getClassesInDomain(prop, ontology.getOntology());
  490. if (classesInDomain.contains(classWithAnnos) && classesInDomain.size() == 1) {
  491. // check the range
  492. List<OWLClass> classesInRange = getClassesInRange(prop, ontology.getOntology());
  493. if (classesInRange.size() > 1) {
  494. constructionProperties.add(prop);
  495. }
  496. if (isConstructionProperty(prop, ontology.getOntology())) {
  497. constructionProperties.add(prop);
  498. }
  499. }
  500. }
  501.  
  502. return constructionProperties;
  503. }
  504.  
  505. private static List<OWLObjectProperty> getAllObjectPropertiesInOntology(IEOntology ontology) {
  506.  
  507. List<OWLObjectProperty> allProps = new LinkedList<>();
  508. OWLOntologyPropertyWrapper rootProperty = getRootProperty(ontology);
  509.  
  510. Collection<OWLOntologyPropertyWrapper> allSubsumedChildren = rootProperty.getAllSubsumedChildren();
  511.  
  512. for (OWLOntologyPropertyWrapper wrapper : allSubsumedChildren) {
  513.  
  514. allProps.add(wrapper.getWrappedProperty());
  515. }
  516. return allProps;
  517. }
  518.  
  519. public static void deleteAnnotationFromOntology(OWLAnnotation anno, OWLClass owlClass, OWLOntology ontology) {
  520. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  521. Set<OWLAnnotationAssertionAxiom> annotationAssertionAxioms = ontology
  522. .getAnnotationAssertionAxioms(owlClass.getIRI());
  523.  
  524. for (OWLAnnotationAssertionAxiom axiom : annotationAssertionAxioms) {
  525. if (axiom.getAnnotation().equals(anno)) {
  526. manager.removeAxiom(ontology, axiom);
  527. }
  528. }
  529.  
  530. }
  531.  
  532. public static void removeClassFromRange(OWLObjectProperty property, OWLClass owlClass, OWLOntology ontology) {
  533. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  534. Set<OWLObjectPropertyRangeAxiom> objectPropertyRangeAxioms = ontology.getObjectPropertyRangeAxioms(property);
  535.  
  536. for (OWLObjectPropertyRangeAxiom axiom : objectPropertyRangeAxioms) {
  537. if (axiom.getRange().getClassesInSignature().contains(owlClass)) {
  538. manager.removeAxiom(ontology, axiom);
  539. }
  540. }
  541.  
  542. }
  543.  
  544. public static void removeClassFromDomain(OWLObjectProperty property, OWLClass owlClass, OWLOntology ontology) {
  545. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  546. Set<OWLObjectPropertyDomainAxiom> objectPropertyDomainAxioms = ontology.getObjectPropertyDomainAxioms(property);
  547.  
  548. for (OWLObjectPropertyDomainAxiom axiom : objectPropertyDomainAxioms) {
  549. if (axiom.getDomain().getClassesInSignature().contains(owlClass)) {
  550. manager.removeAxiom(ontology, axiom);
  551. }
  552. }
  553.  
  554. }
  555.  
  556. // test if the according annotation is present
  557. public static boolean isConstructionProperty(OWLObjectProperty property, OWLOntology ontology) {
  558.  
  559. Set<OWLAnnotationAssertionAxiom> annotationAssertionAxioms = property.getAnnotationAssertionAxioms(ontology);
  560.  
  561. for (OWLAnnotationAssertionAxiom axiom : annotationAssertionAxioms) {
  562.  
  563. OWLAnnotation annotation = axiom.getAnnotation();
  564. String fragment = annotation.getProperty().getIRI().getFragment();
  565. if (fragment.equals(ANNOTATION_CONSTRUCTION_PROPERTY)) {
  566. return true;
  567. }
  568.  
  569. }
  570. return false;
  571. }
  572.  
  573. public static void setConstructionProperty(OWLObjectProperty property, OWLOntology ontology,
  574. boolean setConstructionProperty) {
  575.  
  576. boolean isConstructionProp = isConstructionProperty(property, ontology);
  577. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  578. if (isConstructionProp && setConstructionProperty) {
  579. // it is already a construction property
  580. return;
  581. }
  582.  
  583. else if (isConstructionProp && !setConstructionProperty) {
  584. // we need to delete the annotation
  585. Set<OWLAnnotationAssertionAxiom> annotationAssertionAxioms = property
  586. .getAnnotationAssertionAxioms(ontology);
  587.  
  588. for (OWLAnnotationAssertionAxiom axiom : annotationAssertionAxioms) {
  589.  
  590. OWLAnnotation annotation = axiom.getAnnotation();
  591. String fragment = annotation.getProperty().getIRI().getFragment();
  592. if (fragment.equals(ANNOTATION_CONSTRUCTION_PROPERTY)) {
  593. manager.removeAxiom(ontology, axiom);
  594. }
  595.  
  596. }
  597. } else if (!isConstructionProp && setConstructionProperty) {
  598. // we need to add the annotation
  599. OWLAnnotation constructionAnnotation = createAnnotation(ANNOTATION_CONSTRUCTION_PROPERTY,
  600. property.getIRI().getFragment(), ontology);
  601. addAnnotationToEntity(constructionAnnotation, property, ontology);
  602.  
  603. } else {
  604. // it is already no construction property
  605. return;
  606. }
  607. }
  608.  
  609. // this means that the amount of classes in the domain=1 and the amount of
  610. // classes in the range>1
  611. public static boolean isDefiniteConstructionProperty(OWLObjectProperty property, OWLOntology ontology) {
  612.  
  613. return (getClassesInDomain(property, ontology).size() == 1 && getClassesInRange(property, ontology).size() > 1);
  614. }
  615.  
  616. public static void setIrreflexiveProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
  617. if (property.isIrreflexive(ontology) && !selection) {
  618. // delete the axiom
  619. Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
  620.  
  621. for (OWLAxiom axiom : referencingAxioms) {
  622. if (axiom instanceof OWLIrreflexiveObjectPropertyAxiom) {
  623. getManager().removeAxiom(ontology, axiom);
  624. }
  625. }
  626. } else if (!property.isIrreflexive(ontology) && selection) {
  627. // add the axiom
  628. OWLIrreflexiveObjectPropertyAxiom owlIrreflexiveObjectPropertyAxiom = getFactory()
  629. .getOWLIrreflexiveObjectPropertyAxiom(property);
  630. getManager().addAxiom(ontology, owlIrreflexiveObjectPropertyAxiom);
  631.  
  632. }
  633. }
  634.  
  635. public static void setReflexiveProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
  636. if (property.isReflexive(ontology) && !selection) {
  637. // delete the axiom
  638. Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
  639.  
  640. for (OWLAxiom axiom : referencingAxioms) {
  641. if (axiom instanceof OWLReflexiveObjectPropertyAxiom) {
  642. getManager().removeAxiom(ontology, axiom);
  643. }
  644. }
  645. } else if (!property.isReflexive(ontology) && selection) {
  646. // add the axiom
  647. OWLReflexiveObjectPropertyAxiom owlReflexiveObjectPropertyAxiom = getFactory()
  648. .getOWLReflexiveObjectPropertyAxiom(property);
  649. getManager().addAxiom(ontology, owlReflexiveObjectPropertyAxiom);
  650.  
  651. }
  652.  
  653. }
  654.  
  655. public static void setIsAsymmetricProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
  656. if (property.isAsymmetric(ontology) && !selection) {
  657. // delete the axiom
  658. Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
  659.  
  660. for (OWLAxiom axiom : referencingAxioms) {
  661. if (axiom instanceof OWLAsymmetricObjectPropertyAxiom) {
  662. getManager().removeAxiom(ontology, axiom);
  663. }
  664. }
  665. } else if (!property.isAsymmetric(ontology) && selection) {
  666. // add the axiom
  667. OWLAsymmetricObjectPropertyAxiom owlAsymmetricObjectPropertyAxiom = getFactory()
  668. .getOWLAsymmetricObjectPropertyAxiom(property);
  669. getManager().addAxiom(ontology, owlAsymmetricObjectPropertyAxiom);
  670.  
  671. }
  672. }
  673.  
  674. public static void setIsSymmetricProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
  675. if (property.isSymmetric(ontology) && !selection) {
  676. // delete the axiom
  677. Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
  678.  
  679. for (OWLAxiom axiom : referencingAxioms) {
  680. if (axiom instanceof OWLSymmetricObjectPropertyAxiom) {
  681. getManager().removeAxiom(ontology, axiom);
  682. }
  683. }
  684. } else if (!property.isSymmetric(ontology) && selection) {
  685. // add the axiom
  686. OWLSymmetricObjectPropertyAxiom owlSymmetricObjectPropertyAxiom = getFactory()
  687. .getOWLSymmetricObjectPropertyAxiom(property);
  688. getManager().addAxiom(ontology, owlSymmetricObjectPropertyAxiom);
  689.  
  690. }
  691.  
  692. }
  693.  
  694. public static void setIsTransitiveProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
  695. if (property.isTransitive(ontology) && !selection) {
  696. // delete the axiom
  697. Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
  698.  
  699. for (OWLAxiom axiom : referencingAxioms) {
  700. if (axiom instanceof OWLTransitiveObjectPropertyAxiom) {
  701. getManager().removeAxiom(ontology, axiom);
  702. }
  703. }
  704. } else if (!property.isTransitive(ontology) && selection) {
  705. // add the axiom
  706. OWLTransitiveObjectPropertyAxiom owlTransitiveObjectPropertyAxiom = getFactory()
  707. .getOWLTransitiveObjectPropertyAxiom(property);
  708. getManager().addAxiom(ontology, owlTransitiveObjectPropertyAxiom);
  709.  
  710. }
  711.  
  712. }
  713.  
  714. public static void setIsInverseFunctionalProperty(OWLObjectProperty property, OWLOntology ontology,
  715. boolean selection) {
  716. if (property.isInverseFunctional(ontology) && !selection) {
  717. // delete the axiom
  718. Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
  719.  
  720. for (OWLAxiom axiom : referencingAxioms) {
  721. if (axiom instanceof OWLInverseFunctionalObjectPropertyAxiom) {
  722. getManager().removeAxiom(ontology, axiom);
  723. }
  724. }
  725. } else if (!property.isInverseFunctional(ontology) && selection) {
  726. // add the axiom
  727. OWLInverseFunctionalObjectPropertyAxiom owlInverseFunctionalObjectPropertyAxiom = getFactory()
  728. .getOWLInverseFunctionalObjectPropertyAxiom(property);
  729. getManager().addAxiom(ontology, owlInverseFunctionalObjectPropertyAxiom);
  730.  
  731. }
  732.  
  733. }
  734.  
  735. public static void setIsFunctional(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
  736.  
  737. if (property.isFunctional(ontology) && !selection) {
  738. // delete the axiom
  739. Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
  740.  
  741. for (OWLAxiom axiom : referencingAxioms) {
  742. if (axiom instanceof OWLFunctionalObjectPropertyAxiom) {
  743. getManager().removeAxiom(ontology, axiom);
  744. }
  745. }
  746. } else if (!property.isFunctional(ontology) && selection) {
  747. // add the axiom
  748. OWLFunctionalObjectPropertyAxiom owlFunctionalObjectPropertyAxiom = getFactory()
  749. .getOWLFunctionalObjectPropertyAxiom(property);
  750. getManager().addAxiom(ontology, owlFunctionalObjectPropertyAxiom);
  751.  
  752. }
  753.  
  754. }
  755.  
  756. public static void removeChildProperty(OWLObjectProperty currentParent, OWLObjectProperty child,
  757. OWLOntology ontology) {
  758.  
  759. OWLAxiom originalAxiom = getFactory().getOWLSubObjectPropertyOfAxiom(child, currentParent);
  760. OWLOntologyChange chg = new RemoveAxiom(ontology, originalAxiom);
  761. getManager().applyChange(chg);
  762.  
  763. }
  764.  
  765. public static boolean doesEntityExist(String newIRI, OWLOntology ontology) {
  766.  
  767. return ontology.containsEntityInSignature(IRI.create(newIRI));
  768. }
  769.  
  770. public static OWLClass getClassByIRI(String iriToCheck, OWLOntology ontology) {
  771.  
  772. OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  773. OWLDataFactory factory = manager.getOWLDataFactory();
  774.  
  775. if (iriToCheck.toLowerCase().matches("thing"))
  776. return getOWLThing(ontology);
  777.  
  778. IRI classIri = IRI.create(getOntologyIRI(ontology).toString() + "#" + iriToCheck);
  779.  
  780. OWLClass owlClass = factory.getOWLClass(classIri);
  781. return owlClass;
  782. }
  783.  
  784. public static boolean isFrameProperty(OWLObjectProperty wrappedProperty, OWLOntology ontology) {
  785.  
  786. return (getClassesInDomain(wrappedProperty, ontology).size() == 0
  787. && getClassesInRange(wrappedProperty, ontology).size() > 0);
  788.  
  789. }
  790.  
  791. // check transitive...
  792. public static boolean isSubClassOf(OWLClass potentialSubClass, OWLClass potentialParent, OWLOntology ontology) {
  793.  
  794. if (potentialParent.isOWLThing())
  795. return true;
  796. List<OWLClass> allSubsumedClasses = getAllSubsumedClasses(potentialParent, ontology);
  797.  
  798. return allSubsumedClasses.contains(potentialSubClass);
  799.  
  800. }
  801.  
  802. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement