Search in sources :

Example 11 with Value

use of org.openrdf.model.Value in project stanbol by apache.

the class SesameYard method findRepresentation.

@Override
public QueryResultList<Representation> findRepresentation(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    RepositoryConnection con = null;
    TupleQueryResult results = null;
    try {
        con = repository.getConnection();
        con.begin();
        //execute the query
        int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
        results = executeSparqlFieldQuery(con, query, limit, false);
        //parse the results and generate the Representations
        //create an own valueFactors so that all the data of the query results
        //are added to the same Sesame Model
        Model model = new TreeModel();
        RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
        List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit) : new ArrayList<Representation>();
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof URI) {
                //copy all data to the model and create the representation
                RdfRepresentation rep = createRepresentationGraph(con, valueFactory, (URI) value);
                //link the result with the query result
                model.add(queryRoot, queryResult, value);
                representations.add(rep);
            }
        //ignore non URI results
        }
        con.commit();
        return new SesameQueryResultList(model, query, representations);
    } catch (RepositoryException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } catch (QueryEvaluationException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } finally {
        if (results != null) {
            //close the result if present
            try {
                results.close();
            } catch (QueryEvaluationException ignore) {
            /* ignore */
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            /* ignore */
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException) URI(org.openrdf.model.URI) TreeModel(org.openrdf.model.impl.TreeModel) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Model(org.openrdf.model.Model) TreeModel(org.openrdf.model.impl.TreeModel) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult) RdfValueFactory(org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)

Example 12 with Value

use of org.openrdf.model.Value in project stanbol by apache.

the class RdfRepresentation method remove.

@Override
public void remove(String field, Object parsedValue) {
    if (field == null) {
        throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
    } else if (field.isEmpty()) {
        throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
    }
    if (parsedValue == null) {
        log.warn("NULL parsed as value in remove method for symbol " + getId() + " and field " + field + " -> call ignored");
        return;
    }
    URI property = sesameFactory.createURI(field);
    Collection<Object> values = new ArrayList<Object>();
    ModelUtils.checkValues(factory, parsedValue, values);
    for (Object value : values) {
        if (value instanceof Value) {
            //native support for Sesame types!
            removeValue(property, (Value) value);
        } else if (value instanceof RdfWrapper) {
            //for Sesame RDF wrapper we can directly use the Value
            removeValue(property, ((RdfWrapper) value).getValue());
        } else if (value instanceof Reference) {
            removeValue(property, sesameFactory.createURI(((Reference) value).getReference()));
        } else if (value instanceof Text) {
            removeValue(property, sesameFactory.createLiteral(((Text) value).getText(), ((Text) value).getLanguage()));
        } else {
            //else add an typed Literal!
            removeValue(property, createTypedLiteral(value));
        }
    }
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ArrayList(java.util.ArrayList) Value(org.openrdf.model.Value) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) URI(org.openrdf.model.URI)

Example 13 with Value

use of org.openrdf.model.Value in project stanbol by apache.

the class RdfRepresentation method get.

@Override
@SuppressWarnings("unchecked")
public <T> Iterator<T> get(String field, final Class<T> type) throws UnsupportedTypeException {
    if (field == null) {
        throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
    } else if (field.isEmpty()) {
        throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
    }
    URI property = sesameFactory.createURI(field);
    //filter for values that are compatible with the parsed type
    Iterator<?> iterator = IteratorUtils.filteredIterator(IteratorUtils.transformedIterator(model.filter(subject, property, null).iterator(), // get the object from the statement
    objectTransFormer), new ValueTypeFilter<T>(type));
    if (!Value.class.isAssignableFrom(type)) {
        //if the requested type is not a Sesame value, we need also to
        //transform results
        iterator = IteratorUtils.transformedIterator(// the already filtered values
        iterator, // need to be transformed
        org.apache.stanbol.entityhub.model.sesame.ModelUtils.VALUE_TRANSFORMER);
    }
    return (Iterator<T>) iterator;
}
Also used : Value(org.openrdf.model.Value) Iterator(java.util.Iterator) URI(org.openrdf.model.URI)

Example 14 with Value

use of org.openrdf.model.Value in project vcell by virtualcell.

the class Model2JDOM method createElement.

protected Element createElement(Graph model, Resource resource) {
    Element element = null;
    Iterator<Statement> stmtIter = model.match(resource, RDF.TYPE, null);
    while (stmtIter.hasNext()) {
        Value object = stmtIter.next().getObject();
        if (object instanceof URI) {
            URI type = (URI) object;
            String nameSpaceURI = type.getNamespace();
            String localName = type.getLocalName();
            if (localName != null && localName.length() > 0 && nameSpaceURI != null && nameSpaceURI.length() > 0) {
                NameSpace ns = nsMap.provideNamesSpace(nameSpaceURI);
                element = new Element(localName, Namespace.getNamespace(ns.prefix, ns.uri));
                break;
            }
        }
    }
    if (element == null) {
        element = new Element(TYPELESS_NODE_NAME, nsRDF);
    }
    if (resource instanceof URI) {
        element.setAttribute("about", resource.stringValue(), nsRDF);
    } else {
        element.setAttribute("nodeID", blankNodeID(resource), nsRDF);
    }
    return element;
}
Also used : Statement(org.openrdf.model.Statement) Element(org.jdom.Element) Value(org.openrdf.model.Value) NameSpace(org.sbpax.schemas.util.NameSpace) URI(org.openrdf.model.URI)

Example 15 with Value

use of org.openrdf.model.Value in project vcell by virtualcell.

the class RefGroup method refs.

public Set<MIRIAMRef> refs(Graph graph) {
    Iterator<Statement> stmtIter = graph.match(getResource(), null, null);
    Set<MIRIAMRef> refs = new HashSet<MIRIAMRef>();
    while (stmtIter.hasNext()) {
        Statement statement = stmtIter.next();
        if (RDFBagUtil.isRDFContainerMembershipProperty(statement.getPredicate())) {
            Value object = statement.getObject();
            if (object instanceof URI) {
                URI resourceRef = (URI) object;
                try {
                    refs.add(MIRIAMRef.createFromURN(resourceRef.stringValue()));
                } catch (URNParseFailureException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return refs;
}
Also used : Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value) URI(org.openrdf.model.URI) URNParseFailureException(org.vcell.sybil.models.miriam.MIRIAMRef.URNParseFailureException) HashSet(java.util.HashSet)

Aggregations

Value (org.openrdf.model.Value)46 Statement (org.openrdf.model.Statement)22 URI (org.openrdf.model.URI)22 Resource (org.openrdf.model.Resource)17 HashSet (java.util.HashSet)11 BindingSet (org.openrdf.query.BindingSet)10 TupleQueryResult (org.openrdf.query.TupleQueryResult)8 BNode (org.openrdf.model.BNode)6 Literal (org.openrdf.model.Literal)6 ExpressionQueryResult (edu.mit.simile.backstage.model.data.ExpressionQueryResult)5 ScriptableArrayBuilder (edu.mit.simile.backstage.util.ScriptableArrayBuilder)5 HashMap (java.util.HashMap)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 DefaultScriptableObject (edu.mit.simile.backstage.util.DefaultScriptableObject)4 ArrayList (java.util.ArrayList)4 Graph (org.openrdf.model.Graph)4 SailException (org.openrdf.sail.SailException)4 Map (java.util.Map)3 Set (java.util.Set)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3