Search in sources :

Example 21 with Triple

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));
}
Also used : Triple(org.apache.jena.graph.Triple) Test(org.junit.Test) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 22 with Triple

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));
}
Also used : Triple(org.apache.jena.graph.Triple) Quad(org.apache.jena.sparql.core.Quad) Test(org.junit.Test)

Example 23 with Triple

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);
            }
        }
    }
}
Also used : ReasonerException(org.apache.jena.reasoner.ReasonerException) Triple(org.apache.jena.graph.Triple) TriplePattern(org.apache.jena.reasoner.TriplePattern)

Example 24 with Triple

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));
        }
    }
}
Also used : Triple(org.apache.jena.graph.Triple)

Example 25 with Triple

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());
}
Also used : Triple(org.apache.jena.graph.Triple) Graph(org.apache.jena.graph.Graph) FileOutputStream(java.io.FileOutputStream)

Aggregations

Triple (org.apache.jena.graph.Triple)406 Test (org.junit.Test)139 Node (org.apache.jena.graph.Node)95 BaseTest (org.apache.jena.atlas.junit.BaseTest)66 Graph (org.apache.jena.graph.Graph)54 Quad (org.apache.jena.sparql.core.Quad)25 TriplePath (org.apache.jena.sparql.core.TriplePath)22 ArrayList (java.util.ArrayList)20 StatsMatcher (org.apache.jena.sparql.engine.optimizer.StatsMatcher)19 Var (org.apache.jena.sparql.core.Var)17 TripleWritable (org.apache.jena.hadoop.rdf.types.TripleWritable)15 TriplePattern (org.apache.jena.reasoner.TriplePattern)13 Op (org.apache.jena.sparql.algebra.Op)13 BasicPattern (org.apache.jena.sparql.core.BasicPattern)13 Model (org.apache.jena.rdf.model.Model)12 TransitiveGraphCache (org.apache.jena.reasoner.transitiveReasoner.TransitiveGraphCache)11 LongWritable (org.apache.hadoop.io.LongWritable)10 InfGraph (org.apache.jena.reasoner.InfGraph)10 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)10 QuadWritable (org.apache.jena.hadoop.rdf.types.QuadWritable)8