Search in sources :

Example 31 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class JsonModelWriter method convertFieldValueToJSON.

/**
     * The value to write. Special support for  {@link Reference} and {@link Text}.
     * The {@link #toString()} Method is used to write the "value" key.
     *
     * @param value the value
     * @return the {@link JSONObject} representing the value
     * @throws JSONException
     */
private JSONObject convertFieldValueToJSON(Object value) throws JSONException {
    JSONObject jValue = new JSONObject();
    if (value instanceof Reference) {
        jValue.put("type", "reference");
        jValue.put("xsd:datatype", DataTypeEnum.AnyUri.getShortName());
        jValue.put("value", ((Reference) value).getReference());
    } else if (value instanceof Text) {
        jValue.put("type", "text");
        jValue.put("xml:lang", ((Text) value).getLanguage());
        jValue.put("value", ((Text) value).getText());
    } else if (value instanceof Date) {
        jValue.put("type", "value");
        jValue.put("value", TimeUtils.toString(DataTypeEnum.DateTime, (Date) value));
        jValue.put("xsd:datatype", DataTypeEnum.DateTime.getShortName());
    } else {
        jValue.put("type", "value");
        Set<DataTypeEnum> dataTypes = DataTypeEnum.getPrimaryDataTypes(value.getClass());
        if (!dataTypes.isEmpty()) {
            jValue.put("xsd:datatype", dataTypes.iterator().next().getShortName());
        } else {
            jValue.put("xsd:datatype", DataTypeEnum.String.getShortName());
        }
        jValue.put("value", value);
    }
    return jValue;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) DataTypeEnum(org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) Date(java.util.Date)

Example 32 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RdfRepresentation method add.

//    protected IRI getRepresentationType(){
//        Iterator<IRI> it = this.graphNode.getIRIObjects(REPRESENTATION_TYPE_PROPERTY);
//        return it.hasNext()?it.next():null;
//    }
@Override
public void add(String field, Object value) {
    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 (value == null) {
        throw new IllegalArgumentException("NULL values are not supported by Representations");
    }
    IRI fieldIRI = new IRI(field);
    Collection<Object> values = new ArrayList<Object>();
    //process the parsed value with the Utility Method ->
    // this converts Objects as defined in the specification
    ModelUtils.checkValues(valueFactory, value, values);
    //We still need to implement support for specific types supported by this implementation
    for (Object current : values) {
        if (current instanceof RDFTerm) {
            //native support for Clerezza types!
            graphNode.addProperty(fieldIRI, (RDFTerm) current);
        } else if (current instanceof RdfReference) {
            //treat RDF Implementations special to avoid creating new instances
            graphNode.addProperty(fieldIRI, ((RdfReference) current).getIRI());
        } else if (current instanceof Reference) {
            graphNode.addProperty(fieldIRI, new IRI(((Reference) current).getReference()));
        } else if (current instanceof RdfText) {
            //treat RDF Implementations special to avoid creating new instances
            graphNode.addProperty(fieldIRI, ((RdfText) current).getLiteral());
        } else if (current instanceof Text) {
            addNaturalText(fieldIRI, ((Text) current).getText(), ((Text) current).getLanguage());
        } else {
            //else add an typed Literal!
            addTypedLiteral(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 33 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RdfRepresentationTest method testPlainLiteralToTextConversion.

/*--------------------------------------------------------------------------
     * Additional Tests for special Features of the Clerezza based implementation
     * 
     * This includes mainly support for additional types like PlainLiteral,
     * TypedLiteral, IRIs. The conversion to such types as well as getter for
     * such types.
     *--------------------------------------------------------------------------
     */
/**
     * {@link PlainLiteral} is used for natural language text in the Clerezza
     * RDF API. This tests if adding {@link PlainLiteral}s to the
     * {@link Representation#add(String, Object)} method makes them available
     * as {@link Text} instances via the {@link Representation} API (e.g. 
     * {@link Representation#get(String, String...)}).
     */
@Test
public void testPlainLiteralToTextConversion() {
    String field = "urn:test.RdfRepresentation:test.field";
    Literal noLangLiteral = new PlainLiteralImpl("A plain literal without Language");
    Literal enLiteral = new PlainLiteralImpl("An english literal", new Language("en"));
    Literal deLiteral = new PlainLiteralImpl("Ein Deutsches Literal", new Language("de"));
    Literal deATLiteral = new PlainLiteralImpl("Ein Topfen Verband hilft bei Zerrungen", new Language("de-AT"));
    Collection<Literal> plainLiterals = Arrays.asList(noLangLiteral, enLiteral, deLiteral, deATLiteral);
    Representation rep = createRepresentation(null);
    rep.add(field, plainLiterals);
    //now test, that the Plain Literals are available as natural language
    //tests via the Representation Interface!
    //1) one without a language
    Iterator<Text> noLangaugeTexts = rep.get(field, (String) null);
    assertTrue(noLangaugeTexts.hasNext());
    Text noLanguageText = noLangaugeTexts.next();
    assertEquals(noLangLiteral.getLexicalForm(), noLanguageText.getText());
    assertNull(noLanguageText.getLanguage());
    //only a single result
    assertFalse(noLangaugeTexts.hasNext());
    //2) one with a language
    Iterator<Text> enLangaugeTexts = rep.get(field, "en");
    assertTrue(enLangaugeTexts.hasNext());
    Text enLangageText = enLangaugeTexts.next();
    assertEquals(enLiteral.getLexicalForm(), enLangageText.getText());
    assertEquals(enLiteral.getLanguage().toString(), enLangageText.getLanguage());
    //only a single result
    assertFalse(enLangaugeTexts.hasNext());
    //3) test to get all natural language values
    Set<String> stringValues = new HashSet<String>();
    for (Literal plainLiteral : plainLiterals) {
        stringValues.add(plainLiteral.getLexicalForm());
    }
    Iterator<Text> texts = rep.getText(field);
    while (texts.hasNext()) {
        assertTrue(stringValues.remove(texts.next().getText()));
    }
    assertTrue(stringValues.isEmpty());
}
Also used : Language(org.apache.clerezza.commons.rdf.Language) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) Literal(org.apache.clerezza.commons.rdf.Literal) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 34 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RdfRepresentationTest method testPlainLiteralToTextConversion.

/*--------------------------------------------------------------------------
     * Additional Tests for special Features of the Clerezza based implementation
     * 
     * This includes mainly support for additional types like PlainLiteral,
     * TypedLiteral, UriRefs. The conversion to such types as well as getter for
     * such types.
     *--------------------------------------------------------------------------
     */
/**
     * {@link PlainLiteral} is used for natural language text in the Clerezza
     * RDF API. This tests if adding {@link PlainLiteral}s to the
     * {@link Representation#add(String, Object)} method makes them available
     * as {@link Text} instances via the {@link Representation} API (e.g. 
     * {@link Representation#get(String, String...)}).
     */
@Test
public void testPlainLiteralToTextConversion() {
    String field = "urn:test.RdfRepresentation:test.field";
    Literal noLangLiteral = valueFactory.getSesameFactory().createLiteral("A plain literal without Language");
    Literal enLiteral = valueFactory.getSesameFactory().createLiteral("An english literal", "en");
    Literal deLiteral = valueFactory.getSesameFactory().createLiteral("Ein Deutsches Literal", "de");
    Literal deATLiteral = valueFactory.getSesameFactory().createLiteral("Ein Topfen Verband hilft bei Zerrungen", "de-AT");
    Collection<Literal> plainLiterals = Arrays.asList(noLangLiteral, enLiteral, deLiteral, deATLiteral);
    Representation rep = createRepresentation(null);
    rep.add(field, plainLiterals);
    //now test, that the Plain Literals are available as natural language
    //tests via the Representation Interface!
    //1) one without a language
    Iterator<Text> noLangaugeTexts = rep.get(field, (String) null);
    assertTrue(noLangaugeTexts.hasNext());
    Text noLanguageText = noLangaugeTexts.next();
    assertEquals(noLangLiteral.getLabel(), noLanguageText.getText());
    assertNull(noLanguageText.getLanguage());
    //only a single result
    assertFalse(noLangaugeTexts.hasNext());
    //2) one with a language
    Iterator<Text> enLangaugeTexts = rep.get(field, "en");
    assertTrue(enLangaugeTexts.hasNext());
    Text enLangageText = enLangaugeTexts.next();
    assertEquals(enLiteral.getLabel(), enLangageText.getText());
    assertEquals(enLiteral.getLanguage(), enLangageText.getLanguage());
    //only a single result
    assertFalse(enLangaugeTexts.hasNext());
    //3) test to get all natural language values
    Set<String> stringValues = new HashSet<String>();
    for (Literal plainLiteral : plainLiterals) {
        stringValues.add(plainLiteral.getLabel());
    }
    Iterator<Text> texts = rep.getText(field);
    while (texts.hasNext()) {
        assertTrue(stringValues.remove(texts.next().getText()));
    }
    assertTrue(stringValues.isEmpty());
}
Also used : Literal(org.openrdf.model.Literal) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 35 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RdfRepresentation method add.

@Override
public void add(String field, Object value) throws IllegalArgumentException {
    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 (value == null) {
        throw new IllegalArgumentException("NULL values are not supported by Representations");
    }
    URI property = sesameFactory.createURI(field);
    Collection<Object> values = new ArrayList<Object>();
    //process the parsed value with the Utility Method ->
    // this converts Objects as defined in the specification
    ModelUtils.checkValues(factory, value, values);
    //We still need to implement support for specific types supported by this implementation
    for (Object current : values) {
        if (current instanceof Value) {
            //native support for Sesame types!
            addValue(property, (Value) current);
        } else if (current instanceof RdfWrapper) {
            //for Sesame RDF wrapper we can directly use the Value
            addValue(property, ((RdfWrapper) current).getValue());
        } else if (current instanceof Reference) {
            addValue(property, sesameFactory.createURI(((Reference) current).getReference()));
        } else if (current instanceof Text) {
            addValue(property, sesameFactory.createLiteral(((Text) current).getText(), ((Text) current).getLanguage()));
        } else {
            //else add an typed Literal!
            addValue(property, createTypedLiteral(current));
        }
    }
}
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)

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