Search in sources :

Example 21 with Literal

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

the class UpdateBuilderTest method example1.

// testsbased on the examples
/*
	 * 	Example 1: Adding some triples to a graph

	This snippet describes two RDF triples to be inserted into the default graph of the Graph Store.

	PREFIX dc: <http://purl.org/dc/elements/1.1/>
	INSERT DATA
	{ 
	  <http://example/book1> dc:title "A new book" ;
	                         dc:creator "A.N.Other" .
	}

	Data before:

	# Default graph
	@prefix dc: <http://purl.org/dc/elements/1.1/> .
	@prefix ns: <http://example.org/ns#> .

	<http://example/book1> ns:price 42 .

	Data after:

	# Default graph
	@prefix dc: <http://purl.org/dc/elements/1.1/> .
	@prefix ns: <http://example.org/ns#> .

	<http://example/book1> ns:price 42 .
	<http://example/book1> dc:title "A new book" .
	<http://example/book1> dc:creator "A.N.Other" .

	 */
@Test
public void example1() {
    Node n = NodeFactory.createURI("http://example/book1");
    Node priceN = NodeFactory.createURI("http://example.org/ns#price");
    Node priceV = NodeFactory.createLiteral("42");
    UpdateBuilder builder = new UpdateBuilder().addPrefix("dc", DC_11.NS).addInsert(n, DC_11.title, "A new book").addInsert(n, DC_11.creator, "A.N.Other");
    List<Triple> triples = new ArrayList<Triple>();
    triples.add(new Triple(n, priceN, priceV));
    Graph g = new CollectionGraph(triples);
    Model m = ModelFactory.createModelForGraph(g);
    m.setNsPrefix("dc", DC_11.NS);
    m.setNsPrefix("ns", "http://example.org/ns#");
    UpdateAction.execute(builder.build(), m);
    Resource r = ResourceFactory.createResource(n.getURI());
    Property rPriceP = ResourceFactory.createProperty(priceN.getURI());
    Literal rPriceV = ResourceFactory.createPlainLiteral("42");
    assertTrue(m.contains(r, rPriceP, rPriceV));
    assertTrue(m.contains(r, DC_11.title, "A new book"));
    assertTrue(m.contains(r, DC_11.creator, "A.N.Other"));
    assertEquals(3, triples.size());
}
Also used : Triple(org.apache.jena.graph.Triple) Graph(org.apache.jena.graph.Graph) CollectionGraph(org.apache.jena.graph.impl.CollectionGraph) CollectionGraph(org.apache.jena.graph.impl.CollectionGraph) Node(org.apache.jena.graph.Node) Literal(org.apache.jena.rdf.model.Literal) ArrayList(java.util.ArrayList) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) UpdateBuilder(org.apache.jena.arq.querybuilder.UpdateBuilder) Property(org.apache.jena.rdf.model.Property) Test(org.junit.Test)

Example 22 with Literal

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

the class UpdateBuilderExampleTests method example10.

/**
	 * Example 10:
	 * 
	 * @see https://www.w3.org/TR/sparql11-update/#example_10
	 */
@Test
public void example10() {
    Resource book1 = ResourceFactory.createResource("http://example/book1");
    Resource book3 = ResourceFactory.createResource("http://example/book3");
    Resource book4 = ResourceFactory.createResource("http://example/book4");
    Literal d1996 = ResourceFactory.createTypedLiteral("1996-01-01T00:00:00-02:00", XSDDatatype.XSDdateTime);
    Literal d2000 = ResourceFactory.createTypedLiteral("2000-01-01T00:00:00-02:00", XSDDatatype.XSDdateTime);
    Node graphName1 = NodeFactory.createURI("http://example/bookStore");
    Node graphName2 = NodeFactory.createURI("http://example/bookStore2");
    Model m1 = ModelFactory.createDefaultModel();
    m1.add(book1, DC_11.title, "Fundamentals of Compiler Design");
    m1.add(book1, DC_11.date, d1996);
    m1.add(book1, RDF.type, DCTypes.PhysicalObject);
    m1.add(book3, DC_11.title, "SPARQL 1.1 Tutorial");
    Model m2 = ModelFactory.createDefaultModel();
    m2.add(book4, DC_11.title, "SPARQL 1.1 Tutorial");
    Dataset ds = DatasetFactory.create();
    ds.addNamedModel(graphName1.getURI(), m1);
    ds.addNamedModel(graphName2.getURI(), m2);
    ExprFactory factory = new ExprFactory();
    SelectBuilder ins = new SelectBuilder().addGraph(graphName2, new SelectBuilder().addWhere("?book", "?p", "?v"));
    SelectBuilder whr = new SelectBuilder().addGraph(graphName1, new SelectBuilder().addWhere("?book", DC_11.date, "?date").addFilter(factory.lt("?date", d2000)).addWhere("?book", "?p", "?v"));
    UpdateBuilder builder = new UpdateBuilder().addPrefix("dc", DC_11.NS).addPrefix("xsd", XSD.NS).addInsert(ins).addWhere(whr);
    UpdateRequest req = builder.buildRequest();
    builder = new UpdateBuilder().with(graphName1).addDelete("?book", "?p", "?v").addWhere("?book", DC_11.date, "?date").addWhere("?book", RDF.type, DCTypes.PhysicalObject).addFilter(factory.lt("?date", d2000)).addWhere("?book", "?p", "?v");
    builder.appendTo(req);
    UpdateAction.execute(req, ds);
    m1 = ds.getNamedModel(graphName1.getURI());
    assertEquals(1, m1.listStatements().toList().size());
    assertTrue(m1.contains(book3, DC_11.title, "SPARQL 1.1 Tutorial"));
    m2 = ds.getNamedModel(graphName2.getURI());
    assertEquals(4, m2.listStatements().toList().size());
    assertEquals(3, m2.listStatements(book1, null, (RDFNode) null).toList().size());
    assertTrue(m2.contains(book1, DC_11.title, "Fundamentals of Compiler Design"));
    assertTrue(m2.contains(book1, DC_11.date, d1996));
    assertTrue(m2.contains(book1, RDF.type, DCTypes.PhysicalObject));
    assertEquals(1, m2.listStatements(book4, null, (RDFNode) null).toList().size());
    assertTrue(m2.contains(book4, DC_11.title, "SPARQL 1.1 Tutorial"));
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) Dataset(org.apache.jena.query.Dataset) Literal(org.apache.jena.rdf.model.Literal) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Resource(org.apache.jena.rdf.model.Resource) Model(org.apache.jena.rdf.model.Model) UpdateBuilder(org.apache.jena.arq.querybuilder.UpdateBuilder) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Example 23 with Literal

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

the class RDFOutput method asRDF.

public Resource asRDF(Model model, boolean result) {
    Resource results = model.createResource();
    results.addProperty(RDF.type, ResultSetGraphVocab.ResultSet);
    Literal lit = model.createTypedLiteral(result);
    results.addProperty(ResultSetGraphVocab.p_boolean, lit);
    return results;
}
Also used : Literal(org.apache.jena.rdf.model.Literal) Resource(org.apache.jena.rdf.model.Resource)

Example 24 with Literal

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

the class ResultSetUtils method resultSetToStringList.

/**
     * Extracts a List filled with the binding of selectElement variable for each
     * query solution, turned into a string (URIs or lexical forms).  
     * Exhausts the result set.  Create a rewindable one to use multiple times. 
     * @see org.apache.jena.query.ResultSetFactory
     */
public static List<String> resultSetToStringList(ResultSet rs, String selectElement, String literalOrResource) {
    // feature suggested by James Howison
    List<String> items = new ArrayList<>();
    while (rs.hasNext()) {
        QuerySolution qs = rs.nextSolution();
        RDFNode rn = qs.get(selectElement);
        if (rn.isLiteral())
            items.add(((Literal) rn).getLexicalForm());
        else if (rn.isURIResource())
            items.add(((Resource) rn).getURI());
        else if (rn.isAnon()) {
            items.add(((Resource) rn).getId().getLabelString());
        } else
            throw new ARQException("Unknow thing in results : " + rn);
    }
    return items;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ARQException(org.apache.jena.sparql.ARQException) Literal(org.apache.jena.rdf.model.Literal) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 25 with Literal

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

the class qtest method oneManifestEarl.

static void oneManifestEarl(String testManifest) {
    String name = "ARQ";
    String releaseName = "ARQ";
    String version = "2.9.1";
    String homepage = "http://jena.apache.org/";
    // Null for bNode.
    String systemURI = "http://jena.apache.org/#arq";
    // Include information later.
    EarlReport report = new EarlReport(systemURI, name, version, homepage);
    ScriptTestSuiteFactory.results = report;
    Model model = report.getModel();
    model.setNsPrefix("dawg", TestManifest.getURI());
    // Update the EARL report. 
    Resource jena = model.createResource().addProperty(FOAF.homepage, model.createResource("http://jena.apache.org/"));
    // ARQ is part fo Jena.
    Resource arq = report.getSystem().addProperty(DCTerms.isPartOf, jena);
    // Andy wrote the test software (updates the thing being tested as well as they are the same). 
    Resource who = model.createResource(FOAF.Person).addProperty(FOAF.name, "Andy Seaborne").addProperty(FOAF.homepage, model.createResource("http://people.apache.org/~andy"));
    Resource reporter = report.getReporter();
    reporter.addProperty(DC.creator, who);
    model.setNsPrefix("doap", DOAP.getURI());
    model.setNsPrefix("xsd", XSD.getURI());
    // DAWG specific stuff.
    Resource system = report.getSystem();
    system.addProperty(RDF.type, DOAP.Project);
    system.addProperty(DOAP.name, name);
    system.addProperty(DOAP.homepage, homepage);
    system.addProperty(DOAP.maintainer, who);
    Resource release = model.createResource(DOAP.Version);
    system.addProperty(DOAP.release, release);
    Node today_node = NodeFactoryExtra.todayAsDate();
    Literal today = model.createTypedLiteral(today_node.getLiteralLexicalForm(), today_node.getLiteralDatatype());
    release.addProperty(DOAP.created, today);
    // Again
    release.addProperty(DOAP.name, releaseName);
    TestSuite suite = ScriptTestSuiteFactory.make(testManifest);
    SimpleTestRunner.runSilent(suite);
    ScriptTestSuiteFactory.results.getModel().write(System.out, "TTL");
}
Also used : TestSuite(junit.framework.TestSuite) ARQTestSuite(org.apache.jena.sparql.ARQTestSuite) Node(org.apache.jena.graph.Node) Literal(org.apache.jena.rdf.model.Literal) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) EarlReport(org.apache.jena.sparql.junit.EarlReport)

Aggregations

Literal (org.apache.jena.rdf.model.Literal)42 Resource (org.apache.jena.rdf.model.Resource)17 Test (org.junit.Test)15 Model (org.apache.jena.rdf.model.Model)12 Property (org.apache.jena.rdf.model.Property)9 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)7 RDFNode (org.apache.jena.rdf.model.RDFNode)7 Node (org.apache.jena.graph.Node)6 HashSet (java.util.HashSet)4 QuerySolution (org.apache.jena.query.QuerySolution)4 Store (org.apache.jena.sdb.Store)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)2 SecuredLiteral (org.apache.jena.permissions.model.SecuredLiteral)2 Dataset (org.apache.jena.query.Dataset)2 Query (org.apache.jena.query.Query)2 QueryExecution (org.apache.jena.query.QueryExecution)2 ResultSet (org.apache.jena.query.ResultSet)2