Search in sources :

Example 6 with Reference

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));
        }
    }
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ArrayList(java.util.ArrayList) Value(org.openrdf.model.Value) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) URI(org.openrdf.model.URI)

Example 7 with Reference

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());
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) BNode(org.openrdf.model.BNode) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Model(org.openrdf.model.Model) URIImpl(org.openrdf.model.impl.URIImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) URI(org.openrdf.model.URI) BNodeImpl(org.openrdf.model.impl.BNodeImpl) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 8 with Reference

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);
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ArrayList(java.util.ArrayList) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) Text(org.apache.stanbol.entityhub.servicesapi.model.Text)

Example 9 with Reference

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());
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URI(java.net.URI) Test(org.junit.Test)

Example 10 with Reference

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));
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URI(java.net.URI) Test(org.junit.Test)

Aggregations

Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)30 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)12 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)7 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)7 IRI (org.apache.clerezza.commons.rdf.IRI)5 URI (java.net.URI)4 URL (java.net.URL)3 URI (org.openrdf.model.URI)3 HashSet (java.util.HashSet)2 Language (org.apache.clerezza.commons.rdf.Language)2 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)2 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)2 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)2 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)2 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 Value (org.openrdf.model.Value)2 Date (java.util.Date)1