use of org.openrdf.model.Literal in project blueprints by tinkerpop.
the class SailTest method testGetLiteralsFromTripleStore.
@Test
public void testGetLiteralsFromTripleStore() throws Exception {
Literal l;
String prefix = "urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/";
XMLGregorianCalendar calendar;
ValueFactory vf = sail.getValueFactory();
SailConnection sc = sail.getConnection();
// Get an actual plain literal from the triple store.
URI ford = vf.createURI(prefix + "ford");
l = (Literal) toSet(sc.getStatements(ford, RDFS.COMMENT, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("he really knows where his towel is", l.getLabel());
assertNull(l.getDatatype());
URI thor = vf.createURI(prefix + "thor");
// Get an actual language-tagged literal from the triple store.
URI foafName = vf.createURI("http://xmlns.com/foaf/0.1/name");
Iterator<Statement> iter = toSet(sc.getStatements(thor, foafName, null, false)).iterator();
boolean found = false;
while (iter.hasNext()) {
l = (Literal) iter.next().getObject();
if (l.getLanguage().equals("en")) {
found = true;
assertEquals("Thor", l.getLabel());
assertNull(l.getDatatype());
}
// if (l.getLanguage().equals("is")) {
// found = true;
// assertEquals("?�r", l.getLabel());
// }
}
assertTrue(found);
// Get an actual data-typed literal from the triple-store.
URI msnChatID = vf.createURI("http://xmlns.com/foaf/0.1/msnChatID");
l = (Literal) toSet(sc.getStatements(thor, msnChatID, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("Thorster123", l.getLabel());
assertEquals(XMLSchema.STRING, l.getDatatype());
// Test Literal.xxxValue() methods for Literals read from the triple
// store
URI valueUri, hasValueUri;
hasValueUri = vf.createURI(prefix + "hasValue");
valueUri = vf.createURI(prefix + "stringValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("foo", l.getLabel());
assertEquals(XMLSchema.STRING, l.getDatatype());
valueUri = vf.createURI(prefix + "byteValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("99", l.getLabel());
assertEquals(XMLSchema.BYTE, l.getDatatype());
assertEquals((byte) 'c', l.byteValue());
valueUri = vf.createURI(prefix + "booleanValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("false", l.getLabel());
assertEquals(XMLSchema.BOOLEAN, l.getDatatype());
assertEquals(false, l.booleanValue());
valueUri = vf.createURI(prefix + "intValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("42", l.getLabel());
assertEquals(XMLSchema.INT, l.getDatatype());
assertEquals(42, l.intValue());
valueUri = vf.createURI(prefix + "shortValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("42", l.getLabel());
assertEquals(XMLSchema.SHORT, l.getDatatype());
assertEquals((short) 42, l.shortValue());
valueUri = vf.createURI(prefix + "longValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("42", l.getLabel());
assertEquals(XMLSchema.LONG, l.getDatatype());
assertEquals(42l, l.longValue());
valueUri = vf.createURI(prefix + "floatValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("3.1415926", l.getLabel());
assertEquals(XMLSchema.FLOAT, l.getDatatype());
assertEquals((float) 3.1415926, l.floatValue());
valueUri = vf.createURI(prefix + "doubleValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("3.1415926", l.getLabel());
assertEquals(XMLSchema.DOUBLE, l.getDatatype());
assertEquals(3.1415926, l.doubleValue());
valueUri = vf.createURI(prefix + "dateTimeValue");
calendar = XMLDatatypeUtil.parseCalendar("2002-10-10T12:00:00-05:00");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("2002-10-10T12:00:00-05:00", l.getLabel());
assertEquals(XMLSchema.DATETIME, l.getDatatype());
assertEquals(calendar, l.calendarValue());
sc.rollback();
sc.close();
}
use of org.openrdf.model.Literal in project blueprints by tinkerpop.
the class SailTest method testEvaluate.
// tuple queries ///////////////////////////////////////////////////////////
@Test
public void testEvaluate() throws Exception {
Set<String> languages;
String prefix = "urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/";
URI thorUri = sail.getValueFactory().createURI(prefix + "thor");
SailConnection sc = sail.getConnection();
try {
sc.begin();
URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
sc.addStatement(uriA, uriB, uriC);
sc.commit();
sc.begin();
SPARQLParser parser = new SPARQLParser();
BindingSet bindings = new EmptyBindingSet();
String baseURI = "http://example.org/bogus/";
String queryStr;
ParsedQuery query;
CloseableIteration<? extends BindingSet, QueryEvaluationException> results;
int count;
// s ?p ?o SELECT
queryStr = "SELECT ?y ?z WHERE { <http://example.org/uriA> ?y ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI y = (URI) set.getValue("y");
Value z = (Value) set.getValue("z");
assertNotNull(y);
assertNotNull(z);
// System.out.println("y = " + y + ", z = " + z);
}
results.close();
assertTrue(count > 0);
// s p ?o SELECT using a namespace prefix
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z WHERE { <" + prefix + "thor> foaf:name ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
languages = new HashSet<String>();
while (results.hasNext()) {
count++;
BindingSet set = results.next();
Literal z = (Literal) set.getValue("z");
assertNotNull(z);
languages.add(z.getLanguage());
}
results.close();
assertTrue(count > 0);
assertEquals(2, languages.size());
assertTrue(languages.contains("en"));
assertTrue(languages.contains("is"));
// ?s p o SELECT using a plain literal value with no language tag
queryStr = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "SELECT ?s WHERE { ?s rdfs:comment \"he really knows where his towel is\" }";
URI fordUri = sail.getValueFactory().createURI(prefix + "ford");
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI s = (URI) set.getValue("s");
assertNotNull(s);
assertEquals(s, fordUri);
}
results.close();
assertTrue(count > 0);
// ?s p o SELECT using a language-specific literal value
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\"@en }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI s = (URI) set.getValue("s");
assertNotNull(s);
assertEquals(s, thorUri);
}
results.close();
assertTrue(count > 0);
// The language tag is necessary
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\" }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
results.next();
}
results.close();
assertEquals(0, count);
// ?s p o SELECT using a typed literal value
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\"^^xsd:string }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI s = (URI) set.getValue("s");
assertNotNull(s);
assertEquals(s, thorUri);
}
results.close();
assertTrue(count > 0);
// The data type is necessary
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\" }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
results.next();
}
results.close();
assertEquals(0, count);
// s ?p o SELECT
// TODO: commented out languages for now
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?p WHERE { <" + prefix + "thor> ?p \"Thor\"@en }";
query = parser.parseQuery(queryStr, baseURI);
URI foafNameUri = sail.getValueFactory().createURI("http://xmlns.com/foaf/0.1/name");
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI p = (URI) set.getValue("p");
assertNotNull(p);
assertEquals(p, foafNameUri);
}
results.close();
assertTrue(count > 0);
// context-specific SELECT
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <" + prefix + "ctx1>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
languages = new HashSet<String>();
while (results.hasNext()) {
count++;
BindingSet set = results.next();
Literal z = (Literal) set.getValue("z");
assertNotNull(z);
languages.add(z.getLanguage());
}
results.close();
assertTrue(count > 0);
assertEquals(2, languages.size());
assertTrue(languages.contains("en"));
assertTrue(languages.contains("is"));
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <http://example.org/emptycontext>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
results.next();
}
results.close();
assertEquals(0, count);
// s p o? select without and with inferencing
// TODO commented out waiting for inferencing
// queryStr =
// "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
// + "SELECT ?o\n"
// + "WHERE { <" + prefix + "instance1> rdf:type ?o }";
// query = parser.parseQuery(queryStr, baseURI);
// results = sc.evaluate(query.getTupleExpr(), query.getDataset(),
// bindings, false);
// count = 0;
// while (results.hasNext()) {
// count++;
// BindingSet set = results.next();
// URI o = (URI) set.getValue("o");
// assertEquals(prefix + "classB", o.toString());
// }
// results.close();
// assertEquals(1, count);
// results = sc.evaluate(query.getTupleExpr(), query.getDataset(),
// bindings, true);
// count = 0;
// boolean foundA = false, foundB = false;
// while (results.hasNext()) {
// count++;
// BindingSet set = results.next();
// URI o = (URI) set.getValue("o");
// String s = o.toString();
// if (s.equals(prefix + "classA")) {
// foundA = true;
// } else if (s.equals(prefix + "classB")) {
// foundB = true;
// }
// }
// results.close();
// assertEquals(2, count);
// assertTrue(foundA);
// assertTrue(foundB);
} finally {
sc.rollback();
sc.close();
}
}
use of org.openrdf.model.Literal in project blueprints by tinkerpop.
the class SailTest method testGetStatementsP_SOG.
@Test
public void testGetStatementsP_SOG() throws Exception {
SailConnection sc = sail.getConnection();
try {
sc.begin();
URI uriA = sail.getValueFactory().createURI("http://example.org/test/P_SOG#a");
URI uriB = sail.getValueFactory().createURI("http://example.org/test/P_SOG#b");
URI uriC = sail.getValueFactory().createURI("http://example.org/test/P_SOG#c");
URI foo = sail.getValueFactory().createURI("http://example.org/ns#foo");
URI firstName = sail.getValueFactory().createURI("http://example.org/ns#firstName");
Literal plainLitA = sail.getValueFactory().createLiteral("arbitrary plain literal 238445");
Literal fooLabel = sail.getValueFactory().createLiteral("foo", XMLSchema.STRING);
int before, after;
// Add statement to the implicit null context.
sc.removeStatements(null, uriA, null);
sc.commit();
sc.begin();
before = countStatements(sc, null, uriA, null, false);
sc.addStatement(uriB, uriA, uriC);
sc.commit();
sc.begin();
after = countStatements(sc, null, uriA, null, false);
assertEquals(0, before);
assertEquals(1, after);
// Add plain literal statement to the default context.
sc.removeStatements(null, uriA, null);
sc.commit();
sc.begin();
before = countStatements(sc, null, uriA, null, false);
sc.addStatement(uriA, uriA, plainLitA);
sc.addStatement(uriA, uriB, plainLitA);
sc.addStatement(uriB, uriB, plainLitA);
sc.commit();
sc.begin();
after = countStatements(sc, null, uriA, null, false);
assertEquals(0, before);
assertEquals(1, after);
// Add string-typed literal statement to the default context.
sc.removeStatements(null, firstName, null);
sc.commit();
sc.begin();
before = countStatements(sc, null, firstName, null, false);
sc.addStatement(foo, firstName, fooLabel);
sc.commit();
sc.begin();
after = countStatements(sc, null, firstName, null, false);
assertEquals(0, before);
assertEquals(1, after);
assertEquals(foo, toSet(sc.getStatements(null, firstName, null, false)).iterator().next().getSubject());
// Add statement to a non-null context.
sc.removeStatements(null, uriA, null);
sc.commit();
sc.begin();
before = countStatements(sc, null, uriA, null, false);
sc.addStatement(uriB, uriA, uriC, uriA);
sc.commit();
sc.begin();
after = countStatements(sc, null, uriA, null, false);
assertEquals(0, before);
assertEquals(1, after);
sc.removeStatements(null, uriA, null);
sc.commit();
sc.begin();
before = countStatements(sc, null, uriA, null, false);
sc.addStatement(uriB, uriA, uriC, uriC);
sc.addStatement(uriC, uriA, uriA, uriA);
sc.commit();
sc.begin();
sc.addStatement(uriA, uriA, uriB, uriB);
sc.commit();
sc.begin();
after = countStatements(sc, null, uriA, null, false);
assertEquals(0, before);
assertEquals(3, after);
} finally {
sc.rollback();
sc.close();
}
}
use of org.openrdf.model.Literal in project blueprints by tinkerpop.
the class SailTest method testCreateLiteralsThroughValueFactory.
// URIs ////////////////////////////////////////////////////////////////////
// literals ////////////////////////////////////////////////////////////////
// Note: this test will always pass as long as we're using ValueFactoryImpl
@Test
public void testCreateLiteralsThroughValueFactory() throws Exception {
Literal l;
ValueFactory vf = sail.getValueFactory();
l = vf.createLiteral("a plain literal");
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("a plain literal", l.getLabel());
assertNull(l.getDatatype());
l = vf.createLiteral("auf Deutsch, bitte", "de");
assertNotNull(l);
assertEquals("de", l.getLanguage());
assertEquals("auf Deutsch, bitte", l.getLabel());
assertNull(l.getDatatype());
// Test data-typed createLiteral methods
l = vf.createLiteral("foo", XMLSchema.STRING);
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("foo", l.getLabel());
assertEquals(XMLSchema.STRING, l.getDatatype());
l = vf.createLiteral(42);
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("42", l.getLabel());
assertEquals(42, l.intValue());
assertEquals(XMLSchema.INT, l.getDatatype());
l = vf.createLiteral(42l);
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("42", l.getLabel());
assertEquals(42l, l.longValue());
assertEquals(XMLSchema.LONG, l.getDatatype());
l = vf.createLiteral((short) 42);
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("42", l.getLabel());
assertEquals((short) 42, l.shortValue());
assertEquals(XMLSchema.SHORT, l.getDatatype());
l = vf.createLiteral(true);
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("true", l.getLabel());
assertEquals(true, l.booleanValue());
assertEquals(XMLSchema.BOOLEAN, l.getDatatype());
l = vf.createLiteral((byte) 'c');
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("99", l.getLabel());
assertEquals((byte) 'c', l.byteValue());
assertEquals(XMLSchema.BYTE, l.getDatatype());
XMLGregorianCalendar calendar = XMLDatatypeUtil.parseCalendar("2002-10-10T12:00:00-05:00");
l = vf.createLiteral(calendar);
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("2002-10-10T12:00:00-05:00", l.getLabel());
assertEquals(calendar, l.calendarValue());
assertEquals(XMLSchema.DATETIME, l.getDatatype());
}
use of org.openrdf.model.Literal in project backstage by zepheira.
the class DomLensNode method testCondition.
protected boolean testCondition(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
if (_conditionTest == null) {
return true;
}
boolean r = false;
try {
ExpressionQueryResult eqr = _conditionExpression.computeOutputOnValue(value, database, connection);
if (eqr != null) {
TupleQueryResult queryResult = eqr.tupleQuery.evaluate();
try {
if ("if-exists".equals(_conditionTest)) {
r = queryResult.hasNext();
} else if ("if".equals(_conditionTest)) {
if (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
Value value2 = bindingSet.getValue(eqr.resultVar.getName());
if (value2 instanceof Literal) {
r = ((Literal) value2).booleanValue();
}
}
}
} finally {
queryResult.close();
}
} else {
if (value instanceof Literal) {
r = ((Literal) value).booleanValue();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.put("condition", result, r);
return r;
}
Aggregations