Search in sources :

Example 21 with Statement

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

the class ExTDB6 method main.

public static void main(String[] args) throws Exception {
    /// turn off the "No BGP optimizer warning"
    TDB.setOptimizerWarningFlag(false);
    final IRIFactory iriFactory = IRIFactory.semanticWebImplementation();
    final String DATASET_DIR_NAME = "data0";
    final Dataset data0 = TDBFactory.createDataset(DATASET_DIR_NAME);
    // show the currently registered names
    for (Iterator<String> it = data0.listNames(); it.hasNext(); ) {
        out.println("NAME=" + it.next());
    }
    out.println("getting named model...");
    /// this is the OWL portion
    final Model model = data0.getNamedModel(MY_NS);
    out.println("Model := " + model);
    out.println("getting graph...");
    /// this is the DATA in that MODEL
    final Graph graph = model.getGraph();
    out.println("Graph := " + graph);
    if (graph.isEmpty()) {
        final Resource product1 = model.createResource(iriFactory.construct(MY_NS + "product/1").toString());
        final Property hasName = model.createProperty(MY_NS, "#hasName");
        final Statement stmt = model.createStatement(product1, hasName, model.createLiteral("Beach Ball", "en"));
        out.println("Statement = " + stmt);
        model.add(stmt);
        // just for fun
        out.println("Triple := " + stmt.asTriple().toString());
    } else {
        out.println("Graph is not Empty; it has " + graph.size() + " Statements");
        long t0, t1;
        t0 = System.currentTimeMillis();
        final Query q = QueryFactory.create("PREFIX exns: <" + MY_NS + "#>\n" + "PREFIX exprod: <" + MY_NS + "product/>\n" + " SELECT * " + // +" WHERE { exprod:1 exns:hasName ?name }"
        " WHERE { ?res ?pred ?obj }");
        out.println("Query := " + q);
        t1 = System.currentTimeMillis();
        out.println("QueryFactory.TIME=" + (t1 - t0));
        t0 = System.currentTimeMillis();
        final QueryExecution qExec = QueryExecutionFactory.create(q, model);
        t1 = System.currentTimeMillis();
        out.println("QueryExecutionFactory.TIME=" + (t1 - t0));
        try {
            t0 = System.currentTimeMillis();
            ResultSet rs = qExec.execSelect();
            t1 = System.currentTimeMillis();
            out.println("executeSelect.TIME=" + (t1 - t0));
            while (rs.hasNext()) {
                QuerySolution sol = rs.next();
                out.println("Solution := " + sol);
                for (Iterator<String> names = sol.varNames(); names.hasNext(); ) {
                    final String name = names.next();
                    out.println("\t" + name + " := " + sol.get(name));
                }
            }
        } finally {
            qExec.close();
        }
    }
    out.println("closing graph");
    graph.close();
    out.println("closing model");
    model.close();
    //out.println("closing DataSetGraph");
    //dsg.close();
    out.println("closing DataSet");
    data0.close();
}
Also used : Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) Graph(org.apache.jena.graph.Graph) Model(org.apache.jena.rdf.model.Model) IRIFactory(org.apache.jena.iri.IRIFactory) Property(org.apache.jena.rdf.model.Property)

Example 22 with Statement

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

the class TestConcurrentAccess method mrswGraph1.

@Test
public void mrswGraph1() {
    Model m = create().getDefaultModel();
    Resource r = m.createResource("x");
    ExtendedIterator<Statement> iter1 = m.listStatements(r, null, (RDFNode) null);
    assertNotNull(iter1.next());
    ExtendedIterator<Statement> iter2 = m.listStatements(r, null, (RDFNode) null);
    assertNotNull(iter2.next());
    for (; iter2.hasNext(); ) iter2.next();
    assertNotNull(iter1.next());
}
Also used : Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Test(org.junit.Test) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 23 with Statement

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

the class TestConcurrentAccess method mrswGraph3.

@Test(expected = ConcurrentModificationException.class)
public void mrswGraph3() {
    Model m = create().getDefaultModel();
    Resource r = m.createResource("x");
    ExtendedIterator<Statement> iter1 = m.listStatements(r, null, (RDFNode) null);
    assertNotNull(iter1.next());
    Triple t = SSE.parseTriple("(<y> <p> 99)");
    m.getGraph().delete(t);
    // Bad
    iter1.hasNext();
}
Also used : Triple(org.apache.jena.graph.Triple) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Test(org.junit.Test) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 24 with Statement

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

the class StandardAnalyzerAssembler method toList.

private List<String> toList(Resource list) {
    List<String> result = new ArrayList<>();
    Resource current = list;
    while (current != null && !current.equals(RDF.nil)) {
        Statement stmt = current.getProperty(RDF.first);
        if (stmt == null) {
            throw new TextIndexException("stop word list not well formed");
        }
        RDFNode node = stmt.getObject();
        if (!node.isLiteral()) {
            throw new TextIndexException("stop word is not a literal : " + node);
        }
        result.add(((Literal) node).getLexicalForm());
        stmt = current.getProperty(RDF.rest);
        if (stmt == null) {
            throw new TextIndexException("stop word list not terminated by rdf:nil");
        }
        node = stmt.getObject();
        if (!node.isResource()) {
            throw new TextIndexException("stop word list node is not a resource : " + node);
        }
        current = (Resource) node;
    }
    return result;
}
Also used : TextIndexException(org.apache.jena.query.text.TextIndexException) Statement(org.apache.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 25 with Statement

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

the class TestInMemDatasetAssembler method directDataLinkToQuads.

@Test
public void directDataLinkToQuads() throws IOException {
    // first make a file of quads to load later
    final Model model = createDefaultModel();
    final Path quads = createTempFile("quadExample", ".nq");
    final Resource quadsURI = model.createResource(quads.toFile().toURI().toString());
    final Resource simpleExample = model.createResource("test:simpleExample");
    simpleExample.addProperty(type, DatasetAssemblerVocab.tDatasetTxnMem);
    simpleExample.addProperty(data, quadsURI);
    final DatasetGraph dsg = createTxnMem().asDatasetGraph();
    model.listStatements().mapWith(Statement::asTriple).mapWith(t -> new Quad(quadsURI.asNode(), t)).forEachRemaining(dsg::add);
    try (OutputStream out = new FileOutputStream(quads.toFile())) {
        write(out, dsg, NQUADS);
    }
    final Dataset dataset = assemble(simpleExample);
    final Model assembledDefaultModel = dataset.getDefaultModel();
    final Model assembledNamedModel = dataset.getNamedModel(quadsURI.getURI());
    assertTrue(assembledDefaultModel.isEmpty());
    assertTrue(assembledNamedModel.contains(assembledNamedModel.createStatement(simpleExample, data, quadsURI)));
}
Also used : Path(java.nio.file.Path) DEFAULT(org.apache.jena.assembler.Mode.DEFAULT) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) JA(org.apache.jena.assembler.JA) Model(org.apache.jena.rdf.model.Model) Statement(org.apache.jena.rdf.model.Statement) NTRIPLES(org.apache.jena.riot.RDFFormat.NTRIPLES) Quad(org.apache.jena.sparql.core.Quad) Resource(org.apache.jena.rdf.model.Resource) MemoryModel(org.apache.jena.assembler.JA.MemoryModel) Path(java.nio.file.Path) Dataset(org.apache.jena.query.Dataset) OutputStream(java.io.OutputStream) DatasetAssemblerVocab.pGraphName(org.apache.jena.sparql.core.assembler.DatasetAssemblerVocab.pGraphName) Iterator(java.util.Iterator) DatasetAssemblerVocab.pNamedGraph(org.apache.jena.sparql.core.assembler.DatasetAssemblerVocab.pNamedGraph) NQUADS(org.apache.jena.riot.Lang.NQUADS) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Test(org.junit.Test) RDFDataMgr.write(org.apache.jena.riot.RDFDataMgr.write) ModelFactory.createDefaultModel(org.apache.jena.rdf.model.ModelFactory.createDefaultModel) Files.createTempFile(java.nio.file.Files.createTempFile) Node(org.apache.jena.graph.Node) DatasetFactory.createTxnMem(org.apache.jena.query.DatasetFactory.createTxnMem) RDF.type(org.apache.jena.vocabulary.RDF.type) CannotConstructException(org.apache.jena.assembler.exceptions.CannotConstructException) JA.data(org.apache.jena.assembler.JA.data) Assert(org.junit.Assert) Quad(org.apache.jena.sparql.core.Quad) Statement(org.apache.jena.rdf.model.Statement) Dataset(org.apache.jena.query.Dataset) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Model(org.apache.jena.rdf.model.Model) MemoryModel(org.apache.jena.assembler.JA.MemoryModel) ModelFactory.createDefaultModel(org.apache.jena.rdf.model.ModelFactory.createDefaultModel) Resource(org.apache.jena.rdf.model.Resource) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Aggregations

Statement (org.apache.jena.rdf.model.Statement)38 Model (org.apache.jena.rdf.model.Model)18 Resource (org.apache.jena.rdf.model.Resource)15 Test (org.junit.Test)9 StmtIterator (org.apache.jena.rdf.model.StmtIterator)8 Triple (org.apache.jena.graph.Triple)7 ArrayList (java.util.ArrayList)6 BaseTest (org.apache.jena.atlas.junit.BaseTest)6 RDFNode (org.apache.jena.rdf.model.RDFNode)5 Property (org.apache.jena.rdf.model.Property)4 IOException (java.io.IOException)2 Dataset (org.apache.jena.query.Dataset)2 TextIndexException (org.apache.jena.query.text.TextIndexException)2 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 Files.createTempFile (java.nio.file.Files.createTempFile)1 Path (java.nio.file.Path)1