Skip to content

Commit

Permalink
Merge pull request #265 from mrnolte/236
Browse files Browse the repository at this point in the history
Automatic GCI conversion
  • Loading branch information
sasjonge authored Jul 26, 2022
2 parents d8ab8be + 4e4644a commit b43026d
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
5 changes: 4 additions & 1 deletion scripts/java/src/main/java/main/CIRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ public class CIRunner implements CommandLineRunner {
@Autowired
private OntologySaver ontologySaver;

@Autowired
private SubclassNothingRewriter gciRewriter;


@Override
public void run(final String... args) throws Exception {
final CIRunnable[] toRun = {isDefinedInAdder, versionInfoAdder, collapser, ontologySaver};
final CIRunnable[] toRun = {gciRewriter, isDefinedInAdder, versionInfoAdder, collapser, ontologySaver};
for (final var next : toRun) {
next.run();
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/java/src/main/java/main/ci_runners/Collapser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

Expand All @@ -21,6 +22,7 @@
import java.util.stream.Collectors;

@Component
@Lazy
public class Collapser implements CIRunnable {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashSet;

@Component
@Lazy
public class IsDefinedInAdder implements CIRunnable {

/**
Expand Down
2 changes: 2 additions & 0 deletions scripts/java/src/main/java/main/ci_runners/OntologySaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy
public class OntologySaver implements CIRunnable {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main.ci_runners;

import main.OntologyManager;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Lazy
public class SubclassNothingRewriter implements CIRunnable {

/**
* {@link Logger} of this class.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(SubclassNothingRewriter.class);
private static final OWLDataFactory df = OWLManager.getOWLDataFactory();
private final OntologyManager ontologyManager;

@Autowired
public SubclassNothingRewriter(final OntologyManager ontologyManager) {
this.ontologyManager = ontologyManager;
}


@Override
public void run() {
ontologyManager.getOntologyManager().getOntologies().forEach(SubclassNothingRewriter::rewriteGCIs);
}

private static void rewriteGCIs(final OWLOntology ontology) {
@SuppressWarnings("OptionalGetWithoutIsPresent") final var iri = ontology.getOntologyID().getOntologyIRI()
.get();
final var gcis = ontology.getGeneralClassAxioms();
if (gcis.isEmpty()) {
return;
}
LOGGER.info("Rewriting the following GCIs from ontology {}", iri);

gcis.forEach(next -> {
final Optional<OWLAxiom> rewritten = rewrite(next);
if (rewritten.isEmpty()) {
return;
}
LOGGER.info("Rewrote {} to {}", next, rewritten.get());
ontology.removeAxiom(next);
ontology.add(rewritten.get());
});

}

@SuppressWarnings("ChainOfInstanceofChecks")
private static Optional<OWLAxiom> rewrite(final OWLAxiom gci) {
if (gci instanceof OWLSubClassOfAxiom subClassOfAxiom) {
if (!subClassOfAxiom.getSuperClass().isOWLNothing()) {
return Optional.empty();
}
if (subClassOfAxiom.getSubClass() instanceof OWLObjectIntersectionOf intersection) {
return rewriteIntersectionSubclassNothing(intersection);
}
if (subClassOfAxiom.getSubClass() instanceof OWLObjectSomeValuesFrom someValuesFrom) {
final var rewriting = df.getOWLObjectPropertyRangeAxiom(someValuesFrom.getProperty(),
df.getOWLObjectComplementOf(
someValuesFrom.getFiller()),
gci.getAnnotations());
return Optional.of(rewriting);
}
}
return Optional.empty();
}

private static Optional<OWLAxiom> rewriteIntersectionSubclassNothing(final OWLObjectIntersectionOf intersection) {
var atomicClass = intersection.operands().filter(AsOWLClass::isOWLClass).findAny();
if (atomicClass.isEmpty()) {
atomicClass = intersection.operands().findFirst();
}
if (atomicClass.isEmpty()) {
throw new IllegalArgumentException("Corrupted GCI:" + intersection + " subclass of owl:Nothing");
}

final OWLClassExpression finalAtomicClass = atomicClass.get();
final var rewriting = df.getOWLSubClassOfAxiom(finalAtomicClass, df.getOWLObjectComplementOf(
df.getOWLObjectIntersectionOf(intersection.operands().filter(next -> !next.equals(finalAtomicClass)))));
return Optional.of(rewriting);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy
public class VersionInfoAdder implements CIRunnable {


/**
* {@link Logger} of this class.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(VersionInfoAdder.class);

private final OntologyManager ontologyManager;


private final String versionInfo;

@Autowired
Expand Down

0 comments on commit b43026d

Please sign in to comment.