use of org.apache.stanbol.entityhub.servicesapi.model.Reference 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.Reference in project stanbol by apache.
the class RdfRepresentationTest method testBNodeFiltering.
/**
* Test for STANBOL-1301
*/
@Test
public void testBNodeFiltering() {
URI concept = new URIImpl("http://example.org/mySkos#Concept123");
Representation r = createRepresentation(concept.stringValue());
assertTrue(r instanceof RdfRepresentation);
RdfRepresentation rep = (RdfRepresentation) r;
//add the example as listed in STANBOL-1301 to directly to the
//Sesame Model backing the created Representation
Model m = rep.getModel();
m.add(concept, RDF.TYPE, SKOS.CONCEPT);
m.add(concept, DCTERMS.IDENTIFIER, new LiteralImpl("123"));
m.add(concept, SKOS.PREF_LABEL, new LiteralImpl("Concept123", "en"));
BNode note1 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19576");
m.add(concept, SKOS.SCOPE_NOTE, note1);
m.add(note1, DCTERMS.CREATOR, new LiteralImpl("User1"));
m.add(note1, DCTERMS.CREATED, new LiteralImpl("2013-03-03T02:02:02Z", XMLSchema.DATETIME));
m.add(note1, RDFS.COMMENT, new LiteralImpl("The scope of this example global", "en"));
BNode note2 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19634");
m.add(concept, SKOS.SCOPE_NOTE, note2);
m.add(note2, DCTERMS.CREATOR, new LiteralImpl("User2"));
m.add(note2, DCTERMS.CREATED, new LiteralImpl("2013-03-03T04:04:04Z", XMLSchema.DATETIME));
m.add(note2, RDFS.COMMENT, new LiteralImpl("Der Geltungsbereich ist Global", "de"));
//now assert that BNodes are not reported via the Representation API
Iterator<Object> scopeNotes = rep.get(SKOS.SCOPE_NOTE.stringValue());
assertFalse(scopeNotes.hasNext());
Iterator<Reference> scopeNoteRefs = rep.getReferences(SKOS.SCOPE_NOTE.stringValue());
assertFalse(scopeNoteRefs.hasNext());
}
use of org.apache.stanbol.entityhub.servicesapi.model.Reference 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.Reference in project stanbol by apache.
the class RepresentationTest method testURIToReferenceConversion.
/**
* Tests if {@link Reference} instances are correctly generated for {@link URI}. This test also depends on
* the correct implementation of the {@link Reference#equals(Object)} method
*
* @throws URISyntaxException
*/
@Test
public void testURIToReferenceConversion() throws URISyntaxException {
String field = "urn:the.field:used.for.this.Test";
URI uri = new URI("http://www.test.org/uriTest");
ValueFactory vf = getValueFactory();
Representation rep = createRepresentation(null);
// test conversion
rep.add(field, uri);
Iterator<Reference> refs = rep.getReferences(field);
assertTrue(refs.hasNext());
assertEquals(refs.next().getReference(), uri.toString());
assertFalse(refs.hasNext());
// test multiple adds do not generate duplicate References
rep.add(field, uri);
assertTrue(asCollection(rep.get(field)).size() == 1);
// test adding a equivalent reference
rep.add(field, vf.createReference(uri.toString()));
assertTrue(asCollection(rep.get(field)).size() == 1);
// test removing
rep.remove(field, uri);
assertFalse(rep.get(field).hasNext());
}
use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.
the class RepresentationTest method testFieldRemoval.
@Test
public void testFieldRemoval() throws URISyntaxException {
String field = "urn:the.field:used.for.this.Test";
ValueFactory vf = getValueFactory();
Representation rep = createRepresentation(null);
// Test removal for References
String strRef = "urn:testValue";
rep.addReference(field, strRef);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.removeReference(field, strRef);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
Reference ref = vf.createReference("urn:testValue2");
rep.add(field, ref);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.remove(field, ref);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
// test removal for texts (with and without language)
String strText = "test text";
String strTextLang = "en";
rep.addNaturalText(field, strText, strTextLang);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.removeNaturalText(field, strText, strTextLang);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
String strTextNoLang = "test text without lang";
rep.addNaturalText(field, strTextNoLang);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.removeNaturalText(field, strTextNoLang);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
// there is also the possibility to explicitly parse null as language
// could internally case differences however externally this is the same
rep.addNaturalText(field, strTextNoLang, (String) null);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.removeNaturalText(field, strTextNoLang, (String) null);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
Text text = vf.createText("Das ist ein Text zum testen des Text Objektes", "de");
rep.add(field, text);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.remove(field, text);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
// Test a dataTypes values
Integer intValue = 42;
rep.add(field, intValue);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.remove(field, intValue);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
// Some Values are converted by the add(String field,Object value) Method
// to other data types. This MUST also be assured for removal
// NOTE: testing the conversions is done in other test methods!
URI testURI = new URI("http://www.test.org/test");
rep.add(field, testURI);
assertTrue(asCollection(rep.getFieldNames()).contains(field));
rep.remove(field, testURI);
assertFalse(asCollection(rep.getFieldNames()).contains(field));
}
Aggregations