Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ale-git-user committed Apr 17, 2017
2 parents 30ae620 + 9f8ceb0 commit 8328f8c
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Stream<? extends OWLAxiom>> {

Expand Down Expand Up @@ -87,14 +88,17 @@ public Stream<? extends OWLAxiom> 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
* the ontology
* @param writer
* the writer
*/
public FuncionalSyntaxRefsetObjetRenderer(OWLOntology ontology, Writer writer) {
public OWLFuncionalSyntaxRefsetObjetRenderer(OWLOntology ontology, Writer writer) {
ont = ontology;
this.writer = writer;
defaultPrefixManager = new DefaultPrefixManager();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<IRI> 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<IRI> 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<OWLAxiom> writtenAxioms = new HashSet<>();
List<OWLEntity> signature = sortOptionally(ontology.signature());
Collection<IRI> illegals = OWLDocumentFormat.determineIllegalPunnings(addMissingDeclarations, signature
Expand All @@ -251,7 +257,7 @@ private void writeSortedEntities(String bannerComment, String entityTypeName, St
List<? extends OWLEntity> sortOptionally = sortOptionally(entities);
if (!sortOptionally.isEmpty()) {
writeEntities(bannerComment, entityTypeName, sortOptionally, writtenAxioms);
writeReturn();
//writeReturn();
}
}

Expand All @@ -272,10 +278,10 @@ private void writeEntities(String comment, String entityTypeName, List<? extends
continue;
}
if (!haveWrittenBanner) {
writeln("############################");
writeln("# " + comment);
writeln("############################");
writeReturn();
//writeln("############################");
//writeln("# " + comment);
//writeln("############################");
//writeReturn();
haveWrittenBanner = true;
}
writeEntity2(owlEntity, entityTypeName, sortOptionally(axiomsForEntity), sortOptionally(list),
Expand Down Expand Up @@ -316,12 +322,12 @@ protected void writeEntity(OWLEntity entity, Set<OWLAxiom> alreadyWrittenAxioms)

protected void writeEntity2(OWLEntity entity, String entityTypeName, List<? extends OWLAxiom> axiomsForEntity,
List<OWLAnnotationAssertionAxiom> annotationAssertionAxioms, Set<OWLAxiom> 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) {
Expand Down Expand Up @@ -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);
Expand Down
46 changes: 46 additions & 0 deletions src/com/termmed/owl/OWLFunctionalSyntaxRefsetRenderer.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/com/termmed/owl/RF2OwlRefsetRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
56 changes: 56 additions & 0 deletions src/com/termmed/owl/TestRenderer.java
Original file line number Diff line number Diff line change
@@ -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.");
}

}

0 comments on commit 8328f8c

Please sign in to comment.