Search in sources :

Example 1 with Model

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

the class RdfRepresentationTest method testBNodeFiltering.

/**
     * Test for STANBOL-1301
     */
@Test
public void testBNodeFiltering() {
    URI concept = new URIImpl("http://example.org/mySkos#Concept123");
    Representation r = createRepresentation(concept.stringValue());
    assertTrue(r instanceof RdfRepresentation);
    RdfRepresentation rep = (RdfRepresentation) r;
    //add the example as listed in STANBOL-1301 to directly to the
    //Sesame Model backing the created Representation
    Model m = rep.getModel();
    m.add(concept, RDF.TYPE, SKOS.CONCEPT);
    m.add(concept, DCTERMS.IDENTIFIER, new LiteralImpl("123"));
    m.add(concept, SKOS.PREF_LABEL, new LiteralImpl("Concept123", "en"));
    BNode note1 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19576");
    m.add(concept, SKOS.SCOPE_NOTE, note1);
    m.add(note1, DCTERMS.CREATOR, new LiteralImpl("User1"));
    m.add(note1, DCTERMS.CREATED, new LiteralImpl("2013-03-03T02:02:02Z", XMLSchema.DATETIME));
    m.add(note1, RDFS.COMMENT, new LiteralImpl("The scope of this example global", "en"));
    BNode note2 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19634");
    m.add(concept, SKOS.SCOPE_NOTE, note2);
    m.add(note2, DCTERMS.CREATOR, new LiteralImpl("User2"));
    m.add(note2, DCTERMS.CREATED, new LiteralImpl("2013-03-03T04:04:04Z", XMLSchema.DATETIME));
    m.add(note2, RDFS.COMMENT, new LiteralImpl("Der Geltungsbereich ist Global", "de"));
    //now assert that BNodes are not reported via the Representation API
    Iterator<Object> scopeNotes = rep.get(SKOS.SCOPE_NOTE.stringValue());
    assertFalse(scopeNotes.hasNext());
    Iterator<Reference> scopeNoteRefs = rep.getReferences(SKOS.SCOPE_NOTE.stringValue());
    assertFalse(scopeNoteRefs.hasNext());
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) BNode(org.openrdf.model.BNode) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Model(org.openrdf.model.Model) URIImpl(org.openrdf.model.impl.URIImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) URI(org.openrdf.model.URI) BNodeImpl(org.openrdf.model.impl.BNodeImpl) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 2 with Model

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

the class SesameYard method createRepresentationGraph.

/**
     * Extracts the triples that belong to the {@link Representation} with the
     * parsed id from the Sesame repository.
     * @param con the repository connection
     * @param valueFactory the {@link RdfValueFactory} to use
     * @param uri the subject of the Representation to extract
     * @return the representation with the extracted data.
     * @throws RepositoryException 
     */
protected RdfRepresentation createRepresentationGraph(RepositoryConnection con, RdfValueFactory valueFactory, URI uri) throws RepositoryException {
    RdfRepresentation rep = valueFactory.createRdfRepresentation(uri);
    Model model = rep.getModel();
    extractRepresentation(con, model, uri, new HashSet<BNode>());
    return rep;
}
Also used : BNode(org.openrdf.model.BNode) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Model(org.openrdf.model.Model) TreeModel(org.openrdf.model.impl.TreeModel)

Example 3 with Model

use of org.openrdf.model.Model 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 4 with Model

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

the class SesameModelWriter method toRDF.

private Model toRDF(QueryResultList<?> resultList) {
    final Model resultGraph;
    Class<?> type = resultList.getType();
    if (String.class.isAssignableFrom(type)) {
        //create a new ImmutableGraph
        resultGraph = new LinkedHashModel();
        for (Object result : resultList) {
            //add a triple to each reference in the result set
            resultGraph.add(QUERY_RESULT_LIST, QUERY_RESULT, sesameFactory.createURI(result.toString()));
        }
    } else {
        //first determine the type of the resultList
        final boolean isSignType;
        if (Representation.class.isAssignableFrom(type)) {
            isSignType = false;
        } else if (Representation.class.isAssignableFrom(type)) {
            isSignType = true;
        } else {
            //incompatible type -> throw an Exception
            throw new IllegalArgumentException("Parsed type " + type + " is not supported");
        }
        //special treatment for RdfQueryResultList for increased performance
        if (resultList instanceof SesameQueryResultList) {
            resultGraph = ((SesameQueryResultList) resultList).getModel();
            if (isSignType) {
                //if we build a ResultList for Signs, we need to do more things
                //first remove all triples representing results
                resultGraph.filter(null, QUERY_RESULT, null).clear();
                //to the Sign IDs
                for (Object result : resultList) {
                    URI signId = sesameFactory.createURI(((Entity) result).getId());
                    addEntityTriplesToGraph(resultGraph, (Entity) result);
                    resultGraph.add(QUERY_RESULT_LIST, QUERY_RESULT, signId);
                }
            }
        } else {
            //any other implementation of the QueryResultList interface
            //create a new graph
            resultGraph = new LinkedHashModel();
            if (Representation.class.isAssignableFrom(type)) {
                for (Object result : resultList) {
                    URI resultId;
                    if (!isSignType) {
                        addRDFTo(resultGraph, (Representation) result);
                        resultId = sesameFactory.createURI(((Representation) result).getId());
                    } else {
                        addRDFTo(resultGraph, (Entity) result);
                        resultId = sesameFactory.createURI(((Entity) result).getId());
                    }
                    //Note: In case of Representation this Triple points to
                    //      the representation. In case of Signs it points to
                    //      the sign.
                    resultGraph.add(QUERY_RESULT_LIST, QUERY_RESULT, resultId);
                }
            }
        }
    }
    return resultGraph;
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) SesameQueryResultList(org.apache.stanbol.entityhub.yard.sesame.SesameQueryResultList) Model(org.openrdf.model.Model) LinkedHashModel(org.openrdf.model.impl.LinkedHashModel) JSONObject(org.codehaus.jettison.json.JSONObject) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) LinkedHashModel(org.openrdf.model.impl.LinkedHashModel) URI(org.openrdf.model.URI)

Example 5 with Model

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

the class SesameModelWriter method write.

@Override
public void write(QueryResultList<?> result, OutputStream out, MediaType mediaType) throws WebApplicationException, IOException {
    Model queryRdf = toRDF(result);
    //we need also to the JSON formatted FieldQuery as a literal to the
    //RDF data.
    FieldQuery query = result.getQuery();
    if (query != null) {
        try {
            JSONObject fieldQueryJson = FieldQueryToJsonUtils.toJSON(query, nsPrefixService);
            if (fieldQueryJson != null) {
                //add the triple with the fieldQuery
                queryRdf.add(QUERY_RESULT_LIST, FIELD_QUERY, sesameFactory.createLiteral(fieldQueryJson.toString()));
            }
        } catch (JSONException e) {
            log.warn(String.format("Unable to serialize Fieldquery '%s' to JSON! " + "Query response will not contain the serialized query.", query), e);
        }
    }
    //now serialise the data
    writeRdf(queryRdf, out, mediaType);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) JSONObject(org.codehaus.jettison.json.JSONObject) Model(org.openrdf.model.Model) LinkedHashModel(org.openrdf.model.impl.LinkedHashModel) JSONException(org.codehaus.jettison.json.JSONException)

Aggregations

Model (org.openrdf.model.Model)11 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)7 URI (org.openrdf.model.URI)7 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)6 TreeModel (org.openrdf.model.impl.TreeModel)5 RdfValueFactory (org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 BNode (org.openrdf.model.BNode)3 LinkedHashModel (org.openrdf.model.impl.LinkedHashModel)3 RepositoryConnection (org.openrdf.repository.RepositoryConnection)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)2 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)2 JSONObject (org.codehaus.jettison.json.JSONObject)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 RepositoryException (org.openrdf.repository.RepositoryException)2 HashMap (java.util.HashMap)1