diff --git a/src/com/termmed/owl/FuncionalSyntaxRefsetObjetRenderer.java b/src/com/termmed/owl/OWLFuncionalSyntaxRefsetObjetRenderer.java similarity index 90% rename from src/com/termmed/owl/FuncionalSyntaxRefsetObjetRenderer.java rename to src/com/termmed/owl/OWLFuncionalSyntaxRefsetObjetRenderer.java index 1c4bf6e..7c60775 100644 --- a/src/com/termmed/owl/FuncionalSyntaxRefsetObjetRenderer.java +++ b/src/com/termmed/owl/OWLFuncionalSyntaxRefsetObjetRenderer.java @@ -27,6 +27,7 @@ import org.semanticweb.owlapi.util.DefaultPrefixManager; import org.semanticweb.owlapi.util.EscapeUtils; import org.semanticweb.owlapi.vocab.OWLXMLVocabulary; +import uk.ac.manchester.cs.owl.owlapi.OWLEquivalentClassesAxiomImpl; import javax.annotation.Nonnull; import java.io.IOException; @@ -44,7 +45,7 @@ /** * Created by alo on 4/17/17. */ -public class FuncionalSyntaxRefsetObjetRenderer implements OWLObjectVisitor { +public class OWLFuncionalSyntaxRefsetObjetRenderer implements OWLObjectVisitor { class AxiomRetriever implements OWLEntityVisitorEx> { @@ -87,6 +88,9 @@ public Stream visit(OWLAnnotationProperty property) { private boolean writeEntitiesAsURIs = true; private boolean addMissingDeclarations = true; protected AnnotationValueShortFormProvider labelMaker = null; + private String rootSctid = "138875005"; + private String moduleId = "900000000000207008"; + private String refsetId = "733073007"; /** * @param ontology @@ -94,7 +98,7 @@ public Stream visit(OWLAnnotationProperty property) { * @param writer * the writer */ - public FuncionalSyntaxRefsetObjetRenderer(OWLOntology ontology, Writer writer) { + public OWLFuncionalSyntaxRefsetObjetRenderer(OWLOntology ontology, Writer writer) { ont = ontology; this.writer = writer; defaultPrefixManager = new DefaultPrefixManager(); @@ -147,6 +151,7 @@ public void setPrefixManager(PrefixManager prefixManager) { } protected void writePrefix(String prefix, String namespace) { + writeRefsetColumns(refsetId, rootSctid, moduleId); write("Prefix"); writeOpenBracket(); write(prefix); @@ -202,29 +207,30 @@ private void writeFullIRI(IRI iri) { @Override public void visit(OWLOntology ontology) { + writeRefsetHeader(); writePrefixes(); - writeReturn(); - writeReturn(); - write(ONTOLOGY); - writeOpenBracket(); - if (!ontology.isAnonymous()) { - writeFullIRI(ontology.getOntologyID().getOntologyIRI().get()); - Optional versionIRI = ontology.getOntologyID().getVersionIRI(); - if (versionIRI.isPresent()) { - writeReturn(); - writeFullIRI(versionIRI.get()); - } - writeReturn(); - } - ontology.importsDeclarations().forEach(decl -> { - write(IMPORT); - writeOpenBracket(); - writeFullIRI(decl.getIRI()); - writeCloseBracket(); - writeReturn(); - }); + //writeReturn(); + //writeReturn(); + //write(ONTOLOGY); + //writeOpenBracket(); +// if (!ontology.isAnonymous()) { +// writeFullIRI(ontology.getOntologyID().getOntologyIRI().get()); +// Optional versionIRI = ontology.getOntologyID().getVersionIRI(); +// if (versionIRI.isPresent()) { +// writeReturn(); +// writeFullIRI(versionIRI.get()); +// } +// writeReturn(); +// } +// ontology.importsDeclarations().forEach(decl -> { +// write(IMPORT); +// writeOpenBracket(); +// writeFullIRI(decl.getIRI()); +// writeCloseBracket(); +// writeReturn(); +// }); sortOptionally(ontology.annotations()).forEach(this::acceptAndReturn); - writeReturn(); +// writeReturn(); Set writtenAxioms = new HashSet<>(); List signature = sortOptionally(ontology.signature()); Collection illegals = OWLDocumentFormat.determineIllegalPunnings(addMissingDeclarations, signature @@ -251,7 +257,7 @@ private void writeSortedEntities(String bannerComment, String entityTypeName, St List sortOptionally = sortOptionally(entities); if (!sortOptionally.isEmpty()) { writeEntities(bannerComment, entityTypeName, sortOptionally, writtenAxioms); - writeReturn(); + //writeReturn(); } } @@ -272,10 +278,10 @@ private void writeEntities(String comment, String entityTypeName, List alreadyWrittenAxioms) protected void writeEntity2(OWLEntity entity, String entityTypeName, List axiomsForEntity, List annotationAssertionAxioms, Set alreadyWrittenAxioms) { - writeln("# " + entityTypeName + ": " + getIRIString(entity) + " (" + getEntityLabel(entity) + ")"); - writeReturn(); + //writeln("# " + entityTypeName + ": " + getIRIString(entity) + " (" + getEntityLabel(entity) + ")"); + //writeReturn(); annotationAssertionAxioms.stream().filter(alreadyWrittenAxioms::add).forEach(this::acceptAndReturn); axiomsForEntity.stream().filter(this::shouldWrite).filter(alreadyWrittenAxioms::add).forEach( this::acceptAndReturn); - writeReturn(); + //writeReturn(); } private boolean shouldWrite(OWLAxiom ax) { @@ -466,11 +472,63 @@ protected void writeReturn() { write("\n"); } + protected void writeRefsetColumns(String refsetId, String referencedComponentId, String moduleId) { + String row = UUID.randomUUID() + "\t" + + "20170731" + "\t1\t" + moduleId + "\t" + + refsetId + "\t" + referencedComponentId + "\t"; + write(row); + } + protected void writeAnnotations(OWLAxiom ax) { ax.annotations().forEach(this::acceptAndSpace); } + protected void writeRefsetHeader() { + String row = "UUID\teffectiveTime\tactive\tmoduleId\trefsetId\treferencedComponentId\tannotation"; + write(row); + writeReturn(); + } + protected void writeAxiomStart(OWLXMLVocabulary v, OWLAxiom axiom) { + if (axiom.isOfType(AxiomType.DECLARATION)) { + OWLDeclarationAxiom da = (OWLDeclarationAxiom) axiom; + String referencedComponentId = rootSctid; + try { + referencedComponentId = new Scanner(da.getEntity().toStringID()).useDelimiter("\\D+").next(); + } catch (Exception e) { + // No id in the identity + } + writeRefsetColumns(refsetId, referencedComponentId, moduleId); + } else if (axiom.isOfType(AxiomType.ANNOTATION_ASSERTION)) { + OWLAnnotationAssertionAxiom da = (OWLAnnotationAssertionAxiom) axiom; + String referencedComponentId = rootSctid; + try { + referencedComponentId = new Scanner(da.getSubject().asIRI().toString()).useDelimiter("\\D+").next(); + } catch (Exception e) { + // No id in the identity + } + writeRefsetColumns(refsetId, referencedComponentId, moduleId); + } else if (axiom.isOfType(AxiomType.EQUIVALENT_CLASSES)){ + OWLEquivalentClassesAxiom da = (OWLEquivalentClassesAxiom) axiom; + String referencedComponentId = rootSctid; + try { + referencedComponentId = new Scanner(da.namedClasses().findFirst().toString()).useDelimiter("\\D+").next(); + } catch (Exception e) { + // No id in the identity + } + writeRefsetColumns(refsetId, referencedComponentId, moduleId); + } else if (axiom.isOfType(AxiomType.SUBCLASS_OF)){ + OWLSubClassOfAxiom da = (OWLSubClassOfAxiom) axiom; + String referencedComponentId = rootSctid; + try { + referencedComponentId = new Scanner(da.getSubClass().toString()).useDelimiter("\\D+").next(); + } catch (Exception e) { + // No id in the identity + } + writeRefsetColumns(refsetId, referencedComponentId, moduleId); + } else { + writeRefsetColumns(refsetId, rootSctid, moduleId); + } write(v); writeOpenBracket(); writeAnnotations(axiom); diff --git a/src/com/termmed/owl/OWLFunctionalSyntaxRefsetRenderer.java b/src/com/termmed/owl/OWLFunctionalSyntaxRefsetRenderer.java new file mode 100644 index 0000000..d61a30b --- /dev/null +++ b/src/com/termmed/owl/OWLFunctionalSyntaxRefsetRenderer.java @@ -0,0 +1,46 @@ +/* + * + * * Copyright (C) 2014 termMed IT + * * www.termmed.com + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +package com.termmed.owl; + +import org.semanticweb.owlapi.functional.renderer.FunctionalSyntaxObjectRenderer; +import org.semanticweb.owlapi.io.AbstractOWLRenderer; +import org.semanticweb.owlapi.io.OWLRendererException; +import org.semanticweb.owlapi.io.OWLRendererIOException; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLRuntimeException; + +import java.io.PrintWriter; + +/** + * Created by alo on 4/17/17. + */ +public class OWLFunctionalSyntaxRefsetRenderer extends AbstractOWLRenderer { + @Override + public void render(OWLOntology ontology, PrintWriter writer) throws OWLRendererException { + try { + OWLFuncionalSyntaxRefsetObjetRenderer ren = new OWLFuncionalSyntaxRefsetObjetRenderer(ontology, + writer); + ontology.accept(ren); + writer.flush(); + } catch (OWLRuntimeException e) { + throw new OWLRendererIOException(e); + } + } +} diff --git a/src/com/termmed/owl/RF2OwlRefsetRenderer.java b/src/com/termmed/owl/RF2OwlRefsetRenderer.java index 79acc08..3ba41a0 100644 --- a/src/com/termmed/owl/RF2OwlRefsetRenderer.java +++ b/src/com/termmed/owl/RF2OwlRefsetRenderer.java @@ -98,7 +98,7 @@ private void renderAxiom(OWLAxiom axiom, Writer writer) throws OWLOntologyCreati refsetId = "733073007"; } String referencedComponentId = new Scanner(body).useDelimiter("\\D+").next(); - String moduleId = "900000000000207008"; + String moduleId = "733073007"; String row = UUID.randomUUID() + "\t" + "20170731" + "\t1\t" + moduleId + "\t" + refsetId + "\t" + referencedComponentId + "\t" + diff --git a/src/com/termmed/owl/TestRenderer.java b/src/com/termmed/owl/TestRenderer.java new file mode 100644 index 0000000..9482532 --- /dev/null +++ b/src/com/termmed/owl/TestRenderer.java @@ -0,0 +1,56 @@ +/* + * + * * Copyright (C) 2014 termMed IT + * * www.termmed.com + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +package com.termmed.owl; + +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer; +import org.semanticweb.owlapi.functional.renderer.OWLFunctionalSyntaxRenderer; +import org.semanticweb.owlapi.io.OWLObjectRenderer; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyManager; + +import java.io.File; +import java.io.PrintWriter; + +/** + * Created by alo on 4/17/17. + */ +public class TestRenderer { + + private static long startTime = 0; + private static long stopTime = 0; + private static OWLFunctionalSyntaxRefsetRenderer fr = new OWLFunctionalSyntaxRefsetRenderer(); + + public static void main(String[] args) throws Exception { + startTime = System.currentTimeMillis(); + OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); +// File testOntology = new File("/Users/alo/Downloads/termspace-owl (39).xml"); + File testOntology = new File("/Users/alo/Downloads/conceptsOwlComplete-cd-alo.xml"); + System.out.println("testOntology: " + testOntology.getName()); + OWLOntology ontology = manager.loadOntologyFromOntologyDocument(testOntology); + System.out.println("Terminology loaded in: " + (System.currentTimeMillis() - startTime) + " ms."); + startTime = System.currentTimeMillis(); + PrintWriter writer2 = new PrintWriter("owlRefset-complete.txt", "UTF-8"); + fr.render(ontology,writer2); + writer2.close(); + System.out.println("OWL Refset created in: " + (System.currentTimeMillis() - startTime) + " ms."); + } + +}