Search in sources :

Example 51 with TripleImpl

use of org.apache.clerezza.commons.rdf.impl.utils.TripleImpl in project stanbol by apache.

the class RestfulLangidentEngine method computeEnhancements.

/**
     * Compute enhancements for supplied ContentItem. The results of the process
     * are expected to be stored in the metadata of the content item.
     * <p/>
     * The client (usually an {@link org.apache.stanbol.enhancer.servicesapi.EnhancementJobManager}) should take care of
     * persistent storage of the enhanced {@link org.apache.stanbol.enhancer.servicesapi.ContentItem}.
     * <p/>
     * This method creates a new POSContentPart using {@link org.apache.stanbol.enhancer.engines.pos.api.POSTaggerHelper#createContentPart} from a text/plain part and
     * stores it as a new part in the content item. The metadata is not changed.
     *
     * @throws org.apache.stanbol.enhancer.servicesapi.EngineException
     *          if the underlying process failed to work as
     *          expected
     */
@Override
public void computeEnhancements(final ContentItem ci) throws EngineException {
    //get the plain text Blob
    Map.Entry<IRI, Blob> textBlob = getPlainText(this, ci, false);
    Blob blob = textBlob.getValue();
    //send the text to the server
    final HttpPost request = new HttpPost(serviceUrl);
    request.setEntity(new InputStreamEntity(blob.getStream(), blob.getContentLength(), ContentType.create(blob.getMimeType(), blob.getParameter().get("charset"))));
    //execute the request
    List<LangSuggestion> detected;
    try {
        detected = AccessController.doPrivileged(new PrivilegedExceptionAction<List<LangSuggestion>>() {

            public List<LangSuggestion> run() throws ClientProtocolException, IOException {
                return httpClient.execute(request, new LangIdentResponseHandler(ci, objectMapper));
            }
        });
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (e instanceof ClientProtocolException) {
            throw new EngineException(this, ci, "Exception while executing Request " + "on RESTful Language Identification Service at " + serviceUrl, e);
        } else if (e instanceof IOException) {
            throw new EngineException(this, ci, "Exception while executing Request " + "on RESTful Language Identification Service at " + serviceUrl, e);
        } else {
            throw RuntimeException.class.cast(e);
        }
    }
    Graph metadata = ci.getMetadata();
    log.debug("Detected Languages for ContentItem {} and Blob {}");
    ci.getLock().writeLock().lock();
    try {
        //write TextAnnotations for the detected languages
        for (LangSuggestion suggestion : detected) {
            // add a hypothesis
            log.debug(" > {}@{}", suggestion.getLanguage(), suggestion.hasProbability() ? suggestion.getProbability() : "-,--");
            IRI textEnhancement = EnhancementEngineHelper.createTextEnhancement(ci, this);
            metadata.add(new TripleImpl(textEnhancement, DC_LANGUAGE, new PlainLiteralImpl(suggestion.getLanguage())));
            metadata.add(new TripleImpl(textEnhancement, DC_TYPE, DCTERMS_LINGUISTIC_SYSTEM));
            if (suggestion.hasProbability()) {
                metadata.add(new TripleImpl(textEnhancement, ENHANCER_CONFIDENCE, literalFactory.createTypedLiteral(suggestion.getProbability())));
            }
        }
    } finally {
        ci.getLock().writeLock().unlock();
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) HttpPost(org.apache.http.client.methods.HttpPost) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) PrivilegedActionException(java.security.PrivilegedActionException) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) HttpException(org.apache.http.HttpException) ClientProtocolException(org.apache.http.client.ClientProtocolException) PrivilegedActionException(java.security.PrivilegedActionException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) ClientProtocolException(org.apache.http.client.ClientProtocolException) Graph(org.apache.clerezza.commons.rdf.Graph) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Map(java.util.Map) HashMap(java.util.HashMap)

Example 52 with TripleImpl

use of org.apache.clerezza.commons.rdf.impl.utils.TripleImpl 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 53 with TripleImpl

use of org.apache.clerezza.commons.rdf.impl.utils.TripleImpl 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 54 with TripleImpl

use of org.apache.clerezza.commons.rdf.impl.utils.TripleImpl 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 55 with TripleImpl

use of org.apache.clerezza.commons.rdf.impl.utils.TripleImpl in project stanbol by apache.

the class ClerezzaRuleStore method removeRule.

@Override
public Recipe removeRule(Recipe recipe, Rule rule) {
    Graph tripleCollection = tcManager.getGraph(recipe.getRecipeID());
    // remove from the graph recipe all the triples having the ruleID as subject.
    Iterator<Triple> triplesIterator = tripleCollection.filter(rule.getRuleID(), null, null);
    while (triplesIterator.hasNext()) {
        tripleCollection.remove(triplesIterator.next());
    }
    // remove from the graph recipe the triple recipeID hasRule ruleID
    tripleCollection.remove(new TripleImpl(recipe.getRecipeID(), Symbols.hasRule, rule.getRuleID()));
    recipe.removeRule(rule);
    return recipe;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Aggregations

TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)143 IRI (org.apache.clerezza.commons.rdf.IRI)104 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)69 Graph (org.apache.clerezza.commons.rdf.Graph)66 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)49 Triple (org.apache.clerezza.commons.rdf.Triple)41 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)26 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)23 HashMap (java.util.HashMap)20 Language (org.apache.clerezza.commons.rdf.Language)20 Literal (org.apache.clerezza.commons.rdf.Literal)20 LiteralFactory (org.apache.clerezza.rdf.core.LiteralFactory)20 IOException (java.io.IOException)18 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)17 Test (org.junit.Test)16 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)15 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)14 HashSet (java.util.HashSet)13 StringSource (org.apache.stanbol.enhancer.servicesapi.impl.StringSource)13 BlankNode (org.apache.clerezza.commons.rdf.BlankNode)11