Search in sources :

Example 11 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text 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 12 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class InMemoryRepresentation method removeNaturalText.

@SuppressWarnings("unchecked")
@Override
public void removeNaturalText(String field, String text, String... languages) {
    if (field == null) {
        throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
    } else if (field.isEmpty()) {
        throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
    }
    Object values = representation.get(field);
    if (values == null) {
        return;
    }
    if (values instanceof Collection<?>) {
        int removed = 0;
        for (Iterator<Text> it = new TextIterator(valueFactory, ((Collection<Object>) values).iterator(), languages); it.hasNext(); ) {
            //go to the next element
            Text label = it.next();
            if (text.equals(label.getText())) {
                //and remove it
                it.remove();
                removed++;
            }
        }
        if (removed > 0) {
            //if some elements where removed
            //check if there is only a singe or no elements left for the field
            int size = ((Collection<Object>) values).size();
            if (size == 1) {
                representation.put(field, ((Collection<Object>) values).iterator().next());
            } else if (size < 1) {
                representation.remove(field);
            }
        }
    } else if (text.equals(getNaturalLanguageValue(values, languages))) {
        representation.remove(field);
    }
//else there is a single value that does not fit -> nothing todo
}
Also used : TextIterator(org.apache.stanbol.entityhub.servicesapi.util.TextIterator) Collection(java.util.Collection) Text(org.apache.stanbol.entityhub.servicesapi.model.Text)

Example 13 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testGetNaturalTextWithAnyLanguageByParsingAnEmptyArray.

@Test
public void testGetNaturalTextWithAnyLanguageByParsingAnEmptyArray() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = initNaturalLanguageTest(field);
    // test Iterator for any language (by parsing an empty list)
    Iterator<Text> allTexts = rep.get(field, new String[] {});
    assertNotNull(allTexts);
    assertTrue(asCollection(allTexts).size() == NL_TEST_all.size());
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) Test(org.junit.Test)

Example 14 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testRemoveAllNaturalLanguageValuesByParsingNullAsLanguageArray.

@Test
public void testRemoveAllNaturalLanguageValuesByParsingNullAsLanguageArray() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = initNaturalLanguageTest(field);
    // add a reference to ensure that only texts (and strings) are removed
    String testReference = "http://www.test.org/test";
    rep.addReference(field, testReference);
    // test removal of all natural language values by parsing only a single argument
    rep.removeAllNaturalText(field);
    Iterator<Text> texts = rep.get(field, (String[]) null);
    assertFalse(texts.hasNext());
    // text of the added reference is still present
    assertTrue(rep.get(field).hasNext());
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) Test(org.junit.Test)

Example 15 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testRemoveNaturalTextWithCorrectAndWrongLanguage.

@Test
public void testRemoveNaturalTextWithCorrectAndWrongLanguage() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = initNaturalLanguageTest(field);
    // remove a specific text, parse one correct and one wrong lang
    rep.removeNaturalText(field, NL_TEST_en2, "de", "en");
    Set<String> textSet = new HashSet<String>(NL_TEST_all);
    // remove all remaining values
    for (Iterator<Text> texts = rep.getText(field); texts.hasNext(); textSet.remove(texts.next().getText())) ;
    // and check that the value we expect to be removed is still in the set
    assertTrue(textSet.size() == 1);
    assertTrue(textSet.contains(NL_TEST_en2));
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Text (org.apache.stanbol.entityhub.servicesapi.model.Text)50 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)32 Test (org.junit.Test)24 HashSet (java.util.HashSet)14 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)12 ArrayList (java.util.ArrayList)11 IRI (org.apache.clerezza.commons.rdf.IRI)6 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)4 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)4 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)4 RepresentationTest (org.apache.stanbol.entityhub.test.model.RepresentationTest)4 Graph (org.apache.clerezza.commons.rdf.Graph)3 Language (org.apache.clerezza.commons.rdf.Language)3 Literal (org.apache.clerezza.commons.rdf.Literal)3 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)3 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)3 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)3 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)3 URI (java.net.URI)2 URL (java.net.URL)2