use of org.apache.jena.graph.Triple in project jena by apache.
the class TestAPI method testARQConstructQuad_a_1.
// // Execute a test both with and without regex optimization enabled
// // Check the number of results
// private void XexecRegexTest(int expected, String queryString)
// {
// Object b = ARQ.getContext().get(ARQ.enableRegexConstraintsOpt) ;
// try {
// ARQ.getContext().set(ARQ.enableRegexConstraintsOpt, "false") ;
// int count1 = queryAndCount(queryString) ;
// ARQ.getContext().set(ARQ.enableRegexConstraintsOpt, "true") ;
// int count2 = queryAndCount(queryString) ;
// assertEquals("Different number of results", count1, count2) ;
// if ( expected >= 0 )
// assertEquals("Unexpected number of results", expected, count1) ;
// } finally {
// ARQ.getContext().set(ARQ.enableRegexConstraintsOpt, b) ;
// }
// }
// ARQ Construct Quad Tests:
// Two types of query strings: a) construct triple string; b) construct quad string;
// Two kinds of query methods: 1) execTriples(); 2) execQuads();
// Test a)+1)
@Test
public void testARQConstructQuad_a_1() {
String queryString = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH ?g { ?s ?p ?o } }";
Query q = QueryFactory.create(queryString, Syntax.syntaxARQ);
QueryExecution qExec = QueryExecutionFactory.create(q, d);
Iterator<Triple> ts = qExec.execConstructTriples();
Model result = ModelFactory.createDefaultModel();
while (ts.hasNext()) {
Triple t = ts.next();
Statement stmt = ModelUtils.tripleToStatement(result, t);
if (stmt != null)
result.add(stmt);
}
assertEquals(3, result.size());
assertTrue(m.isIsomorphicWith(result));
}
use of org.apache.jena.graph.Triple in project jena by apache.
the class AbstractDatasetGraphFind method find_union_01.
@Test
public void find_union_01() {
List<Quad> x = toList(dsg.find(Quad.unionGraph, null, null, null));
assertEquals(3, x.size());
x.stream().allMatch(q -> q.getGraph().equals(Quad.unionGraph));
List<Triple> z = x.stream().map(Quad::asTriple).collect(Collectors.toList());
assertTrue(z.contains(q4.asTriple()));
assertTrue(z.contains(q5.asTriple()));
Quad qx = Quad.create(Quad.unionGraph, q4.asTriple());
assertTrue(x.contains(qx));
Quad qz = Quad.create(Quad.unionGraph, q2.asTriple());
assertFalse(x.contains(qz));
}
use of org.apache.jena.graph.Triple in project jena by apache.
the class RETEConflictSet method execute.
/**
* Execute a single rule firing.
*/
public static void execute(RETERuleContext context, boolean isAdd) {
Rule rule = context.getRule();
BindingEnvironment env = context.getEnv();
ForwardRuleInfGraphI infGraph = (ForwardRuleInfGraphI) context.getGraph();
if (infGraph.shouldTrace()) {
logger.info("Fired rule: " + rule.toShortString());
}
RETEEngine engine = context.getEngine();
engine.incRuleCount();
List<Triple> matchList = null;
if (infGraph.shouldLogDerivations() && isAdd) {
// Create derivation record
matchList = new ArrayList<>(rule.bodyLength());
for (int i = 0; i < rule.bodyLength(); i++) {
Object clause = rule.getBodyElement(i);
if (clause instanceof TriplePattern) {
matchList.add(env.instantiate((TriplePattern) clause));
}
}
}
for (int i = 0; i < rule.headLength(); i++) {
Object hClause = rule.getHeadElement(i);
if (hClause instanceof TriplePattern) {
Triple t = env.instantiate((TriplePattern) hClause);
// that we can't record in RDF
if (isAdd) {
if (!context.contains(t)) {
engine.addTriple(t, true);
if (infGraph.shouldLogDerivations()) {
infGraph.logDerivation(t, new RuleDerivation(rule, t, matchList, infGraph));
}
}
} else {
if (context.contains(t)) {
// Remove the generated triple
engine.deleteTriple(t, true);
}
}
// }
} else if (hClause instanceof Functor && isAdd) {
Functor f = (Functor) hClause;
Builtin imp = f.getImplementor();
if (imp != null) {
imp.headAction(f.getBoundArgs(env), f.getArgLength(), context);
} else {
throw new ReasonerException("Invoking undefined Functor " + f.getName() + " in " + rule.toShortString());
}
} else if (hClause instanceof Rule) {
Rule r = (Rule) hClause;
if (r.isBackward()) {
if (isAdd) {
infGraph.addBRule(r.instantiate(env));
} else {
infGraph.deleteBRule(r.instantiate(env));
}
} else {
throw new ReasonerException("Found non-backward subrule : " + r);
}
}
}
}
use of org.apache.jena.graph.Triple in project jena by apache.
the class MonitorModel method snapshot.
/**
* Compute the differences between the current monitored graph and the last
* snapshot. The changes will also be forwarded to any listeners.
* Then take a new snapshot.
* @param additions a place in which the set of newly added statements should be noted, can be null
* @param deletions a place in which the set of newly deleted statements should be noted, can be null
*/
public void snapshot(List<Statement> additions, List<Statement> deletions) {
List<Triple> additionsTemp = (additions != null) ? new ArrayList<>() : null;
List<Triple> deletionsTemp = (deletions != null) ? new ArrayList<>() : null;
((MonitorGraph) getGraph()).snapshot(additionsTemp, deletionsTemp);
if (additions != null) {
for (Triple anAdditionsTemp : additionsTemp) {
additions.add(this.asStatement(anAdditionsTemp));
}
}
if (deletions != null) {
for (Triple aDeletionsTemp : deletionsTemp) {
deletions.add(this.asStatement(aDeletionsTemp));
}
}
}
use of org.apache.jena.graph.Triple in project jena by apache.
the class AbstractTripleBlankNodeTests method writeTuples.
@Override
protected void writeTuples(File f, List<Triple> tuples) throws FileNotFoundException {
Graph g = GraphFactory.createGraphMem();
for (Triple t : tuples) {
g.add(t);
}
RDFDataMgr.write(new FileOutputStream(f), g, getLanguage());
}
Aggregations