/** * 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 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>()); }
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; }
/** * Initialization consists of setting aboxOntology, if not set - defaults to a new ontology using tbox. * @throws OWLOntologyCreationException */ private void init() throws OWLOntologyCreationException { // abox -> tbox if (aboxOntology == null) { LOG.debug("Creating abox ontology. mgr = "+getOWLOntologyManager()); Optional<IRI> tBoxIRI = tboxOntology.getOntologyID().getOntologyIRI(); if (tBoxIRI.isPresent()) { IRI ontologyIRI = IRI.create(tBoxIRI.get()+"__abox"); aboxOntology = getOWLOntologyManager().getOntology(ontologyIRI); if (aboxOntology != null) { LOG.warn("Clearing existing abox ontology"); getOWLOntologyManager().removeOntology(aboxOntology); } aboxOntology = getOWLOntologyManager().createOntology(ontologyIRI); AddImport ai = new AddImport(aboxOntology, getOWLDataFactory().getOWLImportsDeclaration(tBoxIRI.get())); getOWLOntologyManager().applyChange(ai); } else { aboxOntology = getOWLOntologyManager().createOntology(); } } if (LOG.isDebugEnabled()) { LOG.debug(modelId+" manager(T) = "+tboxOntology.getOWLOntologyManager()); LOG.debug(modelId+" manager(A) = "+aboxOntology.getOWLOntologyManager()); LOG.debug(modelId+" id(T) = "+tboxOntology.getOntologyID()); LOG.debug(modelId+" id(A) = "+aboxOntology.getOntologyID()); } }
@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); }
/** * adds an imports declaration between the source ontology and extOnt * * @param extOnt */ public void addImport(OWLOntology extOnt) { Optional<IRI> ontologyIRI = extOnt.getOntologyID().getOntologyIRI(); if (ontologyIRI.isPresent()) { AddImport ai = new AddImport(getSourceOntology(), getDataFactory().getOWLImportsDeclaration(ontologyIRI.get())); getManager().applyChange(ai); } else { throw new RuntimeException("Could not add ontology as import, missing ontology ID: "+extOnt); } }
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)); }
public OWLOntology convertOntology(Map<Object,Object> m) throws OWLOntologyCreationException { Set<OWLAxiom> axioms = null; IRI ontologyIRI = null; List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>(); List<IRI> importsIRIs = new ArrayList<IRI>(); for (Object k : m.keySet()) { if (k.equals("axioms")) { axioms = convertAxioms((Object[]) m.get(k)); } else if (k.equals("iri")) { ontologyIRI = IRI.create((String) m.get(k)); } else if (k.equals("annotations")) { // TODO } else if (k.equals("imports")) { IRI importIRI = IRI.create((String) m.get(k)); importsIRIs.add(importIRI); } } OWLOntology ont = manager.createOntology(ontologyIRI); for (IRI importsIRI : importsIRIs) { AddImport ai = new AddImport(ont, manager.getOWLDataFactory().getOWLImportsDeclaration(importsIRI)); changes.add(ai); } manager.applyChanges(changes); return ont; }
@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()); }
public OWLOntology getMergedOntology() { final IRI mergedOntologyIRI = IRI.create(queryOntology.getOntologyID() .getDefaultDocumentIRI() + "-merged"); final OWLOntologyManager mm = controller.getOWLOntologyManager(); if (mm.contains(mergedOntologyIRI)) { return mm.getOntology(mergedOntologyIRI); } else { try { final OWLOntology mergedOntology = mm .createOntology(mergedOntologyIRI); mm.setOntologyFormat(mergedOntology, new RDFXMLOntologyFormat()); final String mergedOntologyFileName = mergedOntologyIRI .toURI() .toString() .substring( mergedOntologyIRI.toURI().toString() .lastIndexOf("/") + 1) + ".owl"; mm.setOntologyDocumentIRI( mergedOntology, IRI.create(controller.getRuleSpec().getOutputDir() .toURI() + "/" + mergedOntologyFileName)); mm.applyChange(new AddImport(mergedOntology, mm .getOWLDataFactory().getOWLImportsDeclaration( queryOntology.getOntologyID() .getDefaultDocumentIRI()))); return mergedOntology; } catch (OWLOntologyCreationException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
@Override public void updateOntology( OWLOntology generatedOntology, IRI generatedOntologyIRI, IRI previousOntologyIRI, URI physicalURI) { final OWLModelManager mm = getOWLModelManager(); IRI iri = generatedOntologyIRI; OWLOntology generatedOntologyToDelete = null; final List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>(); final Collection<OWLOntology> ontologies = mm.getOntologies(); for (OWLOntology oo : ontologies) { if (iri.equals(oo.getOntologyID().getOntologyIRI())) { log.info("Removing ontology " + iri); generatedOntologyToDelete = oo; } changes.add(new RemoveImport(oo, getOWLOntologyManager().getOWLDataFactory() .getOWLImportsDeclaration(iri))); } mm.applyChanges(changes); changes.clear(); if (generatedOntologyToDelete != null) { if ( !mm.removeOntology(generatedOntologyToDelete) ) { log.info("Removing ontology " + generatedOntologyToDelete.getOntologyID() + " NOT succesful."); } } changes.add(new SetOntologyID(generatedOntology, iri)); changes.add(new AddImport(generatedOntology, mm.getOWLDataFactory() .getOWLImportsDeclaration(previousOntologyIRI))); mm.applyChanges(changes); mm.setPhysicalURI(generatedOntology, physicalURI); }
@Override public void updateOntology( OWLOntology generatedOntology, IRI generatedOntologyIRI, IRI previousOntologyIRI, URI physicalURI) { IRI iri = generatedOntologyIRI; OWLOntology generatedOntologyToDelete = null; final List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>(); final Collection<OWLOntology> ontologies = m.getOntologies(); for (OWLOntology oo : ontologies) { if (iri.equals(oo.getOntologyID().getOntologyIRI())) { log.info("Removing ontology " + iri); generatedOntologyToDelete = oo; } changes.add(new RemoveImport(oo, getOWLOntologyManager() .getOWLDataFactory().getOWLImportsDeclaration(iri))); } m.applyChanges(changes); changes.clear(); if (generatedOntologyToDelete != null) { m.removeOntology(generatedOntologyToDelete); } changes.add(new SetOntologyID(generatedOntology, iri)); changes.add(new AddImport(generatedOntology, m.getOWLDataFactory() .getOWLImportsDeclaration(previousOntologyIRI))); m.applyChanges(changes); m.setOntologyDocumentIRI(generatedOntology, IRI.create(physicalURI)); try { m.saveOntology(generatedOntology); } catch (OWLOntologyStorageException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public OWLOntologyChange visit(AddImport addImport) { return new RemoveImport(addImport.getOntology(), addImport.getImportDeclaration()); }
public OWLOntologyChange visit(RemoveImport removeImport) { return new AddImport(removeImport.getOntology(), removeImport.getImportDeclaration()); }
@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)); }
@Override public void visit(AddImport change) { defaultVisit(change); }
@Override public Boolean visit(AddImport change) { Objects.requireNonNull(change); return false; }