Search in sources :

Example 91 with RDFTerm

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

the class GraphMultiplexer method getDependents.

@Override
public Set<OWLOntologyID> getDependents(OWLOntologyID dependency) {
    Set<OWLOntologyID> dependents = new HashSet<OWLOntologyID>();
    IRI dep = buildResource(dependency);
    log.debug("Getting depents for {}", dependency);
    synchronized (meta) {
        Iterator<Triple> it = meta.filter(null, DEPENDS_ON_URIREF, dep);
        while (it.hasNext()) {
            RDFTerm sub = it.next().getSubject();
            log.debug(" ... found {} (inverse).", sub);
            if (sub instanceof IRI)
                dependents.add(buildPublicKey((IRI) sub));
            else
                log.warn(" ... Unexpected literal value!");
        }
        it = meta.filter(dep, HAS_DEPENDENT_URIREF, null);
        while (it.hasNext()) {
            RDFTerm obj = it.next().getObject();
            log.debug(" ... found {} (inverse).", obj);
            if (obj instanceof IRI)
                dependents.add(buildPublicKey((IRI) obj));
            else
                log.warn(" ... Unexpected literal value!");
        }
    }
    return dependents;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) HashSet(java.util.HashSet)

Example 92 with RDFTerm

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

the class EntityLinkingEngineTest method testEngine.

/**
     * This tests if the Enhancements created by the Engine confirm to the
     * rules defined for the Stanbol Enhancement Structure.
     * @throws IOException
     * @throws EngineException
     */
@Test
public void testEngine() throws IOException, EngineException {
    EntityLinkerConfig linkerConfig = new EntityLinkerConfig();
    linkerConfig.setRedirectProcessingMode(RedirectProcessingMode.FOLLOW);
    //this is assumed by this test
    linkerConfig.setMinFoundTokens(2);
    EntityLinkingEngine engine = new EntityLinkingEngine("dummy", searcher, new TextProcessingConfig(), linkerConfig, labelTokenizer);
    ContentItem ci = ciFactory.createContentItem(new StringSource(TEST_TEXT));
    //tells the engine that this is an English text
    ci.getMetadata().add(new TripleImpl(ci.getUri(), DC_LANGUAGE, new PlainLiteralImpl("en")));
    //and add the AnalysedText instance used for this test
    ci.addPart(AnalysedText.ANALYSED_TEXT_URI, TEST_ANALYSED_TEXT);
    //compute the enhancements
    engine.computeEnhancements(ci);
    //validate the enhancement results
    Map<IRI, RDFTerm> expectedValues = new HashMap<IRI, RDFTerm>();
    expectedValues.put(ENHANCER_EXTRACTED_FROM, ci.getUri());
    expectedValues.put(DC_CREATOR, LiteralFactory.getInstance().createTypedLiteral(engine.getClass().getName()));
    //adding null as expected for confidence makes it a required property
    expectedValues.put(Properties.ENHANCER_CONFIDENCE, null);
    //validate create fise:TextAnnotations
    int numTextAnnotations = validateAllTextAnnotations(ci.getMetadata(), TEST_TEXT, expectedValues);
    assertEquals("Four fise:TextAnnotations are expected by this Test", 4, numTextAnnotations);
    //validate create fise:EntityAnnotations
    int numEntityAnnotations = validateAllEntityAnnotations(ci, expectedValues);
    assertEquals("Five fise:EntityAnnotations are expected by this Test", 5, numEntityAnnotations);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) EntityLinkerConfig(org.apache.stanbol.enhancer.engines.entitylinking.config.EntityLinkerConfig) TextProcessingConfig(org.apache.stanbol.enhancer.engines.entitylinking.config.TextProcessingConfig) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) HashMap(java.util.HashMap) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Test(org.junit.Test)

Example 93 with RDFTerm

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

the class EntityLinkingEngineTest method validateAllEntityAnnotations.

/**
     * Similar to {@link EnhancementStructureHelper#validateAllEntityAnnotations(org.apache.clerezza.commons.rdf.Graph, Map)}
     * but in addition checks fise:confidence [0..1] and entityhub:site properties
     * @param ci
     * @param expectedValues
     * @return
     */
private static int validateAllEntityAnnotations(ContentItem ci, Map<IRI, RDFTerm> expectedValues) {
    Iterator<Triple> entityAnnotationIterator = ci.getMetadata().filter(null, RDF_TYPE, ENHANCER_ENTITYANNOTATION);
    int entityAnnotationCount = 0;
    while (entityAnnotationIterator.hasNext()) {
        IRI entityAnnotation = (IRI) entityAnnotationIterator.next().getSubject();
        // test if selected Text is added
        validateEntityAnnotation(ci.getMetadata(), entityAnnotation, expectedValues);
        //validate also that the confidence is between [0..1]
        Iterator<Triple> confidenceIterator = ci.getMetadata().filter(entityAnnotation, ENHANCER_CONFIDENCE, null);
        //Confidence is now checked by the EnhancementStructureHelper (STANBOL-630)
        //            assertTrue("Expected fise:confidence value is missing (entityAnnotation "
        //                    +entityAnnotation+")",confidenceIterator.hasNext());
        //            Double confidence = LiteralFactory.getInstance().createObject(Double.class,
        //                (TypedLiteral)confidenceIterator.next().getObject());
        //            assertTrue("fise:confidence MUST BE <= 1 (value= '"+confidence
        //                    + "',entityAnnotation " +entityAnnotation+")",
        //                    1.0 >= confidence.doubleValue());
        //            assertTrue("fise:confidence MUST BE >= 0 (value= '"+confidence
        //                    +"',entityAnnotation "+entityAnnotation+")",
        //                    0.0 <= confidence.doubleValue());
        //Test the entityhub:site property (STANBOL-625)
        IRI ENTITYHUB_SITE = new IRI(NamespaceEnum.entityhub + "site");
        Iterator<Triple> entitySiteIterator = ci.getMetadata().filter(entityAnnotation, ENTITYHUB_SITE, null);
        assertTrue("Expected entityhub:site value is missing (entityAnnotation " + entityAnnotation + ")", entitySiteIterator.hasNext());
        RDFTerm siteResource = entitySiteIterator.next().getObject();
        assertTrue("entityhub:site values MUST BE Literals", siteResource instanceof Literal);
        assertEquals("'" + TEST_REFERENCED_SITE_NAME + "' is expected as " + "entityhub:site value", TEST_REFERENCED_SITE_NAME, ((Literal) siteResource).getLexicalForm());
        assertFalse("entityhub:site MUST HAVE only a single value", entitySiteIterator.hasNext());
        entityAnnotationCount++;
    }
    return entityAnnotationCount;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) Literal(org.apache.clerezza.commons.rdf.Literal) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Example 94 with RDFTerm

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

the class EnhancementEngineHelper method set.

/**
     * Replaces all current values of the property for the resource
     * with the parsed values
     * @param graph the graph
     * @param resource the resource
     * @param property the property
     * @param value the value. In case it is an instance of {@link RDFTerm} it
     * is directly added to the graph. Otherwise the parsed {@link LiteralFactory}
     * is used to create a {@link TypedLiteral} for the parsed value.
     * @param literalFactory the {@link LiteralFactory} used in case the parsed
     * value is not an {@link RDFTerm}
     */
public static void set(Graph graph, BlankNodeOrIRI resource, IRI property, Collection<?> values, LiteralFactory literalFactory) {
    Iterator<Triple> currentValues = graph.filter(resource, property, null);
    while (currentValues.hasNext()) {
        currentValues.next();
        currentValues.remove();
    }
    if (values != null) {
        for (Object value : values) {
            if (value instanceof RDFTerm) {
                graph.add(new TripleImpl(resource, property, (RDFTerm) value));
            } else if (value != null) {
                graph.add(new TripleImpl(resource, property, literalFactory.createTypedLiteral(value)));
            }
        }
    }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 95 with RDFTerm

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

the class EnhancementEngineHelper method extractEnhancementProperties.

/**
     * Extracts all EnhancementProperties from the parsed Node and adds them to
     * the parsed map
     * @param properties The Map to add the extracted properties. extracted values
     * are appended to existing values.
     * @param graph the RDF graph containing the data
     * @param node the node to extract the properties from
     * @param level the name of the level (only used for logging)
     */
private static void extractEnhancementProperties(Map<String, Object> properties, Graph graph, BlankNodeOrIRI node, String level) {
    log.debug(" - extract {} properties from {}", level, node);
    Iterator<Triple> props = graph.filter(node, null, null);
    while (props.hasNext()) {
        Triple t = props.next();
        String propUri = t.getPredicate().getUnicodeString();
        if (propUri.startsWith(EHPROP_NS)) {
            String prop = propUri.substring(EHPROP_NS_LENGTH);
            RDFTerm resource = t.getObject();
            Object value = extractEnhancementPropertyValue(resource);
            if (value != null && !prop.isEmpty()) {
                Object current = properties.get(prop);
                if (log.isDebugEnabled()) {
                    if (current != null) {
                        log.debug(" ... append {} property '{}' to {}='{}'", new Object[] { level, value, prop, current });
                    } else {
                        log.debug(" ... add {} property {}='{}'", new Object[] { level, prop, value });
                    }
                }
                if (current instanceof Collection<?>) {
                    ((Collection) current).add(value);
                } else if (current != null) {
                    Collection<Object> col = new ArrayList<Object>(4);
                    col.add(current);
                    col.add(value);
                    properties.put(prop, col);
                } else {
                    properties.put(prop, value);
                }
            }
        }
    }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) Collection(java.util.Collection) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Aggregations

RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)126 IRI (org.apache.clerezza.commons.rdf.IRI)84 Triple (org.apache.clerezza.commons.rdf.Triple)70 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)48 Literal (org.apache.clerezza.commons.rdf.Literal)35 Test (org.junit.Test)35 HashSet (java.util.HashSet)30 HashMap (java.util.HashMap)28 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)26 Graph (org.apache.clerezza.commons.rdf.Graph)24 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)18 ArrayList (java.util.ArrayList)17 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)16 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)13 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)13 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)12 Collection (java.util.Collection)10 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)10 Lock (java.util.concurrent.locks.Lock)9 IOException (java.io.IOException)5