Skip to content

Commit

Permalink
Adding Owl to Oml adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
melaasar committed Apr 4, 2024
1 parent b0b3268 commit 61fd09c
Show file tree
Hide file tree
Showing 20 changed files with 2,258 additions and 977 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
subprojects {
group = 'io.opencaesar.adapters'
version = '2.5.0'
version = '2.6.0'

ext.versions = [
oml: '2.+',
oml_tools: '2.+',
owl_tools: '2.+',
owl: '5.1.17',
owl: '5.5.0',
jcommander: '1.72',
log4j: '1.2.17',
slf4j: '1.7.33',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ public Task configure(Closure closure) {
}

/**
* Path of the input OML catalog.
* Path of the input Oml catalog.
*
* @return File Property
*/
@Input
public abstract Property<File> getInputCatalogPath();

/**
* Root OML ontology IRI.
* Root Oml ontology IRI.
*
* @return String Property
*/
Expand Down Expand Up @@ -142,7 +142,7 @@ public Task configure(Closure closure) {
public abstract Property<Boolean> getDebug();

/**
* The collection of input OML files referenced by the input Oml catalog
* The collection of input Oml files referenced by the input Oml catalog
*
* @return ConfigurableFileCollection
*/
Expand Down Expand Up @@ -215,7 +215,7 @@ public void run(InputChanges inputChanges) {
inputChanges.getFileChanges(getInputFiles()).forEach(f -> deltas.add(f.getFile()));
Oml2OwlApp.mainWithDeltas(deltas, args.toArray(new String[0]));
} else {
Oml2OwlApp.mainWithDeltas(null, args.toArray(new String[0]));
Oml2OwlApp.main(args.toArray(new String[0]));
}
} catch (Exception e) {
throw new TaskExecutionException(this, e);
Expand Down
2 changes: 1 addition & 1 deletion oml2owl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'io.opencaesar.owl:oml2owl-gradle:+'
classpath 'io.opencaesar.adapters:oml2owl-gradle:+'
}
}
task oml2owl(type:io.opencaesar.oml2owl.Oml2OwlTask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public CloseVocabularyBundle(final VocabularyBundle bundle) {
}

/**
* Returns a transitively-reduced concept taxonomy, rooted at Universal, from a collection of OML ontologies.
* Returns a transitively-reduced concept taxonomy, rooted at Universal, from a collection of Oml ontologies.
*
* @param allVocabularies
* @return concept taxonomy
Expand Down
100 changes: 59 additions & 41 deletions oml2owl/src/main/java/io/opencaesar/oml2owl/Oml2Owl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnonymousIndividual;
Expand All @@ -42,6 +41,7 @@
import org.semanticweb.owlapi.model.SWRLIArgument;
import org.semanticweb.owlapi.model.SWRLObjectPropertyAtom;
import org.semanticweb.owlapi.vocab.OWLFacet;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;

import io.opencaesar.oml.Annotation;
import io.opencaesar.oml.AnnotationProperty;
Expand All @@ -58,7 +58,6 @@
import io.opencaesar.oml.DescriptionBundle;
import io.opencaesar.oml.DifferentFromPredicate;
import io.opencaesar.oml.DoubleLiteral;
import io.opencaesar.oml.Element;
import io.opencaesar.oml.Import;
import io.opencaesar.oml.InstanceEnumerationAxiom;
import io.opencaesar.oml.IntegerLiteral;
Expand Down Expand Up @@ -100,16 +99,18 @@
import io.opencaesar.oml.UnreifiedRelation;
import io.opencaesar.oml.Vocabulary;
import io.opencaesar.oml.VocabularyBundle;
import io.opencaesar.oml.util.OmlConstants;
import io.opencaesar.oml.util.OmlSwitch;

class Oml2Owl extends OmlSwitch<Void> {

public static final List<String> BUILT_IN_ONTOLOGIES = Arrays.asList(new String[] {
private static final List<String> BUILT_IN_ONTOLOGIES = Arrays.asList(new String[] {
"http://www.w3.org/2001/XMLSchema",
"http://www.w3.org/1999/02/22-rdf-syntax-ns",
"http://www.w3.org/2000/01/rdf-schema",
"http://www.w3.org/2002/07/owl",
"http://www.w3.org/2003/11/swrlb"});
"http://www.w3.org/2003/11/swrlb"
});

private final Resource inputResource;
private final OwlApi owl;
Expand All @@ -128,38 +129,52 @@ public OWLOntology run() {

@Override
public Void caseAnnotation(final Annotation annotation) {
addAnnotation(annotation.getAnnotatedElement(), annotation);
var element = annotation.getAnnotatedElement();
if (element instanceof Ontology) {
owl.addOntologyAnnotation(ontology, createAnnotation(annotation));
} else if (element instanceof Rule) {
var rule = (Rule) annotation.getOwningElement();
if (rule.isRef()) { // annotations on rule definitions are handled with the definitions
owl.addAnnotationAssertion(ontology, rule.getIri(), createAnnotation(annotation));
}
} else {
owl.addAnnotationAssertion(ontology, ((Member)element).getIri(), createAnnotation(annotation));
}
return null;
}

@Override
public Void caseVocabulary(final Vocabulary vocabulary) {
ontology = owl.createOntology(vocabulary.getPrefix(), vocabulary.getNamespace());
owl.addOntologyAnnotation(ontology, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.Vocabulary)));
ontology = createOntology(vocabulary, OmlConstants.Vocabulary);
return null;
}

@Override
public Void caseVocabularyBundle(final VocabularyBundle bundle) {
ontology = owl.createOntology(bundle.getPrefix(), bundle.getNamespace());
owl.addOntologyAnnotation(ontology, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.VocabularyBundle)));
ontology = createOntology(bundle, OmlConstants.VocabularyBundle);
return null;
}

@Override
public Void caseDescription(final Description description) {
ontology = owl.createOntology(description.getPrefix(), description.getNamespace());
owl.addOntologyAnnotation(ontology, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.Description)));
ontology = createOntology(description, OmlConstants.Description);
return null;
}

@Override
public Void caseDescriptionBundle(final DescriptionBundle bundle) {
ontology = owl.createOntology(bundle.getPrefix(), bundle.getNamespace());
owl.addOntologyAnnotation(ontology, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.DescriptionBundle)));
ontology = createOntology(bundle, OmlConstants.DescriptionBundle);
return null;
}

private OWLOntology createOntology(final Ontology omlOntology, String typeIri) {
OWLOntology ontology = owl.createOntology(omlOntology.getPrefix(), omlOntology.getNamespace());
owl.addOntologyAnnotation(ontology, owl.getAnnotation(OmlConstants.type, owl.createIri(typeIri)));
owl.addOntologyAnnotation(ontology, owl.getAnnotation(OmlConstants.namespace, owl.createIri(omlOntology.getNamespace())));
owl.addOntologyAnnotation(ontology, owl.getAnnotation(OmlConstants.prefix, owl.getLiteral(omlOntology.getPrefix())));
return ontology;
}

@Override
public Void caseImport(final Import import_) {
final String iri = import_.getIri();
Expand Down Expand Up @@ -231,7 +246,6 @@ public Void caseScalarProperty(final ScalarProperty property) {
final String propertyIri = property.getIri();
if (!property.isRef()) {
owl.addDataProperty(ontology, propertyIri);
owl.addAnnotationAssertion(ontology, propertyIri, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.scalarProperty)));
}
property.getDomains().forEach(t -> owl.addDataPropertyDomain(ontology, propertyIri, t.getIri()));
property.getRanges().forEach(t -> owl.addDataPropertyRange(ontology, propertyIri, t.getIri()));
Expand All @@ -246,7 +260,7 @@ public Void caseStructuredProperty(final StructuredProperty property) {
final String propertyIri = property.getIri();
if (!property.isRef()) {
owl.addObjectProperty(ontology, propertyIri);
owl.addAnnotationAssertion(ontology, propertyIri, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.structuredProperty)));
owl.addAnnotationAssertion(ontology, propertyIri, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.StructuredProperty)));
}
property.getDomains().forEach(t -> owl.addObjectPropertyDomain(ontology, propertyIri, t.getIri()));
property.getRanges().forEach(t -> owl.addObjectPropertyRange(ontology, propertyIri, t.getIri()));
Expand All @@ -265,7 +279,7 @@ public Void caseUnreifiedRelation(final UnreifiedRelation relation) {
protected void handleForwardRelation(final RelationBase base) {
final String forwardIri = getForwardIri(base);
final String forwardName = forwardIri.replace(base.getOntology().getNamespace(), "");
final String relationKindIri = (base instanceof RelationEntity) ? OmlConstants.forwardRelation : OmlConstants.UnreifiedRelation;
final String relationKindIri = (base instanceof RelationEntity) ? OmlConstants.ForwardRelation : OmlConstants.UnreifiedRelation;

if (!base.isRef()) {
owl.addObjectProperty(ontology, forwardIri);
Expand All @@ -278,8 +292,8 @@ protected void handleForwardRelation(final RelationBase base) {
antedecents.add(owl.getObjectPropertyAtom(OmlConstants.sourceRelation, owl.getSWRLVariable("r"), owl.getSWRLVariable("s")));
antedecents.add(owl.getObjectPropertyAtom(OmlConstants.targetRelation, owl.getSWRLVariable("r"), owl.getSWRLVariable("t")));
final SWRLObjectPropertyAtom consequent = owl.getObjectPropertyAtom(forwardIri, owl.getSWRLVariable("s"), owl.getSWRLVariable("t"));
final OWLAnnotation annotation = owl.getAnnotation(RDFS.LABEL.toString(), owl.getLiteral(forwardName+" derivation"));
owl.addRule(ontology, Collections.singletonList(consequent), antedecents, annotation);
final OWLAnnotation annotation = owl.getAnnotation(OWLRDFVocabulary.RDFS_LABEL.toString(), owl.getLiteral(forwardName+" derivation"));
owl.addRule(ontology, antedecents, Collections.singletonList(consequent), annotation);
}
}

Expand Down Expand Up @@ -313,7 +327,8 @@ public Void caseReverseRelation(final ReverseRelation relation) {
RelationBase base = relation.getRelationBase();
final String reverseIri = relation.getIri();
owl.addObjectProperty(ontology, reverseIri);
owl.addAnnotationAssertion(ontology, reverseIri, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.reverseRelation)));
owl.addAnnotationAssertion(ontology, reverseIri, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.ReverseRelation)));
owl.addAnnotationAssertion(ontology, reverseIri, owl.getAnnotation(OmlConstants.relationBase, owl.createIri(base.getIri())));
owl.addInverseProperties(ontology, reverseIri, getForwardIri(base));

base.getSources().forEach(t -> owl.addObjectPropertyRange(ontology, reverseIri, t.getIri()));
Expand Down Expand Up @@ -346,12 +361,19 @@ public Void caseReverseRelation(final ReverseRelation relation) {
public Void caseRule(final Rule rule) {
if (!rule.isRef()) {
List<OWLAnnotation> annotations = rule.getOwnedAnnotations().stream().map(it -> createAnnotation(it)).collect(Collectors.toList());
if (annotations.stream().filter(a -> a.getProperty().getIRI().toString().equals(RDFS.LABEL.toString())).count() == 0) {
annotations = Collections.singletonList(owl.getAnnotation(RDFS.LABEL.toString(), owl.getLiteral(rule.getName())));
}

// HACK: ideally, the following annotation should have IRI values. However, due to some odd
// reason, when the OWL API reads them back, their subject is not the rule, but rather another
// anonmymous individual that has no obvious relation to the rule. As a workaround, define those
// annotations with literal values for now.
//annotations.add(owl.getAnnotation(OmlConstants.name, owl.createIri(rule.getIri())));
//annotations.add(owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.Rule)));
annotations.add(owl.getAnnotation(OmlConstants.name, owl.getLiteral(rule.getIri())));
annotations.add(owl.getAnnotation(OmlConstants.type, owl.getLiteral(OmlConstants.Rule)));

owl.addRule(ontology,
rule.getConsequent().stream().flatMap(p -> getAtom(p).stream()).collect(Collectors.toList()),
rule.getAntecedent().stream().flatMap(p -> getAtom(p).stream()).collect(Collectors.toList()),
rule.getConsequent().stream().flatMap(p -> getAtom(p).stream()).collect(Collectors.toList()),
toArray(annotations));
}
return null;
Expand All @@ -363,6 +385,7 @@ public Void caseConceptInstance(final ConceptInstance instance) {
final OWLNamedIndividual individual = instance.isRef() ?
owl.getNamedIndividual(instanceIri) :
owl.addNamedIndividual(ontology, instanceIri);
owl.addAnnotationAssertion(ontology, instance.getIri(), owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.ConceptInstance)));
instance.getOwnedPropertyValues().forEach(it -> appliesTo(it, individual));
return null;
}
Expand All @@ -373,6 +396,7 @@ public Void caseRelationInstance(final RelationInstance instance) {
final OWLNamedIndividual individual = instance.isRef() ?
owl.getNamedIndividual(instanceIri) :
owl.addNamedIndividual(ontology, instanceIri);
owl.addAnnotationAssertion(ontology, instance.getIri(), owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.RelationInstance)));
instance.getSources().forEach(s ->
owl.addObjectPropertyAssertion(ontology,
instanceIri,
Expand Down Expand Up @@ -417,13 +441,13 @@ public Void caseClassifierEquivalenceAxiom(final ClassifierEquivalenceAxiom axio
public Void caseScalarEquivalenceAxiom(final ScalarEquivalenceAxiom axiom) {
final ArrayList<OWLFacetRestriction> restrictions = new ArrayList<>();
if (axiom.getLength() != null) {
restrictions.add(owl.getFacetRestriction(OWLFacet.LENGTH, owl.getLiteral((axiom.getLength()).longValue())));
restrictions.add(owl.getFacetRestriction(OWLFacet.LENGTH, owl.getLiteral((axiom.getLength()).intValue())));
}
if (axiom.getMaxLength() != null) {
restrictions.add(owl.getFacetRestriction(OWLFacet.MAX_LENGTH, owl.getLiteral((axiom.getMaxLength()).longValue())));
restrictions.add(owl.getFacetRestriction(OWLFacet.MAX_LENGTH, owl.getLiteral((axiom.getMaxLength()).intValue())));
}
if (axiom.getMinLength() != null) {
restrictions.add(owl.getFacetRestriction(OWLFacet.MIN_LENGTH, owl.getLiteral((axiom.getMinLength()).longValue())));
restrictions.add(owl.getFacetRestriction(OWLFacet.MIN_LENGTH, owl.getLiteral((axiom.getMinLength()).intValue())));
}
if (axiom.getPattern() != null) {
restrictions.add(owl.getFacetRestriction(OWLFacet.PATTERN, owl.getLiteral(axiom.getPattern())));
Expand Down Expand Up @@ -511,29 +535,29 @@ public OWLClassExpression handlePropertyCardinalityRestrictionAxiom(final Proper
if (axiom.getProperty() instanceof ScalarProperty) {
if (axiom.getKind() == CardinalityRestrictionKind.MIN) {
return owl.getDataMinCardinality(
axiom.getProperty().getIri(), (int) axiom.getCardinality(),
axiom.getProperty().getIri(), axiom.getCardinality(),
(axiom.getRange() != null) ? axiom.getRange().getIri() : null);
} else if (axiom.getKind() == CardinalityRestrictionKind.MAX) {
return owl.getDataMaxCardinality(
axiom.getProperty().getIri(), (int) axiom.getCardinality(),
axiom.getProperty().getIri(), axiom.getCardinality(),
(axiom.getRange() != null) ? axiom.getRange().getIri() : null);
} else {
return owl.getDataExactCardinality(
axiom.getProperty().getIri(), (int) axiom.getCardinality(),
axiom.getProperty().getIri(), axiom.getCardinality(),
(axiom.getRange() != null) ? axiom.getRange().getIri() : null);
}
} else {
if (axiom.getKind() == CardinalityRestrictionKind.MIN) {
return owl.getObjectMinCardinality(
axiom.getProperty().getIri(), (int) axiom.getCardinality(),
axiom.getProperty().getIri(), axiom.getCardinality(),
(axiom.getRange() != null) ? axiom.getRange().getIri() : null);
} else if (axiom.getKind() == CardinalityRestrictionKind.MAX) {
return owl.getObjectMaxCardinality(
axiom.getProperty().getIri(), (int) axiom.getCardinality(),
axiom.getProperty().getIri(), axiom.getCardinality(),
(axiom.getRange() != null) ? axiom.getRange().getIri() : null);
} else {
return owl.getObjectExactCardinality(
axiom.getProperty().getIri(), (int) axiom.getCardinality(),
axiom.getProperty().getIri(), axiom.getCardinality(),
(axiom.getRange() != null) ? axiom.getRange().getIri() : null);
}
}
Expand Down Expand Up @@ -618,14 +642,6 @@ protected OWLAnnotation createAnnotation(final Annotation annotation) {
}
}

protected void addAnnotation(final Element element, final Annotation annotation) {
if (element instanceof Ontology) {
owl.addOntologyAnnotation(ontology, createAnnotation(annotation));
} else if (element instanceof Member) {
owl.addAnnotationAssertion(ontology, ((Member)element).getIri(), createAnnotation(annotation));
}
}

protected void specializes(final Term specific, final Term general) {
if (specific instanceof Aspect && general instanceof Aspect) {
specializes((Aspect) specific, (Aspect) general);
Expand Down Expand Up @@ -843,6 +859,8 @@ protected OWLLiteral getLiteral(final DoubleLiteral literal) {

protected OWLAnonymousIndividual createIndividual(final StructureInstance instance) {
final OWLAnonymousIndividual individual = owl.getAnonymousIndividual();
owl.addAnnotationAssertion(ontology, individual, owl.getAnnotation(OmlConstants.type, owl.createIri(OmlConstants.StructureInstance)));
owl.addClassAssertion(ontology, individual, instance.getType().getIri());
instance.getOwnedPropertyValues().forEach(it -> appliesTo(it, individual));
return individual;
}
Expand All @@ -861,7 +879,7 @@ protected String getForwardIri(final RelationBase base) {
}

static boolean isBuiltInOntology(final String iri) {
return Oml2Owl.BUILT_IN_ONTOLOGIES.contains(iri);
return BUILT_IN_ONTOLOGIES.contains(iri);
}

private static String toFirstUpper(String s) {
Expand Down
Loading

0 comments on commit 61fd09c

Please sign in to comment.