-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI to picocli + fixing logging, comparator
- Loading branch information
Showing
8 changed files
with
250 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
owldiff-cli/src/main/java/cz/cvut/kbss/owldiff/cli/ChangeTypeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
178
owldiff-cli/src/main/java/cz/cvut/kbss/owldiff/cli/Diff.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
owldiff-cli/src/main/java/cz/cvut/kbss/owldiff/cli/DiffCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
owldiff-cli/src/main/java/cz/cvut/kbss/owldiff/cli/GitDiff.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.