Skip to content

Commit

Permalink
CLI to picocli + fixing logging, comparator
Browse files Browse the repository at this point in the history
  • Loading branch information
psiotwo committed Nov 15, 2022
1 parent 0dbe9e3 commit c64ff01
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 314 deletions.
19 changes: 12 additions & 7 deletions owldiff-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>cz.cvut.kbss</groupId>
Expand All @@ -33,22 +33,27 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>com.github.rvesse</groupId>
<artifactId>airline</artifactId>
<version>2.8.0</version>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.7.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cz.cvut.kbss.owldiff.cli;

import cz.cvut.kbss.owldiff.change.OWLChangeType;

public class ChangeTypeUtils {

public static String changeTypeName(OWLChangeType ct) {
switch (ct) {
case SYNTACTIC_ORIG_REST:
return "-";
case SYNTACTIC_UPD_REST:
return "+";
case ENTAILEXPL_INFERRED:
case ENTAILEXPL_POSSIBLY_REMOVE:
case CEX_ISIN_DIFFR_DIFFL:
case CEX_ISIN_DIFFR:
case CEX_ISIN_DIFFL:
case HEURISTIC_SAME_SUB_AND_SUPER_CLASSES:
case HEURISTIC_SINGLE_UNMATCHED_SIBLING:
default:
throw new UnsupportedOperationException();
}
}
}
178 changes: 54 additions & 124 deletions owldiff-cli/src/main/java/cz/cvut/kbss/owldiff/cli/Diff.java
Original file line number Diff line number Diff line change
@@ -1,153 +1,83 @@
package cz.cvut.kbss.owldiff.cli;

import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
import cz.cvut.kbss.owldiff.change.OWLChangeType;
import cz.cvut.kbss.owldiff.change.SyntacticAxiomChange;
import cz.cvut.kbss.owldiff.diff.syntactic.SyntacticDiff;
import cz.cvut.kbss.owldiff.diff.syntactic.SyntacticDiffOutput;
import cz.cvut.kbss.owldiff.ontology.OntologyHandler;
import cz.cvut.kbss.owldiff.syntax.ManchesterSyntax;
import lombok.extern.slf4j.Slf4j;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.IRIDocumentSource;
import org.semanticweb.owlapi.model.*;

import java.io.File;
import java.net.URI;
import java.text.MessageFormat;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.IRIDocumentSource;
import org.semanticweb.owlapi.io.OWLRendererException;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.MissingImportHandlingStrategy;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OntologyConfigurator;

import static cz.cvut.kbss.owldiff.cli.ChangeTypeUtils.changeTypeName;

@Slf4j
@Command(name = "diff")
public class Diff {

@Arguments(title = "File paths", description = "Path of the oldFile and newFile")
private List<String> paths;

public void run() throws OWLOntologyCreationException, OWLRendererException {
if (paths.size() != 2) {
log.error("Exactly two files must be provided, but got {0} only", paths.size());
return;
}

String oldFF = paths.get(0);
String newFF = paths.get(1);

URI oldF;
URI newF;
if (new File(oldFF).exists()) {
oldF = new File(oldFF).getAbsoluteFile().toURI();
} else {
log.error("File does not exist {}");
throw new RuntimeException();
}

if (new File(newFF).exists()) {
newF = new File(newFF).getAbsoluteFile().toURI();
} else {
log.error("File does not exist {}");
throw new RuntimeException();
}

if (oldF.isAbsolute() && newF.isAbsolute()) {
final OWLOntologyManager originalM = OWLManager
.createOWLOntologyManager();
originalM.setOntologyConfigurator(
new OntologyConfigurator()
.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT)
.setReportStackTraces(true)
.setStrict(false));
originalM.getIRIMappers().add( (ontologyIRI) -> {
if (Pattern.compile(oldFF).matcher(ontologyIRI.toString()).matches()) {
return ontologyIRI;
} else {
return IRI.create(String.format("file://%s", ontologyIRI.toString()));
}});
final OWLOntology originalO =
originalM.loadOntologyFromOntologyDocument(new IRIDocumentSource(
IRI.create(oldF)));

final OWLOntologyManager updateM = OWLManager.createOWLOntologyManager();
updateM.setOntologyConfigurator(new OntologyConfigurator().setMissingImportHandlingStrategy(
MissingImportHandlingStrategy.SILENT)
.setReportStackTraces(true)
.setStrict(false)
);
updateM.getIRIMappers().add( (ontologyIRI) -> {
if (Pattern.compile(newFF).matcher(ontologyIRI.toString()).matches()) {
return ontologyIRI;
} else {
return IRI.create(String.format("file://%s", ontologyIRI.toString()));
}});

OWLOntology updateO =
updateM.loadOntologyFromOntologyDocument(new IRIDocumentSource(IRI.create(newF)));
public void run(final String original, final String updated) throws OWLOntologyCreationException {
final OWLOntology originalO = createFreshOntology(original);
final OWLOntology updateO = createFreshOntology(updated);

SyntacticDiff d = new SyntacticDiff(new OntologyHandler() {
@Override public OWLOntology getOriginalOntology() {
return originalO;
}

@Override public OWLOntology getUpdateOntology() {
return updateO;
}
});
final SyntacticDiff d = new SyntacticDiff(new OntologyHandler() {
@Override
public OWLOntology getOriginalOntology() {
return originalO;
}

final SyntacticDiffOutput o = d.diff();
@Override
public OWLOntology getUpdateOntology() {
return updateO;
}
});

ManchesterSyntax s = new ManchesterSyntax();
o.getOWLChanges()
final SyntacticDiffOutput o = d.diff();
final ManchesterSyntax s = new ManchesterSyntax();
o.getOWLChanges()
.stream().sorted(
new ChangeComparator()
.thenComparing(SyntacticAxiomChange::getOWLChangeType))
new SyntacticChangeComparator())
.forEach(c ->
System.out.println(MessageFormat.format("{0} {1}", changeTypeName(c.getOWLChangeType()),
s.writeAxiom(c.getAxiom(), false, null, false)))
System.out.println(MessageFormat.format("{0} {1}",
changeTypeName(c.getOWLChangeType()),
s.writeAxiom(c.getAxiom(), false, null, false)))
);

} else {
log.error("Files are not absolute");
}
}

private class ChangeComparator implements Comparator<SyntacticAxiomChange> {

private MainOwlEntityResolver resolver;
private URI ensureFileExists(final String file) {
final File f = new File(file);

public ChangeComparator() {
this.resolver = new MainOwlEntityResolver();
if (!f.exists()) {
throw new IllegalArgumentException(MessageFormat.format("File \"{0}\" does not exist.", file));
} else if (!f.isAbsolute()) {
throw new IllegalArgumentException(MessageFormat.format("File \"{0}\" is not absolute.", file));
} else {
return f.getAbsoluteFile().toURI();
}
}

public int compare(SyntacticAxiomChange c1, SyntacticAxiomChange c2) {
c1.getAxiom().accept(resolver);
final IRI c1Iri = resolver.getEntity();

c2.getAxiom().accept(resolver);
final IRI c2Iri = resolver.getEntity();

if ( c1Iri != null && c1Iri.equals(c2Iri) ) {
return 0;
} else if (c1Iri != null && c2Iri != null) {
return c1Iri.compareTo(c2Iri);
} else {
return c1.getAxiom().compareTo(c2.getAxiom());
}
}
private OWLOntology createFreshOntology(final String original) throws OWLOntologyCreationException {
final OntologyConfigurator configurator = new OntologyConfigurator()
.setMissingImportHandlingStrategy(
MissingImportHandlingStrategy.SILENT)
.setReportStackTraces(true)
.setStrict(false);
final OWLOntologyManager originalM = OWLManager
.createOWLOntologyManager();
originalM.setOntologyConfigurator(configurator);
originalM.getIRIMappers().add(createIRIMapper(original));
return
originalM.loadOntologyFromOntologyDocument(new IRIDocumentSource(
IRI.create(ensureFileExists(original))));
}

private String changeTypeName(OWLChangeType ct) {
switch (ct) {
case SYNTACTIC_ORIG_REST: return "-";
case SYNTACTIC_UPD_REST: return "+";
default: throw new UnsupportedOperationException();
}
private OWLOntologyIRIMapper createIRIMapper(final String file) {
final Pattern p = Pattern.compile(file);
return (ontologyIRI) -> p.matcher(ontologyIRI.toString()).matches() ?
ontologyIRI :
IRI.create(String.format("file://%s", ontologyIRI));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cz.cvut.kbss.owldiff.cli;

import lombok.extern.slf4j.Slf4j;
import picocli.CommandLine;

import java.util.concurrent.Callable;

@Slf4j
@CommandLine.Command(name = "diff", mixinStandardHelpOptions = true,
description = "Compares OWL ontologies.")
public class DiffCommand implements Callable<Integer> {

@CommandLine.Parameters(description = "Original ontology file.")
private String original;

@CommandLine.Parameters(description = "Updated ontology file.")
private String updated;

public static void main(String[] args) {
new CommandLine(new DiffCommand()).execute(args);
}

public Integer call() {
try {
new Diff().run(original, updated);
} catch (Exception e) {
log.error("Exception during command execution: {}", e.getMessage());
log.debug("Details:", e);
return -1;
}
return 0;
}
}
33 changes: 0 additions & 33 deletions owldiff-cli/src/main/java/cz/cvut/kbss/owldiff/cli/GitDiff.java

This file was deleted.

Loading

0 comments on commit c64ff01

Please sign in to comment.