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());
}
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"));
}
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;
}
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;
}
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");
}
Aggregations