Search in sources :

Example 1 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation in project stanbol by apache.

the class RdfResultListTest method testRdfResultSorting.

/**
     * Providing a sorted Iteration over query results stored in an RDF
     * graph is not something trivial. Therefore this test
     */
@Test
public void testRdfResultSorting() {
    SortedMap<Double, RdfRepresentation> sorted = new TreeMap<Double, RdfRepresentation>();
    Graph resultGraph = new IndexedGraph();
    RdfValueFactory vf = new RdfValueFactory(resultGraph);
    IRI resultListNode = new IRI(RdfResourceEnum.QueryResultSet.getUri());
    IRI resultProperty = new IRI(RdfResourceEnum.queryResult.getUri());
    for (int i = 0; i < 100; i++) {
        Double rank;
        do {
            //avoid duplicate keys
            rank = Math.random();
        } while (sorted.containsKey(rank));
        RdfRepresentation r = vf.createRepresentation("urn:sortTest:rep." + i);
        //link the representation with the query result set
        resultGraph.add(new TripleImpl(resultListNode, resultProperty, r.getNode()));
        r.set(RdfResourceEnum.resultScore.getUri(), rank);
        sorted.put(rank, r);
    }
    RdfQueryResultList resultList = new RdfQueryResultList(new FieldQueryImpl(), resultGraph);
    if (log.isDebugEnabled()) {
        log.debug("---DEBUG Sorting ---");
        for (Iterator<Representation> it = resultList.iterator(); it.hasNext(); ) {
            Representation r = it.next();
            log.debug("{}: {}", r.getFirst(RdfResourceEnum.resultScore.getUri()), r.getId());
        }
    }
    log.debug("---ASSERT Sorting ---");
    for (Iterator<Representation> it = resultList.iterator(); it.hasNext(); ) {
        Representation r = it.next();
        Double lastkey = sorted.lastKey();
        Representation last = sorted.get(lastkey);
        Assert.assertEquals("score: " + r.getFirst(RdfResourceEnum.resultScore.getUri()) + " of Representation " + r.getId() + " is not as expected " + last.getFirst(RdfResourceEnum.resultScore.getUri()) + " of Representation " + last.getId() + "!", r, last);
        sorted.remove(lastkey);
    }
    Assert.assertTrue(sorted.isEmpty());
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) TreeMap(java.util.TreeMap) FieldQueryImpl(org.apache.stanbol.entityhub.core.query.FieldQueryImpl) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) Test(org.junit.Test)

Example 2 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation in project stanbol by apache.

the class TrackingDereferencerBase method copyLdPath.

/**
     * Executes the {@link #ldpathProgram} using the parsed URI as context and
     * writes the the results to the parsed ImmutableGraph
     * @param uri the context
     * @param rdfBackend the RdfBackend the LDPath program is executed on
     * @param ldpathProgram The {@link Program} parsed via the dereference context
     * @param langs the set of languages to dereference
     * @param graph the graph to store the results
     * @param writeLock the write lock for the graph
     * @throws DereferenceException on any {@link EntityhubException} while
     * executing the LDPath program
     */
private void copyLdPath(IRI uri, RDFBackend<Object> rdfBackend, Program<Object> ldpathProgram, Set<String> langs, Graph graph, Lock writeLock) throws DereferenceException {
    //A RdfReference needs to be used as context
    RdfReference context = valueFactory.createReference(uri);
    //create the representation that stores results in an intermediate
    //graph (we do not want partial results on an error
    Graph ldPathResults = new SimpleGraph();
    RdfRepresentation result = valueFactory.createRdfRepresentation(uri, ldPathResults);
    //execute the LDPath Program and write results to the RDF ImmutableGraph
    try {
        for (org.apache.marmotta.ldpath.model.fields.FieldMapping<?, Object> mapping : ldpathProgram.getFields()) {
            Collection<?> values;
            try {
                values = mapping.getValues(rdfBackend, context);
            } catch (RuntimeException e) {
                throw new DereferenceException(uri, e);
            }
            if (values != null && !values.isEmpty()) {
                String fieldName = mapping.getFieldName();
                if (langs == null || langs.isEmpty()) {
                    result.add(fieldName, values);
                } else {
                    //filter for languages
                    for (Object value : values) {
                        if ((!(value instanceof Text)) || langs.contains(((Text) value).getLanguage())) {
                            result.add(fieldName, value);
                        }
                    //else text with filtered language ... do not add
                    }
                }
            }
        }
    } catch (EntityhubException e) {
        throw new DereferenceException(uri, e);
    }
    if (log.isTraceEnabled()) {
        log.trace("dereferenced via LDPath {}", ModelUtils.getRepresentationInfo(result));
    }
    if (!ldPathResults.isEmpty()) {
        //copy the results
        writeLock.lock();
        try {
            graph.addAll(ldPathResults);
        } finally {
            writeLock.unlock();
        }
    }
}
Also used : Text(org.apache.stanbol.entityhub.servicesapi.model.Text) DereferenceException(org.apache.stanbol.enhancer.engines.dereference.DereferenceException) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) RdfReference(org.apache.stanbol.entityhub.model.clerezza.RdfReference)

Example 3 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation in project stanbol by apache.

the class TrackingDereferencerBase method copyMapped.

/**
     * Applies the field mappings to the representation and stores the results
     * in the graph
     * @param uri the uri of the entity to dereference
     * @param rep the data for the entity as in the entityhub
     * @param fieldMapper the {@link FieldMapper} parsed from the dereference context
     * @param langs the set of languages to dereference
     * @param graph the graph to store the mapping results
     * @param writeLock the write lock for the graph
     */
private void copyMapped(IRI uri, Representation rep, FieldMapper fieldMapper, Set<String> langs, Graph graph, Lock writeLock) {
    //NOTE: The fieldMapper parsed via the context does already have a
    //      filter for the parsed languages. Because of that the old code
    //      adding such a language filter is no longer needed
    //        FieldMapper fieldMapper;
    //        if(!langs.isEmpty()){ //if we need to filter for specific languages
    //            fieldMapper = this.fieldMapper != null ? this.fieldMapper.clone() :
    //                new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
    //            fieldMapper.addMapping(new FieldMapping(new TextConstraint(
    //                (String)null, langs.toArray(new String[langs.size()]))));
    //        } else { //just use the fieldMapper as parsed in the config
    //            fieldMapper = this.fieldMapper;
    //        }
    //execute the field mappings
    writeLock.lock();
    try {
        RdfRepresentation clerezzaRep = valueFactory.createRdfRepresentation(uri, graph);
        fieldMapper.applyMappings(rep, clerezzaRep, valueFactory);
        if (log.isTraceEnabled()) {
            log.trace("dereferenced via Mappings {}", ModelUtils.getRepresentationInfo(clerezzaRep));
        }
    } finally {
        writeLock.unlock();
    }
}
Also used : RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)

Example 4 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation in project stanbol by apache.

the class ReferencedSiteRootResource method license2Representation.

private Representation license2Representation(String id, License license) {
    RdfValueFactory valueFactory = RdfValueFactory.getInstance();
    RdfRepresentation rep = valueFactory.createRepresentation(id);
    if (license.getName() != null) {
        rep.add("http://purl.org/dc/terms/license", license.getName());
        rep.add("http://www.w3.org/2000/01/rdf-schema#label", license.getName());
        rep.add("http://purl.org/dc/terms/title", license.getName());
    }
    if (license.getText() != null) {
        rep.add("http://www.w3.org/2000/01/rdf-schema#description", license.getText());
    }
    rep.add("http://creativecommons.org/ns#licenseUrl", license.getUrl() == null ? id : license.getUrl());
    return rep;
}
Also used : RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)

Example 5 with RdfRepresentation

use of org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation in project stanbol by apache.

the class ReferencedSiteRootResource method site2Representation.

/*
     * Referenced Site Metadata
     */
/**
     * Transforms a site to a Representation that can be serialised 
     * @param context
     * @return
     */
private Representation site2Representation(Site site, String id) {
    RdfValueFactory valueFactory = RdfValueFactory.getInstance();
    RdfRepresentation rep = valueFactory.createRepresentation(id);
    String namespace = NamespaceEnum.entityhub.getNamespace();
    rep.add(namespace + "localMode", site.supportsLocalMode());
    rep.add(namespace + "supportsSearch", site.supportsSearch());
    SiteConfiguration config = site.getConfiguration();
    rep.add("http://www.w3.org/2000/01/rdf-schema#label", config.getName());
    rep.add("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", valueFactory.createReference(namespace + "ReferencedSite"));
    if (config.getDescription() != null) {
        rep.add("http://www.w3.org/2000/01/rdf-schema#description", config.getDescription());
    }
    if (config.getAttribution() != null) {
        rep.add("http://creativecommons.org/ns#attributionName", config.getAttribution());
    }
    if (config.getAttributionUrl() != null) {
        rep.add("http://creativecommons.org/ns#attributionURL", config.getAttributionUrl());
    }
    //add the licenses
    if (config.getLicenses() != null) {
        int count = 0;
        for (License license : config.getLicenses()) {
            String licenseUrl;
            if (license.getUrl() != null) {
                licenseUrl = license.getUrl();
            } else {
                licenseUrl = id + (!id.endsWith("/") ? "/" : "") + LICENSE_PATH + '/' + LICENSE_NAME + (count > 0 ? count : "");
                count++;
            }
            //if defined add the name to dc:license
            if (license.getName() != null) {
                rep.add("http://purl.org/dc/terms/license", licenseUrl);
            }
            //link to the license via cc:license
            rep.add("http://creativecommons.org/ns#license", licenseUrl);
        }
    }
    if (config.getEntityPrefixes() != null) {
        for (String prefix : config.getEntityPrefixes()) {
            rep.add(namespace + "entityPrefix", prefix);
        }
    } else {
        //all entities are allowed/processed
        rep.add(namespace + "entityPrefix", "*");
    }
    if (config instanceof ReferencedSiteConfiguration) {
        ReferencedSiteConfiguration refConfig = (ReferencedSiteConfiguration) config;
        if (refConfig.getCacheStrategy() != null) {
            rep.add(namespace + "cacheStrategy", valueFactory.createReference(namespace + "cacheStrategy-" + refConfig.getCacheStrategy().name()));
        }
        //add the accessUri and queryUri
        if (refConfig.getAccessUri() != null) {
            rep.add(namespace + "accessUri", valueFactory.createReference(refConfig.getAccessUri()));
        }
        if (refConfig.getQueryUri() != null) {
            rep.add(namespace + "queryUri", valueFactory.createReference(refConfig.getQueryUri()));
        }
    }
    return rep;
}
Also used : ReferencedSiteConfiguration(org.apache.stanbol.entityhub.servicesapi.site.ReferencedSiteConfiguration) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) License(org.apache.stanbol.entityhub.servicesapi.site.License) SiteConfiguration(org.apache.stanbol.entityhub.servicesapi.site.SiteConfiguration) ReferencedSiteConfiguration(org.apache.stanbol.entityhub.servicesapi.site.ReferencedSiteConfiguration) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)

Aggregations

RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)10 Graph (org.apache.clerezza.commons.rdf.Graph)4 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)4 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)3 Test (org.junit.Test)3 IRI (org.apache.clerezza.commons.rdf.IRI)2 Triple (org.apache.clerezza.commons.rdf.Triple)2 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)2 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)2 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)2 TreeMap (java.util.TreeMap)1 Lock (java.util.concurrent.locks.Lock)1 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)1 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)1 DereferenceException (org.apache.stanbol.enhancer.engines.dereference.DereferenceException)1 FieldQueryImpl (org.apache.stanbol.entityhub.core.query.FieldQueryImpl)1 RdfReference (org.apache.stanbol.entityhub.model.clerezza.RdfReference)1 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)1 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)1 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)1