Search in sources :

Example 11 with RdfValueFactory

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

the class ResourceAdapterTest method testDouble.

/**
 * Test related to STANBOL-698
 */
@Test
public void testDouble() {
    Graph graph = new IndexedGraph();
    IRI id = new IRI("http://www.example.org/test");
    IRI doubleTestField = new IRI("http://www.example.org/field/double");
    LiteralFactory lf = LiteralFactory.getInstance();
    graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(Double.NaN)));
    graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(Double.POSITIVE_INFINITY)));
    graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(Double.NEGATIVE_INFINITY)));
    RdfValueFactory vf = new RdfValueFactory(graph);
    Representation r = vf.createRepresentation(id.getUnicodeString());
    Set<Double> expected = new HashSet<Double>(Arrays.asList(Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));
    Iterator<Double> dit = r.get(doubleTestField.getUnicodeString(), Double.class);
    while (dit.hasNext()) {
        Double val = dit.next();
        Assert.assertNotNull(val);
        Assert.assertTrue(expected.remove(val));
    }
    Assert.assertTrue(expected.isEmpty());
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Graph(org.apache.clerezza.commons.rdf.Graph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with RdfValueFactory

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

the class NamedEntityTaggingEngine method computeEnhancements.

public void computeEnhancements(ContentItem ci) throws EngineException {
    final Site site;
    if (referencedSiteID != null) {
        // lookup the referenced site
        site = siteManager.getSite(referencedSiteID);
        // ensure that it is present
        if (site == null) {
            String msg = String.format("Unable to enhance %s because Referenced Site %s is currently not active!", ci.getUri().getUnicodeString(), referencedSiteID);
            log.warn(msg);
            // throw new EngineException(msg);
            return;
        }
        // and that it supports offline mode if required
        if (isOfflineMode() && !site.supportsLocalMode()) {
            log.warn("Unable to enhance ci {} because OfflineMode is not supported by ReferencedSite {}.", ci.getUri().getUnicodeString(), site.getId());
            return;
        }
    } else {
        // null indicates to use the Entityhub to lookup Entities
        site = null;
    }
    Graph graph = ci.getMetadata();
    LiteralFactory literalFactory = LiteralFactory.getInstance();
    // Retrieve the existing text annotations (requires read lock)
    Map<NamedEntity, List<IRI>> textAnnotations = new HashMap<NamedEntity, List<IRI>>();
    // the language extracted for the parsed content or NULL if not
    // available
    String contentLangauge;
    ci.getLock().readLock().lock();
    try {
        contentLangauge = EnhancementEngineHelper.getLanguage(ci);
        for (Iterator<Triple> it = graph.filter(null, RDF_TYPE, TechnicalClasses.ENHANCER_TEXTANNOTATION); it.hasNext(); ) {
            IRI uri = (IRI) it.next().getSubject();
            if (graph.filter(uri, Properties.DC_RELATION, null).hasNext()) {
                // skip
                continue;
            }
            NamedEntity namedEntity = NamedEntity.createFromTextAnnotation(graph, uri);
            if (namedEntity != null) {
                // This is a first occurrence, collect any subsumed
                // annotations
                List<IRI> subsumed = new ArrayList<IRI>();
                for (Iterator<Triple> it2 = graph.filter(null, Properties.DC_RELATION, uri); it2.hasNext(); ) {
                    subsumed.add((IRI) it2.next().getSubject());
                }
                textAnnotations.put(namedEntity, subsumed);
            }
        }
    } finally {
        ci.getLock().readLock().unlock();
    }
    // search the suggestions
    Map<NamedEntity, List<Suggestion>> suggestions = new HashMap<NamedEntity, List<Suggestion>>(textAnnotations.size());
    for (Entry<NamedEntity, List<IRI>> entry : textAnnotations.entrySet()) {
        try {
            List<Suggestion> entitySuggestions = computeEntityRecommentations(site, entry.getKey(), entry.getValue(), contentLangauge);
            if (entitySuggestions != null && !entitySuggestions.isEmpty()) {
                suggestions.put(entry.getKey(), entitySuggestions);
            }
        } catch (EntityhubException e) {
            throw new EngineException(this, ci, e);
        }
    }
    // now write the results (requires write lock)
    ci.getLock().writeLock().lock();
    try {
        RdfValueFactory factory = RdfValueFactory.getInstance();
        Map<String, Representation> entityData = new HashMap<String, Representation>();
        for (Entry<NamedEntity, List<Suggestion>> entitySuggestions : suggestions.entrySet()) {
            List<IRI> subsumed = textAnnotations.get(entitySuggestions.getKey());
            List<BlankNodeOrIRI> annotationsToRelate = new ArrayList<BlankNodeOrIRI>(subsumed);
            annotationsToRelate.add(entitySuggestions.getKey().getEntity());
            for (Suggestion suggestion : entitySuggestions.getValue()) {
                log.debug("Add Suggestion {} for {}", suggestion.getEntity().getId(), entitySuggestions.getKey());
                EnhancementRDFUtils.writeEntityAnnotation(this, literalFactory, graph, ci.getUri(), annotationsToRelate, suggestion, nameField, // header)?!
                contentLangauge == null ? DEFAULT_LANGUAGE : contentLangauge);
                if (dereferenceEntities) {
                    entityData.put(suggestion.getEntity().getId(), suggestion.getEntity().getRepresentation());
                }
            }
        }
        // Representations to add! If false entityData will be empty
        for (Representation rep : entityData.values()) {
            graph.addAll(factory.toRdfRepresentation(rep).getRdfGraph());
        }
    } finally {
        ci.getLock().writeLock().unlock();
    }
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) List(java.util.List) ArrayList(java.util.ArrayList) QueryResultList(org.apache.stanbol.entityhub.servicesapi.query.QueryResultList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory) Triple(org.apache.clerezza.commons.rdf.Triple) Graph(org.apache.clerezza.commons.rdf.Graph) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)

Example 13 with RdfValueFactory

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

the class ClerezzaYard method store.

protected final Representation store(Representation representation, boolean allowCreate, boolean canNotCreateIsError) throws IllegalArgumentException, YardException {
    if (representation == null) {
        return null;
    }
    log.debug("store Representation " + representation.getId());
    IRI id = new IRI(representation.getId());
    final Lock writeLock = writeLockGraph();
    try {
        Iterator<Triple> current = graph.filter(id, null, null);
        boolean contains = current.hasNext();
        while (current.hasNext()) {
            // delete current
            current.next();
            current.remove();
        }
        if (!contains && !allowCreate) {
            if (canNotCreateIsError) {
                throw new IllegalArgumentException("Parsed Representation " + representation.getId() + " in not managed by this Yard " + getName() + "(id=" + getId() + ")");
            } else {
                return null;
            }
        }
        // get the graph for the Representation and add it to the store
        RdfRepresentation toAdd = ((RdfValueFactory) getValueFactory()).toRdfRepresentation(representation);
        // log.info("  > add "+toAdd.size()+" triples to Yard "+getId());
        Iterator<Triple> it = toAdd.getRdfGraph().filter(toAdd.getNode(), null, null);
        if (!it.hasNext()) {
            // TODO: Note somewhere that this Triple is reserved and MUST NOT
            // be used by externally.
            graph.add(new TripleImpl(toAdd.getNode(), MANAGED_REPRESENTATION, TRUE_LITERAL));
        } else {
            while (it.hasNext()) {
                graph.add(it.next());
            }
        }
        return toAdd;
    } finally {
        writeLock.unlock();
    }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) Lock(java.util.concurrent.locks.Lock)

Aggregations

RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)13 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)9 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)8 Graph (org.apache.clerezza.commons.rdf.Graph)7 HashSet (java.util.HashSet)6 IRI (org.apache.clerezza.commons.rdf.IRI)6 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)5 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)5 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)4 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)3 LiteralFactory (org.apache.clerezza.rdf.core.LiteralFactory)3 LDPathParseException (org.apache.marmotta.ldpath.exception.LDPathParseException)3 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)3 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Lock (java.util.concurrent.locks.Lock)2 Triple (org.apache.clerezza.commons.rdf.Triple)2 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)2