Search in sources :

Example 21 with ImmutableGraph

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

the class ClerezzaYard 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);
    int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
    Query sparqlQuery;
    //NOTE:
    // - use the endpoint type standard, because we do not know what type of
    //   SPARQL implementation is configured for Clerezza via OSGI
    String sparqlQueryString = SparqlQueryUtils.createSparqlConstructQuery(query, limit, EndpointTypeEnum.Standard);
    try {
        sparqlQuery = QueryParser.getInstance().parse(sparqlQueryString);
    } catch (ParseException e) {
        log.error("ParseException for SPARQL Query in findRepresentation");
        log.error("FieldQuery: " + query);
        log.error("SPARQL Query: " + sparqlQueryString);
        throw new YardException("Unable to parse SPARQL query generated for the parse FieldQuery", e);
    }
    Object resultObject = tcManager.executeSparqlQuery(sparqlQuery, graph);
    final Graph resultGraph;
    if (resultObject instanceof Graph) {
        resultGraph = (Graph) resultObject;
    } else if (resultObject instanceof ImmutableGraph) {
        resultGraph = new IndexedGraph();
        resultGraph.addAll((ImmutableGraph) resultObject);
    } else {
        log.error("Unable to create " + Graph.class + " instance for query reults of type " + resultObject.getClass() + " (this indicates that the used SPARQL Query was not of type CONSTRUCT)");
        log.error("FieldQuery: " + query);
        log.error("SPARQL Query: " + sparqlQueryString);
        throw new YardException("Unable to process results of Query");
    }
    return new RdfQueryResultList(query, resultGraph);
}
Also used : YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Query(org.apache.clerezza.rdf.core.sparql.query.Query) SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) RdfQueryResultList(org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList) SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph)

Example 22 with ImmutableGraph

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

the class XsltExtractor method extract.

public synchronized void extract(String id, Document doc, Map<String, Object> params, Graph result) throws ExtractorException {
    if (params == null) {
        params = new HashMap<String, Object>();
    }
    params.put(this.uriParameter, id);
    initTransformerParameters(params);
    Source source = new DOMSource(doc);
    ByteArrayOutputStream writer = new ByteArrayOutputStream(8192);
    StreamResult output = new StreamResult(writer);
    try {
        this.transformer.transform(source, output);
        if (LOG.isDebugEnabled()) {
            String rdf = writer.toString("UTF-8");
            LOG.debug(rdf);
        }
        InputStream reader = new ByteArrayInputStream(writer.toByteArray());
        Parser rdfParser = Parser.getInstance();
        ImmutableGraph graph = rdfParser.parse(reader, this.syntax);
        result.addAll(graph);
    } catch (TransformerException e) {
        throw new ExtractorException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ExtractorException(e.getMessage(), e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Parser(org.apache.clerezza.rdf.core.serializedform.Parser) ByteArrayInputStream(java.io.ByteArrayInputStream) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) TransformerException(javax.xml.transform.TransformerException)

Example 23 with ImmutableGraph

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

the class EventJobManagerImpl method getActiveEngines.

@Override
public List<EnhancementEngine> getActiveEngines() {
    //This implementation return the list of active engined for the default
    //Chain in the order they would be executed
    Chain defaultChain = chainManager.getDefault();
    if (defaultChain == null) {
        throw new IllegalStateException("Currently no enhancement chain is " + "active. Please configure a Chain or enable the default chain");
    }
    ImmutableGraph ep;
    try {
        ep = defaultChain.getExecutionPlan();
    } catch (ChainException e) {
        throw new IllegalStateException("Unable to get Execution Plan for " + "default enhancement chain (name: '" + defaultChain.getName() + "'| class: '" + defaultChain.getClass() + "')!", e);
    }
    return ExecutionPlanHelper.getActiveEngines(engineManager, ep);
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph)

Example 24 with ImmutableGraph

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

the class EnhancementPropertyTest method initExecutionMetadata.

/**
     * Initialises the ExecutionMetadata based on the chain used by the test.
     * This is typically done by the {@link EnhancementJobManager}, but as we do
     * not use one for the tests we need to do this part manually
     * @param chain
     * @throws ChainException
     */
protected void initExecutionMetadata(Chain chain) throws ChainException {
    //init the ExecutionMetadata ... this is normally done by the EnhancementJobManager
    Graph em = ExecutionMetadataHelper.initExecutionMetadataContentPart(contentItem);
    ImmutableGraph ep = chain.getExecutionPlan();
    em.addAll(ep);
    ExecutionMetadataHelper.initExecutionMetadata(em, ep, contentItem.getUri(), chain.getName(), false);
}
Also used : ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph)

Example 25 with ImmutableGraph

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

the class ExecutionPlanHelper method calculateExecutionPlan.

/**
     * Creates an execution plan based on the 
     * {@link ServiceProperties#ENHANCEMENT_ENGINE_ORDERING} of the parsed
     * EnhancementEngines. NOTE that the parsed list is modified as it is sorted by
     * using the {@link EnhancementEngineHelper#EXECUTION_ORDER_COMPARATOR}.<p>
     * A second parameter with the set of optional engines can be used to define
     * what {@link ExecutionPlan#EXECUTION_NODE} in the execution plan should be 
     * marked as {@link ExecutionPlan#OPTIONAL}.
     * @param chainName the name of the Chain to build the execution plan for
     * @param availableEngines the list of engines
     * @param optional the names of optional engines.
     * @param missing the names of missing engines
     * @param enhProps chain scoped enhancement properties. The key of the outer
     * map are the name of the engine or <code>null</code> for the chain. The
     * inner map uses the property as key and the value(s) as value. Multiple
     * values can be parsed as {@link Collection}. Single values will be
     * converted to RDF {@link TypedLiteral}s by using the {@link LiteralFactory}.
     * For types not supported by the LiteralFactory the <code>toString()</code>
     * method will be used. <code>null</code> can be parsed if no enhancement
     * properties are present.
     * @return the execution plan
     * @since 0.12.1
     */
public static ImmutableGraph calculateExecutionPlan(String chainName, List<EnhancementEngine> availableEngines, Set<String> optional, Set<String> missing, Map<String, Map<String, Object>> enhProps) {
    if (chainName == null || chainName.isEmpty()) {
        throw new IllegalArgumentException("The parsed ChainName MUST NOT be empty!");
    }
    Collections.sort(availableEngines, EXECUTION_ORDER_COMPARATOR);
    //now we have all required and possible also optional engines
    //  -> build the execution plan
    Graph ep = new IndexedGraph();
    BlankNodeOrIRI epNode = createExecutionPlan(ep, chainName, enhProps != null ? enhProps.get(null) : null);
    Integer prevOrder = null;
    Set<BlankNodeOrIRI> prev = null;
    Set<BlankNodeOrIRI> current = new HashSet<BlankNodeOrIRI>();
    for (String name : missing) {
        boolean optionalMissing = optional.contains(name);
        BlankNodeOrIRI node = writeExecutionNode(ep, epNode, name, optionalMissing, null, enhProps == null ? null : enhProps.get(name));
        if (!optionalMissing) {
            current.add(node);
        }
    // else add missing optional engines without any dependsOn restrictions
    }
    for (EnhancementEngine engine : availableEngines) {
        String name = engine.getName();
        Integer order = getEngineOrder(engine);
        if (prevOrder == null || !prevOrder.equals(order)) {
            prev = current;
            current = new HashSet<BlankNodeOrIRI>();
            prevOrder = order;
        }
        try {
            BlankNodeOrIRI executionNode = writeExecutionNode(ep, epNode, name, optional.contains(name), prev, enhProps == null ? null : enhProps.get(name));
            current.add(executionNode);
        } catch (RuntimeException e) {
            //add the engine and class to ease debugging in such cases
            log.error("Exception while writing ExecutionNode for Enhancement Eninge: " + engine + "(class: " + engine.getClass() + ")", e);
            //rethrow it
            throw e;
        }
    }
    return ep.getImmutableGraph();
}
Also used : ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) EnhancementEngineHelper.getString(org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) HashSet(java.util.HashSet)

Aggregations

ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)25 Graph (org.apache.clerezza.commons.rdf.Graph)9 IRI (org.semanticweb.owlapi.model.IRI)8 GET (javax.ws.rs.GET)7 Produces (javax.ws.rs.Produces)7 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)7 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)7 HashSet (java.util.HashSet)6 Path (javax.ws.rs.Path)5 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)5 Triple (org.apache.clerezza.commons.rdf.Triple)5 IRI (org.apache.clerezza.commons.rdf.IRI)4 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)4 Test (org.junit.Test)4 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)4 InputStream (java.io.InputStream)3 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)3 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)3 Parser (org.apache.clerezza.rdf.core.serializedform.Parser)3 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)3