Search in sources :

Example 1 with TreeModel

use of org.openrdf.model.impl.TreeModel 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 2 with TreeModel

use of org.openrdf.model.impl.TreeModel in project stanbol by apache.

the class SesameYard method find.

@Override
public final QueryResultList<Representation> find(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, true);
        //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>();
        Map<String, URI> bindings = new HashMap<String, URI>(query.getFieldVariableMappings().size());
        for (Entry<String, String> mapping : query.getFieldVariableMappings().entrySet()) {
            bindings.put(mapping.getValue(), sesameFactory.createURI(mapping.getKey()));
        }
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof URI) {
                URI subject = (URI) value;
                //link the result with the query result
                model.add(queryRoot, queryResult, subject);
                //now copy over the other selected data
                for (String binding : result.getBindingNames()) {
                    URI property = bindings.get(binding);
                    if (property != null) {
                        model.add(subject, property, result.getValue(binding));
                    }
                //else no mapping for the query.getRootVariableName()
                }
                //create a representation and add it to the results
                representations.add(valueFactory.createRdfRepresentation(subject));
            }
        //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) HashMap(java.util.HashMap) 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) 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 3 with TreeModel

use of org.openrdf.model.impl.TreeModel in project stanbol by apache.

the class ResourceAdapterTest method testFloat.

@Test
public void testFloat() {
    Model graph = new TreeModel();
    URI id = vf.createURI("http://www.example.org/test");
    URI floatTestField = vf.createURI("http://www.example.org/field/float");
    graph.add(id, floatTestField, vf.createLiteral(Float.NaN));
    graph.add(id, floatTestField, vf.createLiteral(Float.POSITIVE_INFINITY));
    graph.add(id, floatTestField, vf.createLiteral(Float.NEGATIVE_INFINITY));
    RdfValueFactory valueFactory = new RdfValueFactory(graph, vf);
    Representation r = valueFactory.createRepresentation(id.stringValue());
    Set<Float> expected = new HashSet<Float>(Arrays.asList(Float.NaN, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY));
    Iterator<Float> dit = r.get(floatTestField.stringValue(), Float.class);
    while (dit.hasNext()) {
        Float val = dit.next();
        Assert.assertNotNull(val);
        Assert.assertTrue(expected.remove(val));
    }
    Assert.assertTrue(expected.isEmpty());
}
Also used : TreeModel(org.openrdf.model.impl.TreeModel) TreeModel(org.openrdf.model.impl.TreeModel) Model(org.openrdf.model.Model) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfValueFactory(org.apache.stanbol.entityhub.model.sesame.RdfValueFactory) URI(org.openrdf.model.URI) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with TreeModel

use of org.openrdf.model.impl.TreeModel in project stanbol by apache.

the class ResourceAdapterTest method testDouble.

/**
     * Test related to STANBOL-698
     */
@Test
public void testDouble() {
    Model graph = new TreeModel();
    URI id = vf.createURI("http://www.example.org/test");
    URI doubleTestField = vf.createURI("http://www.example.org/field/double");
    graph.add(id, doubleTestField, vf.createLiteral(Double.NaN));
    graph.add(id, doubleTestField, vf.createLiteral(Double.POSITIVE_INFINITY));
    graph.add(id, doubleTestField, vf.createLiteral(Double.NEGATIVE_INFINITY));
    RdfValueFactory valueFactory = new RdfValueFactory(graph, vf);
    Representation r = valueFactory.createRepresentation(id.stringValue());
    Set<Double> expected = new HashSet<Double>(Arrays.asList(Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));
    Iterator<Double> dit = r.get(doubleTestField.stringValue(), Double.class);
    while (dit.hasNext()) {
        Double val = dit.next();
        Assert.assertNotNull(val);
        Assert.assertTrue(expected.remove(val));
    }
    Assert.assertTrue(expected.isEmpty());
}
Also used : TreeModel(org.openrdf.model.impl.TreeModel) TreeModel(org.openrdf.model.impl.TreeModel) Model(org.openrdf.model.Model) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfValueFactory(org.apache.stanbol.entityhub.model.sesame.RdfValueFactory) URI(org.openrdf.model.URI) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

RdfValueFactory (org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)4 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)4 Model (org.openrdf.model.Model)4 URI (org.openrdf.model.URI)4 TreeModel (org.openrdf.model.impl.TreeModel)4 HashSet (java.util.HashSet)2 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)2 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)2 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)2 Test (org.junit.Test)2 Value (org.openrdf.model.Value)2 BindingSet (org.openrdf.query.BindingSet)2 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)2 TupleQueryResult (org.openrdf.query.TupleQueryResult)2 RepositoryConnection (org.openrdf.repository.RepositoryConnection)2 RepositoryException (org.openrdf.repository.RepositoryException)2 HashMap (java.util.HashMap)1