Search in sources :

Example 16 with IRI

use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.

the class SparqlSearcher method find.

@Override
public final QueryResultList<Representation> find(FieldQuery parsedQuery) throws IOException {
    long start = System.currentTimeMillis();
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    String sparqlQuery = query.toSparqlConstruct();
    long initEnd = System.currentTimeMillis();
    log.debug("  > InitTime: " + (initEnd - start));
    log.debug("  > SPARQL query:\n" + sparqlQuery);
    InputStream in = SparqlEndpointUtils.sendSparqlRequest(getQueryUri(), sparqlQuery, DEFAULT_RDF_CONTENT_TYPE);
    long queryEnd = System.currentTimeMillis();
    log.debug("  > QueryTime: " + (queryEnd - initEnd));
    if (in != null) {
        Graph graph;
        Graph rdfData = parser.parse(in, DEFAULT_RDF_CONTENT_TYPE, new IRI(getBaseUri()));
        if (rdfData instanceof Graph) {
            graph = (Graph) rdfData;
        } else {
            graph = new IndexedGraph(rdfData);
        }
        long parseEnd = System.currentTimeMillis();
        log.debug("  > ParseTime: " + (parseEnd - queryEnd));
        return new RdfQueryResultList(query, graph);
    } else {
        return null;
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfQueryResultList(org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList) InputStream(java.io.InputStream) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph)

Example 17 with IRI

use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.

the class ClerezzaModelWriter method addEntityTriplesToGraph.

/**
 * Adds the Triples that represent the Sign to the parsed graph. Note that
 * this method does not add triples for the representation. However it adds
 * the triple (sign,singRepresentation,representation)
 *
 * @param graph the graph to add the triples
 * @param sign the sign
 */
private void addEntityTriplesToGraph(Graph graph, Entity sign) {
    IRI id = new IRI(sign.getId());
    IRI metaId = new IRI(sign.getMetadata().getId());
    // add the FOAF triples between metadata and content
    graph.add(new TripleImpl(id, FOAF_PRIMARY_TOPIC_OF, metaId));
    graph.add(new TripleImpl(metaId, FOAF_PRIMARY_TOPIC, metaId));
    graph.add(new TripleImpl(metaId, RDF.type, FOAF_DOCUMENT));
    // add the site to the metadata
    // TODO: this should be the HTTP URI and not the id of the referenced site
    Literal siteName = literalFactory.createTypedLiteral(sign.getSite());
    graph.add(new TripleImpl(metaId, SIGN_SITE, siteName));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Literal(org.apache.clerezza.commons.rdf.Literal) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 18 with IRI

use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.

the class ClerezzaModelWriter method toRDF.

private Graph toRDF(QueryResultList<?> resultList) {
    final Graph resultGraph;
    Class<?> type = resultList.getType();
    if (String.class.isAssignableFrom(type)) {
        // create a new ImmutableGraph
        resultGraph = new IndexedGraph();
        for (Object result : resultList) {
            // add a triple to each reference in the result set
            resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, new IRI(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 RdfQueryResultList) {
            resultGraph = ((RdfQueryResultList) resultList).getResultGraph();
            if (isSignType) {
                // if we build a ResultList for Signs, that we need to do more things
                // first remove all triples representing results
                Iterator<Triple> resultTripleIt = resultGraph.filter(QUERY_RESULT_LIST, QUERY_RESULT, null);
                while (resultTripleIt.hasNext()) {
                    resultTripleIt.next();
                    resultTripleIt.remove();
                }
                // to the Sign IDs
                for (Object result : resultList) {
                    IRI signId = new IRI(((Entity) result).getId());
                    addEntityTriplesToGraph(resultGraph, (Entity) result);
                    resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, signId));
                }
            }
        } else {
            // any other implementation of the QueryResultList interface
            // create a new graph
            resultGraph = new IndexedGraph();
            if (Representation.class.isAssignableFrom(type)) {
                for (Object result : resultList) {
                    IRI resultId;
                    if (!isSignType) {
                        addRDFTo(resultGraph, (Representation) result);
                        resultId = new IRI(((Representation) result).getId());
                    } else {
                        addRDFTo(resultGraph, (Entity) result);
                        resultId = new IRI(((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(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, resultId));
                }
            }
        }
    }
    return resultGraph;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Triple(org.apache.clerezza.commons.rdf.Triple) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfQueryResultList(org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList) JSONObject(org.codehaus.jettison.json.JSONObject) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph)

Example 19 with IRI

use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.

the class ClerezzaYard 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);
    final ResultSet result = executeSparqlFieldQuery(query);
    // Note: An other possibility would be to first iterate over all results and add it to
    // a list and create this Iterator than based on the List. This would
    // be the preferenced way if changes in the graph could affect the
    // Iteration over the SPARQL query results.
    Iterator<Representation> representationIterator = new AdaptingIterator<SolutionMapping, Representation>(result, new AdaptingIterator.Adapter<SolutionMapping, Representation>() {

        /**
         * Adapter that gets the rootVariable of the Query (selecting the ID)
         * and creates a Representation for it.
         * @param solution a solution of the query
         * @param type the type (no generics here)
         * @return the representation or <code>null</code> if result is
         * not an IRI or there is no Representation for the result.
         */
        @Override
        public Representation adapt(SolutionMapping solution, Class<Representation> type) {
            RDFTerm resource = solution.get(query.getRootVariableName());
            if (resource instanceof IRI) {
                try {
                    return getRepresentation((IRI) resource, false);
                } catch (IllegalArgumentException e) {
                    log.warn("Unable to create Representation for ID " + resource + "! -> ignore query result");
                    return null;
                }
            } else {
                return null;
            }
        }
    }, Representation.class);
    // created before the method returns.
    return new QueryResultListImpl<Representation>(query, representationIterator, Representation.class);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) SolutionMapping(org.apache.clerezza.rdf.core.sparql.SolutionMapping) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)

Example 20 with IRI

use of org.apache.clerezza.commons.rdf.IRI in project stanbol by apache.

the class ClerezzaYard method activate.

/**
 * Internally used to activate the Yard. In case the Yard runs within a
 * OSGI container it is called by the {@link #activate(ComponentContext)}
 * Method. In case the Yard runs outside of an OSGI Container it is called
 * by the Constructor taking the {@link YardConfig} as parameter
 * @param config The configuration for the new Yard instance
 * @throws IllegalArgumentException In case <code>null</code> is parsed as
 * configuration or the configuration is invalid
 */
private final void activate(ClerezzaYardConfig config) throws IllegalArgumentException {
    super.activate(RdfValueFactory.getInstance(), SparqlFieldQueryFactory.getInstance(), config);
    if (tcManager == null) {
        // this will be the case if we are not in an OSGI environment
        // use the getInstance() method!
        tcManager = TcManager.getInstance();
    }
    this.yardGraphUri = config.getGraphUri();
    if (this.yardGraphUri == null) {
        // use default
        String yardUri = getUriPrefix();
        // remove the "." at the last position of the prefix
        this.yardGraphUri = new IRI(yardUri.substring(0, yardUri.length() - 2));
    }
    try {
        try {
            this.graph = tcManager.getImmutableGraph(yardGraphUri);
            immutable = true;
        } catch (NoSuchEntityException e) {
            this.graph = tcManager.getGraph(yardGraphUri);
            immutable = false;
        }
        log.info("  ... (re)use existing Graph {} for Yard {}", yardGraphUri, config.getName());
    } catch (NoSuchEntityException e) {
        log.info("   ... create new Graph {} for Yard {}", yardGraphUri, config.getName());
        this.graph = tcManager.createGraph(yardGraphUri);
    }
    if (context != null) {
        // within an OSGI environment
        // Register the graph with the Stanbol SPARQL endpoint (STANBOL-677)
        Dictionary<String, Object> graphRegProp = new Hashtable<String, Object>();
        graphRegProp.put("graph.uri", yardGraphUri.getUnicodeString());
        graphRegProp.put(Constants.SERVICE_RANKING, new Integer(-100));
        graphRegProp.put("graph.name", getConfig().getName());
        if (getConfig().getDescription() != null) {
            graphRegProp.put("graph.description", getConfig().getDescription());
        }
        graphRegistration = context.getBundleContext().registerService(Graph.class.getName(), graph, graphRegProp);
    }
// else do not register when running outside OSGI
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Hashtable(java.util.Hashtable) NoSuchEntityException(org.apache.clerezza.rdf.core.access.NoSuchEntityException)

Aggregations

IRI (org.apache.clerezza.commons.rdf.IRI)346 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)113 Graph (org.apache.clerezza.commons.rdf.Graph)109 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)104 Triple (org.apache.clerezza.commons.rdf.Triple)88 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)84 Test (org.junit.Test)78 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)58 HashSet (java.util.HashSet)50 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)46 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)39 HashMap (java.util.HashMap)38 IOException (java.io.IOException)37 ArrayList (java.util.ArrayList)37 Blob (org.apache.stanbol.enhancer.servicesapi.Blob)36 Literal (org.apache.clerezza.commons.rdf.Literal)35 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)31 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)29 Recipe (org.apache.stanbol.rules.base.api.Recipe)29 Language (org.apache.clerezza.commons.rdf.Language)24