Search in sources :

Example 6 with EntityhubLDPath

use of org.apache.stanbol.entityhub.ldpath.EntityhubLDPath in project stanbol by apache.

the class EntityhubDereferenceContext method initLdPath.

protected void initLdPath(String program) throws DereferenceConfigurationException {
    TrackingDereferencerBase<?> dereferencer = getEntityhubDereferencer();
    ValueFactory valueFactory = dereferencer.getValueFactory();
    Program<Object> ldpathProgram;
    if (!StringUtils.isBlank(program)) {
        @SuppressWarnings("rawtypes") RDFBackend<Object> parseBackend = new ParseBackend<Object>(valueFactory);
        EntityhubLDPath parseLdPath = new EntityhubLDPath(parseBackend, valueFactory);
        try {
            ldpathProgram = parseLdPath.parseProgram(new StringReader(program));
        } catch (LDPathParseException e) {
            log.error("Unable to parse Context LDPath pogram: \n {}", program);
            throw new DereferenceConfigurationException("Unable to parse context LDPath program !", e, dereferencer.getClass(), DEREFERENCE_ENTITIES_LDPATH);
        }
        //finally validate if all mappings of the program do use a URI as key
        //also store used fieldNames as we need them later
        Set<String> contextFields = new HashSet<String>();
        for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : ldpathProgram.getFields()) {
            try {
                new URI(mapping.getFieldName());
                contextFields.add(mapping.getFieldName());
            } catch (URISyntaxException e) {
                throw new DereferenceConfigurationException("Parsed LDPath MUST use valid URIs as field names (invalid field name: '" + mapping.getFieldName() + "' | selector: '" + mapping.getSelector().getPathExpression(parseBackend) + "')!", dereferencer.getClass(), DereferenceConstants.DEREFERENCE_ENTITIES_LDPATH);
            }
        }
        //append the mappings configured for the engine
        if (dereferencer.getLdPathProgram() != null) {
            for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : dereferencer.getLdPathProgram().getFields()) {
                if (!contextFields.contains(mapping.getFieldName())) {
                    ldpathProgram.addMapping(mapping);
                }
            //else ignore mappings for fields specified in the context
            }
        }
    } else {
        //no context specific - use the one of the config
        ldpathProgram = dereferencer.getLdPathProgram();
    }
    if (ldpathProgram != null && !ldpathProgram.getFields().isEmpty()) {
        this.ldpathProgram = ldpathProgram;
    } else {
        this.ldpathProgram = null;
    }
}
Also used : ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) StringReader(java.io.StringReader) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) DereferenceConfigurationException(org.apache.stanbol.enhancer.engines.dereference.DereferenceConfigurationException) HashSet(java.util.HashSet)

Example 7 with EntityhubLDPath

use of org.apache.stanbol.entityhub.ldpath.EntityhubLDPath in project stanbol by apache.

the class LDPathHelper method executeLDPath.

/**
     * Executes the LDPath program on the contexts stored in the backend and
     * returns the result as an RDF graph 
     * @param contexts the contexts to execute the program on
     * @param ldpath the LDPath program to execute
     * @param backend the {@link RDFBackend} to use
     * @return The results stored within an RDF graph
     * @throws LDPathParseException if the parsed LDPath program is invalid
     */
private static Graph executeLDPath(RDFBackend<Object> backend, String ldpath, Set<String> contexts) throws LDPathParseException {
    Graph data = new IndexedGraph();
    RdfValueFactory vf = new RdfValueFactory(data);
    EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
    Program<Object> program = ldPath.parseProgram(getReader(ldpath));
    if (log.isDebugEnabled()) {
        log.debug("Execute on Context(s) '{}' LDPath program: \n{}", contexts, program.getPathExpression(backend));
    }
    /*
         * NOTE: We do not need to process the Representations returned by
         * EntityhubLDPath#exdecute, because the RdfValueFactory used uses
         * the local variable "Graph data" to backup all created
         * RdfRepresentation. Because of this all converted data will be
         * automatically added the Graph. The only thing we need to do is to
         * wrap the Graph in the response.
         */
    for (String context : contexts) {
        ldPath.execute(vf.createReference(context), program);
    }
    return data;
}
Also used : IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)

Aggregations

EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)7 LDPathParseException (org.apache.marmotta.ldpath.exception.LDPathParseException)5 HashSet (java.util.HashSet)4 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)4 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)4 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)4 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)3 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)3 StringReader (java.io.StringReader)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)2 Graph (org.apache.clerezza.commons.rdf.Graph)1 DereferenceConfigurationException (org.apache.stanbol.enhancer.engines.dereference.DereferenceConfigurationException)1 EntityhubBackend (org.apache.stanbol.entityhub.ldpath.backend.EntityhubBackend)1 SiteBackend (org.apache.stanbol.entityhub.ldpath.backend.SiteBackend)1 SiteManagerBackend (org.apache.stanbol.entityhub.ldpath.backend.SiteManagerBackend)1 YardBackend (org.apache.stanbol.entityhub.ldpath.backend.YardBackend)1 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)1