use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class AbstractBackend method listSubjects.
@Override
public Collection<Object> listSubjects(Object property, Object object) {
FieldQuery query = createQuery();
if (this.isURI(object)) {
query.setConstraint(property.toString(), new ReferenceConstraint(object.toString()));
} else if (object instanceof Text) {
Text text = (Text) object;
TextConstraint constraint;
if (text.getLanguage() == null) {
constraint = new TextConstraint(text.getText(), PatternType.none, true);
} else {
constraint = new TextConstraint(text.getText(), PatternType.none, true, text.getLanguage());
}
query.setConstraint(property.toString(), constraint);
} else {
Set<DataTypeEnum> dataTypes = DataTypeEnum.getPrimaryDataTypes(object.getClass());
if (dataTypes == null || dataTypes.isEmpty()) {
query.setConstraint(property.toString(), new ValueConstraint(object));
} else {
Collection<String> types = new ArrayList<String>(dataTypes.size());
for (DataTypeEnum type : dataTypes) {
types.add(type.getUri());
}
query.setConstraint(property.toString(), new ValueConstraint(object, types));
}
}
query.setLimit(Integer.valueOf(DEFAULT_MAX_SELECT));
QueryResultList<String> results;
try {
results = query(query);
} catch (EntityhubException e) {
throw new IllegalStateException("Unable to query for resources with value '" + object + "' on property '" + property + "'!", e);
}
Collection<Object> references;
if (results.isEmpty()) {
references = Collections.emptySet();
} else if (results.size() == 1) {
//assuming that a single result is a likely case
references = Collections.singleton((Object) getValueFactory().createReference(results.iterator().next()));
} else {
int offset = 0;
references = new HashSet<Object>(results.size());
for (String result : results) {
references.add(getValueFactory().createReference(result));
}
while (results.size() >= DEFAULT_MAX_SELECT && references.size() <= DEFAULT_MAX_RESULTS - DEFAULT_MAX_SELECT) {
offset = offset + results.size();
query.setOffset(offset);
try {
results = query(query);
} catch (EntityhubException e) {
throw new IllegalStateException("Unable to query for resources with value '" + object + "' on property '" + property + "'!", e);
}
for (String result : results) {
references.add(getValueFactory().createReference(result));
}
}
}
return references;
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RdfRepresentationTest method testTypedLiteralToTextConversion.
/**
* {@link TypedLiteral}s are used to represent literal values for different
* xsd dataTypes within Clerezza. This method tests of {@link TypedLiteral}s
* with the data type xsd:string are correctly treated like {@link String}
* values. This tests especially if they are treated as natural language
* texts without language.
*/
@Test
public void testTypedLiteralToTextConversion() {
String field = "urn:test.RdfRepresentation:test.field";
Literal stringLiteral = valueFactory.getSesameFactory().createLiteral("This is a stirng value", XMLSchema.STRING);
//also add an integer to test that other typed literals are not used as texts
Literal integerLiteral = valueFactory.getSesameFactory().createLiteral(5);
Representation rep = createRepresentation(null);
rep.add(field, Arrays.asList(stringLiteral, integerLiteral));
//test if the literal is returned when asking for natural language text without language
Iterator<Text> noLangTexts = rep.get(field, (String) null);
assertTrue(noLangTexts.hasNext());
assertEquals(stringLiteral.getLabel(), noLangTexts.next().getText());
assertFalse(noLangTexts.hasNext());
//test that string literals are returned when asking for all natural language text values
Iterator<Text> texts = rep.getText(field);
assertTrue(texts.hasNext());
assertEquals(stringLiteral.getLabel(), texts.next().getText());
assertFalse(texts.hasNext());
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RdfRepresentation method remove.
@Override
public void remove(String field, Object parsedValue) {
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");
}
if (parsedValue == null) {
log.warn("NULL parsed as value in remove method for symbol " + getId() + " and field " + field + " -> call ignored");
return;
}
URI property = sesameFactory.createURI(field);
Collection<Object> values = new ArrayList<Object>();
ModelUtils.checkValues(factory, parsedValue, values);
for (Object value : values) {
if (value instanceof Value) {
//native support for Sesame types!
removeValue(property, (Value) value);
} else if (value instanceof RdfWrapper) {
//for Sesame RDF wrapper we can directly use the Value
removeValue(property, ((RdfWrapper) value).getValue());
} else if (value instanceof Reference) {
removeValue(property, sesameFactory.createURI(((Reference) value).getReference()));
} else if (value instanceof Text) {
removeValue(property, sesameFactory.createLiteral(((Text) value).getText(), ((Text) value).getLanguage()));
} else {
//else add an typed Literal!
removeValue(property, createTypedLiteral(value));
}
}
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RdfRepresentation method remove.
@Override
public void remove(String field, Object parsedValue) {
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");
}
if (parsedValue == null) {
log.warn("NULL parsed as value in remove method for symbol " + getId() + " and field " + field + " -> call ignored");
return;
}
IRI fieldIRI = new IRI(field);
Collection<Object> removeValues = new ArrayList<Object>();
ModelUtils.checkValues(valueFactory, parsedValue, removeValues);
//We still need to implement support for specific types supported by this implementation
for (Object current : removeValues) {
if (current instanceof RDFTerm) {
//native support for Clerezza types!
graphNode.deleteProperty(fieldIRI, (RDFTerm) current);
} else if (current instanceof RdfReference) {
//treat RDF Implementations special to avoid creating new instances
graphNode.deleteProperty(fieldIRI, ((RdfReference) current).getIRI());
} else if (current instanceof Reference) {
graphNode.deleteProperty(fieldIRI, new IRI(((Reference) current).getReference()));
} else if (current instanceof RdfText) {
//treat RDF Implementations special to avoid creating new instances
graphNode.deleteProperty(fieldIRI, ((RdfText) current).getLiteral());
} else if (current instanceof Text) {
removeNaturalText(field, ((Text) current).getText(), ((Text) current).getLanguage());
} else {
//else add an typed Literal!
removeTypedLiteral(fieldIRI, current);
}
}
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RdfRepresentationTest method testTypedLiteralToTextConversion.
/**
* {@link TypedLiteral}s are used to represent literal values for different
* xsd dataTypes within Clerezza. This method tests of {@link TypedLiteral}s
* with the data type xsd:string are correctly treated like {@link String}
* values. This tests especially if they are treated as natural language
* texts without language.
*/
@Test
public void testTypedLiteralToTextConversion() {
String field = "urn:test.RdfRepresentation:test.field";
Literal stringLiteral = literalFactory.createTypedLiteral("This is a stirng value");
//also add an integer to test that other typed literals are not used as texts
Literal integerLiteral = literalFactory.createTypedLiteral(new Integer(5));
Representation rep = createRepresentation(null);
rep.add(field, Arrays.asList(stringLiteral, integerLiteral));
//test if the literal is returned when asking for natural language text without language
Iterator<Text> noLangTexts = rep.get(field, (String) null);
assertTrue(noLangTexts.hasNext());
assertEquals(stringLiteral.getLexicalForm(), noLangTexts.next().getText());
assertFalse(noLangTexts.hasNext());
//test that string literals are returned when asking for all natural language text values
Iterator<Text> texts = rep.getText(field);
assertTrue(texts.hasNext());
assertEquals(stringLiteral.getLexicalForm(), texts.next().getText());
assertFalse(texts.hasNext());
}
Aggregations