use of org.apache.jena.graph.Graph in project jena by apache.
the class TestReasoners method testTransitiveRemove.
/**
* Test delete operation for Transtive reasoner.
*/
public void testTransitiveRemove() {
Graph data = Factory.createGraphMem();
Node a = NodeFactory.createURI("a");
Node b = NodeFactory.createURI("b");
Node c = NodeFactory.createURI("c");
Node d = NodeFactory.createURI("d");
Node e = NodeFactory.createURI("e");
Node closedP = RDFS.subClassOf.asNode();
data.add(new Triple(a, RDFS.subClassOf.asNode(), b));
data.add(new Triple(a, RDFS.subClassOf.asNode(), c));
data.add(new Triple(b, RDFS.subClassOf.asNode(), d));
data.add(new Triple(c, RDFS.subClassOf.asNode(), d));
data.add(new Triple(d, RDFS.subClassOf.asNode(), e));
Reasoner reasoner = TransitiveReasonerFactory.theInstance().create(null);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(a, RDFS.subClassOf.asNode(), null), new Object[] { new Triple(a, closedP, a), new Triple(a, closedP, b), new Triple(a, closedP, b), new Triple(a, closedP, c), new Triple(a, closedP, d), new Triple(a, closedP, e) });
TestUtil.assertIteratorValues(this, infgraph.find(b, RDFS.subClassOf.asNode(), null), new Object[] { new Triple(b, closedP, b), new Triple(b, closedP, d), new Triple(b, closedP, e) });
infgraph.delete(new Triple(b, closedP, d));
TestUtil.assertIteratorValues(this, infgraph.find(a, RDFS.subClassOf.asNode(), null), new Object[] { new Triple(a, closedP, a), new Triple(a, closedP, b), new Triple(a, closedP, b), new Triple(a, closedP, c), new Triple(a, closedP, d), new Triple(a, closedP, e) });
TestUtil.assertIteratorValues(this, infgraph.find(b, RDFS.subClassOf.asNode(), null), new Object[] { new Triple(b, closedP, b) });
infgraph.delete(new Triple(a, closedP, c));
TestUtil.assertIteratorValues(this, infgraph.find(a, RDFS.subClassOf.asNode(), null), new Object[] { new Triple(a, closedP, a), new Triple(a, closedP, b) });
TestUtil.assertIteratorValues(this, infgraph.find(b, RDFS.subClassOf.asNode(), null), new Object[] { new Triple(b, closedP, b) });
TestUtil.assertIteratorValues(this, data.find(null, RDFS.subClassOf.asNode(), null), new Object[] { new Triple(a, closedP, b), new Triple(c, closedP, d), new Triple(d, closedP, e) });
}
use of org.apache.jena.graph.Graph in project jena by apache.
the class TestReasoners method doTestMetaLevel.
/**
* Test metalevel add/remove subproperty operations for a reasoner.
*/
public void doTestMetaLevel(ReasonerFactory rf) {
Graph data = Factory.createGraphMem();
Node c1 = NodeFactory.createURI("C1");
Node c2 = NodeFactory.createURI("C2");
Node c3 = NodeFactory.createURI("C3");
Node p = NodeFactory.createURI("p");
Node q = NodeFactory.createURI("q");
Node sC = RDFS.subClassOf.asNode();
Node sP = RDFS.subPropertyOf.asNode();
data.add(new Triple(c2, sC, c3));
data.add(new Triple(c1, p, c2));
Reasoner reasoner = rf.create(null);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] {});
infgraph.add(new Triple(p, q, sC));
TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] {});
infgraph.add(new Triple(q, sP, sP));
TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] { new Triple(c1, sC, c1), new Triple(c1, sC, c2), new Triple(c1, sC, c3) });
infgraph.delete(new Triple(p, q, sC));
TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] {});
}
use of org.apache.jena.graph.Graph in project jena by apache.
the class ReasonerTester method runTest.
/**
* Run a single designated test.
* @param uri the uri of the test, as defined in the manifest file
* @param reasoner the reasoner to be tested
* @param testcase the JUnit test case which is requesting this test
* @return true if the test passes
* @throws IOException if one of the test files can't be found
* @throws JenaException if the test can't be found or fails internally
*/
public boolean runTest(String uri, Reasoner reasoner, TestCase testcase) throws IOException {
// Find the specification for the named test
Resource test = testManifest.getResource(uri);
if (!test.hasProperty(RDF.type, testClass)) {
throw new JenaException("Can't find test: " + uri);
}
String description = test.getRequiredProperty(descriptionP).getObject().toString();
logger.debug("Reasoner test " + test.getURI() + " - " + description);
// Construct the inferred graph
Graph tbox = loadTestFile(test, tboxP);
Graph data = loadTestFile(test, dataP);
InfGraph graph = reasoner.bindSchema(tbox).bind(data);
// Run each query triple and accumulate the results
Graph queryG = loadTestFile(test, queryP);
Graph resultG = Factory.createGraphMem();
Iterator<Triple> queries = queryG.find(null, null, null);
while (queries.hasNext()) {
TriplePattern query = tripleToPattern(queries.next());
logger.debug("Query: " + query);
Iterator<Triple> answers = graph.find(query.asTripleMatch());
while (answers.hasNext()) {
Triple ans = answers.next();
logger.debug("ans: " + TriplePattern.simplePrintString(ans));
resultG.add(ans);
}
}
// Check the total result set against the correct answer
Graph correctG = loadTestFile(test, resultP);
boolean correct = correctG.isIsomorphicWith(resultG);
// ... end of debugging hack
if (testcase != null) {
Assert.assertTrue(description, correct);
}
return correct;
}
use of org.apache.jena.graph.Graph in project jena by apache.
the class SDBModelAssembler method open.
@Override
public Model open(Assembler a, Resource root, Mode mode) {
// Make a model.
// [] rdf:type sdb:Model ;
// sdb:dataset <dataset> ;
// sdb:graphName <someURI> .
// A model (graph) is a (dataset, name) pair where the name can be absent
// meaning the default graph of the dataset.
Resource dataset = GraphUtils.getResourceValue(root, AssemblerVocab.pDataset);
if (dataset == null)
throw new MissingException(root, "No dataset for model or graph");
StoreDesc storeDesc = datasetAssem.openStore(a, dataset, mode);
// Attempt to find a graph name - may be absent.
// Two names : "namedGraph" and "graphName"
String x = GraphUtils.getAsStringValue(root, AssemblerVocab.pNamedGraph1);
if (x == null)
x = GraphUtils.getAsStringValue(root, AssemblerVocab.pNamedGraph2);
// No name - default model.
Graph g = null;
if (x == null)
return SDBFactory.connectDefaultModel(storeDesc);
else
return SDBFactory.connectNamedModel(storeDesc, x);
}
use of org.apache.jena.graph.Graph in project jena by apache.
the class ListBase method listUnboundSubject.
private QueryIterator listUnboundSubject(Binding binding, Var listVar, Node predicate, PropFuncArg argObject, ExecutionContext execCxt) {
// Object?
if (argObject.isList()) {
List<Node> objectArgs = argObject.getArgList();
return execObjectList(binding, listVar, predicate, objectArgs, execCxt);
}
Node obj = argObject.getArg();
if (Var.isVar(obj)) {
Graph graph = execCxt.getActiveGraph();
Set<Node> x = GraphList.findAllLists(graph);
return allLists(binding, x, listVar, obj, argObject, execCxt);
}
// Subject unbound. Object a bound node.
return execObjectBound(binding, listVar, predicate, obj, execCxt);
}
Aggregations