Java Code Examples for org.semanticweb.owlapi.model.OWLOntologyManager#loadOntologyFromOntologyDocument()

The following examples show how to use org.semanticweb.owlapi.model.OWLOntologyManager#loadOntologyFromOntologyDocument() . 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: OwlTestCase.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
@Before
public void loadOwl() throws Exception {
  OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  String uri = Resources.getResource("ontologies/cases/" + getTestName() + ".owl").toURI()
      .toString();
  IRI iri = IRI.create(uri);
  OWLOntology ont = manager.loadOntologyFromOntologyDocument(iri);
  if (performInference) {
    ReasonerConfiguration config = new ReasonerConfiguration();
    config.setFactory(ElkReasonerFactory.class.getCanonicalName());
    config.setAddDirectInferredEdges(true);
    ReasonerUtil util = new ReasonerUtil(config, manager, ont);
    util.reason();
  }
  OWLOntologyWalker walker = new OWLOntologyWalker(manager.getOntologies());

  GraphOwlVisitor visitor = new GraphOwlVisitor(walker, graph, new ArrayList<MappedProperty>());
  walker.walkStructure(visitor);

  OwlPostprocessor postprocessor = new OwlPostprocessor(graphDb, Collections.<String, String>emptyMap());
  postprocessor.processSomeValuesFrom();

  drawGraph();
}
 
Example 2
Source File: OwlInternalProofTest.java    From elk-reasoner with Apache License 2.0 6 votes vote down vote up
@Before
public void before() throws Exception {
	Assume.assumeFalse(TestUtils.ignore(manifest_.getInput(),
			ElkTestUtils.TEST_INPUT_LOCATION, IGNORE_LIST));

	final InputStream input = manifest_.getInput().getUrl().openStream();
	OWLOntologyManager manager = TestOWLManager.createOWLOntologyManager();
	OWLOntology ontology = manager.loadOntologyFromOntologyDocument(input);

	this.prover_ = OWLAPITestUtils.createProver(ontology);

	this.query_ = manifest_.getInput().getQuery();

	this.adapter_ = new OwlInternalProof(
			prover_.getDelegate().getInternalReasoner(), query_);
}
 
Example 3
Source File: Rules.java    From neo4j-sparql-extension with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns a list of rules extracted from the given OWL-2 ontology document.
 * 
 * @param src an ontology document
 * @return a list of rules
 */
public static List<Rule> fromOntology(OWLOntologyDocumentSource src) {
	try {
		// use OWL-API to get a OWLOntology document from source
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		manager.loadOntologyFromOntologyDocument(src);
		Set<OWLOntology> ontologies = manager.getOntologies();
		if (ontologies.isEmpty()) {
			return Collections.EMPTY_LIST;
		} else {
			// use first ontology from given source
			return fromOntology(ontologies.iterator().next());
		}
	} catch (OWLOntologyCreationException ex) {
		throw new IllegalArgumentException(
				"Loading ontology stream failed", ex);
	}
}
 
Example 4
Source File: OwlApiIncrementalReasoningTestDelegate.java    From elk-reasoner with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<OWLAxiom> load() throws Exception {

	final ArrayList<OWLAxiom> changingAxioms = new ArrayList<OWLAxiom>();

	final OWLOntologyManager manager = TestOWLManager
			.createOWLOntologyManager();

	final InputStream stream = getManifest().getInput().getUrl()
			.openStream();
	testOntology_ = manager.loadOntologyFromOntologyDocument(stream);

	for (OWLAxiom axiom : testOntology_.getLogicalAxioms()) {
		if (DYNAMIC_AXIOM_TYPES.contains(axiom.getAxiomType())) {
			changingAxioms.add(axiom);
		}
	}

	return changingAxioms;
}
 
Example 5
Source File: WEKAOntologyConnector.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates an ontology connector using the standard ontology.
 * 
 * @throws OWLOntologyCreationException
 *             If the ontology cannot be created
 */
public WEKAOntologyConnector() throws OWLOntologyCreationException {
	OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
	dataFactory = ontologyManager.getOWLDataFactory();
	InputStream inputStream = Thread.currentThread().getContextClassLoader()
			.getResourceAsStream(ONTOLOGY_FILE_NAME);
	ontology = ontologyManager.loadOntologyFromOntologyDocument(inputStream);
}
 
Example 6
Source File: MarkdownRendererTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Ignore
@Test
public void testRenderUberon() throws Exception {
	OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
	OWLOntology o = 
			manager.loadOntologyFromOntologyDocument(new File("/Users/cjm/repos/uberon/uberon.owl"));
	MarkdownRenderer mr = new 		MarkdownRenderer();
	mr.render(o, "target/uberon");
}
 
Example 7
Source File: OntologyHelper.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new ontology helper instance.
 * 
 * @param ontologyUri the URI giving the location of the ontology.
 * @param config the ontology configuration, containing the property URIs
 * for labels, synonyms, etc.
 * @throws OWLOntologyCreationException if the ontology cannot be read for
 * some reason - internal inconsistencies, etc.
 * @throws URISyntaxException if the URI cannot be parsed.
 */
public OntologyHelper(URI ontologyUri, OntologySettings config) throws OWLOntologyCreationException,
		URISyntaxException {
	this.config = config;

	if (!ontologyUri.isAbsolute()) {
		// Try to read as a file from the resource path
		LOGGER.debug("Ontology URI {} is not absolute - loading from classpath", ontologyUri);
		URL url = this.getClass().getClassLoader().getResource(ontologyUri.toString());
		if (url != null) {
			ontologyUri = url.toURI();
		}
	}
	this.ontologyUri = ontologyUri;
	LOGGER.info("Loading ontology from " + ontologyUri + "...");

	OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
	IRI iri = IRI.create(ontologyUri);
	this.ontology = manager.loadOntologyFromOntologyDocument(iri);
	// Use a buffering reasoner - not interested in ongoing changes
	this.reasoner = new StructuralReasonerFactory().createReasoner(ontology);
	// this.shortFormProvider = new SimpleShortFormProvider();
	this.owlNothingIRI = manager.getOWLDataFactory().getOWLNothing().getIRI();
	
	// Initialise the class map
	initialiseClassMap();
}
 
Example 8
Source File: InverseOfTautologyTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {

  OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  String uri =
      Resources.getResource("ontologies/cases/TestInverseOfTautology.owl").toURI().toString();
  IRI iri = IRI.create(uri);
  manager.loadOntologyFromOntologyDocument(iri);
  OWLOntologyWalker walker = new OWLOntologyWalker(manager.getOntologies());

  MappedProperty mappedProperty = new MappedProperty(NodeProperties.LABEL);
  List<String> properties = new ArrayList<String>();
  properties.add("http://www.w3.org/2000/01/rdf-schema#label");
  properties.add("http://www.w3.org/2004/02/skos/core#prefLabel");
  mappedProperty.setProperties(properties);

  ArrayList<MappedProperty> mappedPropertyList = new ArrayList<MappedProperty>();
  mappedPropertyList.add(mappedProperty);

  GraphOwlVisitor visitor = new GraphOwlVisitor(walker, graph, mappedPropertyList);
  walker.walkStructure(visitor);
  Map<String, String> categories = new HashMap<>();
  try (Transaction tx = graphDb.beginTx()) {
    OwlPostprocessor postprocessor = new OwlPostprocessor(graphDb, categories);
    postprocessor.processCategories(categories);
    postprocessor.processSomeValuesFrom();
    tx.success();
  }
}
 
Example 9
Source File: OwlApiReasoningTestDelegate.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
@Override
public void initWithInterrupts() throws Exception {
	final InputStream input = getManifest().getInput().getUrl()
			.openStream();
	OWLOntologyManager manager = TestOWLManager.createOWLOntologyManager();
	OWLOntology ontology = manager.loadOntologyFromOntologyDocument(input);

	final Random random = new Random(RandomSeedProvider.VALUE);
	reasoner_ = OWLAPITestUtils.createReasoner(ontology, false,
			new TestReasonerInterrupter(new RandomInterruptMonitor(random,
					getInterruptionChance(),
					getInterruptionIntervalNanos())));
}
 
Example 10
Source File: IncrementalClassification.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws OWLOntologyStorageException,
		OWLOntologyCreationException {
	OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

	// Load your ontology
	OWLOntology ont = manager.loadOntologyFromOntologyDocument(new File("path-to-ontology"));

	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(ont);

	// Classify the ontology.
	reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);

	OWLDataFactory factory = manager.getOWLDataFactory();
	OWLClass subClass = factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#AbsoluteShapeState"));
	OWLAxiom removed = factory.getOWLSubClassOfAxiom(subClass, factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#ShapeState")));
	
	OWLAxiom added = factory.getOWLSubClassOfAxiom(subClass, factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#GeneralisedStructure")));
	// Remove an existing axiom, add a new axiom
	manager.addAxiom(ont, added);
	manager.removeAxiom(ont, removed);
	// This is a buffering reasoner, so you need to flush the changes
	reasoner.flush();
	
	// Re-classify the ontology, the changes should be accommodated
	// incrementally (i.e. without re-inferring all subclass relationships)
	// You should be able to see it from the log output
	reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);		
	
	// Terminate the worker threads used by the reasoner.
	reasoner.dispose();
}
 
Example 11
Source File: SavingInferredAxioms.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws OWLOntologyStorageException,
		OWLOntologyCreationException {
	OWLOntologyManager inputOntologyManager = OWLManager.createOWLOntologyManager();
	OWLOntologyManager outputOntologyManager = OWLManager.createOWLOntologyManager();

	// Load your ontology.
	OWLOntology ont = inputOntologyManager.loadOntologyFromOntologyDocument(new File("path-to-ontology"));

	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(ont);

	// Classify the ontology.
	reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);

	// To generate an inferred ontology we use implementations of
	// inferred axiom generators
	List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
	gens.add(new InferredSubClassAxiomGenerator());
	gens.add(new InferredEquivalentClassAxiomGenerator());

	// Put the inferred axioms into a fresh empty ontology.
	OWLOntology infOnt = outputOntologyManager.createOntology();
	InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner,
			gens);
	iog.fillOntology(outputOntologyManager.getOWLDataFactory(), infOnt);

	// Save the inferred ontology.
	outputOntologyManager.saveOntology(infOnt,
			new FunctionalSyntaxDocumentFormat(),
			IRI.create((new File("path-to-output").toURI())));

	// Terminate the worker threads used by the reasoner.
	reasoner.dispose();
}
 
Example 12
Source File: RetrievingInstances.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
/**
 * @param args
 * @throws OWLOntologyCreationException
 */
public static void main(String[] args) throws OWLOntologyCreationException {
	OWLOntologyManager man = OWLManager.createOWLOntologyManager();

	// Load your ontology.
	OWLOntology ont = man
			.loadOntologyFromOntologyDocument(new File(args[0]));
	
	// Create an ELK reasoner.
	OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
	OWLReasoner reasoner = reasonerFactory.createReasoner(ont);
	
	// Precompute instances for each named class in the ontology
	reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS);

	// List representative instances for each class.
	for (OWLClass clazz : ont.getClassesInSignature()) {
		for (Node<OWLNamedIndividual> individual : reasoner.getInstances(
				clazz, true)) {
			System.out.println(clazz + "("
					+ individual.getRepresentativeElement() + ")");
		}
	}

	// Terminate the worker threads used by the reasoner.
	reasoner.dispose();
}
 
Example 13
Source File: GetOntologyId.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private OWLOntology getOntology() throws Exception{
	if(ontologyLocation.endsWith(".owl")){
		//oborelease.isObo2Owl			
		addProperty("oborelease.isowl", "true");
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		return manager.loadOntologyFromOntologyDocument(new File(ontologyLocation));
	}

	addProperty("oborelease.isobo", "true");
	
	Obo2Owl obo2owl = new Obo2Owl();
	
	return obo2owl.convert(ontologyLocation);
}
 
Example 14
Source File: Ancestors2Test.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private OWLGraphWrapper getOntologyWrapper() throws OWLOntologyCreationException{
	OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
	OWLOntology ontology = manager.loadOntologyFromOntologyDocument(getResource("lcstest3.owl"));
	return new OWLGraphWrapper(ontology);
}
 
Example 15
Source File: GraphImportsClosureTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("deprecation")
private OWLGraphWrapper getOntologyWrapper(boolean imp) throws OWLOntologyCreationException{
	OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
	OWLOntology ontology = manager.loadOntologyFromOntologyDocument(getResource("caro_mireot_test.owl"));
	return new OWLGraphWrapper(ontology, imp);
}
 
Example 16
Source File: MarkdownRendererTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private OWLGraphWrapper getOntologyWrapper() throws OWLOntologyCreationException{
	OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
	OWLOntology ontology = manager.loadOntologyFromOntologyDocument(getResource("renderer/caro.owl"));
	return new OWLGraphWrapper(ontology);
}
 
Example 17
Source File: Example_XML_JSON.java    From sparql-dl-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) 
{
	try {
		// Create an ontology manager in the usual way.
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

           // Load the wine ontology from the web.
           OWLOntology ont = manager.loadOntologyFromOntologyDocument(IRI.create("http://www.w3.org/TR/owl-guide/wine.rdf"));

           // Create an instance of an OWL API reasoner (we use the OWL API built-in StructuralReasoner for the purpose of demonstration here)
           StructuralReasonerFactory factory = new StructuralReasonerFactory();
		OWLReasoner reasoner = factory.createReasoner(ont);
           // Optionally let the reasoner compute the most relevant inferences in advance
		reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS,InferenceType.OBJECT_PROPERTY_ASSERTIONS);

		// Create an instance of the SPARQL-DL query engine
		engine = QueryEngine.create(manager, reasoner);
		
		processQuery(
               "PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT * WHERE {\n" +
			    "SubClassOf(wine:PinotBlanc, ?x),\n" +
			    "SubClassOf(?x, wine:Wine)\n" +
			"}"
		);
		
		processQuery(
               "PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"ASK {\n" +
			    "SubClassOf(wine:PinotBlanc, wine:Wine)\n" +
			"}"
		);
       }
       catch(UnsupportedOperationException exception) {
           System.out.println("Unsupported reasoner operation.");
       }
       catch(OWLOntologyCreationException e) {
           System.out.println("Could not load the ontology: " + e.getMessage());
       }
}
 
Example 18
Source File: Example_Basic.java    From sparql-dl-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) 
{
	try {
		// Create an ontology manager
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

		// Load the wine ontology from the web.
           OWLOntology ont = manager.loadOntologyFromOntologyDocument(IRI.create("http://www.w3.org/TR/owl-guide/wine.rdf"));

		// Create an instance of an OWL API reasoner (we use the OWL API built-in StructuralReasoner for the purpose of demonstration here)
           StructuralReasonerFactory factory = new StructuralReasonerFactory();
		OWLReasoner reasoner = factory.createReasoner(ont);
           // Optionally let the reasoner compute the most relevant inferences in advance
		reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS,InferenceType.OBJECT_PROPERTY_ASSERTIONS);

		// Create an instance of the SPARQL-DL query engine
		engine = QueryEngine.create(manager, reasoner, true);

           // Some queries which cover important basic language constructs of SPARQL-DL

           // All white wines (all individuals of the class WhiteWine and sub classes thereof)
		processQuery(
			"SELECT * WHERE {\n" +
			    "Type(?x, <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#WhiteWine>)" +
			"}"	
		);

           // The white wines (the individuals of WhiteWine but not of it's sub classes) 
           processQuery(
			"SELECT * WHERE {\n" +
			    "DirectType(?x, <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#RedTableWine>)" +
			"}"
		);

           // Is PinotBlanc a sub class of Wine?
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"ASK {\n" +
				"SubClassOf(wine:PinotBlanc, wine:Wine)" +
			"}"
		);

           // The direct sub classes of FrenchWine
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT ?x WHERE {\n" +
				"DirectSubClassOf(?x, wine:FrenchWine)" +
			"}"	
		);

		// All individuals
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT * WHERE {\n" +
				"Individual(?x)" +
			"}"
		);

           // All functional ObjectProperties
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT * WHERE {\n" +
                   "ObjectProperty(?x), " +
				"Functional(?x)" +
			"}"
		);

           // The strict sub classes of DryWhiteWine (sub classes with are not equivalent to DryWhiteWine)
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" +
			"SELECT ?x WHERE {\n" +
				"StrictSubClassOf(?x, wine:DryWhiteWine)" +
			"}"
		);

           // All the grapes from which RedTableWines are made from (without duplicates)
		processQuery(
			"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n" + 
			"SELECT DISTINCT ?v WHERE {\n" +
			    "Type(?i, wine:RedTableWine),\n" +
			    "PropertyValue(?i, wine:madeFromGrape, ?v)" +
			"}"	
		);

       }
       catch(UnsupportedOperationException exception) {
           System.out.println("Unsupported reasoner operation.");
       }
       catch(OWLOntologyCreationException e) {
           System.out.println("Could not load the wine ontology: " + e.getMessage());
       }
}
 
Example 19
Source File: AncestorsTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private OWLGraphWrapper getOntologyWrapper() throws OWLOntologyCreationException{
	OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
	OWLOntology ontology = manager.loadOntologyFromOntologyDocument(getResource("test_phenotype.owl"));
	return new OWLGraphWrapper(ontology);
}
 
Example 20
Source File: SolrCommandRunner.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private OWLGraphWrapper getOntologyWrapper(String file) throws OWLOntologyCreationException{
	ParserWrapper pw = new ParserWrapper();
	OWLOntologyManager manager = pw.getManager();
	OWLOntology ontology = manager.loadOntologyFromOntologyDocument(getTestResource(file));
	return new OWLGraphWrapper(ontology);
}