Search in sources :

Example 1 with PropertyRecord

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);
}
Also used : ValueExpr(org.openrdf.query.algebra.ValueExpr) StatementPattern(org.openrdf.query.algebra.StatementPattern) PropertyRecord(edu.mit.simile.backstage.model.data.Database.PropertyRecord) Var(org.openrdf.query.algebra.Var)

Example 2 with PropertyRecord

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);
    }
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Statement(org.openrdf.model.Statement) URI(org.openrdf.model.URI) DefaultScriptableObject(edu.mit.simile.backstage.util.DefaultScriptableObject) PropertyRecord(edu.mit.simile.backstage.model.data.Database.PropertyRecord) Value(org.openrdf.model.Value)

Aggregations

PropertyRecord (edu.mit.simile.backstage.model.data.Database.PropertyRecord)2 DefaultScriptableObject (edu.mit.simile.backstage.util.DefaultScriptableObject)1 ScriptableArrayBuilder (edu.mit.simile.backstage.util.ScriptableArrayBuilder)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Statement (org.openrdf.model.Statement)1 URI (org.openrdf.model.URI)1 Value (org.openrdf.model.Value)1 StatementPattern (org.openrdf.query.algebra.StatementPattern)1 ValueExpr (org.openrdf.query.algebra.ValueExpr)1 Var (org.openrdf.query.algebra.Var)1