Search in sources :

Example 1 with LinkedHashModel

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

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

the class SesameModelWriter method toRDF.

private Model toRDF(Entity entity) {
    Model graph = new LinkedHashModel();
    addRDFTo(graph, entity);
    return graph;
}
Also used : Model(org.openrdf.model.Model) LinkedHashModel(org.openrdf.model.impl.LinkedHashModel) LinkedHashModel(org.openrdf.model.impl.LinkedHashModel)

Aggregations

Model (org.openrdf.model.Model)2 LinkedHashModel (org.openrdf.model.impl.LinkedHashModel)2 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)1 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1 SesameQueryResultList (org.apache.stanbol.entityhub.yard.sesame.SesameQueryResultList)1 JSONObject (org.codehaus.jettison.json.JSONObject)1 URI (org.openrdf.model.URI)1