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();
}
}
}
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
}
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());
}
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());
}
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));
}
Aggregations