Search in sources :

Example 81 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class TestReasoners method testRDFSRemove.

/**
 * Test remove operations on an RDFS reasoner instance.
 * This is an example to test that rebing is invoked correctly rather
 * than an RDFS-specific test.
 */
public void testRDFSRemove() {
    InfModel m = ModelFactory.createRDFSModel(ModelFactory.createDefaultModel());
    String NS = PrintUtil.egNS;
    Property p = m.createProperty(NS, "p");
    Resource D = m.createResource(NS + "D");
    Resource i = m.createResource(NS + "i");
    Resource c = m.createResource(NS + "c");
    Resource d = m.createResource(NS + "d");
    p.addProperty(RDFS.domain, D);
    i.addProperty(p, c);
    i.addProperty(p, d);
    TestUtil.assertIteratorValues(this, i.listProperties(), new Object[] { m.createStatement(i, p, c), m.createStatement(i, p, d), m.createStatement(i, RDF.type, D), m.createStatement(i, RDF.type, RDFS.Resource) });
    i.removeAll(p);
    TestUtil.assertIteratorValues(this, i.listProperties(), new Object[] {});
}
Also used : Resource(org.apache.jena.rdf.model.Resource) InfModel(org.apache.jena.rdf.model.InfModel) Property(org.apache.jena.rdf.model.Property)

Example 82 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class ModelHelper method modelAdd.

/**
 * add to a model all the statements expressed by a string.
 *
 * Does not do any transaction manipulation.
 *
 * @param m
 *            the model to be updated
 * @param facts
 *            a sequence of semicolon-separated "S P O" facts
 * @return the updated model
 */
public static Model modelAdd(Model m, String facts) {
    StringTokenizer semis = new StringTokenizer(facts, ";");
    while (semis.hasMoreTokens()) {
        StringTokenizer st = new StringTokenizer(semis.nextToken());
        Resource sub = resource(st.nextToken());
        Property pred = property(st.nextToken());
        RDFNode obj = rdfNode(st.nextToken());
        m.add(sub, pred, obj);
    }
    return m;
}
Also used : StringTokenizer(java.util.StringTokenizer) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 83 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class TestAPI method testInitialBindingsConstruct2.

@Test
public void testInitialBindingsConstruct2() {
    QuerySolutionMap init = new QuerySolutionMap();
    init.add("o", m.createLiteral("x1"));
    String qs = "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }";
    try (QueryExecution qExec = QueryExecution.model(m).query(qs).initialBinding(init).build()) {
        Model r = qExec.execConstruct();
        assertTrue("Empty model", r.size() > 0);
        Property p1 = m.createProperty(ns + "p1");
        assertTrue("Empty model", r.contains(null, p1, init.get("x1")));
    }
}
Also used : Model(org.apache.jena.rdf.model.Model) Property(org.apache.jena.rdf.model.Property) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap) Test(org.junit.Test)

Example 84 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class TestAPI method testInitialBindingsConstruct1.

@Test
public void testInitialBindingsConstruct1() {
    QuerySolutionMap init = new QuerySolutionMap();
    init.add("z", m.createLiteral("zzz"));
    String qs = "CONSTRUCT {?s ?p ?z} {?s ?p 'x1'}";
    try (QueryExecution qExec = QueryExecution.model(m).query(qs).initialBinding(init).build()) {
        Model r = qExec.execConstruct();
        assertTrue("Empty model", r.size() > 0);
        Property p1 = m.createProperty(ns + "p1");
        assertTrue("Empty model", r.contains(null, p1, init.get("z")));
    }
}
Also used : Model(org.apache.jena.rdf.model.Model) Property(org.apache.jena.rdf.model.Property) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap) Test(org.junit.Test)

Example 85 with Property

use of org.apache.jena.rdf.model.Property in project jena by apache.

the class ExampleConstructQuads method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    // create testing data :
    // 1) default graph data
    Model model = ModelFactory.createDefaultModel();
    Resource s = model.createResource("http://eg.com/s");
    Property p = model.createProperty("http://eg.com/p");
    Resource o = model.createResource("http://eg.com/o");
    model.add(s, p, o);
    Dataset dataset = DatasetFactory.create(model);
    // 2) named graph data
    Model model1 = ModelFactory.createDefaultModel();
    Resource s1 = model.createResource("http://eg.com/s1");
    Property p1 = model.createProperty("http://eg.com/p1");
    Resource o1 = model.createResource("http://eg.com/o1");
    model1.add(s1, p1, o1);
    dataset.addNamedModel("http://eg.com/g1", model1);
    PrintUtil.printOut(dataset.asDatasetGraph().find());
    // construct named graph
    System.out.println("construct named graph:");
    String queryString = "CONSTRUCT { GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
    Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Iterator<Quad> quads = qexec.execConstructQuads();
        PrintUtil.printOut(quads);
    }
    System.out.println(query);
    // construct default graph 1
    System.out.println("construct default graph 1:");
    queryString = "CONSTRUCT {  {<http://eg.com/s1> <http://eg.com/p1> ?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Iterator<Quad> quads = qexec.execConstructQuads();
        PrintUtil.printOut(quads);
    }
    System.out.println(query);
    // construct default graph 2
    System.out.println("construct default graph 2:");
    queryString = "CONSTRUCT {<http://eg.com/s1> <http://eg.com/p1> ?o}  WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
    query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Iterator<Quad> quads = qexec.execConstructQuads();
        PrintUtil.printOut(quads);
    }
    System.out.println(query);
    // construct triples
    System.out.println("construct default graph 2:");
    queryString = "CONSTRUCT {<http://eg.com/s1> <http://eg.com/p1> ?o}  WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
    query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Iterator<Triple> triples = qexec.execConstructTriples();
        PrintUtil.printOut(triples);
    }
    System.out.println(query);
    // construct dataset
    System.out.println("construct dataset:");
    queryString = "CONSTRUCT { GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
    query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Dataset d = qexec.execConstructDataset();
        RDFDataMgr.write(System.out, d, Lang.TRIG);
    }
    System.out.println(query);
    // short form 1
    System.out.println("short form 1:");
    queryString = "CONSTRUCT WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
    query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Iterator<Quad> quads = qexec.execConstructQuads();
        PrintUtil.printOut(quads);
    }
    System.out.println(query);
    // short form 2
    System.out.println("short form 2:");
    queryString = "CONSTRUCT WHERE{ <http://eg.com/s> <http://eg.com/p> ?o }";
    query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Iterator<Quad> quads = qexec.execConstructQuads();
        PrintUtil.printOut(quads);
    }
    System.out.println(query);
    // short form 3
    System.out.println("short form 3:");
    queryString = "CONSTRUCT WHERE{ <http://eg.com/s> <http://eg.com/p> ?o }";
    query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Iterator<Triple> triples = qexec.execConstructTriples();
        PrintUtil.printOut(triples);
    }
    System.out.println(query);
    // short form 4
    System.out.println("short form 4:");
    queryString = "CONSTRUCT WHERE{ {<http://eg.com/s> <http://eg.com/p> ?o} }";
    query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Iterator<Quad> quads = qexec.execConstructQuads();
        PrintUtil.printOut(quads);
    }
    System.out.println(query);
    // short form 4
    System.out.println("default graph projection:");
    queryString = "CONSTRUCT { {<http://eg.com/s> <http://eg.com/p> ?o} GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
    query = QueryFactory.create(queryString, Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        Iterator<Triple> triples = qexec.execConstructTriples();
        PrintUtil.printOut(triples);
    }
    System.out.println(query);
    // run-construct-quad-test
    System.out.println("run-construct-quad-test:");
    File[] tests = new File("testing/ARQ/Syntax/Syntax-ARQ").listFiles((dir, fname) -> {
        if (fname.startsWith("syntax-quad-construct-")) {
            return true;
        }
        return false;
    });
    for (File test : tests) {
        System.out.println("======== File: " + test.getName());
        try (BufferedReader br = new BufferedReader(new FileReader(test))) {
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }
        System.out.println("==== Output of parse/print file " + test.getName());
        try {
            Query q = QueryFactory.read(test.getAbsolutePath(), Syntax.syntaxARQ);
            System.out.println(q);
        } catch (QueryParseException ex) {
            System.out.println("File " + test.getAbsolutePath() + " : " + ex.getMessage());
        }
    }
}
Also used : Quad(org.apache.jena.sparql.core.Quad) Resource(org.apache.jena.rdf.model.Resource) Triple(org.apache.jena.graph.Triple) Model(org.apache.jena.rdf.model.Model) Property(org.apache.jena.rdf.model.Property)

Aggregations

Property (org.apache.jena.rdf.model.Property)90 Resource (org.apache.jena.rdf.model.Resource)59 Model (org.apache.jena.rdf.model.Model)46 RDFNode (org.apache.jena.rdf.model.RDFNode)19 Test (org.junit.Test)18 Statement (org.apache.jena.rdf.model.Statement)17 ArrayList (java.util.ArrayList)14 StmtIterator (org.apache.jena.rdf.model.StmtIterator)14 Literal (org.apache.jena.rdf.model.Literal)11 Iterator (java.util.Iterator)6 Node (org.apache.jena.graph.Node)6 Triple (org.apache.jena.graph.Triple)6 QueryExecution (org.apache.jena.query.QueryExecution)6 OntModel (org.apache.jena.ontology.OntModel)5 Dataset (org.apache.jena.query.Dataset)5 Query (org.apache.jena.query.Query)4 InfModel (org.apache.jena.rdf.model.InfModel)4 Store (org.apache.jena.sdb.Store)4 IOException (java.io.IOException)3 LEGATO (legato.LEGATO)3