Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.uniwue.mk.kall.ie.terminology.util;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import org.semanticweb.owlapi.apibinding.OWLManager;
- import org.semanticweb.owlapi.io.OWLXMLOntologyFormat;
- import org.semanticweb.owlapi.model.AddAxiom;
- import org.semanticweb.owlapi.model.IRI;
- import org.semanticweb.owlapi.model.OWLAnnotation;
- import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
- import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom;
- import org.semanticweb.owlapi.model.OWLAxiom;
- import org.semanticweb.owlapi.model.OWLClass;
- import org.semanticweb.owlapi.model.OWLClassExpression;
- import org.semanticweb.owlapi.model.OWLDataFactory;
- import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
- import org.semanticweb.owlapi.model.OWLEntity;
- import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom;
- import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom;
- import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom;
- import org.semanticweb.owlapi.model.OWLObjectProperty;
- import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
- import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
- import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
- import org.semanticweb.owlapi.model.OWLOntology;
- import org.semanticweb.owlapi.model.OWLOntologyChange;
- import org.semanticweb.owlapi.model.OWLOntologyCreationException;
- import org.semanticweb.owlapi.model.OWLOntologyManager;
- import org.semanticweb.owlapi.model.OWLOntologyStorageException;
- import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom;
- import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
- import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom;
- import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
- import org.semanticweb.owlapi.model.RemoveAxiom;
- import org.semanticweb.owlapi.util.AutoIRIMapper;
- import org.semanticweb.owlapi.util.OWLEntityRemover;
- import org.semanticweb.owlapi.util.OWLEntityRenamer;
- import de.uniwue.mk.kall.ie.terminology.struct.IEOntology;
- import de.uniwue.mk.kall.ie.terminology.struct.OWLOntologyClassWrapper;
- import de.uniwue.mk.kall.ie.terminology.struct.OWLOntologyPropertyWrapper;
- public class OWLUtil {
- /*
- * helper class for accessing all the required Elements of an OWL Ontology
- * used for this annotator project
- */
- public static final String ANNOTATION_CONSTRUCTION_PROPERTY = "CONSTRUCTION_PROPERTY";
- public static OWLOntology loadOntologyFromFile(File ontologyFile) throws OWLOntologyCreationException {
- OWLOntologyManager m = OWLManager.createOWLOntologyManager();
- m.addIRIMapper(new AutoIRIMapper(new File("materializedOntologies"), true));
- IRI create = IRI.create(ontologyFile);
- OWLOntology o = m.loadOntologyFromOntologyDocument(create);
- return o;
- }
- public static void saveOntologyToFile(String path, OWLOntology ont) {
- OWLOntologyManager m = OWLManager.createOWLOntologyManager();
- File output = new File(path);
- IRI docIri = IRI.create(output);
- try {
- m.saveOntology(ont, new OWLXMLOntologyFormat(), docIri);
- } catch (OWLOntologyStorageException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public String getAnnotationCategory(OWLAnnotation anno) {
- String fragment = anno.getProperty().getIRI().getFragment();
- return fragment;
- }
- public static Set<OWLObjectProperty> getSubsumingPropertiesOf(OWLObjectProperty prop, OWLOntology o) {
- Set<OWLObjectPropertyExpression> subsuming = prop.getSuperProperties(o);
- Set<OWLObjectProperty> subsumingProbs = new HashSet<OWLObjectProperty>();
- for (OWLObjectPropertyExpression superProp : subsuming) {
- subsumingProbs.addAll(superProp.getObjectPropertiesInSignature());
- }
- return subsumingProbs;
- }
- public static Set<OWLObjectProperty> getInversePropertiesOf(OWLObjectProperty prop, OWLOntology o) {
- Set<OWLObjectPropertyExpression> inverses = prop.getInverses(o);
- Set<OWLObjectProperty> inverseProbs = new HashSet<OWLObjectProperty>();
- for (OWLObjectPropertyExpression invProp : inverses) {
- inverseProbs.addAll(invProp.getObjectPropertiesInSignature());
- }
- return inverseProbs;
- }
- public static List<OWLObjectProperty> getAllPropertiesAsList(OWLOntology ont) {
- Set<OWLObjectProperty> annotationPropertiesInSignature = ont.getObjectPropertiesInSignature();
- List<OWLObjectProperty> properties = new ArrayList<OWLObjectProperty>(annotationPropertiesInSignature);
- return properties;
- }
- public String getPropertyName(OWLObjectProperty prop) {
- String[] propS = prop.toString().split("#");
- return propS[propS.length - 1].substring(0, propS[propS.length - 1].length() - 1);
- }
- public Set<OWLObjectProperty> getIdenticalProperties(OWLObjectProperty prop, OWLOntology o) {
- Set<OWLObjectPropertyExpression> identical = prop.getEquivalentProperties(o);
- Set<OWLObjectProperty> identicalProbs = new HashSet<OWLObjectProperty>();
- for (OWLObjectPropertyExpression invProp : identical) {
- identicalProbs.addAll(invProp.getObjectPropertiesInSignature());
- }
- return identicalProbs;
- }
- public OWLClass getClassForName(String className, OWLOntology ont) {
- for (OWLClass c : ont.getClassesInSignature()) {
- if (c.getIRI().getFragment().equals(className)) {
- return c;
- }
- }
- return null;
- }
- public static List<OWLObjectProperty> getAllSubsumedProperties(OWLObjectProperty owlProperty, OWLOntology ont) {
- List<OWLObjectProperty> subsumedProperties = new ArrayList<OWLObjectProperty>();
- List<OWLObjectProperty> propertiesToTest = new ArrayList<OWLObjectProperty>();
- propertiesToTest.add(owlProperty);
- while (!propertiesToTest.isEmpty()) {
- OWLObjectProperty actual = propertiesToTest.remove(0);
- // get all subsumed classes and add them
- Set<OWLObjectProperty> propertiesInSignature = actual.getObjectPropertiesInSignature();
- for (OWLObjectProperty oc : propertiesInSignature) {
- List<OWLObjectProperty> accordingClasses = getObjectPropertiesFromExpressions(
- oc.getSubProperties(ont).toArray(new OWLObjectPropertyExpression[0]));
- propertiesToTest.addAll(accordingClasses);
- subsumedProperties.addAll(accordingClasses);
- }
- }
- return subsumedProperties;
- }
- private static List<OWLObjectProperty> getObjectPropertiesFromExpressions(OWLObjectPropertyExpression... array) {
- List<OWLObjectProperty> classes = new LinkedList<>();
- for (OWLObjectPropertyExpression ce : array) {
- classes.addAll(ce.getObjectPropertiesInSignature());
- }
- return classes;
- }
- public static List<OWLClass> getAllSubsumedClasses(OWLClass owlClass, OWLOntology ont) {
- List<OWLClass> subsumedClasses = new ArrayList<OWLClass>();
- List<OWLClass> classesToTest = new ArrayList<OWLClass>();
- classesToTest.add(owlClass);
- while (!classesToTest.isEmpty()) {
- OWLClass actual = classesToTest.remove(0);
- // get all subsumed classes and add them
- Set<OWLClass> classesInSignature = actual.getClassesInSignature();
- for (OWLClass oc : classesInSignature) {
- List<OWLClass> accordingClasses = getClassesFromExpressions(
- oc.getSubClasses(ont).toArray(new OWLClassExpression[0]));
- classesToTest.addAll(accordingClasses);
- subsumedClasses.addAll(accordingClasses);
- }
- }
- return subsumedClasses;
- }
- private static List<OWLClass> getClassesFromExpressions(OWLClassExpression... array) {
- List<OWLClass> classes = new LinkedList<>();
- for (OWLClassExpression ce : array) {
- classes.addAll(ce.getClassesInSignature());
- }
- return classes;
- }
- public static OWLOntologyClassWrapper getRootClass(IEOntology ontology) {
- OWLOntologyClassWrapper root = new OWLOntologyClassWrapper(ontology.getOntology(), null);
- root.setWrappedClass(OWLUtil.getOWLThing(ontology.getOntology()));
- List<OWLOntologyClassWrapper> wrappedClasses = new LinkedList<>();
- Map<OWLClass, OWLOntologyClassWrapper> map = new HashMap<>();
- for (OWLClass c : ontology.getOntology().getClassesInSignature()) {
- if (c.isOWLThing())
- continue;
- OWLOntologyClassWrapper owlOntologyClassWrapper = new OWLOntologyClassWrapper(ontology.getOntology(), c);
- wrappedClasses.add(owlOntologyClassWrapper);
- map.put(c, owlOntologyClassWrapper);
- }
- // assign children (parent gets assigned inherently;
- for (OWLOntologyClassWrapper wrappedClass : wrappedClasses) {
- Set<OWLClassExpression> subClasses = wrappedClass.getWrappedClass().getSubClasses(ontology.getOntology());
- for (OWLClassExpression ce : subClasses) {
- OWLClass next = ce.getClassesInSignature().iterator().next();
- wrappedClass.addChild(map.get(next));
- }
- }
- // add wrapper to root that do not have a parent assigned
- for (OWLOntologyClassWrapper wrapper : wrappedClasses) {
- if (wrapper.getParent() == null) {
- root.addChild(wrapper);
- }
- }
- return root;
- }
- public static OWLClass getOWLThing(OWLOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- return factory.getOWLThing();
- }
- public static OWLObjectProperty getOWLTopObjectProperty(OWLOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- return factory.getOWLTopObjectProperty();
- }
- public static OWLOntologyPropertyWrapper getRootProperty(IEOntology ontology) {
- OWLOntologyPropertyWrapper root = new OWLOntologyPropertyWrapper(ontology.getOntology(),
- getOWLTopObjectProperty(ontology.getOntology()));
- List<OWLOntologyPropertyWrapper> wrappedProperties = new LinkedList<>();
- Map<OWLObjectProperty, OWLOntologyPropertyWrapper> map = new HashMap<>();
- for (OWLObjectProperty prop : ontology.getOntology().getObjectPropertiesInSignature()) {
- OWLOntologyPropertyWrapper owlOntologyPropertyWrapper = new OWLOntologyPropertyWrapper(
- ontology.getOntology(), prop);
- wrappedProperties.add(owlOntologyPropertyWrapper);
- map.put(prop, owlOntologyPropertyWrapper);
- }
- // assign children (parent gets assigned inherently;
- for (OWLOntologyPropertyWrapper wrappedProperty : wrappedProperties) {
- Set<OWLObjectPropertyExpression> subProperties = wrappedProperty.getWrappedProperty()
- .getSubProperties(ontology.getOntology());
- for (OWLObjectPropertyExpression ce : subProperties) {
- OWLObjectProperty next = ce.getObjectPropertiesInSignature().iterator().next();
- wrappedProperty.addChild(map.get(next));
- }
- }
- // add wrapper to root that do not have a parent assigned
- for (OWLOntologyPropertyWrapper wrapper : wrappedProperties) {
- if (wrapper.getParent() == null) {
- root.addChild(wrapper);
- }
- }
- return root;
- }
- public static IRI getOntologyIRI(OWLOntology ontology) {
- return ontology.getOntologyID().getOntologyIRI();
- }
- public static OWLClass createNewClass(IEOntology ontology, String value) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- IRI classIri = IRI.create(getOntologyIRI(ontology.getOntology()).toString() + "#" + value);
- OWLClass owlClass = factory.getOWLClass(classIri);
- return owlClass;
- }
- public static void addEntityToOntology(OWLEntity newClass, IEOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- OWLDeclarationAxiom declarationAxiom = factory.getOWLDeclarationAxiom(newClass);
- manager.addAxiom(ontology.getOntology(), declarationAxiom);
- }
- public static void assignAsSubclass(OWLClass parent, OWLClass child, IEOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- if (parent != null && !parent.equals(OWLUtil.getOWLThing(ontology.getOntology()))) {
- OWLAxiom axiom = factory.getOWLSubClassOfAxiom(child, parent);
- AddAxiom addAxiom = new AddAxiom(ontology.getOntology(), axiom);
- manager.applyChange(addAxiom);
- }
- }
- public static void removeChildClass(OWLClass parent, OWLClass currentChild, OWLOntology ontology) {
- OWLAxiom originalAxiom = getFactory().getOWLSubClassOfAxiom(currentChild, parent);
- OWLOntologyChange chg = new RemoveAxiom(ontology, originalAxiom);
- getManager().applyChange(chg);
- }
- public static void assignAsSubObjectProperty(OWLObjectProperty parent, OWLObjectProperty child,
- IEOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- if (parent != null && !parent.equals(OWLUtil.getOWLTopObjectProperty(ontology.getOntology()))) {
- OWLAxiom axiom = factory.getOWLSubObjectPropertyOfAxiom(child, parent);
- AddAxiom addAxiom = new AddAxiom(ontology.getOntology(), axiom);
- manager.applyChange(addAxiom);
- }
- }
- public static void deleteEntitiyFromOntology(OWLEntity entity, IEOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLEntityRemover remover = new OWLEntityRemover(manager, Collections.singleton(ontology.getOntology()));
- entity.accept(remover);
- manager.applyChanges(remover.getChanges());
- }
- public static OWLObjectProperty createNewProperty(OWLOntology ontology, String value) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- IRI classIri = IRI.create(getOntologyIRI(ontology).toString() + "#" + value);
- OWLObjectProperty owlProperty = factory.getOWLObjectProperty(classIri);
- return owlProperty;
- }
- public static OWLOntology createOntology(String ontIri) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLOntology ontology = null;
- try {
- ontology = manager.createOntology(IRI.create(ontIri));
- } catch (OWLOntologyCreationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return ontology;
- }
- public static List<OWLAnnotation> getAnnotationsOnEntity(OWLClass classWithAnnos, OWLOntology ontology) {
- return new LinkedList<OWLAnnotation>(classWithAnnos.getAnnotations(ontology));
- }
- public static OWLAnnotation createAnnotation(String label, String value, OWLOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- OWLAnnotation owlAnnotation = factory.getOWLAnnotation(
- factory.getOWLAnnotationProperty(IRI.create(getOntologyIRI(ontology).toString() + "#" + label)),
- factory.getOWLLiteral(value));
- return owlAnnotation;
- }
- public static void addAnnotationToEntity(OWLAnnotation entity, OWLEntity classWithAnnos, OWLOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- OWLAxiom ax = factory.getOWLAnnotationAssertionAxiom(classWithAnnos.getIRI(), entity);
- // Add the axiom to the ontology
- manager.applyChange(new AddAxiom(ontology, ax));
- }
- public static List<OWLClass> getClassesInDomain(OWLObjectProperty property, OWLOntology ontology) {
- Set<OWLClassExpression> ranges = property.getDomains(ontology);
- return getClassesFromExpressions(ranges.toArray(new OWLClassExpression[0]));
- }
- public static List<OWLClass> getClassesInRange(OWLObjectProperty property, OWLOntology ontology) {
- Set<OWLClassExpression> ranges = property.getRanges(ontology);
- return getClassesFromExpressions(ranges.toArray(new OWLClassExpression[0]));
- }
- public static void addClassToDomain(OWLObjectProperty property, OWLOntology ontology, OWLClass cl) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- OWLObjectPropertyDomainAxiom owlObjectPropertyDomainAxiom = factory.getOWLObjectPropertyDomainAxiom(property,
- cl);
- manager.addAxiom(ontology, owlObjectPropertyDomainAxiom);
- }
- public static void addClassToRange(OWLObjectProperty property, OWLOntology ontology, OWLClass cl) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- OWLObjectPropertyRangeAxiom owlObjectPropertyDomainAxiom = factory.getOWLObjectPropertyRangeAxiom(property, cl);
- manager.addAxiom(ontology, owlObjectPropertyDomainAxiom);
- }
- private static OWLDataFactory getFactory() {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- return factory;
- }
- private static OWLOntologyManager getManager() {
- return OWLManager.createOWLOntologyManager();
- }
- public static void renameEntity(OWLEntity entity, OWLOntology ontology, String newIRI) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLEntityRenamer renamer = new OWLEntityRenamer(manager, Collections.singleton(ontology));
- List<OWLOntologyChange> changeIRI = renamer.changeIRI(entity, IRI.create(newIRI));
- manager.applyChanges(changeIRI);
- }
- public static List<OWLObjectProperty> getConstructionProperties(OWLClass classWithAnnos, IEOntology ontology) {
- List<OWLObjectProperty> constructionProperties = new LinkedList<>();
- List<OWLObjectProperty> properties = OWLUtil.getAllObjectPropertiesInOntology(ontology);
- for (OWLObjectProperty prop : properties) {
- // get the domain of prop
- List<OWLClass> classesInDomain = getClassesInDomain(prop, ontology.getOntology());
- if (classesInDomain.contains(classWithAnnos) && classesInDomain.size() == 1) {
- // check the range
- List<OWLClass> classesInRange = getClassesInRange(prop, ontology.getOntology());
- if (classesInRange.size() > 1) {
- constructionProperties.add(prop);
- }
- if (isConstructionProperty(prop, ontology.getOntology())) {
- constructionProperties.add(prop);
- }
- }
- }
- return constructionProperties;
- }
- private static List<OWLObjectProperty> getAllObjectPropertiesInOntology(IEOntology ontology) {
- List<OWLObjectProperty> allProps = new LinkedList<>();
- OWLOntologyPropertyWrapper rootProperty = getRootProperty(ontology);
- Collection<OWLOntologyPropertyWrapper> allSubsumedChildren = rootProperty.getAllSubsumedChildren();
- for (OWLOntologyPropertyWrapper wrapper : allSubsumedChildren) {
- allProps.add(wrapper.getWrappedProperty());
- }
- return allProps;
- }
- public static void deleteAnnotationFromOntology(OWLAnnotation anno, OWLClass owlClass, OWLOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- Set<OWLAnnotationAssertionAxiom> annotationAssertionAxioms = ontology
- .getAnnotationAssertionAxioms(owlClass.getIRI());
- for (OWLAnnotationAssertionAxiom axiom : annotationAssertionAxioms) {
- if (axiom.getAnnotation().equals(anno)) {
- manager.removeAxiom(ontology, axiom);
- }
- }
- }
- public static void removeClassFromRange(OWLObjectProperty property, OWLClass owlClass, OWLOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- Set<OWLObjectPropertyRangeAxiom> objectPropertyRangeAxioms = ontology.getObjectPropertyRangeAxioms(property);
- for (OWLObjectPropertyRangeAxiom axiom : objectPropertyRangeAxioms) {
- if (axiom.getRange().getClassesInSignature().contains(owlClass)) {
- manager.removeAxiom(ontology, axiom);
- }
- }
- }
- public static void removeClassFromDomain(OWLObjectProperty property, OWLClass owlClass, OWLOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- Set<OWLObjectPropertyDomainAxiom> objectPropertyDomainAxioms = ontology.getObjectPropertyDomainAxioms(property);
- for (OWLObjectPropertyDomainAxiom axiom : objectPropertyDomainAxioms) {
- if (axiom.getDomain().getClassesInSignature().contains(owlClass)) {
- manager.removeAxiom(ontology, axiom);
- }
- }
- }
- // test if the according annotation is present
- public static boolean isConstructionProperty(OWLObjectProperty property, OWLOntology ontology) {
- Set<OWLAnnotationAssertionAxiom> annotationAssertionAxioms = property.getAnnotationAssertionAxioms(ontology);
- for (OWLAnnotationAssertionAxiom axiom : annotationAssertionAxioms) {
- OWLAnnotation annotation = axiom.getAnnotation();
- String fragment = annotation.getProperty().getIRI().getFragment();
- if (fragment.equals(ANNOTATION_CONSTRUCTION_PROPERTY)) {
- return true;
- }
- }
- return false;
- }
- public static void setConstructionProperty(OWLObjectProperty property, OWLOntology ontology,
- boolean setConstructionProperty) {
- boolean isConstructionProp = isConstructionProperty(property, ontology);
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- if (isConstructionProp && setConstructionProperty) {
- // it is already a construction property
- return;
- }
- else if (isConstructionProp && !setConstructionProperty) {
- // we need to delete the annotation
- Set<OWLAnnotationAssertionAxiom> annotationAssertionAxioms = property
- .getAnnotationAssertionAxioms(ontology);
- for (OWLAnnotationAssertionAxiom axiom : annotationAssertionAxioms) {
- OWLAnnotation annotation = axiom.getAnnotation();
- String fragment = annotation.getProperty().getIRI().getFragment();
- if (fragment.equals(ANNOTATION_CONSTRUCTION_PROPERTY)) {
- manager.removeAxiom(ontology, axiom);
- }
- }
- } else if (!isConstructionProp && setConstructionProperty) {
- // we need to add the annotation
- OWLAnnotation constructionAnnotation = createAnnotation(ANNOTATION_CONSTRUCTION_PROPERTY,
- property.getIRI().getFragment(), ontology);
- addAnnotationToEntity(constructionAnnotation, property, ontology);
- } else {
- // it is already no construction property
- return;
- }
- }
- // this means that the amount of classes in the domain=1 and the amount of
- // classes in the range>1
- public static boolean isDefiniteConstructionProperty(OWLObjectProperty property, OWLOntology ontology) {
- return (getClassesInDomain(property, ontology).size() == 1 && getClassesInRange(property, ontology).size() > 1);
- }
- public static void setIrreflexiveProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
- if (property.isIrreflexive(ontology) && !selection) {
- // delete the axiom
- Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
- for (OWLAxiom axiom : referencingAxioms) {
- if (axiom instanceof OWLIrreflexiveObjectPropertyAxiom) {
- getManager().removeAxiom(ontology, axiom);
- }
- }
- } else if (!property.isIrreflexive(ontology) && selection) {
- // add the axiom
- OWLIrreflexiveObjectPropertyAxiom owlIrreflexiveObjectPropertyAxiom = getFactory()
- .getOWLIrreflexiveObjectPropertyAxiom(property);
- getManager().addAxiom(ontology, owlIrreflexiveObjectPropertyAxiom);
- }
- }
- public static void setReflexiveProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
- if (property.isReflexive(ontology) && !selection) {
- // delete the axiom
- Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
- for (OWLAxiom axiom : referencingAxioms) {
- if (axiom instanceof OWLReflexiveObjectPropertyAxiom) {
- getManager().removeAxiom(ontology, axiom);
- }
- }
- } else if (!property.isReflexive(ontology) && selection) {
- // add the axiom
- OWLReflexiveObjectPropertyAxiom owlReflexiveObjectPropertyAxiom = getFactory()
- .getOWLReflexiveObjectPropertyAxiom(property);
- getManager().addAxiom(ontology, owlReflexiveObjectPropertyAxiom);
- }
- }
- public static void setIsAsymmetricProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
- if (property.isAsymmetric(ontology) && !selection) {
- // delete the axiom
- Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
- for (OWLAxiom axiom : referencingAxioms) {
- if (axiom instanceof OWLAsymmetricObjectPropertyAxiom) {
- getManager().removeAxiom(ontology, axiom);
- }
- }
- } else if (!property.isAsymmetric(ontology) && selection) {
- // add the axiom
- OWLAsymmetricObjectPropertyAxiom owlAsymmetricObjectPropertyAxiom = getFactory()
- .getOWLAsymmetricObjectPropertyAxiom(property);
- getManager().addAxiom(ontology, owlAsymmetricObjectPropertyAxiom);
- }
- }
- public static void setIsSymmetricProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
- if (property.isSymmetric(ontology) && !selection) {
- // delete the axiom
- Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
- for (OWLAxiom axiom : referencingAxioms) {
- if (axiom instanceof OWLSymmetricObjectPropertyAxiom) {
- getManager().removeAxiom(ontology, axiom);
- }
- }
- } else if (!property.isSymmetric(ontology) && selection) {
- // add the axiom
- OWLSymmetricObjectPropertyAxiom owlSymmetricObjectPropertyAxiom = getFactory()
- .getOWLSymmetricObjectPropertyAxiom(property);
- getManager().addAxiom(ontology, owlSymmetricObjectPropertyAxiom);
- }
- }
- public static void setIsTransitiveProperty(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
- if (property.isTransitive(ontology) && !selection) {
- // delete the axiom
- Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
- for (OWLAxiom axiom : referencingAxioms) {
- if (axiom instanceof OWLTransitiveObjectPropertyAxiom) {
- getManager().removeAxiom(ontology, axiom);
- }
- }
- } else if (!property.isTransitive(ontology) && selection) {
- // add the axiom
- OWLTransitiveObjectPropertyAxiom owlTransitiveObjectPropertyAxiom = getFactory()
- .getOWLTransitiveObjectPropertyAxiom(property);
- getManager().addAxiom(ontology, owlTransitiveObjectPropertyAxiom);
- }
- }
- public static void setIsInverseFunctionalProperty(OWLObjectProperty property, OWLOntology ontology,
- boolean selection) {
- if (property.isInverseFunctional(ontology) && !selection) {
- // delete the axiom
- Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
- for (OWLAxiom axiom : referencingAxioms) {
- if (axiom instanceof OWLInverseFunctionalObjectPropertyAxiom) {
- getManager().removeAxiom(ontology, axiom);
- }
- }
- } else if (!property.isInverseFunctional(ontology) && selection) {
- // add the axiom
- OWLInverseFunctionalObjectPropertyAxiom owlInverseFunctionalObjectPropertyAxiom = getFactory()
- .getOWLInverseFunctionalObjectPropertyAxiom(property);
- getManager().addAxiom(ontology, owlInverseFunctionalObjectPropertyAxiom);
- }
- }
- public static void setIsFunctional(OWLObjectProperty property, OWLOntology ontology, boolean selection) {
- if (property.isFunctional(ontology) && !selection) {
- // delete the axiom
- Set<OWLAxiom> referencingAxioms = property.getReferencingAxioms(ontology);
- for (OWLAxiom axiom : referencingAxioms) {
- if (axiom instanceof OWLFunctionalObjectPropertyAxiom) {
- getManager().removeAxiom(ontology, axiom);
- }
- }
- } else if (!property.isFunctional(ontology) && selection) {
- // add the axiom
- OWLFunctionalObjectPropertyAxiom owlFunctionalObjectPropertyAxiom = getFactory()
- .getOWLFunctionalObjectPropertyAxiom(property);
- getManager().addAxiom(ontology, owlFunctionalObjectPropertyAxiom);
- }
- }
- public static void removeChildProperty(OWLObjectProperty currentParent, OWLObjectProperty child,
- OWLOntology ontology) {
- OWLAxiom originalAxiom = getFactory().getOWLSubObjectPropertyOfAxiom(child, currentParent);
- OWLOntologyChange chg = new RemoveAxiom(ontology, originalAxiom);
- getManager().applyChange(chg);
- }
- public static boolean doesEntityExist(String newIRI, OWLOntology ontology) {
- return ontology.containsEntityInSignature(IRI.create(newIRI));
- }
- public static OWLClass getClassByIRI(String iriToCheck, OWLOntology ontology) {
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = manager.getOWLDataFactory();
- if (iriToCheck.toLowerCase().matches("thing"))
- return getOWLThing(ontology);
- IRI classIri = IRI.create(getOntologyIRI(ontology).toString() + "#" + iriToCheck);
- OWLClass owlClass = factory.getOWLClass(classIri);
- return owlClass;
- }
- public static boolean isFrameProperty(OWLObjectProperty wrappedProperty, OWLOntology ontology) {
- return (getClassesInDomain(wrappedProperty, ontology).size() == 0
- && getClassesInRange(wrappedProperty, ontology).size() > 0);
- }
- // check transitive...
- public static boolean isSubClassOf(OWLClass potentialSubClass, OWLClass potentialParent, OWLOntology ontology) {
- if (potentialParent.isOWLThing())
- return true;
- List<OWLClass> allSubsumedClasses = getAllSubsumedClasses(potentialParent, ontology);
- return allSubsumedClasses.contains(potentialSubClass);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement