use of edu.mit.simile.backstage.model.data.Database.PropertyRecord in project backstage by zepheira.
the class Path method computeOutput.
@Override
public ExpressionResult computeOutput(Database database, TupleQueryBuilder builder, Map<String, ValueExpr> variableValues, Map<String, String> variableTypes) throws ExpressionException {
String rootName = _rootVariable != null ? _rootVariable : "value";
ValueExpr valueExpr = variableValues.get(rootName);
String valueType = variableTypes.get(rootName);
for (PathSegment segment : _segments) {
if (valueExpr instanceof Var) {
Var input = (Var) valueExpr;
PropertyRecord record = database.getPropertyRecord(segment.propertyID);
if (record == null) {
throw new ExpressionException("No property can be aliased to '" + segment.propertyID + "'");
}
Var output = builder.makeVar("seg");
Var propertyVar = builder.makeVar("seg", record.uri);
if (segment.forward) {
builder.addTupleExpr(new StatementPattern(input, propertyVar, output));
valueType = record.valueType;
} else {
builder.addTupleExpr(new StatementPattern(output, propertyVar, input));
valueType = "item";
}
valueExpr = output;
} else {
throw new ExpressionException("Non-final set in a path must be a variable.");
}
}
return new ExpressionResult(valueExpr, valueType);
}
use of edu.mit.simile.backstage.model.data.Database.PropertyRecord 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);
}
}
Aggregations