use of org.openrdf.model.Value 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.Value in project backstage by zepheira.
the class ListFacet method createRestrictionClause.
protected ValueExpr createRestrictionClause(TupleQueryBuilder builder, String valueAsString, String valueType, ValueExpr input, ValueExpr previousClauses) {
Value value = stringToValue(valueAsString);
Compare compare = new Compare(input, builder.makeVar("v", value), CompareOp.EQ);
return previousClauses == null ? compare : new Or(previousClauses, compare);
}
use of org.openrdf.model.Value in project backstage by zepheira.
the class ListFacet method createComponentState.
protected List<FacetChoice> createComponentState(TupleQueryResult queryResult) throws QueryEvaluationException {
List<FacetChoice> facetChoices = new ArrayList<FacetChoice>();
Database database = _context.getDatabase();
while (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
Value value = bindingSet.getValue(_valueVar.getName());
Value count = bindingSet.getValue(_countVar.getName());
String s = valueToString(value);
int c = Integer.parseInt(count.stringValue());
FacetChoice fc = new FacetChoice();
fc._value = value;
fc._valueString = s;
fc._count = c;
fc._label = database.valueToLabel(value);
facetChoices.add(fc);
}
return facetChoices;
}
use of org.openrdf.model.Value in project backstage by zepheira.
the class DefaultLens method render.
@Override
public void render(URI item, Scriptable result, Database database, SailRepositoryConnection connection) {
result.put("label", result, database.getItemLabel(database.getItemId(item)));
try {
RepositoryResult<Statement> r = connection.getStatements(item, null, null, true, SailUtilities.noContext);
Map<URI, Set<Value>> propertyToValues = new HashMap<URI, Set<Value>>();
try {
while (r.hasNext()) {
Statement s = r.next();
URI predicate = s.getPredicate();
Set<Value> values = propertyToValues.get(predicate);
if (values == null) {
values = new HashSet<Value>();
propertyToValues.put(predicate, values);
}
values.add(s.getObject());
}
} finally {
r.close();
}
DefaultScriptableObject propertiesO = new DefaultScriptableObject();
for (URI property : propertyToValues.keySet()) {
String propertyID = database.getPropertyId(property);
PropertyRecord propertyRecord = database.getPropertyRecord(propertyID);
DefaultScriptableObject o = new DefaultScriptableObject();
o.put("propertyLabel", o, propertyRecord.label);
o.put("valueType", o, propertyRecord.valueType);
ScriptableArrayBuilder valueArrayBuilder = new ScriptableArrayBuilder();
Set<Value> values = propertyToValues.get(property);
if ("item".equals(propertyRecord.valueType)) {
for (Value value : values) {
if (value instanceof URI) {
URI itemURI = (URI) value;
String itemID = database.getItemId(itemURI);
DefaultScriptableObject v = new DefaultScriptableObject();
v.put("id", v, itemID);
v.put("label", v, database.getItemLabel(itemID));
valueArrayBuilder.add(v);
} else {
valueArrayBuilder.add(((Resource) value).stringValue());
}
}
} else {
for (Value value : values) {
valueArrayBuilder.add(Utilities.valueToString(value));
}
}
o.put("values", o, valueArrayBuilder.toArray());
propertiesO.put(propertyID, propertiesO, o);
}
result.put("propertyValues", result, propertiesO);
} catch (Exception e) {
_logger.error("Failed to generate default lens for item " + item, e);
}
}
use of org.openrdf.model.Value in project backstage by zepheira.
the class DomLensNode method generateContentAttributes.
protected void generateContentAttributes(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
ScriptableArrayBuilder arrayBuilder = new ScriptableArrayBuilder();
for (ContentAttribute a : _contentAttributes) {
DefaultScriptableObject o = new DefaultScriptableObject();
o.put("name", o, a.name);
try {
boolean first = true;
StringBuffer sb = new StringBuffer();
ExpressionQueryResult eqr = a.expression.computeOutputOnValue(value, database, connection);
if (eqr != null) {
TupleQueryResult queryResult = eqr.tupleQuery.evaluate();
try {
while (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
Value value2 = bindingSet.getValue(eqr.resultVar.getName());
if (first) {
first = false;
} else {
sb.append(";");
}
sb.append(renderInnerValueToText(value2, database, connection));
}
} finally {
queryResult.close();
}
}
o.put("value", o, sb.toString());
} catch (Exception e) {
_logger.error("", e);
}
arrayBuilder.add(o);
}
result.put("contentAttributes", result, arrayBuilder.toArray());
}
Aggregations