You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2013/10/08 13:07:18 UTC
svn commit: r1530223 - in /stanbol/trunk/entityhub:
generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/
model/sesame/ model/sesame/src/ model/sesame/src/main/
model/sesame/src/main/java/ model/sesame/src/main/java/org/
model/sesame/src/...
Author: rwesten
Date: Tue Oct 8 11:07:17 2013
New Revision: 1530223
URL: http://svn.apache.org/r1530223
Log:
STANBOL-1166: Implementation of a Sesame model for the Entityhub; minor: Updated the RepresentationTest so that checks for lanugages are case insensitive (as defined by ISO 369)
Added:
stanbol/trunk/entityhub/model/sesame/
stanbol/trunk/entityhub/model/sesame/pom.xml
stanbol/trunk/entityhub/model/sesame/src/
stanbol/trunk/entityhub/model/sesame/src/main/
stanbol/trunk/entityhub/model/sesame/src/main/java/
stanbol/trunk/entityhub/model/sesame/src/main/java/org/
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfBNode.java
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfReference.java
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentation.java
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfText.java
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactory.java
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfWrapper.java
stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/ValueTypeFilter.java
stanbol/trunk/entityhub/model/sesame/src/main/resources/
stanbol/trunk/entityhub/model/sesame/src/test/
stanbol/trunk/entityhub/model/sesame/src/test/java/
stanbol/trunk/entityhub/model/sesame/src/test/java/org/
stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/
stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/
stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/
stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/
stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/
stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentationTest.java
stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactoryTest.java
stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/impl/
stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/impl/ResourceAdapterTest.java
Modified:
stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/RepresentationTest.java
stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/ValueFactoryTest.java
Modified: stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/RepresentationTest.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/RepresentationTest.java?rev=1530223&r1=1530222&r2=1530223&view=diff
==============================================================================
--- stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/RepresentationTest.java (original)
+++ stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/RepresentationTest.java Tue Oct 8 11:07:17 2013
@@ -16,11 +16,11 @@
*/
package org.apache.stanbol.entityhub.test.model;
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertFalse;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertNull;
-import static junit.framework.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import static org.apache.stanbol.entityhub.test.Utils.asCollection;
import java.net.MalformedURLException;
@@ -840,7 +840,8 @@ public abstract class RepresentationTest
Set<String> textSet = new HashSet<String>(Arrays.asList(NL_TEST_de, NL_TEST_de_AT));
while (allDeTexts.hasNext()) {
Text text = allDeTexts.next();
- assertTrue(text.getLanguage().equals("de") || text.getLanguage().equals("de-AT"));
+ assertTrue(text.getLanguage().equalsIgnoreCase("de") ||
+ text.getLanguage().equalsIgnoreCase("de-AT"));
assertTrue(textSet.remove(text.getText())); // remove the found
}
assertTrue(textSet.isEmpty()); // all texts found
Modified: stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/ValueFactoryTest.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/ValueFactoryTest.java?rev=1530223&r1=1530222&r2=1530223&view=diff
==============================================================================
--- stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/ValueFactoryTest.java (original)
+++ stanbol/trunk/entityhub/generic/test/src/main/java/org/apache/stanbol/entityhub/test/model/ValueFactoryTest.java Tue Oct 8 11:07:17 2013
@@ -16,10 +16,10 @@
*/
package org.apache.stanbol.entityhub.test.model;
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertNotSame;
-import static junit.framework.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertTrue;
import java.net.MalformedURLException;
import java.net.URI;
Added: stanbol/trunk/entityhub/model/sesame/pom.xml
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/pom.xml?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/pom.xml (added)
+++ stanbol/trunk/entityhub/model/sesame/pom.xml Tue Oct 8 11:07:17 2013
@@ -0,0 +1,119 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>apache-stanbol-entityhub</artifactId>
+ <version>0.12.0-SNAPSHOT</version>
+ <relativePath>../..</relativePath>
+ </parent>
+
+ <artifactId>org.apache.stanbol.entityhub.model.sesame</artifactId>
+ <packaging>bundle</packaging>
+
+ <name>Apache Stanbol Entityhub Model Sesame</name>
+ <description>This bundle provides implementation of the Entityhub Representation interface
+ based on sesame.
+ </description>
+
+ <inceptionYear>2013</inceptionYear>
+ <licenses>
+ <license>
+ <name>Apache Software License, Version 2.0</name>
+ <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
+ <distribution>repo</distribution>
+ <comments>A business-friendly OSS license</comments>
+ </license>
+ </licenses>
+
+ <scm>
+ <connection>
+ scm:svn:http://svn.apache.org/repos/asf/stanbol/trunk/entityhub/model/sesame
+ </connection>
+ <developerConnection>
+ scm:svn:https://svn.apache.org/repos/asf/stanbol/trunk/entityhub/model/sesame
+ </developerConnection>
+ <url>http://stanbol.apache.org</url>
+ </scm>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Import-Package>
+ org.apache.stanbol.entityhub.servicesapi.model; provide:=true; version="[0.11,0.13)",
+ org.apache.stanbol.entityhub.servicesapi.util; provide:=true; version="[0.11,0.13)",
+ *
+ </Import-Package>
+ <Export-Package>
+ org.apache.stanbol.entityhub.model.sesame;version=${project.version}
+ </Export-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.entityhub.servicesapi</artifactId>
+ <version>0.11.0</version>
+ </dependency>
+ <dependency> <!-- the sesame repository API -->
+ <groupId>org.openrdf.sesame</groupId>
+ <artifactId>sesame-repository-api</artifactId>
+ <version>2.7.7</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-collections</groupId>
+ <artifactId>commons-collections</artifactId>
+ </dependency>
+
+ <!-- for tests -->
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.entityhub.test</artifactId>
+ <version>0.12.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+ <!-- TODO: will need a sesame repository for testing -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
Added: stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfBNode.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfBNode.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfBNode.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfBNode.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,47 @@
+package org.apache.stanbol.entityhub.model.sesame;
+
+import org.apache.stanbol.entityhub.servicesapi.model.Reference;
+import org.openrdf.model.BNode;
+import org.openrdf.model.Value;
+
+/**
+ * Internally used to handle BNodes. Externally mapped to {@link Reference}.
+ * <p>
+ * <b>NOTE:</b> this does not aim to fully support BNodes
+ * @author Rupert Westenthaler
+ *
+ */
+public class RdfBNode implements Reference, RdfWrapper {
+
+ private BNode node;
+
+ protected RdfBNode(BNode node) {
+ this.node = node;
+ }
+
+ @Override
+ public Value getValue() {
+ return node;
+ }
+
+ @Override
+ public String getReference() {
+ return node.getID();
+ }
+
+ @Override
+ public int hashCode() {
+ return node.hashCode();
+ }
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof Reference &&
+ getReference().equals(((Reference)obj).getReference());
+ }
+
+ @Override
+ public String toString() {
+ return node.toString();
+ }
+
+}
\ No newline at end of file
Added: stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfReference.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfReference.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfReference.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfReference.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,51 @@
+package org.apache.stanbol.entityhub.model.sesame;
+
+import org.apache.stanbol.entityhub.servicesapi.model.Reference;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+
+/**
+ * A {@link Reference} implementation backed by a Sesame {@link URI}
+ * @author Rupert Westenthaler
+ *
+ */
+public class RdfReference implements Reference, RdfWrapper {
+
+ private final URI uri;
+
+
+ protected RdfReference(URI uri){
+ this.uri = uri;
+ }
+
+ @Override
+ public String getReference() {
+ return uri.stringValue();
+ }
+ /**
+ * The wrapped Sesame {@link URI}
+ * @return the URI
+ */
+ public URI getURI() {
+ return uri;
+ }
+ @Override
+ public Value getValue() {
+ return uri;
+ }
+
+ @Override
+ public int hashCode() {
+ return uri.hashCode();
+ }
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof Reference &&
+ getReference().equals(((Reference)obj).getReference());
+ }
+
+ @Override
+ public String toString() {
+ return uri.toString();
+ }
+}
\ No newline at end of file
Added: stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentation.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentation.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentation.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentation.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,640 @@
+package org.apache.stanbol.entityhub.model.sesame;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Iterator;
+
+import javax.xml.datatype.Duration;
+import javax.xml.datatype.XMLGregorianCalendar;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.apache.commons.collections.Transformer;
+import org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum;
+import org.apache.stanbol.entityhub.servicesapi.model.Reference;
+import org.apache.stanbol.entityhub.servicesapi.model.Representation;
+import org.apache.stanbol.entityhub.servicesapi.model.Text;
+import org.apache.stanbol.entityhub.servicesapi.model.UnsupportedTypeException;
+import org.apache.stanbol.entityhub.servicesapi.util.ModelUtils;
+import org.openrdf.model.BNode;
+import org.openrdf.model.Literal;
+import org.openrdf.model.Model;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.model.datatypes.XMLDatatypeUtil;
+import org.openrdf.model.vocabulary.XMLSchema;
+import org.openrdf.repository.RepositoryException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A {@link Representation} implementation backed by a Sesame {@link Model}
+ * @author Rupert Westenthaler
+ */
+public class RdfRepresentation implements Representation, RdfWrapper {
+
+ private Logger log = LoggerFactory.getLogger(RdfRepresentation.class);
+
+ private URI subject;
+ private final Model model;
+ private final RdfValueFactory factory;
+ private final org.openrdf.model.ValueFactory sesameFactory;
+
+ /**
+ * Emits {@link Statement#getObject()}
+ */
+ protected Transformer objectTransFormer = new Transformer() {
+
+ @Override
+ public Value transform(Object input) {
+ return ((Statement)input).getObject();
+ }
+ };
+
+ /**
+ * Emits {@link Value#stringValue()}
+ */
+ protected Transformer stringTransformer = new Transformer() {
+
+ @Override
+ public String transform(Object input) {
+ return ((Value)input).stringValue();
+ }
+ };
+
+ /**
+ * A {@link Value} to {@link Object} transformer intended to be used for
+ * {@link IteratorUtils#transformedIterator(Iterator, Transformer)} to
+ * convert
+ */
+ protected Transformer sesameTransformer = new Transformer() {
+
+ @Override
+ public Object transform(Object input) {
+ if(input instanceof Value){
+ Value sesameValue = (Value) input;
+ if(sesameValue instanceof URI){
+ return new RdfReference((URI)sesameValue);
+ } else if(sesameValue instanceof Literal){
+ Literal literal = (Literal)sesameValue;
+ if(literal.getDatatype() == null){ //TODO: adapt to RDF1.1
+ return new RdfText(literal);
+ } else {
+ return transformTypedLiteral(literal);
+ }
+ } else {
+ return new RdfBNode((BNode)sesameValue);
+ }
+ } else { //do not transform objects of other types (incl. null)
+ return input;
+ }
+ }
+ };
+ /**
+ * Transforms typed literals with datatype {@link XMLSchema#STRING} to
+ * {@link Text} instances as required by some {@link Representation}
+ * methods. This transformer is usually used in front of the
+ * {@link #sesameTransformer}.
+ */
+ protected Transformer stringLiteral2TextTransformer = new Transformer() {
+
+ @Override
+ public Object transform(Object input) {
+ if(input instanceof Literal && XMLSchema.STRING.equals(((Literal)input).getDatatype())){
+ return new RdfText((Literal)input);
+ }
+ return input;
+ }
+ };
+ /**
+ * Creates a {@link Representation} for the parsed subject. Data will be
+ * added to the model.
+ * @param subject the subject
+ * @param model the model
+ * @param factory the factory
+ */
+ protected RdfRepresentation(URI subject, Model model, RdfValueFactory factory){
+ this.subject = subject;
+ this.model = model;
+ this.factory = factory;
+ this.sesameFactory = factory.getSesameFactory();
+ }
+ /**
+ * Transforms a typed literal to the according java type.
+ * @param literal
+ * @return
+ */
+ protected Object transformTypedLiteral(Literal literal){
+ URI dataType = literal.getDatatype();
+ if(XMLSchema.INT.equals(dataType)){
+ return literal.intValue();
+ } else if(XMLSchema.LONG.equals(dataType)){
+ return literal.longValue();
+ } else if(XMLSchema.FLOAT.equals(dataType)){
+ return literal.floatValue();
+ } else if(XMLSchema.DOUBLE.equals(dataType)){
+ return literal.doubleValue();
+ } else if(XMLSchema.BOOLEAN.equals(dataType)){
+ return literal.booleanValue();
+ }else if(XMLSchema.INTEGER.equals(dataType)){
+ return literal.integerValue();
+ } else if(XMLSchema.DECIMAL.equals(dataType)){
+ return literal.decimalValue();
+ } else if(XMLSchema.STRING.equals(dataType)){ //explicit handle string
+ //to avoid going to a lot of equals checks
+ return literal.stringValue();
+ } else if(XMLDatatypeUtil.isCalendarDatatype(dataType)){
+ return literal.calendarValue().toGregorianCalendar().getTime();
+ } else if(XMLSchema.BYTE.equals(dataType)){
+ return literal.byteValue();
+ } else if(XMLSchema.SHORT.equals(dataType)){
+ return literal.shortValue();
+ //Start with the more exotic types at the end (for performance reasons)
+ } else if(XMLSchema.NON_NEGATIVE_INTEGER.equals(dataType) ||
+ XMLSchema.NON_POSITIVE_INTEGER.equals(dataType) ||
+ XMLSchema.NEGATIVE_INTEGER.equals(dataType) ||
+ XMLSchema.POSITIVE_INTEGER.equals(dataType)){
+ return literal.longValue();
+ } else if(XMLSchema.GDAY.equals(dataType) ||
+ XMLSchema.GMONTH.equals(dataType) ||
+ XMLSchema.GMONTHDAY.equals(dataType) ||
+ XMLSchema.GYEAR.equals(dataType) ||
+ XMLSchema.GYEARMONTH.equals(dataType)){
+ return literal.calendarValue().toGregorianCalendar().getTime();
+ } else if(XMLSchema.UNSIGNED_BYTE.equals(dataType)){
+ return literal.shortValue();
+ } else if(XMLSchema.UNSIGNED_SHORT.equals(dataType)){
+ return literal.intValue();
+ } else if(XMLSchema.UNSIGNED_INT.equals(dataType)){
+ return literal.longValue();
+ } else{
+ return literal.stringValue();
+ }
+ }
+ @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));
+ }
+ }
+ }
+ /**
+ * Converts a Java object to a Sesame typed Literal
+ * @param value the java value
+ * @return the Sesame literal
+ * @throws IllegalArgumentException it the parsed object could not be
+ * converted to a Sesame typed literal
+ */
+ private Literal createTypedLiteral(Object value){
+ final Literal literal;
+ if(value instanceof Number){
+ Number n = (Number)value;
+ if(value instanceof Integer){
+ literal = sesameFactory.createLiteral(n.intValue());
+ } else if(value instanceof Float){
+ literal = sesameFactory.createLiteral(n.floatValue());
+ } else if(value instanceof Long){
+ literal = sesameFactory.createLiteral(n.longValue());
+ } else if(value instanceof Double){
+ literal = sesameFactory.createLiteral(n.doubleValue());
+ } else if(value instanceof Short){
+ literal = sesameFactory.createLiteral(n.shortValue());
+ } else if(value instanceof Byte){
+ literal = sesameFactory.createLiteral(n.byteValue());
+ } else {
+ literal = null;
+ }
+ } else if(value instanceof Boolean){
+ literal = sesameFactory.createLiteral(((Boolean)value).booleanValue());
+ } else if(value instanceof Date){
+ literal = sesameFactory.createLiteral((Date)value);
+ } else if(value instanceof BigInteger){
+ literal = sesameFactory.createLiteral(value.toString(),
+ DataTypeEnum.Integer.getUri());
+ } else if(value instanceof BigDecimal){
+ literal = sesameFactory.createLiteral(value.toString(),
+ DataTypeEnum.Decimal.getUri());
+ } else if(value instanceof XMLGregorianCalendar){
+ literal = sesameFactory.createLiteral((XMLGregorianCalendar)value);
+ } else if(value instanceof Duration){
+ literal = sesameFactory.createLiteral(value.toString(),
+ XMLSchema.DURATION);
+ } else if(value instanceof String){ //String type literals
+ literal = sesameFactory.createLiteral(value.toString(),
+ XMLSchema.STRING);
+ } else {
+ literal = null;
+ }
+ if(literal == null){
+ throw new IllegalArgumentException("Unable to convert value '"
+ + value + "' to a Sesame typed literal because the java type "
+ + value.getClass().getName() + " can not be mapped to an "
+ + "XML DataType.");
+ }
+ return literal;
+ }
+
+ @Override
+ public void addNaturalText(String field, String text, String... languages) 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(text == null){
+ throw new IllegalArgumentException("NULL values are not supported by Representations");
+ }
+ URI property = sesameFactory.createURI(field);
+ if(languages == null || languages.length == 0){
+ languages = new String []{null};
+ }
+ for(String language : languages){
+ Literal value = sesameFactory.createLiteral(text, language);
+ addValue(property, value);
+ }
+ }
+
+ @Override
+ public void addReference(String field, String reference) 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(reference == null){
+ throw new IllegalArgumentException("NULL values are not supported by Representations");
+ } else if (reference.isEmpty()) {
+ throw new IllegalArgumentException("References MUST NOT be empty!");
+ }
+ addValue(sesameFactory.createURI(field), sesameFactory.createURI(reference));
+ }
+ /**
+ * Adds a value to a property and handles a possible
+ * {@link RepositoryException} while doing so
+ * @param property
+ * @param value
+ * @throws IllegalStateException in case of a {@link RepositoryException}
+ * while adding the value.
+ */
+ private void addValue(URI property, Value value) {
+ model.add(subject, property, value);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Iterator<Object> get(String field) 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");
+ }
+ URI property = sesameFactory.createURI(field);
+ return IteratorUtils.transformedIterator(
+ IteratorUtils.transformedIterator(
+ model.filter(subject, property, null).iterator(),
+ objectTransFormer), // get the object from the statement
+ sesameTransformer); // transform the values
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> Iterator<T> get(String field, final Class<T> type) throws UnsupportedTypeException {
+ 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");
+ }
+ URI property = sesameFactory.createURI(field);
+ //filter for values that are compatible with the parsed type
+ Iterator<?> iterator = IteratorUtils.filteredIterator(
+ IteratorUtils.transformedIterator(
+ model.filter(subject, property, null).iterator(),
+ objectTransFormer), // get the object from the statement
+ new ValueTypeFilter<T>(type));
+ if(!Value.class.isAssignableFrom(type)){
+ //if the requested type is not a Sesame value, we need also to
+ //transform results
+ iterator = IteratorUtils.transformedIterator(
+ iterator, // the already filtered values
+ sesameTransformer); // need to be transformed
+ }
+ return (Iterator<T>)iterator;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Iterator<Text> get(String field, String...languages) {
+ 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");
+ }
+ URI property = sesameFactory.createURI(field);
+ return IteratorUtils.transformedIterator(
+ IteratorUtils.transformedIterator(
+ IteratorUtils.filteredIterator(
+ IteratorUtils.transformedIterator(
+ model.filter(subject, property, null).iterator(),
+ objectTransFormer), // get the object from the statement
+ new ValueTypeFilter<Text>(languages)), //filter languages
+ stringLiteral2TextTransformer), //transform strings to Text
+ sesameTransformer); //transform to Text instances
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Iterator<String> getFieldNames() {
+ return (Iterator<String>)IteratorUtils.transformedIterator(
+ model.predicates().iterator(), stringTransformer);
+ }
+
+ @Override
+ public Object getFirst(String field) {
+ 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");
+ }
+ Iterator<Object> it = get(field);
+ if(it.hasNext()){
+ return it.next();
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public <T> T getFirst(String field, Class<T> type) throws UnsupportedTypeException {
+ 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");
+ }
+ Iterator<T> it = get(field,type);
+ if(it.hasNext()){
+ return it.next();
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public Text getFirst(String field, String...languages) {
+ 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(languages == null){
+ log.debug("NULL parsed as languages -> replacing with \"new String []{null}\"" +
+ " -> assuming a missing explicit cast to (String) in the var arg");
+ languages = new String []{null};
+ }
+ Iterator<Text> it = get(field,languages);
+ if(it.hasNext()){
+ return it.next();
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public Reference getFirstReference(String field) {
+ 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");
+ }
+ Iterator<Reference> it = getReferences(field);
+ return it.hasNext()?it.next():null;
+ }
+
+ @Override
+ public String getId() {
+ return subject.stringValue();
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Iterator<Reference> getReferences(String field) {
+ 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");
+ }
+ URI property = sesameFactory.createURI(field);
+ return IteratorUtils.transformedIterator(
+ IteratorUtils.filteredIterator(
+ IteratorUtils.transformedIterator(
+ model.filter(subject, property, null).iterator(),
+ objectTransFormer), // get the object from the statement
+ new ValueTypeFilter<Reference>(Reference.class)), //filter references
+ sesameTransformer); //transform to Text instances
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Iterator<Text> getText(String field) {
+ 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");
+ }
+ URI property = sesameFactory.createURI(field);
+ return IteratorUtils.transformedIterator(
+ IteratorUtils.transformedIterator(
+ IteratorUtils.filteredIterator(
+ IteratorUtils.transformedIterator(
+ model.filter(subject, property, null).iterator(),
+ objectTransFormer), // get the object from the statement
+ new ValueTypeFilter<Text>(Text.class)), //filter plain literals
+ stringLiteral2TextTransformer),
+ sesameTransformer); //transform to Text instances
+ }
+
+ @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));
+ }
+ }
+ }
+ /**
+ * Removes the value from the parsed property
+ * @param property
+ * @param value
+ */
+ private boolean removeValue(URI property, Value value){
+ if(value != null){
+ return model.remove(subject, property, value);
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public void removeAll(String field) 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");
+ }
+ model.remove(subject, sesameFactory.createURI(field), null);
+ }
+
+ @Override
+ public void removeAllNaturalText(String field, String... languages) 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");
+ }
+ ValueTypeFilter<Literal> vtf = new ValueTypeFilter<Literal>(languages);
+ Iterator<Statement> statements = model.filter(
+ subject, sesameFactory.createURI(field), null).iterator();
+ while(statements.hasNext()){
+ Statement statement = statements.next();
+ if(vtf.evaluate(statement.getObject())){
+ statements.remove();
+ }
+ }
+ }
+
+ public void removeNaturalText(String field, String value, String... languages) {
+ 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){
+ log.warn("NULL parsed as value in remove method for symbol "+getId()+" and field "+field+" -> call ignored");
+ }
+ if(languages == null || languages.length == 0){ //null or no language
+ //need to be interpreted as default language
+ languages = new String []{null};
+ }
+ URI property = sesameFactory.createURI(field);
+ for(String language : languages){
+ removeValue(property, sesameFactory.createLiteral(value, language));
+ if(language == null){ //we need also to remove xsd:string labels
+ removeValue(property, sesameFactory.createLiteral(value, XMLSchema.STRING));
+ }
+ }
+ }
+
+ @Override
+ public void removeReference(String field, String reference) {
+ 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(reference == null){
+ log.warn("NULL parsed as value in remove method for symbol "+getId()+" and field "+field+" -> call ignored");
+ } else {
+ removeValue(sesameFactory.createURI(field), sesameFactory.createURI(reference));
+ }
+
+ }
+
+ @Override
+ public void set(String field, Object value) throws IllegalArgumentException {
+ removeAll(field);
+ if(value != null){
+ add(field,value);
+ }
+ }
+
+ @Override
+ public void setNaturalText(String field, String text, String...languages) {
+ removeAllNaturalText(field, languages);
+ if(text != null){
+ addNaturalText(field, text, languages);
+ }
+ }
+
+ @Override
+ public void setReference(String field, String reference) {
+ removeAll(field);
+ if(reference != null){
+ addReference(field, reference);
+ }
+ }
+
+ public URI getURI() {
+ return subject;
+ }
+
+ @Override
+ public Value getValue() {
+ return subject;
+ }
+
+ @Override
+ public int hashCode() {
+ return subject.hashCode();
+ }
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof Representation &&
+ getId().equals(((Representation)obj).getId());
+ }
+ @Override
+ public String toString() {
+ return subject.toString();
+ }
+
+}
\ No newline at end of file
Added: stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfText.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfText.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfText.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfText.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,66 @@
+package org.apache.stanbol.entityhub.model.sesame;
+
+import org.apache.stanbol.entityhub.servicesapi.model.Text;
+import org.openrdf.model.Literal;
+import org.openrdf.model.Value;
+
+/**
+ * A {@link Text} implementation backed by a Sesame {@link Literal}
+ * @author Rupert Westenthaler
+ *
+ */
+public class RdfText implements Text, RdfWrapper {
+ private final Literal literal;
+
+ protected RdfText(Literal literal) {
+ this.literal = literal;
+ }
+
+ @Override
+ public String getLanguage() {
+ return literal.getLanguage();
+ }
+
+ @Override
+ public String getText() {
+ return literal.getLabel();
+ }
+ /**
+ * The wrapped Sesame {@link Literal}
+ * @return the Literal
+ */
+ public Literal getLiteral() {
+ return literal;
+ }
+ @Override
+ public Value getValue() {
+ return literal;
+ }
+
+ @Override
+ public int hashCode() {
+ return literal.hashCode();
+ }
+ @Override
+ public boolean equals(Object obj) {
+ if(obj instanceof Text &&
+ getText().equals(((Text)obj).getText())){
+ //check the language
+ String l1 = literal.getLanguage();
+ String l2 = ((Text)obj).getLanguage();
+ if(l1 == null){
+ return l2 == null;
+ } else {
+ return l1.equalsIgnoreCase(l2);
+ }
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public String toString() {
+ return literal.toString();
+ }
+
+}
\ No newline at end of file
Added: stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactory.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactory.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactory.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactory.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,174 @@
+package org.apache.stanbol.entityhub.model.sesame;
+
+
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.stanbol.entityhub.servicesapi.model.Reference;
+import org.apache.stanbol.entityhub.servicesapi.model.Representation;
+import org.apache.stanbol.entityhub.servicesapi.model.Text;
+import org.apache.stanbol.entityhub.servicesapi.model.UnsupportedTypeException;
+import org.apache.stanbol.entityhub.servicesapi.model.ValueFactory;
+import org.openrdf.model.Literal;
+import org.openrdf.model.Model;
+import org.openrdf.model.URI;
+import org.openrdf.model.impl.TreeModel;
+import org.openrdf.model.impl.ValueFactoryImpl;
+import org.openrdf.model.vocabulary.XMLSchema;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RdfValueFactory implements ValueFactory{
+
+ protected final Logger log = LoggerFactory.getLogger(RdfValueFactory.class);
+
+ static final Map<Object, Set<URI>> JAVA_TO_XML_DATATYPE_MAPPINGS;
+ static {
+ Map<Object, Set<URI>> mappings = new HashMap<Object,Set<URI>>();
+
+ mappings.put(Boolean.class, Collections.singleton(XMLSchema.BOOLEAN));
+
+ mappings.put(Byte.class, Collections.singleton(XMLSchema.BYTE));
+ mappings.put(Short.class, new HashSet<URI>(Arrays.asList(
+ XMLSchema.SHORT, XMLSchema.UNSIGNED_BYTE)));
+ mappings.put(Integer.class, new HashSet<URI>(Arrays.asList(
+ XMLSchema.INT, XMLSchema.UNSIGNED_SHORT)));
+ mappings.put(Long.class, new HashSet<URI>(Arrays.asList(
+ XMLSchema.LONG, XMLSchema.UNSIGNED_INT,
+ XMLSchema.NEGATIVE_INTEGER, XMLSchema.POSITIVE_INTEGER,
+ XMLSchema.NON_NEGATIVE_INTEGER, XMLSchema.NON_POSITIVE_INTEGER)));
+ mappings.put(BigDecimal.class, new HashSet<URI>(Arrays.asList(
+ XMLSchema.INTEGER, XMLSchema.UNSIGNED_LONG)));
+
+ mappings.put(Float.class, Collections.singleton(XMLSchema.FLOAT));
+ mappings.put(Double.class, Collections.singleton(XMLSchema.DOUBLE));
+ mappings.put(BigDecimal.class, Collections.singleton(XMLSchema.DECIMAL));
+
+ mappings.put(Date.class, new HashSet<URI>(Arrays.asList(
+ XMLSchema.DATE, XMLSchema.DATETIME, XMLSchema.TIME,
+ XMLSchema.GYEARMONTH, XMLSchema.GMONTHDAY, XMLSchema.GYEAR,
+ XMLSchema.GMONTH, XMLSchema.GDAY)));
+
+ mappings.put(String.class, Collections.singleton(XMLSchema.STRING));
+
+ JAVA_TO_XML_DATATYPE_MAPPINGS = Collections.unmodifiableMap(mappings);
+ }
+ /**
+ * Lazy initialised instance returned by {@link #getInstance()}
+ */
+ private static RdfValueFactory singleton;
+
+ protected final org.openrdf.model.ValueFactory sesameFactory;
+ /**
+ * If not <code>null</code> this is used for all
+ * {@link #createRepresentation(String)} calls to this instance.
+ */
+ private Model model;
+
+ /**
+ * Create a RdfValueFactory that does use the same {@link Model} for all
+ * created {@link Representation}s.
+ * @param model the model to use
+ * @param sesameFactory the Sesame ValueFactory or <code>null</code> to use
+ * the default
+ */
+ public RdfValueFactory(Model model, org.openrdf.model.ValueFactory sesameFactory){
+ this.model = model;
+ this.sesameFactory = sesameFactory == null ?
+ ValueFactoryImpl.getInstance() : sesameFactory;;
+ }
+ /**
+ * Creates a {@link RdfValueFactory} for a given Sesame ValueFactory.<p>
+ * Instead of parsing <code>null</code> users should use the
+ * {@link #getInstance()} method.
+ * @param sesameFactory the Sesame ValueFactory or <code>null</code> to use
+ * the default
+ */
+ private RdfValueFactory(org.openrdf.model.ValueFactory sesameFactory){
+ this(null,sesameFactory);
+ }
+ /**
+ * Internally used by the {@link #getInstance()}
+ * @param sesameFactory the Sesame ValueFactory or <code>null</code> to use
+ * the default
+ */
+ private RdfValueFactory(){
+ this(null);
+ }
+ /**
+ * The default instance of this Factory.
+ * @return the instance
+ */
+ public static RdfValueFactory getInstance(){
+ if(singleton == null){
+ singleton = new RdfValueFactory();
+ }
+ return singleton;
+ }
+
+
+ @Override
+ public Reference createReference(Object value) throws UnsupportedTypeException, IllegalArgumentException {
+ if (value == null) {
+ throw new IllegalArgumentException("The parsed value MUST NOT be NULL");
+ } else if (value instanceof URI) {
+ return new RdfReference((URI)value);
+ } else {
+ return new RdfReference(sesameFactory.createURI(value.toString()));
+ }
+ }
+
+ @Override
+ public Representation createRepresentation(String id) throws IllegalArgumentException {
+ if (id == null){
+ throw new IllegalArgumentException("The parsed id MUST NOT be NULL!");
+ } else if(id.isEmpty()){
+ throw new IllegalArgumentException("The parsed id MUST NOT be empty!");
+ } else {
+ //use the set model if present
+ Model model = this.model == null ? new TreeModel() : this.model;
+ URI subject = sesameFactory.createURI(id);
+ return new RdfRepresentation(subject, model, this);
+ }
+ }
+
+ @Override
+ public Text createText(Object value) throws UnsupportedTypeException, IllegalArgumentException {
+ if (value == null) {
+ throw new IllegalArgumentException("The parsed value MUST NOT be NULL");
+ } else if (value instanceof Literal) {
+ return new RdfText((Literal) value);
+ } else {
+ return createText(value.toString(), null);
+ }
+ }
+
+ @Override
+ public Text createText(String text, String language) throws IllegalArgumentException {
+ if(text == null){
+ throw new IllegalArgumentException("The parsed text MUST NOT be NULL");
+ } else if(text.isEmpty()){
+ throw new IllegalArgumentException("Tha parsed Text MUST NOT be empty!");
+ }
+ if(language != null && language.isEmpty()){
+ language = null;
+ }
+ return new RdfText(sesameFactory.createLiteral(text, language));
+ }
+
+ /**
+ * Getter for the Sesame {@link org.openrdf.model.ValueFactory} used by
+ * this Entityhub {@link ValueFactory}.
+ * @return the Sesame value factory
+ */
+ public org.openrdf.model.ValueFactory getSesameFactory(){
+ return sesameFactory;
+ }
+}
Added: stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfWrapper.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfWrapper.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfWrapper.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/RdfWrapper.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,16 @@
+package org.apache.stanbol.entityhub.model.sesame;
+
+import org.openrdf.model.Value;
+
+/**
+ * Interface that allows access to the wrapped Sesame {@link Value}
+ * @author Rupert Westenthaler
+ *
+ */
+public interface RdfWrapper {
+ /**
+ * Getter for the wrapped Sesame {@link Value}
+ * @return the value
+ */
+ Value getValue();
+}
\ No newline at end of file
Added: stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/ValueTypeFilter.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/ValueTypeFilter.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/ValueTypeFilter.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/main/java/org/apache/stanbol/entityhub/model/sesame/ValueTypeFilter.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,87 @@
+package org.apache.stanbol.entityhub.model.sesame;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+
+import org.apache.commons.collections.Predicate;
+import org.apache.stanbol.entityhub.servicesapi.model.Reference;
+import org.apache.stanbol.entityhub.servicesapi.model.Text;
+import org.openrdf.model.BNode;
+import org.openrdf.model.Literal;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.model.vocabulary.XMLSchema;
+
+/**
+ * Hold the Java Object to xml schema mappings for Sesame.
+ * Used as {@link Predicate} for filtering Objects of statements.
+ * @author Rupert Westenthaler
+ *
+ * @param <T>
+ */
+class ValueTypeFilter<T> implements Predicate {
+
+ private boolean referenceState;
+ private boolean plainLiteralState;
+ private Set<URI> xmlTypes;
+ private Set<String> languages;
+ private Class<? extends Value> sesameType;
+
+ public ValueTypeFilter(String...languages) {
+ if(languages == null || languages.length < 1){
+ this.languages = null;
+ } else if( languages.length == 1){
+ String language = languages[0];
+ if(language != null){
+ language = language.toLowerCase(Locale.ROOT);
+ }
+ this.languages = Collections.singleton(language);
+ } else {
+ this.languages = new HashSet<String>();
+ for(String language : languages){
+ if(language != null){
+ language = language.toLowerCase(Locale.ROOT);
+ }
+ this.languages.add(language);
+ }
+ }
+ this.plainLiteralState = true;
+ }
+
+ public ValueTypeFilter(Class<T> type) {
+ if(type.equals(Text.class)){
+ plainLiteralState = true;
+ } else if (type.equals(Reference.class)){
+ referenceState = true;
+ } else if(Value.class.isAssignableFrom(type)){
+ sesameType = type.asSubclass(Value.class);
+ } else {
+ xmlTypes = RdfValueFactory.JAVA_TO_XML_DATATYPE_MAPPINGS.get(type);
+ }
+ }
+
+ @Override
+ public boolean evaluate(Object object) {
+ if(sesameType != null){
+ return sesameType.isAssignableFrom(object.getClass());
+ } else if(referenceState && object instanceof URI || object instanceof BNode){
+ return true;
+ } else if(object instanceof Literal){
+ Literal literal = (Literal)object;
+ //TODO: adapt to RDF1.1:
+ if(plainLiteralState && ( literal.getDatatype() == null ||
+ XMLSchema.STRING.equals(literal.getDatatype())) && (
+ languages == null || languages.contains(literal.getLanguage()))){
+ return true;
+ } else if(!plainLiteralState && xmlTypes != null && literal.getDatatype() != null){
+ return xmlTypes.contains(literal.getDatatype());
+ } else { //wrong literal type
+ return false;
+ }
+ } else { //wrong Value type or not a Value at all
+ return false;
+ }
+ }
+}
\ No newline at end of file
Added: stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentationTest.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentationTest.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentationTest.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfRepresentationTest.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.stanbol.entityhub.model.sesame;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.stanbol.entityhub.servicesapi.model.Representation;
+import org.apache.stanbol.entityhub.servicesapi.model.Text;
+import org.apache.stanbol.entityhub.servicesapi.model.ValueFactory;
+import org.apache.stanbol.entityhub.test.model.RepresentationTest;
+import org.junit.Before;
+import org.junit.Test;
+import org.openrdf.model.Literal;
+import org.openrdf.model.vocabulary.XMLSchema;
+
+
+public class RdfRepresentationTest extends RepresentationTest {
+
+ protected RdfValueFactory valueFactory;
+
+
+ @Before
+ public void init(){
+ this.valueFactory = RdfValueFactory.getInstance();
+ }
+
+ @Override
+ protected Object getUnsupportedValueInstance() {
+ return null; //indicates that all kinds of Objects are supported!
+ }
+
+ @Override
+ protected ValueFactory getValueFactory() {
+ return valueFactory;
+ }
+ /*--------------------------------------------------------------------------
+ * 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());
+ assertFalse(noLangaugeTexts.hasNext()); //only a single result
+ //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().toString(), enLangageText.getLanguage());
+ assertFalse(enLangaugeTexts.hasNext());//only a single result
+ //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());
+ }
+ /**
+ * {@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());
+ }
+ /**
+ * {@link TypedLiteral}s are used to represent literal values for different
+ * xsd dataTypes within Clerezza. This method tests if xsd dataTypes are
+ * converted to the corresponding java types.
+ * This is dependent on the {@link LiteralFactory} implementation used by
+ * the {@link RdfRepresentation} implementation.
+ */
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testTypedLiteralToValueConversion(){
+ String field = "urn:test.RdfRepresentation:test.field";
+ Integer integerValue = 5;
+ Literal integerLiteral = valueFactory.getSesameFactory().createLiteral(integerValue);
+ Date dateValue = new Date();
+ Literal dateLiteeral = valueFactory.getSesameFactory().createLiteral(dateValue);
+ Double doubleValue = Math.PI;
+ Literal doubleLiteral = valueFactory.getSesameFactory().createLiteral(doubleValue);
+ String stringValue = "This is a string literal value";
+ Literal stringLiteral = valueFactory.getSesameFactory().createLiteral(stringValue,XMLSchema.STRING);
+ Representation rep = createRepresentation(null);
+ Collection<Literal> typedLiterals =
+ Arrays.asList(integerLiteral,doubleLiteral,stringLiteral,dateLiteeral);
+ rep.add(field, typedLiterals);
+
+ //now check that such values are available via Sesame Literal
+ Iterator<Literal> typedLiteralValues = rep.get(field, Literal.class);
+ int size = 0;
+ while(typedLiteralValues.hasNext()){
+ Literal next = typedLiteralValues.next();
+ assertTrue(typedLiterals.contains(next));
+ size++;
+ }
+ assertTrue(typedLiterals.size() == size);
+
+ //now check that the values are available via the java object types
+ //1) integer
+ Iterator<Integer> intValues = rep.get(field, Integer.class);
+ assertTrue(intValues.hasNext());
+ assertEquals(integerValue, intValues.next());
+ assertFalse(intValues.hasNext());
+ //2) double
+ Iterator<Double> doubleValues = rep.get(field, Double.class);
+ assertTrue(doubleValues.hasNext());
+ assertEquals(doubleValue, doubleValues.next());
+ assertFalse(doubleValues.hasNext());
+ //3) string
+ Iterator<String> stringValues = rep.get(field, String.class);
+ assertTrue(stringValues.hasNext());
+ String value = stringValues.next();
+ assertEquals(stringValue, value);
+ assertFalse(stringValues.hasNext());
+ //4) date
+ Iterator<Date> dateValues = rep.get(field,Date.class);
+ assertTrue(dateValues.hasNext());
+ assertEquals(dateValue, dateValues.next());
+ assertFalse(dateValues.hasNext());
+ }
+ //TODO add tests for adding Integers, Doubles, ... and getting TypedLiterals
+ public static void main(String[] args) {
+ RdfRepresentationTest test = new RdfRepresentationTest();
+ test.init();
+ test.testTypedLiteralToValueConversion();
+ }
+}
Added: stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactoryTest.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactoryTest.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactoryTest.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/RdfValueFactoryTest.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.stanbol.entityhub.model.sesame;
+
+import org.apache.stanbol.entityhub.servicesapi.model.ValueFactory;
+import org.apache.stanbol.entityhub.test.model.ValueFactoryTest;
+import org.junit.Before;
+
+
+public class RdfValueFactoryTest extends ValueFactoryTest {
+
+ protected RdfValueFactory valueFactory;
+
+ @Before
+ public void init(){
+ this.valueFactory = RdfValueFactory.getInstance();
+ }
+
+ @Override
+ protected Object getUnsupportedReferenceType() {
+ return null; //all references are supported (no test for valid IRIs are done by Clerezza)
+ }
+
+ @Override
+ protected Object getUnsupportedTextType() {
+ return null; //all Types are supported
+ }
+
+ @Override
+ protected ValueFactory getValueFactory() {
+ return valueFactory;
+ }
+}
Added: stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/impl/ResourceAdapterTest.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/impl/ResourceAdapterTest.java?rev=1530223&view=auto
==============================================================================
--- stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/impl/ResourceAdapterTest.java (added)
+++ stanbol/trunk/entityhub/model/sesame/src/test/java/org/apache/stanbol/entityhub/model/sesame/impl/ResourceAdapterTest.java Tue Oct 8 11:07:17 2013
@@ -0,0 +1,109 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.entityhub.model.sesame.impl;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.stanbol.entityhub.model.sesame.RdfValueFactory;
+import org.apache.stanbol.entityhub.servicesapi.model.Representation;
+import org.junit.Assert;
+import org.junit.Test;
+import org.openrdf.model.Model;
+import org.openrdf.model.URI;
+import org.openrdf.model.ValueFactory;
+import org.openrdf.model.impl.TreeModel;
+import org.openrdf.model.impl.ValueFactoryImpl;
+
+public class ResourceAdapterTest {
+
+
+ private ValueFactory vf = ValueFactoryImpl.getInstance();
+
+ /**
+ * Test related to STANBOL-698
+ */
+ @Test
+ public void testDouble(){
+ Model graph = new TreeModel();
+ URI id = vf.createURI("http://www.example.org/test");
+ URI doubleTestField = vf.createURI("http://www.example.org/field/double");
+ graph.add(id, doubleTestField, vf.createLiteral(Double.NaN));
+ graph.add(id, doubleTestField, vf.createLiteral(Double.POSITIVE_INFINITY));
+ graph.add(id, doubleTestField, vf.createLiteral(Double.NEGATIVE_INFINITY));
+
+ RdfValueFactory valueFactory = new RdfValueFactory(graph,vf);
+ Representation r = valueFactory.createRepresentation(id.stringValue());
+ Set<Double> expected = new HashSet<Double>(Arrays.asList(
+ Double.NaN, Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY));
+ Iterator<Double> dit = r.get(doubleTestField.stringValue(), Double.class);
+ while(dit.hasNext()){
+ Double val = dit.next();
+ Assert.assertNotNull(val);
+ Assert.assertTrue(expected.remove(val));
+ }
+ Assert.assertTrue(expected.isEmpty());
+ }
+
+ @Test
+ public void testFloat(){
+ Model graph = new TreeModel();
+ URI id = vf.createURI("http://www.example.org/test");
+ URI floatTestField = vf.createURI("http://www.example.org/field/float");
+ graph.add(id, floatTestField, vf.createLiteral(Float.NaN));
+ graph.add(id, floatTestField, vf.createLiteral(Float.POSITIVE_INFINITY));
+ graph.add(id, floatTestField, vf.createLiteral(Float.NEGATIVE_INFINITY));
+
+ RdfValueFactory valueFactory = new RdfValueFactory(graph,vf);
+ Representation r = valueFactory.createRepresentation(id.stringValue());
+ Set<Float> expected = new HashSet<Float>(Arrays.asList(
+ Float.NaN, Float.POSITIVE_INFINITY,Float.NEGATIVE_INFINITY));
+ Iterator<Float> dit = r.get(floatTestField.stringValue(), Float.class);
+ while(dit.hasNext()){
+ Float val = dit.next();
+ Assert.assertNotNull(val);
+ Assert.assertTrue(expected.remove(val));
+ }
+ Assert.assertTrue(expected.isEmpty());
+ }
+// TODO: how to create NAN, POSITIVE_INFINITY, NEGATIVE_INVINITY instances for BigDecimal
+// @Test
+// public void testBigDecimal(){
+// MGraph graph = new IndexedMGraph();
+// UriRef id = new UriRef("http://www.example.org/test");
+// UriRef doubleTestField = new UriRef("http://www.example.org/field/double");
+// LiteralFactory lf = LiteralFactory.getInstance();
+// graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(BigDecimal.valueOf(Double.NaN))));
+// graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(BigDecimal.valueOf(Double.POSITIVE_INFINITY))));
+// graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(BigDecimal.valueOf(Double.NEGATIVE_INFINITY))));
+//
+// RdfValueFactory vf = new RdfValueFactory(graph);
+// Representation r = vf.createRepresentation(id.getUnicodeString());
+// Set<BigDecimal> expected = new HashSet<BigDecimal>(Arrays.asList(
+// BigDecimal.valueOf(Double.NaN), BigDecimal.valueOf(Double.POSITIVE_INFINITY),
+// BigDecimal.valueOf(Double.NEGATIVE_INFINITY)));
+// Iterator<BigDecimal> dit = r.get(doubleTestField.getUnicodeString(), BigDecimal.class);
+// while(dit.hasNext()){
+// BigDecimal val = dit.next();
+// Assert.assertNotNull(val);
+// Assert.assertTrue(expected.remove(val));
+// }
+// Assert.assertTrue(expected.isEmpty());
+// }
+}