Java Code Examples for org.semanticweb.owlapi.model.OWLOntology#getAnnotations()

The following examples show how to use org.semanticweb.owlapi.model.OWLOntology#getAnnotations() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnmergeOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Given a list of ontologies and a target ontology, remove all the axioms from the listed
 * ontologies and their import closure from the target ontology.
 *
 * @param ontologies the list of ontologies to unmerge
 * @param targetOntology the ontology to remove axioms from
 * @param includeAnnotations true if ontology annotations should be remove
 */
public static void unmergeFrom(
    List<OWLOntology> ontologies, OWLOntology targetOntology, boolean includeAnnotations) {
  for (OWLOntology ontology : ontologies) {
    logger.info("Removing axioms from: " + ontology);
    targetOntology.getOWLOntologyManager().removeAxioms(targetOntology, ontology.getAxioms());
    if (includeAnnotations) {
      for (OWLAnnotation annotation : ontology.getAnnotations()) {
        RemoveOntologyAnnotation remove =
            new RemoveOntologyAnnotation(targetOntology, annotation);
        targetOntology.getOWLOntologyManager().applyChange(remove);
      }
    }
    for (OWLOntology imported : ontology.getImportsClosure()) {
      targetOntology.getOWLOntologyManager().removeAxioms(targetOntology, imported.getAxioms());
    }
  }
}
 
Example 2
Source File: OntologyMetadata.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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));
	}
}
 
Example 3
Source File: Mooncat.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void removeDirectives() {
	// TODO decide: move the set of directives into a constant/static collection?
	
	Set<IRI> directivesIRIs = new HashSet<IRI>();
	directivesIRIs.add(Obo2OWLVocabulary.IRI_OIO_LogicalDefinitionViewRelation.getIRI());
	directivesIRIs.add(Obo2OWLVocabulary.IRI_OIO_treatXrefsAsEquivalent.getIRI());
	directivesIRIs.add(Obo2OWLVocabulary.IRI_OIO_treatXrefsAsGenusDifferentia.getIRI());
	directivesIRIs.add(Obo2OWLVocabulary.IRI_OIO_treatXrefsAsHasSubClass.getIRI());
	directivesIRIs.add(Obo2OWLVocabulary.IRI_OIO_treatXrefsAsIsA.getIRI());
	directivesIRIs.add(Obo2OWLVocabulary.IRI_OIO_treatXrefsAsRelationship.getIRI());
	directivesIRIs.add(Obo2OWLVocabulary.IRI_OIO_treatXrefsAsReverseGenusDifferentia.getIRI());
	
	OWLOntology o = graph.getSourceOntology();
	for(OWLAnnotation ann : o.getAnnotations()) {
		final OWLAnnotationProperty property = ann.getProperty();
		if (directivesIRIs.contains(property.getIRI())) {
			manager.applyChange(new RemoveOntologyAnnotation(o, ann));
		}
	}
}
 
Example 4
Source File: OWLGraphWrapperExtended.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String getOntologyAnnotationValue(OWLOntology o, OboFormatTag tag) {
	IRI dateTagIRI = Obo2Owl.trTagToIRI(tag.getTag());
	Set<OWLAnnotation> annotations = o.getAnnotations();
	for (OWLAnnotation annotation : annotations) {
		OWLAnnotationProperty property = annotation.getProperty();
		if(dateTagIRI.equals(property.getIRI())) {
			OWLAnnotationValue value = annotation.getValue();
			if (value != null) {
				if (value instanceof IRI) {
					return ((IRI) value).toString();
				}
				else if (value instanceof OWLLiteral) {
					return ((OWLLiteral) value).getLiteral();
				}
			}
		}
	}
	return null;
}
 
Example 5
Source File: SolrCommandRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Experimental method for trying out the loading of complex_annotation doc type
 * 
 * @param opts
 * @throws Exception
 */
@CLIMethod("--solr-load-complex-annotations")
public void experimentalLoadComplexAnnotationSolr(Opts opts) throws Exception {

	// Check to see if the global url has been set.
	String url = sortOutSolrURL(globalSolrURL);				

	// Only proceed if our environment was well-defined.
	if( legoCatalogs == null || legoFiles == null || legoCatalogs.isEmpty() || legoFiles.isEmpty() ){
		LOG.warn("Lego environment not well defined--skipping.");
	}else{

		// Ready the environment for every pass.
		ParserWrapper pw = new ParserWrapper();
		// Add all of the catalogs.
		for( File legoCatalog : legoCatalogs ){
			pw.addIRIMapper(new CatalogXmlIRIMapper(legoCatalog));				
		}
		OWLOntologyManager manager = pw.getManager();
		OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();

		// Actual loading--iterate over our list and load individually.
		for( File legoFile : legoFiles ){
			String fname = legoFile.getName();
			OWLReasoner currentReasoner = null;
			OWLOntology ontology = null;

			// TODO: Temp cover for missing group labels and IDs.
			//String agID = legoFile.getCanonicalPath();
			String agLabel = StringUtils.removeEnd(fname, ".owl");
			String agID = new String(agLabel);

			try {
				ontology = pw.parseOWL(IRI.create(legoFile));
				currentReasoner = reasonerFactory.createReasoner(ontology);

				// Some sanity checks--some of the genereated ones are problematic.
				boolean consistent = currentReasoner.isConsistent();
				if( consistent == false ){
					LOG.info("Skip since inconsistent: " + fname);
					continue;
				}
				Set<OWLClass> unsatisfiable = currentReasoner.getUnsatisfiableClasses().getEntitiesMinusBottom();

				// TODO - make configurable to allow fail fast
				if (unsatisfiable.isEmpty() == false) {
					LOG.info("Skip since unsatisfiable: " + fname);
					continue;
				}

				Set<OWLNamedIndividual> individuals = ontology.getIndividualsInSignature();
				Set<OWLAnnotation> modelAnnotations = ontology.getAnnotations();

				OWLGraphWrapper currentGraph = new OWLGraphWrapper(ontology);						
				try {
					LOG.info("Trying complex annotation load of: " + fname);
					ComplexAnnotationSolrDocumentLoader loader =
							new ComplexAnnotationSolrDocumentLoader(url, currentGraph, currentReasoner, individuals, modelAnnotations, agID, agLabel, fname);
					loader.load();
				} catch (SolrServerException e) {
					LOG.info("Complex annotation load of " + fname + " at " + url + " failed!");
					e.printStackTrace();
					System.exit(1);
				}
			} finally {
				// Cleanup reasoner and ontology.
				if (currentReasoner != null) {
					currentReasoner.dispose();
				}
				if (ontology != null) {
					manager.removeOntology(ontology);
				}
			}
		}
	}
}
 
Example 6
Source File: SolrCommandRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Experimental method for trying out the loading of complex_annotation doc type.
 * Works with --read-ca-list <file>.
 * 
 * @param opts
 * @throws Exception
 */
@CLIMethod("--solr-load-complex-exp")
public void loadComplexAnnotationSolr(Opts opts) throws Exception {

	// Check to see if the global url has been set.
	String url = sortOutSolrURL(globalSolrURL);				

	// Only proceed if our environment was well-defined.
	if( caFiles == null || caFiles.isEmpty() ){
		LOG.warn("LEGO environment not well defined--will skip loading LEGO/CA.");
	}else{

		// NOTE: These two lines are remainders from old code, and I'm not sure of their place in this world of ours.
		// I wish there was an arcitecture diagram somehwere...
		OWLOntologyManager manager = pw.getManager();
		OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();

		// Actual loading--iterate over our list and load individually.
		for( String fname : caFiles ){
			OWLReasoner currentReasoner = null;
			OWLOntology ontology = null;

			// TODO: Temp cover for missing group labels and IDs.
			//String agID = legoFile.getCanonicalPath();
			String pretmp = StringUtils.removeEnd(fname, ".owl");
			String[] bits = StringUtils.split(pretmp, "/");
			String agID = bits[bits.length -1];
			String agLabel = new String(StringUtils.replaceOnce(agID, ":", "_"));

			try {
				ontology = pw.parseOWL(IRI.create(fname));
				currentReasoner = reasonerFactory.createReasoner(ontology);

				// Some sanity checks--some of the genereated ones are problematic.
				boolean consistent = currentReasoner.isConsistent();
				if( consistent == false ){
					LOG.info("Skip since inconsistent: " + fname);
					continue;
				}
				Set<OWLClass> unsatisfiable = currentReasoner.getUnsatisfiableClasses().getEntitiesMinusBottom();
				if (unsatisfiable.isEmpty() == false) {
					LOG.info("Skip since unsatisfiable: " + fname);
					continue;
				}

				Set<OWLNamedIndividual> individuals = ontology.getIndividualsInSignature();
				Set<OWLAnnotation> modelAnnotations = ontology.getAnnotations();
				OWLGraphWrapper currentGraph = new OWLGraphWrapper(ontology);						
				try {
					LOG.info("Trying complex annotation load of: " + fname);
					ComplexAnnotationSolrDocumentLoader loader =
							new ComplexAnnotationSolrDocumentLoader(url, currentGraph, currentReasoner, individuals, modelAnnotations, agID, agLabel, fname);
					loader.load();
				} catch (SolrServerException e) {
					LOG.info("Complex annotation load of " + fname + " at " + url + " failed!");
					e.printStackTrace();
					System.exit(1);
				}
			} finally {
				// Cleanup reasoner and ontology.
				if (currentReasoner != null) {
					currentReasoner.dispose();
				}
				if (ontology != null) {
					manager.removeOntology(ontology);
				}
			}
		}
	}
}
 
Example 7
Source File: OntologyMetadataMarkdownWriter.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static String renderMarkdown(OWLGraphWrapper g, String baseDir, boolean isIncludeImage) {

		OWLOntology ont = g.getSourceOntology();
		StringBuilder out = new StringBuilder();

		String ontId = g.getOntologyId();

		Set<OWLAnnotation> oAnns = ont.getAnnotations();
		System.err.println("NUM1:"+oAnns.size());

		String title = getVal("title", oAnns);

		out.append("## Ontology: "+title+"\n\n");
		out.append("IRI: "+rurl(ont)+"\n\n");

		String desc = getVal("description", oAnns);
		out.append("### Description\n\n");
		out.append(desc+"\n\n");

		Set<OWLOntology> imports = ont.getImports();
		if (imports.size() > 0) {
			out.append("### Imports\n\n");
			for (OWLOntology im : imports) {
				out.append(" * "+rurl(im)+"\n");
			}
		}
		if (isIncludeImage) {
			String imgFn = baseDir + "/" + ontId + ".png";
			out.append("![]("+imgFn+")\n\n");
		}

		System.err.println("NUM:"+oAnns.size());
		if (oAnns.size() > 0) {
			out.append("### Annotations:\n\n");
			for (OWLAnnotation ann : oAnns) {
				String annLabel = g.getLabelOrDisplayId(ann.getProperty());
				OWLAnnotationValue v = ann.getValue();
				String dv = v.toString();
				if (v instanceof OWLLiteral) {
					OWLLiteral lv = ((OWLLiteral)v);
					dv = lv.getLiteral();
					IRI dt = lv.getDatatype().getIRI();
					//System.out.println("DT = "+dt);
					if (dt.equals(OWL2Datatype.XSD_ANY_URI.getIRI())) {
						dv = href(lv.getLiteral());
					}
				}
				out.append(" * "+href(ann.getProperty().getIRI().toString(),annLabel)+" : "+dv+"\n");
			}
		}

		return out.toString();
	}
 
Example 8
Source File: GraphOwlVisitorTestBase.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws Exception {
  if (builtGraph) {
    // TODO: UGH - need a better pattern for this
    return;
  }
  graph = createInstance();

  String uri = Resources.getResource("ontologies/family.owl").toURI().toString();
  manager.loadOntologyFromOntologyDocument(IRI.create(uri));

  List<MappedProperty> propertyMap = new ArrayList<>();
  MappedProperty age = mock(MappedProperty.class);
  when(age.getName()).thenReturn("isAged");
  when(age.getProperties()).thenReturn(newArrayList(ROOT + "/hasAge"));
  propertyMap.add(age);
  MappedProperty versionInfo = mock(MappedProperty.class);
  when(versionInfo.getName()).thenReturn("versionInfo");
  when(versionInfo.getProperties()).thenReturn(newArrayList(OWL + "versionInfo"));
  propertyMap.add(versionInfo);
  OWLOntologyWalker walker = new OWLOntologyWalker(manager.getOntologies());
  GraphOwlVisitor visitor = new GraphOwlVisitor(walker, graph, propertyMap);
  walker.walkStructure(visitor);
  
  for (OWLOntology ontology : manager.getOntologies()){
    String ontologyIri = OwlApiUtils.getIri(ontology);
    for (OWLAnnotation annotation : ontology.getAnnotations()) { // Get annotations on ontology iri
      OWLAnnotationSubject ontologySubject = IRI.create(ontologyIri);
      OWLAnnotationAssertionAxiom object = 
          new OWLAnnotationAssertionAxiomImpl(ontologySubject,
              annotation.getProperty(), annotation.getValue(),
              new ArrayList<OWLAnnotation>());
      visitor.visit(object);
    }
  }
  graph.shutdown();
  graphDb = new TestGraphDatabaseFactory().newEmbeddedDatabase(new File(path));
  tx = graphDb.beginTx();
  nodeIndex = graphDb.index().getNodeAutoIndexer().getAutoIndex();
  builtGraph = true;
}