Search in sources :

Example 6 with Text

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;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) DataTypeEnum(org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 7 with Text

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());
}
Also used : Literal(org.openrdf.model.Literal) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 8 with Text

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));
        }
    }
}
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 9 with Text

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);
        }
    }
}
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 10 with Text

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());
}
Also used : Literal(org.apache.clerezza.commons.rdf.Literal) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) 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