/** * Translate the given {@link GafDocument} into an OWL representation of the LEGO model. * * @param gaf * @return lego ontology * @throws OWLException * @throws UnknownIdentifierException */ public OWLOntology translate(GafDocument gaf) throws OWLException, UnknownIdentifierException { final OWLOntologyManager m = graph.getManager(); OWLOntology lego = m.createOntology(IRI.generateDocumentIRI()); OWLOntology sourceOntology = graph.getSourceOntology(); OWLOntologyID ontologyID = sourceOntology.getOntologyID(); if (ontologyID != null) { Optional<IRI> ontologyIRI = ontologyID.getOntologyIRI(); if (ontologyIRI.isPresent()) { OWLDataFactory f = m.getOWLDataFactory(); OWLImportsDeclaration importDeclaration = f.getOWLImportsDeclaration(ontologyIRI.get()); m.applyChange(new AddImport(lego, importDeclaration )); } } translate(gaf.getGeneAnnotations(), lego); return lego; }
public OntologyMetadata(OWLOntology ont) { super(); OWLOntologyID id = ont.getOntologyID(); if (id.getOntologyIRI().isPresent()) ontologyIRI = id.getOntologyIRI().get().toString(); if (id.getVersionIRI().isPresent()) versionIRI = id.getVersionIRI().get().toString(); importDirectives = new HashSet<String>(); for (OWLImportsDeclaration oid : ont.getImportsDeclarations()) { importDirectives.add(oid.getIRI().toString()); } classCount = ont.getClassesInSignature().size(); namedIndividualCount = ont.getIndividualsInSignature().size(); axiomCount = ont.getAxiomCount(); annotations = new HashSet<OntologyAnnotation>(); for (OWLAnnotation ann : ont.getAnnotations()) { annotations.add(new OntologyAnnotation(ann)); } }
public void addImportsFromSupportOntologies() { OWLOntology sourceOntology = getSourceOntology(); OWLDataFactory factory = getDataFactory(); for (OWLOntology o : getSupportOntologySet()) { Optional<IRI> ontologyIRI = o.getOntologyID().getOntologyIRI(); if (ontologyIRI.isPresent()) { OWLImportsDeclaration importsDeclaration = factory.getOWLImportsDeclaration(ontologyIRI.get()); AddImport ai = new AddImport(sourceOntology, importsDeclaration); LOG.info("Applying: "+ai); getManager().applyChange(ai); } else { String msg = "Could not add import due to missing ontology id: "+o; LOG.error(msg); throw new RuntimeException(msg); } } this.setSupportOntologySet(new HashSet<OWLOntology>()); }
public void mergeImportClosure(boolean isRemovedImportsDeclarations) throws OWLOntologyCreationException { OWLOntologyManager manager = getManager(); Set<OWLOntology> imports = sourceOntology.getImportsClosure(); for (OWLOntology o : imports) { if (o.equals(sourceOntology)) continue; String comment = "Includes "+summarizeOntology(o); LOG.info(comment); addCommentToOntology(sourceOntology, comment); manager.addAxioms(sourceOntology, o.getAxioms()); } Set<OWLImportsDeclaration> oids = sourceOntology.getImportsDeclarations(); for (OWLImportsDeclaration oid : oids) { RemoveImport ri = new RemoveImport(sourceOntology, oid); getManager().applyChange(ri); } }
/** * Merge a specific ontology from the import closure into the main ontology. * Removes the import statement. * * @param ontologyIRI id of the ontology to merge * @throws OWLOntologyCreationException */ public void mergeSpecificImport(IRI ontologyIRI) throws OWLOntologyCreationException { OWLOntologyManager manager = getManager(); Set<OWLOntology> imports = sourceOntology.getImportsClosure(); for (OWLOntology o : imports) { if (o.equals(sourceOntology)) continue; Optional<IRI> currentIRI = o.getOntologyID().getOntologyIRI(); if (currentIRI.isPresent() && currentIRI.get().equals(ontologyIRI)) { String comment = "Includes "+summarizeOntology(o); LOG.info(comment); addCommentToOntology(sourceOntology, comment); manager.addAxioms(sourceOntology, o.getAxioms()); } } Set<OWLImportsDeclaration> oids = sourceOntology.getImportsDeclarations(); for (OWLImportsDeclaration oid : oids) { if (ontologyIRI.equals(oid.getIRI())) { RemoveImport ri = new RemoveImport(sourceOntology, oid); getManager().applyChange(ri); } } }
private static List<OWLOntologyChange> handleSupportOntologies(OWLGraphWrapper graph) { OWLOntology ontology = graph.getSourceOntology(); OWLOntologyManager manager = ontology.getOWLOntologyManager(); OWLDataFactory factory = manager.getOWLDataFactory(); List<OWLOntologyChange> removeImportChanges = new ArrayList<OWLOntologyChange>(); Set<OWLOntology> supportOntologySet = graph.getSupportOntologySet(); for (OWLOntology support : supportOntologySet) { Optional<IRI> supportIRI = support.getOntologyID().getOntologyIRI(); if(supportIRI.isPresent()) { IRI ontologyIRI = supportIRI.get(); OWLImportsDeclaration importDeclaration = factory.getOWLImportsDeclaration(ontologyIRI); ChangeApplied status = manager.applyChange(new AddImport(ontology, importDeclaration)); if (ChangeApplied.SUCCESSFULLY == status) { // the change was successful, create remove import for later removeImportChanges.add(new RemoveImport(ontology, importDeclaration)); } } } return removeImportChanges; }
void addOntologyStructure(OWLOntologyManager manager, OWLOntology ontology) { long parent = graph.createNode(OwlApiUtils.getIri(ontology)); graph.addLabel(parent, OwlLabels.OWL_ONTOLOGY); for (OWLImportsDeclaration importDeclaration : ontology.getImportsDeclarations()) { OWLOntology childOnt = manager.getImportedOntology(importDeclaration); if (null == childOnt) { // TODO: Why is childOnt sometimes null (when importing rdf)? continue; } long child = graph.createNode(OwlApiUtils.getIri(childOnt)); graph.addLabel(parent, OwlLabels.OWL_ONTOLOGY); if (graph.getRelationship(child, parent, OwlRelationships.RDFS_IS_DEFINED_BY).isPresent()) { continue; } graph.createRelationship(child, parent, OwlRelationships.RDFS_IS_DEFINED_BY); addOntologyStructure(manager, childOnt); } }
@Test public void testAnnotations() throws Exception { // setup test model/ontology OWLOntology o = m.createOntology(); OWLImportsDeclaration importDeclaration = f.getOWLImportsDeclaration(g.getSourceOntology().getOntologyID().getOntologyIRI().get()); m.applyChange(new AddImport(o, importDeclaration)); final IRI i1IRI = IRI.generateDocumentIRI(); final OWLNamedIndividual ni1 = f.getOWLNamedIndividual(i1IRI); // declare individual m.addAxiom(o, f.getOWLDeclarationAxiom(ni1)); // add annotations m.addAxiom(o, f.getOWLAnnotationAssertionAxiom(i1IRI, f.getOWLAnnotation(f.getOWLAnnotationProperty( AnnotationShorthand.comment.getAnnotationProperty()), f.getOWLLiteral("Comment 1")))); m.addAxiom(o, f.getOWLAnnotationAssertionAxiom(i1IRI, f.getOWLAnnotation(f.getOWLAnnotationProperty( AnnotationShorthand.comment.getAnnotationProperty()), f.getOWLLiteral("Comment 2")))); // declare type m.addAxiom(o, f.getOWLClassAssertionAxiom(g.getOWLClassByIdentifier("GO:0000003"), ni1)); MolecularModelJsonRenderer r = new MolecularModelJsonRenderer(null, o, null, curieHandler); JsonOwlIndividual jsonOwlIndividualOriginal = r.renderObject(ni1); assertEquals(2, jsonOwlIndividualOriginal.annotations.length); String json = MolecularModelJsonRenderer.renderToJson(jsonOwlIndividualOriginal, true); JsonOwlIndividual jsonOwlIndividualParse = MolecularModelJsonRenderer.parseFromJson(json, JsonOwlIndividual.class); assertNotNull(jsonOwlIndividualParse); assertEquals(jsonOwlIndividualOriginal, jsonOwlIndividualParse); }
private void testSimpleClassExpression(OWLClassExpression ce, String expectedJsonType) throws Exception { // setup test model/ontology OWLOntology o = m.createOntology(); OWLImportsDeclaration importDeclaration = f.getOWLImportsDeclaration(g.getSourceOntology().getOntologyID().getOntologyIRI().get()); m.applyChange(new AddImport(o, importDeclaration)); // create indivdual with a ce type final IRI i1IRI = IRI.generateDocumentIRI(); final OWLNamedIndividual ni1 = f.getOWLNamedIndividual(i1IRI); // declare individual m.addAxiom(o, f.getOWLDeclarationAxiom(ni1)); // declare type m.addAxiom(o, f.getOWLClassAssertionAxiom(ce, ni1)); MolecularModelJsonRenderer r = new MolecularModelJsonRenderer(null, o, null, curieHandler); JsonOwlIndividual jsonOwlIndividualOriginal = r.renderObject(ni1); String json = MolecularModelJsonRenderer.renderToJson(jsonOwlIndividualOriginal, true); assertTrue(json, json.contains("\"type\": \""+expectedJsonType+"\"")); JsonOwlIndividual jsonOwlIndividualParse = MolecularModelJsonRenderer.parseFromJson(json, JsonOwlIndividual.class); assertNotNull(jsonOwlIndividualParse); assertEquals(jsonOwlIndividualOriginal, jsonOwlIndividualParse); Set<OWLClassExpression> ces = TestJsonOwlObjectParser.parse(new OWLGraphWrapper(o), jsonOwlIndividualParse.type); assertEquals(1, ces.size()); assertEquals(ce, ces.iterator().next()); }
@Deprecated public void addImport(String importedIRIString) { OWLImportsDeclaration iax = dataFactory.getOWLImportsDeclaration(IRI.create(importedIRIString)); //AddImport addAx = new AddImport(ontology, iax); AddImport addAx = new AddImport(getOntology(), iax); manager.applyChange(addAx); }
public void mergeOntology(OWLOntology extOnt, LabelPolicy labelPolicy) throws OWLOntologyCreationException { OWLOntologyManager manager = getManager(); LOG.info("Merging "+extOnt+" policy: "+labelPolicy); for (OWLAxiom axiom : extOnt.getAxioms()) { if (labelPolicy != LabelPolicy.ALLOW_DUPLICATES) { if (axiom instanceof OWLAnnotationAssertionAxiom) { OWLAnnotationAssertionAxiom aa = (OWLAnnotationAssertionAxiom)axiom; if (aa.getProperty().isLabel()) { OWLAnnotationSubject subj = aa.getSubject(); if (subj instanceof IRI) { Optional<OWLLiteral> label = null; for (OWLAnnotationAssertionAxiom a1 : sourceOntology.getAnnotationAssertionAxioms(subj)) { if (a1.getProperty().isLabel()) { label = a1.getValue().asLiteral(); } } if (label != null && label.isPresent()) { if (labelPolicy == LabelPolicy.PRESERVE_SOURCE) { LOG.info("Preserving existing label:" +subj+" "+label+" // ditching: "+axiom); continue; } if (labelPolicy == LabelPolicy.PRESERVE_EXT) { LOG.info("Replacing:" +subj+" "+label+" with: "+axiom); LOG.error("NOT IMPLEMENTED"); } } } } } } manager.applyChange(new AddAxiom(sourceOntology, axiom)); } for (OWLImportsDeclaration oid: extOnt.getImportsDeclarations()) { manager.applyChange(new AddImport(sourceOntology, oid)); } addCommentToOntology(sourceOntology, "Includes "+summarizeOntology(extOnt)); }
private Object[] convertSet(Set objs) { Object[] arr = new Object[objs.size()]; int i=0; for (Object obj : objs) { if (obj instanceof OWLAxiom) arr[i] = convert((OWLAxiom) obj); else if (obj instanceof OWLImportsDeclaration) arr[i] = convert((OWLImportsDeclaration) obj); else arr[i] = convert((OWLObject) obj); i++; } return arr; }
@Test public void test() throws Exception { // load the base ontology ParserWrapper pw = new ParserWrapper(); OWLOntology direct = pw.parseOBO(getResourceIRIString("graph/xref_test.obo")); OWLGraphWrapper directGraph = new OWLGraphWrapper(direct); // check that the test class has the expected number of xrefs OWLClass c = directGraph.getOWLClassByIdentifier("FOO:0001"); List<String> directDefXrefs = directGraph.getDefXref(c); assertEquals(2, directDefXrefs.size()); List<String> directXrefs = directGraph.getXref(c); assertEquals(2, directXrefs.size()); // create an ontology using an import OWLOntologyManager manager = pw.getManager(); OWLDataFactory factory = manager.getOWLDataFactory(); OWLOntology importer = manager.createOntology(); OWLImportsDeclaration importDeclaration = factory.getOWLImportsDeclaration(direct.getOntologyID().getOntologyIRI().orNull()); manager.applyChange(new AddImport(importer, importDeclaration)); OWLGraphWrapper importerGraph = new OWLGraphWrapper(importer); // check that the wrapper uses also imports for lookups of xrefs List<String> importedDefXrefs = importerGraph.getDefXref(c); assertEquals(2, importedDefXrefs.size()); List<String> importedXrefs = importerGraph.getXref(c); assertEquals(2, importedXrefs.size()); }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof OWLImportsDeclaration)) { return false; } OWLImportsDeclaration other = (OWLImportsDeclaration) obj; return iri.equals(other.getIRI()); }
public BinaryOWLImportsDeclarationSet(BinaryOWLInputStream inputStream) throws IOException, BinaryOWLParseException { int size = inputStream.readInt(); if(size == 0) { importsDeclarations = Collections.emptySet(); } else { Set<OWLImportsDeclaration> read = new LinkedHashSet<OWLImportsDeclaration>(size); for(int i = 0; i < size; i++) { BinaryOWLImportsDeclaration binDecl = new BinaryOWLImportsDeclaration(inputStream); read.add(binDecl.getImportsDeclaration()); } importsDeclarations = Collections.unmodifiableSet(read); } }
public void write(BinaryOWLOutputStream dataOutput) throws IOException { dataOutput.writeInt(importsDeclarations.size()); for(OWLImportsDeclaration declaration : importsDeclarations) { BinaryOWLImportsDeclaration binDecl = new BinaryOWLImportsDeclaration(declaration); binDecl.write(dataOutput); } }
@Test public void testUpdateImports() throws Exception { final OWLOntologyManager m = OWLManager.createOWLOntologyManager(); final OWLDataFactory f = m.getOWLDataFactory(); // setup other import final IRI other = IRI.generateDocumentIRI(); m.createOntology(other); // setup additional final IRI add1 = IRI.generateDocumentIRI(); m.createOntology(add1); final IRI add2 = IRI.generateDocumentIRI(); m.createOntology(add2); final Set<IRI> additional = new HashSet<IRI>(); additional.add(add1); additional.add(add2); // setup tbox final IRI tboxIRI = IRI.generateDocumentIRI(); m.createOntology(tboxIRI); // setup abox final OWLOntology abox = m.createOntology(IRI.generateDocumentIRI()); // add initial imports to abox m.applyChange(new AddImport(abox, f.getOWLImportsDeclaration(other))); // update imports CoreMolecularModelManager.updateImports(abox, tboxIRI, additional); // check the resulting imports Set<OWLImportsDeclaration> declarations = abox.getImportsDeclarations(); assertEquals(4, declarations.size()); Set<IRI> declaredImports = new HashSet<IRI>(); for (OWLImportsDeclaration importsDeclaration : declarations) { declaredImports.add(importsDeclaration.getIRI()); } assertEquals(4, declaredImports.size()); assertTrue(declaredImports.contains(tboxIRI)); assertTrue(declaredImports.contains(add1)); assertTrue(declaredImports.contains(add1)); assertTrue(declaredImports.contains(other)); }
public void visit(OWLImportsDeclaration axiom) { }
public Boolean visit(OWLImportsDeclaration axiom) { return Boolean.TRUE; }
/** * Testing correctness of the reasoner with respect to ontology changes * <p> * removing the import declaration for </impA> */ @Test public void testRemovingImpA() throws Exception { OWLOntologyManager man = TestOWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = man.getOWLDataFactory(); // set up resolution of prefixes PrefixManager pm = new DefaultPrefixManager(); pm.setDefaultPrefix("http://www.example.com/main#"); pm.setPrefix("A:", "http://www.example.com/A#"); pm.setPrefix("B:", "http://www.example.com/B#"); // define query classes OWLClass mainX = dataFactory.getOWLClass(":X", pm); OWLClass mainY = dataFactory.getOWLClass(":Y", pm); OWLClass extA = dataFactory.getOWLClass("A:A", pm); OWLClass extB = dataFactory.getOWLClass("B:B", pm); OWLClass extC = dataFactory.getOWLClass("B:C", pm); // loading the root ontology OWLOntology root = loadOntology(man, "root.owl"); // Create an ELK reasoner. OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(root); try { // ************************************ // ** removing the import declaration for </impA> // ************************************ OWLImportsDeclaration importA = new OWLImportsDeclarationImpl( IRI.create("http://www.example.com#impA")); OWLOntologyChange change = new RemoveImport(root, importA); man.applyChange(change); reasoner.flush(); // Now the root ontology should not import anything assertEquals(root.getAxiomCount(), 3); assertEquals(root.getImportsClosure().size(), 1); assertEquals(getAxioms(root).size(), 3); // reasoner queries -- only subsumptions of the root ontology are // there assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( mainY)); assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( extA)); assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity( extB)); assertFalse(reasoner.getSuperClasses(extA, true).containsEntity( extB)); assertFalse(reasoner.getSuperClasses(extB, true).containsEntity( extC)); } finally { reasoner.dispose(); } }
public Object convert(OWLImportsDeclaration obj) { return convert(((OWLImportsDeclaration)obj).getIRI()); }
@Override public int compareTo(OWLImportsDeclaration o) { return iri.compareTo(o.getIRI()); }
/** * Remove all imports from the given ontology * @param ont OWL ontology */ private void removeImports(OWLOntology ont) { for(OWLImportsDeclaration importDecl : ont.getImportsDeclarations()) ont.getOWLOntologyManager().applyChange(new RemoveImport(ont.getOWLOntologyManager().getImportedOntology(importDecl), importDecl)); }
@Override public void handleImportsDeclarations(Set<OWLImportsDeclaration> importsDeclarations) throws E { }
public <E extends Throwable> void read(DataInputStream dis, BinaryOWLOntologyDocumentHandler<E> handler, OWLDataFactory df) throws IOException, BinaryOWLParseException, UnloadableImportException, E { BinaryOWLInputStream inputStream = new BinaryOWLInputStream(dis, df, VERSION); // Metadata BinaryOWLMetadataChunk chunk = new BinaryOWLMetadataChunk(inputStream); BinaryOWLMetadata metadata = chunk.getMetadata(); handler.handleDocumentMetaData(metadata); handler.handleBeginInitialDocumentBlock(); // Ontology ID BinaryOWLOntologyID serializer = new BinaryOWLOntologyID(inputStream); OWLOntologyID ontologyID = serializer.getOntologyID(); handler.handleOntologyID(ontologyID); // Imported ontologies BinaryOWLImportsDeclarationSet importsDeclarationSet = new BinaryOWLImportsDeclarationSet(inputStream); Set<OWLImportsDeclaration> importsDeclarations = importsDeclarationSet.getImportsDeclarations(); handler.handleImportsDeclarations(importsDeclarations); // IRI Table IRILookupTable iriLookupTable = new IRILookupTable(dis); // Used to be literal table // Skip 1 byte - the interning marker inputStream.skip(1); LookupTable lookupTable = new LookupTable(iriLookupTable); BinaryOWLInputStream lookupTableStream = new BinaryOWLInputStream(dis, lookupTable, df, VERSION); // Ontology Annotations Set<OWLAnnotation> annotations = lookupTableStream.readOWLObjects(); handler.handleOntologyAnnotations(annotations); // Axiom tables - axioms by type for (int i = 0; i < AxiomType.AXIOM_TYPES.size(); i++) { Set<OWLAxiom> axiomsOfType = lookupTableStream.readOWLObjects(); handler.handleAxioms(axiomsOfType); } handler.handleEndInitialDocumentBlock(); handler.handleBeginDocumentChangesBlock(); BinaryOWLInputStream changesInputStream = new BinaryOWLInputStream(dis, df, VERSION); // Read any changes that have been appended to the end of the file - no look up table for this readOntologyChanges(changesInputStream, handler); handler.handleEndDocumentChangesBlock(); handler.handleEndDocument(); }
@Override public RemoveImportData read(BinaryOWLInputStream inputStream) throws IOException { IRI iri = inputStream.readIRI(); OWLImportsDeclaration declaration = inputStream.getDataFactory().getOWLImportsDeclaration(iri); return new RemoveImportData(declaration); }
@Override public void write(AddImportData changeData, BinaryOWLOutputStream outputStream) throws IOException { OWLImportsDeclaration declaration = changeData.getDeclaration(); IRI iri = declaration.getIRI(); outputStream.writeIRI(iri); }
@Override public AddImportData read(BinaryOWLInputStream inputStream) throws IOException, BinaryOWLParseException { IRI iri = inputStream.readIRI(); OWLImportsDeclaration declaration = inputStream.getDataFactory().getOWLImportsDeclaration(iri); return new AddImportData(declaration); }
public BinaryOWLImportsDeclaration(OWLImportsDeclaration importsDeclaration) { this.importsDeclaration = importsDeclaration; }
public OWLImportsDeclaration getImportsDeclaration() { return importsDeclaration; }
public BinaryOWLImportsDeclarationSet(Set<OWLImportsDeclaration> importsDeclarations) { this.importsDeclarations = Collections.unmodifiableSet(new LinkedHashSet<OWLImportsDeclaration>(importsDeclarations)); }
public Set<OWLImportsDeclaration> getImportsDeclarations() { return importsDeclarations; }
void handleImportsDeclarations(Set<OWLImportsDeclaration> importsDeclarations) throws E;
Set<OWLImportsDeclaration> getImportsDeclarations();