Java Code Examples for org.semanticweb.owlapi.reasoner.OWLReasoner#flush()

The following examples show how to use org.semanticweb.owlapi.reasoner.OWLReasoner#flush() . 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: ProvenanceReasonerWrapper.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public boolean isEdgeEntailed(OWLEdge e, OWLOntology currentOntology, OWLReasoner reasoner) {
	OWLOntologyManager m = getManager();

	Set<OWLSubClassOfAxiom> scas = currentOntology.getSubClassAxiomsForSubClass(e.c);
	Set<OWLSubClassOfAxiom> rmAxioms = new HashSet<OWLSubClassOfAxiom>();
	for (OWLSubClassOfAxiom sca : scas) {
		if (sca.getSuperClass().equals(e.p)) {
			LOG.info("REMOVING: "+sca);
			rmAxioms.add(sca);
		}
	}
	boolean isEdgeAsserted = rmAxioms.size() > 0;
	if (isEdgeAsserted) {
		m.removeAxioms(currentOntology, rmAxioms);
		reasoner.flush();
	}
	boolean isEntailed;
	isEntailed = reasoner.getSuperClasses(e.c, false).containsEntity(e.p);
	if (isEdgeAsserted) {
		m.addAxioms(currentOntology, rmAxioms);
		reasoner.flush();
	}

	return isEntailed;
}
 
Example 2
Source File: SimJSONTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testInfoProfile() throws OWLOntologyCreationException, IOException, UnknownOWLClassException, OBOFormatParserException {

	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOBO(getResource("sim/mp-subset-1.obo").getAbsolutePath());
	g =  new OWLGraphWrapper(sourceOntol);
	parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);

	owlpp = new OWLPrettyPrinter(g);
	final int truncLen = 200;
	
	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {

		createOwlSim();
			//sos.setReasoner(reasoner);
		LOG.info("Reasoner="+owlsim.getReasoner());

		SimJSONEngine sj = new SimJSONEngine(g, owlsim);

		//sos.saveOntology("/tmp/z.owl");

		reasoner.flush();
		
		owlsim.createElementAttributeMapFromOntology();
		owlsim.computeSystemStats();
		
		for (OWLNamedIndividual i : sourceOntol.getIndividualsInSignature()) {
			String jsonStr = sj.getAttributeInformationProfile(owlsim.getAttributesForElement(i));
			LOG.info("InformationInfo:"+jsonStr);
		}
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 3
Source File: FindMatchesSimTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testBasicSim() throws Exception {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOWL(getResourceIRIString("sim/mp-subset-1.obo"));
	g =  new OWLGraphWrapper(sourceOntol);
	parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);
	setOutput("target/find-matches-test.out");

	owlpp = new OWLPrettyPrinter(g);

	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {

		this.createOwlSim();
		owlsim.createElementAttributeMapFromOntology();

		//sos.saveOntology("/tmp/z.owl");

		reasoner.flush();
		for (OWLNamedIndividual i : sourceOntol.getIndividualsInSignature()) {

			renderer.getResultOutStream().println("\nI = "+i);
			
			List<ElementPairScores> scoreSets = owlsim.findMatches(i, "MGI");
			int rank = 1;
			for (ElementPairScores s : scoreSets) {
				renderer.getResultOutStream().println("\n  RANK = "+rank);
				renderer.printPairScores(s);
				rank++;
			}
		}
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 4
Source File: SimJSONTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSearch() throws Exception {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOBO(getResource("sim/mp-subset-1.obo").getAbsolutePath());
	g =  new OWLGraphWrapper(sourceOntol);
	parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);

	owlpp = new OWLPrettyPrinter(g);
	final int truncLen = 200;
	
	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {

		createOwlSim();
			//sos.setReasoner(reasoner);
		LOG.info("Reasoner="+owlsim.getReasoner());

		SimJSONEngine sj = new SimJSONEngine(g, owlsim);

		//sos.saveOntology("/tmp/z.owl");

		reasoner.flush();
		
		owlsim.createElementAttributeMapFromOntology();
		owlsim.computeSystemStats();
		

		for (OWLNamedIndividual i : sourceOntol.getIndividualsInSignature()) {
			Set<OWLClass> atts = owlsim.getAttributesForElement(i);
			String jsonStr = sj.search(atts, "MGI", true, false);
			
			LOG.info(jsonStr);
		}
		
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 5
Source File: SimStatsTests.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testIxI() throws Exception {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOBO(getResource("sim/mp-subset-1.obo").getAbsolutePath());
	g =  new OWLGraphWrapper(sourceOntol);
	parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);
	setOutput("target/basic-owlsim-test.out");

	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {
		this.createOwlSim();
		owlsim.createElementAttributeMapFromOntology();
		
		reasoner.flush();
		owlsim.computeSystemStats();
		owlsim.calculateMetricStats(owlsim.getAllElements(),owlsim.getAllElements());

		String[] metrics = {"bmaAsymIC","bmaSymIC","bmaInverseAsymIC", "combinedScore", "simJ", "simGIC","maxIC"};
		for (String m : metrics) {
			LOG.info("Test Summary(mean) for "+m+": "+owlsim.getMetricStats(Stat.MEAN).get(m).getSummary());
			LOG.info("Test Summary(min) for "+m+": "+owlsim.getMetricStats(Stat.MIN).get(m).getSummary());
			LOG.info("Test Summary(max) for "+m+": "+owlsim.getMetricStats(Stat.MAX).get(m).getSummary());
		}
	} finally {
		reasoner.dispose();
	}
	
}
 
Example 6
Source File: OwlSimVarianceTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testPValue() throws Exception {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOBO(getResource("sim/mp-subset-1.obo").getAbsolutePath());
	g =  new OWLGraphWrapper(sourceOntol);
	parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);

	LOG.info("Initialize OwlSim ..");

	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	reasoner.flush();
	
	try {
		owlsim = owlSimFactory.createOwlSim(sourceOntol);
		owlsim.createElementAttributeMapFromOntology();
		owlsim.computeSystemStats();
	} catch (UnknownOWLClassException e) {
		e.printStackTrace();
	} finally {
		reasoner.dispose();
	}

	// Source sourceOntol and g are used as background knowledge ... 
	OWLSimReferenceBasedStatistics refBasedStats = new OWLSimReferenceBasedStatistics(owlsim, sourceOntol, g);
	
	IRI iri = IRI.create("http://purl.obolibrary.org/obo/MGI_101761");
	
	String[] testClasses = new String[] {"MP:0002758", "MP:0002772", "MP:0005448", "MP:0003660"};
	Set<OWLClass> testClassesSet = new HashSet<OWLClass>();
	for (String testClass : testClasses) {
		testClassesSet.add(this.getOBOClass(testClass));
	}

	PValue pValue = refBasedStats.getPValue(testClassesSet, iri);
	LOG.info(pValue.getSimplePValue());

}
 
Example 7
Source File: SimStatsTests.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
	public void testStats() throws IOException, OWLOntologyCreationException, MathException, UnknownOWLClassException, OBOFormatParserException {
		ParserWrapper pw = new ParserWrapper();
		sourceOntol = pw.parseOBO(getResource("sim/mp-subset-1.obo").getAbsolutePath());
		g =  new OWLGraphWrapper(sourceOntol);
		parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);
		setOutput("target/basic-owlsim-test.out");

		// assume buffering
		OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
		try {
			this.createOwlSim();
			owlsim.createElementAttributeMapFromOntology();
			
			reasoner.flush();
			owlsim.computeSystemStats();
			LOG.info("Overall statistical summary for Test:");
			LOG.info(owlsim.getSystemStats().toString());
			LOG.info("Averaged statistical summary for Individuals in Test:");
//			LOG.info(owlsim.getSummaryStatistics(Stat.MEAN).toString());
			LOG.info("individuals: "+owlsim.getSummaryStatistics().n.getN());
			LOG.info("mean(n/indiv): "+String.format("%1$.5f", owlsim.getSummaryStatistics().n.getMean()));
			LOG.info("mean(meanIC): "+String.format("%1$.5f", owlsim.getSummaryStatistics().mean.getMean()));
			LOG.info("mean(maxIC): "+String.format("%1$.5f", owlsim.getSummaryStatistics().max.getMean()));
			LOG.info("max(maxIC): "+String.format("%1$.5f", owlsim.getSummaryStatistics().max.getMax()));
			LOG.info("mean(sumIC): "+String.format("%1$.5f", owlsim.getSummaryStatistics().max.getMean()));
		}
		finally {
			reasoner.dispose();
		}
	}
 
Example 8
Source File: BasicOWLSimTestUsingSOS.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test 
public void testGetEntropy() throws OWLOntologyCreationException, IOException, OBOFormatParserException, UnknownOWLClassException {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOBO(getResourceIRIString("sim/mp-subset-1.obo"));
	g =  new OWLGraphWrapper(sourceOntol);
	parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);

	owlpp = new OWLPrettyPrinter(g);

	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {

		owlsim = new SimpleOwlSim(sourceOntol);
		((SimpleOwlSim) owlsim).setReasoner(reasoner);

		reasoner.flush();
		Double e = owlsim.getEntropy();
		LOG.info("ENTROPY OF ONTOLOGY = "+e);

		for (String subset : g.getAllUsedSubsets()) {
			LOG.info("SUBSET:"+subset);
			e = owlsim.getEntropy(g.getOWLClassesInSubset(subset));
			LOG.info(" ENTROPY OF "+subset+" = "+e);
		}
	}
	finally {
		reasoner.dispose();
	}		
}
 
Example 9
Source File: RedundantAxiomTagger.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void tagRedundantAxioms(OWLReasoner reasoner) {
    OWLOntology ont = reasoner.getRootOntology();
    OWLOntologyManager mgr = ont.getOWLOntologyManager();
    OWLDataFactory df = mgr.getOWLDataFactory();
    OWLAnnotationProperty anProp = df.getOWLAnnotationProperty(IRI.create("http://www.geneontology.org/formats/oboInOwl#source"));
    for (OWLSubClassOfAxiom ax : ont.getAxioms(AxiomType.SUBCLASS_OF)) {
        if (!ax.getSuperClass().isAnonymous()) {
            OWLClass supc = (OWLClass) ax.getSuperClass();
            
            mgr.removeAxiom(ont, ax);
            reasoner.flush();
            NodeSet<OWLClass> ancs = reasoner.getSuperClasses(ax.getSubClass(), false);
            //LOG.info(ax + " ANCS="+ancs);
            if (ancs.containsEntity( supc)) {
                String direct = "indirect";
                if (reasoner.getSuperClasses(ax.getSubClass(), true).containsEntity( supc)) {
                    direct = "direct";
                }
                LOG.info("SCA = "+ax+" D="+direct);
                OWLAnnotation ann = df.getOWLAnnotation(anProp, df.getOWLLiteral(direct));
                OWLAxiom newAxiom = changeAxiomAnnotations(ax, Collections.singleton(ann), df);
                mgr.addAxiom(ont, newAxiom);
            }
            else {
                // put it back
                mgr.addAxiom(ont, ax);
            }
        }
    }
   
}
 
Example 10
Source File: PhenoSimHQETest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testPhenoSimMouse() throws IOException, OWLOntologyCreationException, OWLOntologyStorageException, MathException {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOWL(getResourceIRIString("test_phenotype.owl"));
	g =  new OWLGraphWrapper(sourceOntol);
	 owlpp = new OWLPrettyPrinter(g);
	
	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {
		pproc = new PhenoSimHQEPreProcessor();
		pproc.setInputOntology(sourceOntol);
		pproc.setOutputOntology(sourceOntol);
		pproc.setReasoner(reasoner);
		pproc.setOWLPrettyPrinter(owlpp);
		((PhenoSimHQEPreProcessor)pproc).defaultLCSElementFrequencyThreshold = 0.7;

		//sos.setSimPreProcessor(pproc);
		//sos.preprocess();
		pproc.preprocess();
		reasoner.flush();

		sos = new SimpleOwlSim(sourceOntol);
		sos.setSimPreProcessor(pproc);
		sos.createElementAttributeMapFromOntology();

		//sos.saveOntology("/tmp/z.owl");

		reasoner.flush();
		for (OWLNamedIndividual i : sourceOntol.getIndividualsInSignature()) {
			for (OWLNamedIndividual j : sourceOntol.getIndividualsInSignature()) {
				showSim(i,j);
			}
		}
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 11
Source File: PhenoSimHQETest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testPhenoSim() throws IOException, OWLOntologyCreationException, OWLOntologyStorageException, MathException {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOWL(getResourceIRIString("q-in-e.omn"));
	g =  new OWLGraphWrapper(sourceOntol);
	 owlpp = new OWLPrettyPrinter(g);
	
	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {
		pproc = new PhenoSimHQEPreProcessor();
		pproc.setInputOntology(sourceOntol);
		pproc.setOutputOntology(sourceOntol);
		pproc.setReasoner(reasoner);
		pproc.setOWLPrettyPrinter(owlpp);
		((PhenoSimHQEPreProcessor)pproc).defaultLCSElementFrequencyThreshold = 0.7;

		//sos.setSimPreProcessor(pproc);
		//sos.preprocess();
		pproc.preprocess();
		reasoner.flush();

		sos = new SimpleOwlSim(sourceOntol);
		sos.setSimPreProcessor(pproc);
		sos.createElementAttributeMapFromOntology();

		//sos.saveOntology("/tmp/z.owl");

		reasoner.flush();
		for (OWLNamedIndividual i : sourceOntol.getIndividualsInSignature()) {
			for (OWLNamedIndividual j : sourceOntol.getIndividualsInSignature()) {
				showSim(i,j);
			}
		}
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 12
Source File: CoannotatedTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test fetching commonly co-annotated classes in the entire corpus, which
 * uses the TF-IDF method.
 * @throws Exception
 */
@Test
public void testGetCoannotatedClassesForAttribute() throws Exception {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOWL(getResourceIRIString("sim/mp-subset-1.obo"));
	g =  new OWLGraphWrapper(sourceOntol);
	parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);

	owlpp = new OWLPrettyPrinter(g);

	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {

		this.createOwlSim();
		owlsim.createElementAttributeMapFromOntology();

		reasoner.flush();
		owlsim.populateFullCoannotationMatrix();
		Set<OWLNamedIndividual> inds = sourceOntol.getIndividualsInSignature();
		Set<OWLClass> cs = owlsim.getAllAttributeClasses();
		LOG.info("Dumping coannotations and scores for all annotated classes");
		for (OWLClass c : cs) {
			List<ClassCount> lcc = owlsim.getCoannotatedClassesForAttribute(c,inds.size());
			for (ClassCount cc : lcc) {
				LOG.info(owlpp.render(c)+owlpp.render(cc.c)+cc.score);
			}
		}
	}
	finally {
		reasoner.dispose();
	}
}
 
Example 13
Source File: CoannotatedTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test fetching commonly co-annotated classes in a set of individuals 
 * vs the background (entire) corpus using the TF-IDF method.
 * @throws Exception
 * @throws MathException
 */
@Test
public void coAnnotatedTest() throws Exception, MathException {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOWL(getResourceIRIString("sim/mp-subset-1.obo"));
	g =  new OWLGraphWrapper(sourceOntol);
	parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);

	owlpp = new OWLPrettyPrinter(g);

	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {

		this.createOwlSim();
		owlsim.createElementAttributeMapFromOntology();

		reasoner.flush();
		owlsim.populateFullCoannotationMatrix();
		Set<OWLNamedIndividual> inds = sourceOntol.getIndividualsInSignature();
		for (OWLNamedIndividual i : inds) {
			//get a random set of other individuals to do the subset
			List<ClassCount> coaClasses = owlsim.getCoAnnotatedClassesForIndividual(i);
			LOG.info("Found "+coaClasses.size()+" coannotated classes for "+g.getIdentifier(i));
			for (ClassCount cc : coaClasses) {
				LOG.info(owlpp.render(cc.c)+cc.score);
			}
		}
	}
	finally {
		reasoner.dispose();
	}
	

}
 
Example 14
Source File: PhenoSimHQEPreProcessorTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testPhenoSim() throws IOException, OWLOntologyCreationException, OWLOntologyStorageException, MathException {
	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOWL(getResourceIRIString("q-in-e.omn"));
	g =  new OWLGraphWrapper(sourceOntol);
	 owlpp = new OWLPrettyPrinter(g);
	
	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {
		pproc = new PhenoSimHQEPreProcessor();
		pproc.setInputOntology(sourceOntol);
		pproc.setOutputOntology(sourceOntol);
		pproc.setReasoner(reasoner);
		((PhenoSimHQEPreProcessor)pproc).defaultLCSElementFrequencyThreshold = 0.9;


		pproc.preprocess();
		reasoner.flush();


		reasoner.flush();
		for (OWLNamedIndividual i : sourceOntol.getIndividualsInSignature()) {
			for (OWLNamedIndividual j : sourceOntol.getIndividualsInSignature()) {
				//showLCS(i,j);
			}
		}

		// note: may be sensitive to ordering..
		testLCS("hypoplastic and affected retina phenotype",
				"hypoplastic and affected retina phenotype",
				"hypoplastic and affected retina phenotype");

		testLCS("hypoplastic and affected retina phenotype",
				"hyperplastic and affected ommatidium phenotype",
				"[abnormal_morphology] and [affected photoreceptor-based entity] phenotype");

		testLCS("hyperplastic and affected hand phenotype",
				"hypoplastic and affected hindlimb phenotype",
				"[abnormal_morphology] and [affected limb structure] phenotype");
	
	}
	finally{
		reasoner.dispose();
	}
	 
}
 
Example 15
Source File: ElkReasonerTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
/**
 * Testing correctness of the reasoner when changes are made to other, imported or not, ontologies
 * 
 */
@Test
public void testChangesToOtherOntologies() 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 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");
	// the imported ontologies must be loaded
	OWLOntology ontoA = man.getOntology(IRI.create("http://www.example.com/A"));
	OWLOntology ontoB = man.getOntology(IRI.create("http://www.example.com/B"));

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

	try {
		
		assertTrue(reasoner.getSuperClasses(extA, false).containsEntity(
				extC));
		assertTrue(reasoner.getSuperClasses(mainY, false).containsEntity(
				extC));
		
		// ************************************
		// ** removing an axiom "A:A is-a B:B" from impA
		// ************************************
		OWLAxiom axiom = dataFactory.getOWLSubClassOfAxiom(extA, extB);
		man.removeAxiom(ontoA, axiom);
		reasoner.flush();
		
		assertFalse(reasoner.getSuperClasses(extA, false).containsEntity(
				extC));
		// put it back
		man.addAxiom(ontoA, axiom);
		reasoner.flush();
		
		assertTrue(reasoner.getSuperClasses(extA, false).containsEntity(
				extC));

		// ************************************
		// ** removing an axiom "B:B is-a B:C" from impB
		// ************************************
		axiom = dataFactory.getOWLSubClassOfAxiom(extB, extC);
		man.removeAxiom(ontoB, axiom);
		reasoner.flush();
		
		assertFalse(reasoner.getSuperClasses(mainY, false).containsEntity(
				extC));

	}
	finally {
		reasoner.dispose();
	}
}
 
Example 16
Source File: ElkReasonerTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
/**
 * Testing correctness of the reasoner with respect to ontology changes
 * <p>
 * removing the import declaration for </impA>,
 * adding the import declaration for </impB> and removing
 * ":Y is-a B:B"
 */
@Test
public void testRemovingImpAAddingImpBRemovingYB() 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 {

		// ************************************
		// ** adding the import declaration for </impB> and removing
		// ":Y is-a B:B"
		// ************************************

		OWLImportsDeclaration importA = new OWLImportsDeclarationImpl(
				IRI.create("http://www.example.com#impA"));
		OWLOntologyChange change = new RemoveImport(root, importA);
		man.applyChange(change);
		OWLImportsDeclaration importB = new OWLImportsDeclarationImpl(
				IRI.create("http://www.example.com#impB"));
		change = new AddImport(root, importB);
		man.applyChange(change);
		OWLSubClassOfAxiom axiom = dataFactory.getOWLSubClassOfAxiom(mainY, extB);
		man.removeAxiom(root, axiom);
		reasoner.flush();

		// Now ontology should import only ontology </impB>
		assertEquals(root.getAxiomCount(), 2);
		assertEquals(root.getImportsClosure().size(), 2);
		assertEquals(getAxioms(root).size(), 4);

		// reasoner queries -- only subsumptions of the root
		// ontology </impB> and there
		assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity(
				mainY));
		assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity(
				extA));
		assertFalse(reasoner.getSuperClasses(mainY, true).containsEntity(
				extB));
		assertFalse(reasoner.getSuperClasses(extA, true).containsEntity(
				extB));
		assertTrue(reasoner.getSuperClasses(extB, true)
				.containsEntity(extC));

	} finally {
		reasoner.dispose();
	}

}
 
Example 17
Source File: ElkReasonerTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
	}

}
 
Example 18
Source File: ElkReasonerTest.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
/**
 * Testing correctness of the reasoner with respect to ontology changes
 * 
 * removing an axiom ":X is-a :Y"
 */
@Test
public void testRemovingXY() 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 an axiom ":X is-a :Y"
		// ************************************
		OWLAxiom axiom = dataFactory.getOWLSubClassOfAxiom(mainX, mainY);
		man.removeAxiom(root, axiom);
		reasoner.flush();

		// the root ontology contains one fewer axioms
		assertEquals(root.getAxiomCount(), 2);
		// the number of ontologies in the import closure does not change
		assertEquals(root.getImportsClosure().size(), 3);
		// the total number of axioms reduces
		assertEquals(getAxioms(root).size(), 5);

		// reasoner queries -- first subsumption is gone
		assertFalse(reasoner.getSuperClasses(mainX, true).containsEntity(
				mainY));
		assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity(
				extA));
		assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity(
				extB));
		assertTrue(reasoner.getSuperClasses(extA, true)
				.containsEntity(extB));
		assertTrue(reasoner.getSuperClasses(extB, true)
				.containsEntity(extC));

	} finally {
		reasoner.dispose();
	}

}
 
Example 19
Source File: QueryingUnnamedClassExpressions.java    From elk-reasoner with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws OWLOntologyCreationException {
	OWLOntologyManager man = OWLManager.createOWLOntologyManager();
	OWLDataFactory dataFactory = man.getOWLDataFactory();

	// Load your ontology.
	OWLOntology ont = man.loadOntologyFromOntologyDocument(new File(
			"c:/ontologies/ontology.owl"));

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

	// Create your desired query class expression. In this example we
	// will query ObjectIntersectionOf(A ObjectSomeValuesFrom(R B)).
	PrefixManager pm = new DefaultPrefixManager("http://example.org/");
	OWLClass A = dataFactory.getOWLClass(":A", pm);
	OWLObjectProperty R = dataFactory.getOWLObjectProperty(":R", pm);
	OWLClass B = dataFactory.getOWLClass(":B", pm);
	OWLClassExpression query = dataFactory.getOWLObjectIntersectionOf(A,
			dataFactory.getOWLObjectSomeValuesFrom(R, B));

	// Create a fresh name for the query.
	OWLClass newName = dataFactory.getOWLClass(IRI.create("temp001"));
	// Make the query equivalent to the fresh class
	OWLAxiom definition = dataFactory.getOWLEquivalentClassesAxiom(newName,
			query);
	man.addAxiom(ont, definition);

	// Remember to either flush the reasoner after the ontology change
	// or create the reasoner in non-buffering mode. Note that querying
	// a reasoner after an ontology change triggers re-classification of
	// the whole ontology which might be costly. Therefore, if you plan
	// to query for multiple complex class expressions, it will be more
	// efficient to add the corresponding definitions to the ontology at
	// once before asking any queries to the reasoner.
	reasoner.flush();

	// You can now retrieve subclasses, superclasses, and instances of
	// the query class by using its new name instead.
	reasoner.getSubClasses(newName, true);
	reasoner.getSuperClasses(newName, true);
	reasoner.getInstances(newName, false);

	// After you are done with the query, you should remove the definition
	man.removeAxiom(ont, definition);

	// You can now add new definitions for new queries in the same way

	// After you are done with all queries, do not forget to free the
	// resources occupied by the reasoner
	reasoner.dispose();
}
 
Example 20
Source File: SimJSONTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testInfoProfileWithSubscores() throws OWLOntologyCreationException, IOException, UnknownOWLClassException, OBOFormatParserException {

	ParserWrapper pw = new ParserWrapper();
	sourceOntol = pw.parseOBO(getResource("sim/mp-subset-1.obo").getAbsolutePath());
	g =  new OWLGraphWrapper(sourceOntol);
	parseAssociations(getResource("sim/mgi-gene2mp-subset-1.tbl"), g);

	owlpp = new OWLPrettyPrinter(g);

	Set<OWLClass> upperLevelClasses = new HashSet<OWLClass>();
	upperLevelClasses.add(g.getOWLClassByIdentifier("MP:0000001")); //root, should be the same as overall
	upperLevelClasses.add(g.getOWLClassByIdentifier("MP:0000003")); //Adipose Tissue Morphology
	upperLevelClasses.add(g.getOWLClassByIdentifier("MP:0001544")); //Abnormal Blood morphology
	String id = "BOGUS:1234567";
	IRI iri = g.getIRIByIdentifier(id);
	OWLClass c = g.getDataFactory().getOWLClass(iri);
	LOG.info("Unresolvable id:"+id+". Making temp class element:"+c.toString());
	upperLevelClasses.add(c);

	upperLevelClasses.add(g.getOWLClassByIdentifier("MP:0002160")); //Abnormal Reproductive System Morphology
	upperLevelClasses.add(g.getOWLClassByIdentifier("MP:0002152")); //Abnormal Brain Morphology
	upperLevelClasses.add(g.getOWLClassByIdentifier("MP:0003631")); //Nervous System Phenotype
	id = "BOGUS:2345678";
	iri = g.getIRIByIdentifier(id);
	c = g.getDataFactory().getOWLClass(iri);
	LOG.info("Unresolvable id:"+id+". Making temp class element:"+c.toString());
	upperLevelClasses.add(c);

	
	// assume buffering
	OWLReasoner reasoner = new ElkReasonerFactory().createReasoner(sourceOntol);
	try {

		createOwlSim();
		LOG.info("Reasoner="+owlsim.getReasoner());

		SimJSONEngine sj = new SimJSONEngine(g, owlsim);

		reasoner.flush();
		
		owlsim.createElementAttributeMapFromOntology();
		owlsim.computeSystemStats();
		
		for (OWLNamedIndividual i : sourceOntol.getIndividualsInSignature()) {
			String jsonStr = sj.getAttributeInformationProfile(owlsim.getAttributesForElement(i),upperLevelClasses);
			LOG.info("InformationProfile:"+jsonStr);
		}
	}
	finally {
		reasoner.dispose();
	}
}