You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2015/03/23 17:43:04 UTC
[4/5] clerezza git commit: CLEREZZA-966: moved rdf.scala.utils into
the hierarchy
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4d35191b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/CollectedIter.scala
----------------------------------------------------------------------
diff --git a/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/CollectedIter.scala b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/CollectedIter.scala
new file mode 100644
index 0000000..ec36962
--- /dev/null
+++ b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/CollectedIter.scala
@@ -0,0 +1,133 @@
+/*
+ * 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.clerezza.rdf.scala.utils
+
+import java.util.ConcurrentModificationException
+import java.util.Iterator
+import _root_.scala.collection
+import collection.mutable._
+import collection.immutable
+import _root_.scala.collection.JavaConversions._
+import java.util.concurrent.locks.Lock
+
+/**
+ *
+ * A Collection that groups the elements of an iterator, giving a view over it as over a
+ * sequence.
+ *
+ * The collection takes a function returning an iterator, in order to allow for cases where the
+ * iterator needs to be called from the beginning again. i.e. when a ConcurrentModificationException
+ * occurs and the iteration is repeated in a section used using the provided readLock.
+ */
+class CollectedIter[T](iterCreator: () => Iterator[T], readLock: Lock) extends immutable.Seq[T] {
+
+ def this(jList : java.util.List[T], readLock: Lock) = this(() => jList.iterator(), readLock)
+ def this() = this( ()=> java.util.Collections.emptyList[T].iterator(),null)
+
+ var iter = iterCreator()
+ var firstIter = true
+
+ private val collectedElems = new ArrayBuffer[T]()
+
+ /**
+ * This method allows the position to be expressed between parenthesis
+ */
+ def apply(pos : Int) = {
+ ensureReadTill(pos)
+ collectedElems(pos)
+ }
+
+
+ /**
+ * returns a new fully expanded and sorted CollectediterCreator
+ */
+ def sort(lt : (T,T) => Boolean) = {
+ val sortedElems = iterator.toList.sortWith(lt)
+ //TODO this re-expands everything, return sorted-list directly
+ new CollectedIter[T](sortedElems, readLock)
+
+ }
+
+ /**
+ * Operator style syntax to access a position.
+ */
+ def %(pos: Int) = apply(pos)
+
+ private def ensureReadTill(pos: Int) {
+ try {
+
+ while (iter.hasNext && (collectedElems.length-1 <= pos)) {
+ val next = iter.next()
+ if (firstIter || !collectedElems.contains(next)) {
+ collectedElems += next
+ }
+ }
+ } catch {
+ case e: ConcurrentModificationException => {
+ readLock.lock()
+ try {
+ iter = iterCreator()
+ firstIter = false
+ //going beyond pos, do reduce chance we have to aquire another lock
+ val biggerPos = if (pos < (Integer.MAX_VALUE - 100)) {
+ pos + 100
+ } else {
+ Integer.MAX_VALUE
+ }
+ while (iter.hasNext && (collectedElems.length-1 <= biggerPos)) {
+ val next = iter.next()
+ if (!collectedElems.contains(next)) {
+ collectedElems += next
+ }
+ }
+ } finally {
+ readLock.unlock()
+ }
+ }
+ case e: Exception => throw e
+ }
+ }
+
+ override def length : Int = {
+ length(Integer.MAX_VALUE)
+ }
+
+ /**
+ * The value returned is same or less than the length of the collection,
+ * the underlying Iterator isn't expanded till more than <code>max</code>. If
+ * the result is smaller than max it is the length of the collection.
+ */
+ def length(max: Int) : Int = {
+ ensureReadTill(max)
+ collectedElems.length
+ }
+
+ override def toString() = {
+ if (length(1) > 0) {
+ apply(0).toString
+ } else {
+ "empty"
+ }
+ }
+
+ override def iterator = {
+ ensureReadTill(Integer.MAX_VALUE)
+ collectedElems.iterator
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4d35191b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzLiteral.scala
----------------------------------------------------------------------
diff --git a/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzLiteral.scala b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzLiteral.scala
new file mode 100644
index 0000000..c9fcc11
--- /dev/null
+++ b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzLiteral.scala
@@ -0,0 +1,81 @@
+/*
+ * 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.clerezza.rdf.scala.utils
+
+//import org.apache.clerezza.rdf.core.impl.{TypedLiteralImpl, PlainLiteralImpl}
+import org.apache.clerezza.rdf.ontologies.XSD
+import org.apache.commons.rdf.Iri
+import org.apache.commons.rdf.Language
+import org.apache.commons.rdf.Literal
+import org.apache.commons.rdf.impl.utils.PlainLiteralImpl
+import org.apache.commons.rdf.impl.utils.TypedLiteralImpl
+//import org.apache.commons.rdf.core.{TypedLiteral, Language, PlainLiteral, Iri}
+
+object EzLiteral extends EzLiteralImplicits
+
+trait EzLiteralImplicits {
+
+ implicit def string2lit(str: String) = new EzLiteral(str)
+
+}
+
+/**
+ * An EzLiteral is a typed string literal - ie, just a String literal - that comes with N3/turtle like methods
+ * to map to other types of literals.
+ *
+ * This makes it useful when combined with the EzLiteralImplicit for writing out literals
+ *
+ * <code>
+ * "ABCDEFGHIJKLMN" -- a plain string converted to a EzLiteral
+ * "some text in english".lang(en) -- an english literal
+ * "1234"^^XSD.int -- a number
+ * </code>
+ *
+ * @author bblfish
+ */
+class EzLiteral(string: String) extends TypedLiteralImpl(string,XSD.string) {
+
+ /**
+ * @return a plain literal with language specified by lang
+ */
+ def lang(lng: String): Literal = lang(new Language(lng))
+
+ /**
+ * @return a plain literal with language specified by lang
+ */
+ def lang(lng: Language): Literal = new PlainLiteralImpl(string, lng)
+
+ /**
+ * Map to a Typed Literal of given type
+ */
+ def ^^(typ: Iri): Literal = new TypedLiteralImpl(string, typ)
+
+ /**
+ * alias for iri
+ */
+ @deprecated(message ="use `iri`", since="1.0.0")
+ def uri = iri
+
+ /**
+ * Map to an IRI of given lexical form
+ */
+ def iri = new Iri(string)
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4d35191b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzMGraph.scala
----------------------------------------------------------------------
diff --git a/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzMGraph.scala b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzMGraph.scala
new file mode 100644
index 0000000..6ddb92b
--- /dev/null
+++ b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzMGraph.scala
@@ -0,0 +1,88 @@
+/*
+ * 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.clerezza.rdf.scala.utils
+
+import org.apache.clerezza.rdf.core._
+import org.apache.clerezza.rdf.core.impl._
+import org.apache.commons.rdf.BlankNode
+import org.apache.commons.rdf.BlankNodeOrIri
+import org.apache.commons.rdf.Graph
+import org.apache.commons.rdf.Iri
+import org.apache.commons.rdf.RdfTerm
+import org.apache.commons.rdf.Triple
+import org.apache.commons.rdf.impl.utils.AbstractGraph
+import org.apache.commons.rdf.impl.utils.simple.SimpleGraph
+import scala.collection.mutable.HashMap
+
+
+/**
+ * EzGraph enhances graph writing, it can make writing rdf graphs in code a lot more
+ * readable, as it avoids a lot of repetition.
+ *
+ * @param graph: a Triple collection
+ * @author hjs, reto
+ */
+class EzGraph(val baseTc: Graph) extends AbstractGraph with TcDependentConversions {
+
+
+ def this() = this (new SimpleGraph())
+
+ def performFilter(subject: BlankNodeOrIri, predicate: Iri,
+ obj: RdfTerm): java.util.Iterator[Triple] = baseTc.filter(subject, predicate, obj)
+
+ override def performSize = baseTc.size
+
+ override def add(t: Triple) = baseTc.add(t)
+
+ /**
+ * Add all triples into the other graph to this one
+ */
+ def +=(other: Graph) = {
+ if (baseTc ne other) baseTc.addAll(other)
+ }
+
+ /**
+ * create a new bnode
+ */
+ def bnode = blankNode
+
+ def blankNode: BlankNode = {
+ new BlankNode
+ }
+
+ private val namedBnodes = new HashMap[String,BlankNode]
+
+ /**
+ * create a new named bnode based EzGraphNode with the preferred writing style
+ */
+ def b_(name: String): BlankNode = {
+ namedBnodes.get(name) match {
+ case Some(bnode) => bnode
+ case None => {
+ val bn = new BlankNode
+ namedBnodes.put(name, bn);
+ bn
+ }
+ }
+ }
+
+}
+
+
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4d35191b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/NameSpace.scala
----------------------------------------------------------------------
diff --git a/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/NameSpace.scala b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/NameSpace.scala
new file mode 100644
index 0000000..19be8aa
--- /dev/null
+++ b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/NameSpace.scala
@@ -0,0 +1,37 @@
+/*
+ * 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.clerezza.rdf.scala.utils
+
+import org.apache.commons.rdf.Iri
+
+/**
+ * A IRI-namespace prefix
+ */
+class NameSpace(prefix: String) {
+
+ /**
+ * returns a Iri applying this namespace prefix to the given symbol
+ */
+ def +(s: Symbol) = new Iri(prefix + s.name)
+
+ /**
+ * returns a Iri applying this prefix to the given string
+ */
+ def +(s: String) = new Iri(prefix + s)
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4d35191b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala
----------------------------------------------------------------------
diff --git a/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala
new file mode 100644
index 0000000..8af2452
--- /dev/null
+++ b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala
@@ -0,0 +1,123 @@
+/*
+ * 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.clerezza.rdf.scala.utils
+
+
+import org.apache.clerezza.rdf.ontologies.XSD
+import org.apache.clerezza.rdf.utils.GraphNode
+import java.math.BigInteger
+import java.net.URI
+import java.net.URL
+import java.util.Date
+import org.apache.clerezza.rdf.core._
+import org.apache.commons.rdf.Graph
+import org.apache.commons.rdf.Iri
+import org.apache.commons.rdf.Literal
+import org.apache.commons.rdf.RdfTerm
+import org.apache.commons.rdf.impl.utils.PlainLiteralImpl
+import org.apache.commons.rdf.impl.utils.simple.SimpleGraph
+import org.apache.commons.rdf.impl.utils.simple.SimpleImmutableGraph
+
+/**
+* This object provides the implicit conversions. Typically this is used by
+* adding
+* {{{
+* import org.apache.clerezza.rdf.scala.utils.Preamble._
+* }}} near the top of the
+* file using SCB Utilities for Scala
+*
+* @author bblfish, reto
+*/
+object Preamble extends TcIndependentConversions {
+
+}
+
+/**
+* This class provides the implicit conversions of its companion Object and
+* additional conversions that require an evaluation graph, i.e. the conversion
+* from a resource to a RichGraphNode.
+*
+* Typically this is used by
+* adding
+* {{{
+* val preamble = new org.apache.clerezza.rdf.scala.utils.Preamble(myGraph)
+* import preamble._
+* }}}
+* before the
+* code section using the conversions
+*
+* @author bblfish, reto
+*/
+class Preamble(val baseTc: Graph) extends TcDependentConversions {
+
+}
+protected trait TcDependentConversions extends TcIndependentConversions {
+
+ def baseTc: Graph
+
+ implicit def toRichGraphNode(resource: RdfTerm) = {
+ new RichGraphNode(new GraphNode(resource, baseTc))
+ }
+}
+
+protected trait TcIndependentConversions extends EzLiteralImplicits {
+ implicit def toRichGraphNode(node: GraphNode) = {
+ new RichGraphNode(node)
+ }
+
+ implicit def toFirstElement(c: CollectedIter[RichGraphNode]) = {
+ if (c.length(1) > 0) {
+ c(0)
+ } else {
+ TcIndependentConversions.emptyLiteral
+ }
+ }
+
+ private val litFactory = LiteralFactory.getInstance
+
+
+ implicit def lit2String(lit: Literal) = lit.getLexicalForm
+
+ implicit def date2lit(date: Date) = litFactory.createTypedLiteral(date)
+
+ implicit def int2lit(int: Int) = litFactory.createTypedLiteral(int)
+
+ implicit def bigint2lit(bint: BigInt) = litFactory.createTypedLiteral(bint.underlying())
+
+ implicit def bigint2lit(bigInt: BigInteger) = litFactory.createTypedLiteral(bigInt)
+
+ implicit def bool2lit(boolean: Boolean) = litFactory.createTypedLiteral(boolean)
+
+ implicit def long2lit(long: Long) = litFactory.createTypedLiteral(long)
+
+ implicit def double2lit(double: Double) = litFactory.createTypedLiteral(double)
+
+ implicit def uriRef2Prefix(uriRef: Iri) = new NameSpace(uriRef.getUnicodeString)
+
+ implicit def URItoIri(uri: URI) = new Iri(uri.toString)
+
+ implicit def URLtoIri(url: URL) = new Iri(url.toExternalForm)
+
+}
+protected object TcIndependentConversions {
+ val emptyGraph = new SimpleImmutableGraph(new SimpleGraph)
+ val emptyLiteral = new RichGraphNode(new GraphNode(new PlainLiteralImpl(""), emptyGraph))
+
+}
+
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4d35191b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/RichGraphNode.scala
----------------------------------------------------------------------
diff --git a/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/RichGraphNode.scala b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/RichGraphNode.scala
new file mode 100644
index 0000000..4f2addc
--- /dev/null
+++ b/rdf/scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/RichGraphNode.scala
@@ -0,0 +1,244 @@
+/*
+ * 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.clerezza.rdf.scala.utils
+
+import org.apache.clerezza.rdf.core.LiteralFactory
+import org.apache.clerezza.rdf.ontologies.RDF
+import org.apache.clerezza.rdf.utils.GraphNode
+import java.util.Iterator
+import _root_.scala.collection.JavaConversions._
+import _root_.scala.reflect.Manifest
+import org.apache.commons.rdf.Graph
+import org.apache.commons.rdf.Iri
+import org.apache.commons.rdf.Literal
+import org.apache.commons.rdf.RdfTerm
+import org.apache.commons.rdf.impl.utils.simple.SimpleGraph
+import org.apache.clerezza.rdf.utils.UnionGraph
+
+
+/**
+ * A RichGraphNode decorates A GraphNode with additional method to be part on a DSL-style scala library.
+ *
+ * The default constructor is same a the GraphNode constructor, i.e. it takes the node and its context
+ * Triple-collection
+ *
+ * @param resource the node represented by this RichGraphNode
+ * @param graph the Graph that describes the resource
+ */
+class RichGraphNode(resource: RdfTerm, graph: Graph ) extends GraphNode(resource, graph) {
+
+ /**
+ * Construct a RichGraphNode given an existing [[GraphNde]]
+ *
+ * @param node The GraphNode to be wrapped
+ */
+ def this(node: GraphNode) = this(node.getNode, node.getGraph)
+
+ /**
+ * Operator syntax shortcut to get all objects as <code>RichGraphNode</code>
+ *
+ * @return all objects of the specified property of the node wrapped by this object
+ */
+ def /(property: Iri): CollectedIter[RichGraphNode] = {
+ new CollectedIter[RichGraphNode](() => new GraphNodeIter(getObjects(property)), readLock)
+ }
+
+ /**
+ * Operator syntax shortcut to get all subjects as <code>RichGraphNode</code>ref
+ *
+ * @param property the property for which the subjects pointing to this node by that property are requested
+ * @return the matching resources
+ */
+ def /-(property: Iri): CollectedIter[RichGraphNode] = {
+ new CollectedIter[RichGraphNode](() => new GraphNodeIter(getSubjects(property)), readLock)
+ }
+
+ /**
+ * Get the elements of the rdf:List represented by this node
+ * @return a List with the elements of the rdf:List represented by this node
+ */
+ def !! = (for (listElem <- asList) yield {
+ new RichGraphNode(new GraphNode(listElem, getGraph))
+ }).toList
+
+ /**
+ * get a specified of the rdf:List represented by this node
+ *
+ * @return the specified index value
+ */
+ def %!!(index: Int) = new RichGraphNode(new GraphNode(asList.get(index),
+ getGraph))
+
+ /**
+ * produces a default String representation for the node, this is the lexical form of literals,
+ * the unicode-string for Iri and for BlankNodes the value returned by toString
+ *
+ * @return the default string representation of the node
+ */
+ def * : String = {
+ getNode() match {
+ case lit: Literal => lit.getLexicalForm
+ case uri: Iri => uri.getUnicodeString
+ case wrappedNode => wrappedNode.toString
+ }
+ }
+
+ private def asClass[T](clazz : Class[T]) : T= {
+ val typedLiteral = getNode().asInstanceOf[Literal]
+ clazz match {
+ case c if(c == classOf[Boolean]) => LiteralFactory.getInstance().createObject(
+ classOf[java.lang.Boolean], typedLiteral).booleanValue.asInstanceOf[T]
+ case _ => LiteralFactory.getInstance().createObject(clazz, typedLiteral)
+ }
+ }
+
+ /**
+ * Creates an instance of specified Class-Type representing the value of the literal wrapped by this
+ * <code>GraphNode</code>
+ *
+ * @return the literal represented by this node as instance of the specified type
+ */
+ def as[T](implicit m: Manifest[T]): T = {
+ asClass(m.runtimeClass.asInstanceOf[Class[T]])
+ }
+
+ /**
+ * Operator syntax shortcut to get the <code>RdfTerm</code> wrapped by this
+ * <code>GraphNode</code>
+ *
+ * @return the node represented by this GraphNode as RdfTerm, same as <code>getNode</code>
+ */
+ def ! = {
+ getNode()
+ }
+
+ private class GraphNodeIter[T <: RdfTerm](base: Iterator[T]) extends Iterator[RichGraphNode] {
+ override def hasNext() = {
+ base.hasNext();
+ }
+
+ override def next() : RichGraphNode = {
+ new RichGraphNode(new GraphNode(base.next(), getGraph));
+ }
+
+ override def remove() {
+ base.remove()
+ }
+ }
+
+ /**
+ *Sets the RDF:type of the subject */
+ def a(rdfclass: Iri): RichGraphNode = {
+ addProperty(RDF.`type`, rdfclass)
+ return this
+ }
+
+ /*
+ * create an RichGraphNode from this one where the backing graph is protected from writes by a new
+ * SimpleGraph.
+ */
+ def protect(): RichGraphNode = new RichGraphNode(getNode, new UnionGraph(new SimpleGraph(), graph))
+
+
+ /**
+ * relate the subject via the given relation to....
+ */
+ def --(rel: RdfTerm): DashTuple = new DashTuple(rel)
+
+ def --(rel: RichGraphNode): DashTuple = new DashTuple(rel.getNode)
+
+
+ /**
+ * relate the subject via the inverse of the given relation to....
+ */
+ def <--(tuple: RichGraphNode#DashTuple): RichGraphNode = {
+ val inversePropertyRes = tuple.first.getNode
+ val inverseProperty: Iri = inversePropertyRes match {
+ case p: Iri => p
+ case _ => throw new RuntimeException("DashTuple must be a Iri")
+ }
+ RichGraphNode.this.addInverseProperty(inverseProperty, tuple.second)
+ RichGraphNode.this
+ }
+
+
+ /** class for Inverse relations with the current RichGraphNode.ref as object */
+ //TODO add support for adding many for symmetry reasons
+// class InverseDashTuple(rel: DashTuple) {
+//
+// /**
+// * ...to the following non literal
+// */
+// def --(subj: BlankNodeOrIri): RichGraphNode = {
+// RichGraphNode.this.addInverseProperty(rel, subj)
+// RichGraphNode.this
+// }
+//
+// /**
+// * ...to the following resource (given as a string)
+// */
+// def --(subj: String): RichGraphNode = --(new Iri(subj))
+//
+// /**
+// * ...to the following EzGraphNode
+// * (useful for opening a new parenthesis and specifying other things in more detail
+// */
+// def --(subj: GraphNode): RichGraphNode = {
+// --(subj.getNode.asInstanceOf[BlankNodeOrIri])
+// }
+// // since we can only have inverses from non literals (howto deal with bndoes?)
+// }
+
+ /**
+ * class for relations with the current RichGraphNode.ref as subject
+ */
+ class DashTuple(val second: RdfTerm) {
+
+ val first = RichGraphNode.this
+ /**
+ * ...to the following non resource
+ */
+ def -->(obj: RdfTerm): RichGraphNode = {
+ val property = second match {
+ case u: Iri => u;
+ case _ => throw new RuntimeException("Property must be a Iri")
+ }
+ RichGraphNode.this.addProperty(property, obj)
+ RichGraphNode.this
+ }
+
+
+ /**
+ * ...to the EzGraphNode, which is useful for opening a parenthesis.
+ */
+ def -->(sub: GraphNode): RichGraphNode = {
+ //RichGraphNode.this + sub
+ -->(sub.getNode)
+ }
+ /**
+ * Add one relation for each member of the iterable collection
+ */
+ def -->>[T <: RdfTerm](uris: Iterable[T]): RichGraphNode = {
+ for (u <- uris) -->(u)
+ RichGraphNode.this
+ }
+ }
+}
+
+
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4d35191b/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EzMGraphTest.scala
----------------------------------------------------------------------
diff --git a/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EzMGraphTest.scala b/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EzMGraphTest.scala
new file mode 100644
index 0000000..3e72d87
--- /dev/null
+++ b/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EzMGraphTest.scala
@@ -0,0 +1,161 @@
+/*
+ * 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.clerezza.rdf.scala.utils
+
+import org.apache.commons.rdf.BlankNode
+import org.apache.commons.rdf.ImmutableGraph
+import org.apache.commons.rdf.Iri
+import org.apache.commons.rdf.Language
+import org.apache.commons.rdf.impl.utils.PlainLiteralImpl
+import org.apache.commons.rdf.impl.utils.TripleImpl
+import org.apache.commons.rdf.impl.utils.TypedLiteralImpl
+import org.apache.commons.rdf.impl.utils.simple.SimpleGraph
+import org.junit._
+import org.apache.clerezza.rdf.core._
+import impl._
+import Preamble._
+import org.apache.clerezza.rdf.ontologies._
+
+/**
+ * @author bblfish, reto
+ */
+class EzGraphTest {
+
+ val bblfishModulus = """
+ 9D ☮ 79 ☮ BF ☮ E2 ☮ F4 ☮ 98 ☮ BC ☮ 79 ☮ 6D ☮ AB ☮ 73 ☮ E2 ☮ 8B ☮ 39 ☮ 4D ☮ B5 26 ✜ 68 ✜ 49 ✜ EE ✜ 71 ✜ 87 ✜
+ 06 ✜ 32 ✜ C9 ✜ 9F ✜ 3F ✜ 94 ✜ E5 ✜ CB ✜ 4D ✜ B5 12 ☮ 35 ☮ 13 ☮ 69 ☮ 60 ☮ 81 ☮ 58 ☮ 79 ☮ 66 ☮ F3 ☮ 79 ☮ 20 ☮
+ 91 ☮ 6A ☮ 3F ☮ 42 5A ✜ F6 ✜ 54 ✜ 42 ✜ 88 ✜ B2 ✜ E9 ✜ 19 ✜ 4A ✜ 79 ✜ 87 ✜ 2E ✜ 62 ✜ 44 ✜ 2D ✜ 7C 06 ☽ 78 ☽ F8
+ ☽ FD ☽ 52 ☽ 92 ☽ 6D ☽ CD ☽ D6 ☽ F3 ☽ 28 ☽ 6B ☽ 1F ☽ DB ☽ CB ☽ D3 F2 ☮ 08 ☮ 34 ☮ 72 ☮ A2 ☮ 12 ☮ 75 ☮ AE ☮ D1
+ ☮ 09 ☮ 17 ☮ D0 ☮ 88 ☮ 4C ☮ 04 ☮ 8E 04 ☾ E5 ☾ BF ☾ D1 ☾ 41 ☾ 64 ☾ D1 ☾ F7 ☾ 89 ☾ 6D ☾ 8B ☾ B2 ☾ F2 ☾ 46 ☾ C0
+ ☾ 56 87 ☮ 8D ☮ B8 ☮ 7C ☮ C6 ☮ FE ☮ E9 ☮ 61 ☮ 88 ☮ 08 ☮ 61 ☮ DD ☮ E3 ☮ B8 ☮ B5 ☮ 47 ♥
+ """
+
+ /**import some references in order to reduce dependencies */
+
+ final val hex: Iri = new Iri("http://www.w3.org/ns/auth/cert#hex")
+ final val identity: Iri = new Iri("http://www.w3.org/ns/auth/cert#identity")
+ final val RSAPublicKey: Iri = new Iri("http://www.w3.org/ns/auth/rsa#RSAPublicKey")
+ final val modulus: Iri = new Iri("http://www.w3.org/ns/auth/rsa#modulus")
+ final val public_exponent: Iri = new Iri("http://www.w3.org/ns/auth/rsa#public_exponent")
+
+ val henryUri: String = "http://bblfish.net/#hjs"
+ val retoUri: String = "http://farewellutopia.com/reto/#me"
+ val danbriUri: String = "http://danbri.org/foaf.rdf#danbri"
+
+
+ private val tinyGraph: ImmutableGraph = {
+ val gr = new SimpleGraph
+ val reto = new BlankNode()
+ val danny = new BlankNode()
+ val henry = new Iri(henryUri)
+
+ gr.add(new TripleImpl(reto, RDF.`type`, FOAF.Person))
+ gr.add(new TripleImpl(reto, FOAF.name, new PlainLiteralImpl("Reto Bachman-Gmür", new Language("rm"))))
+ //it is difficult to remember that one needs to put a string literal if one does not want to specify a language
+ gr.add(new TripleImpl(reto, FOAF.title, new TypedLiteralImpl("Mr", XSD.string)))
+ gr.add(new TripleImpl(reto, FOAF.currentProject, new Iri("http://clerezza.org/")))
+ gr.add(new TripleImpl(reto, FOAF.knows, henry))
+ gr.add(new TripleImpl(reto, FOAF.knows, danny))
+
+ gr.add(new TripleImpl(danny, FOAF.name, new PlainLiteralImpl("Danny Ayers", new Language("en"))))
+ gr.add(new TripleImpl(danny, RDF.`type`, FOAF.Person))
+ gr.add(new TripleImpl(danny, FOAF.knows, henry))
+ gr.add(new TripleImpl(danny, FOAF.knows, reto))
+
+ gr.add(new TripleImpl(henry, FOAF.name, new TypedLiteralImpl("Henry Story", XSD.string))) //It is tricky to remember that one needs this for pure strings
+ gr.add(new TripleImpl(henry, FOAF.currentProject, new Iri("http://webid.info/")))
+ gr.add(new TripleImpl(henry, RDF.`type`, FOAF.Person))
+ gr.add(new TripleImpl(henry, FOAF.knows, danny))
+ gr.add(new TripleImpl(henry, FOAF.knows, reto))
+
+ val pk = new BlankNode()
+ gr.add(new TripleImpl(pk, RDF.`type`, RSAPublicKey))
+ gr.add(new TripleImpl(pk, identity, henry))
+ gr.add(new TripleImpl(pk, modulus, LiteralFactory.getInstance().createTypedLiteral(65537)))
+ gr.add(new TripleImpl(pk, public_exponent, new TypedLiteralImpl(bblfishModulus, hex)))
+ gr.getImmutableGraph
+ }
+
+
+ @Test
+ def singleTriple {
+ val expected = {
+ val s = new SimpleGraph
+ s.add(new TripleImpl(henryUri.iri, FOAF.knows, retoUri.iri))
+ s.getImmutableGraph
+ }
+ val ez = new EzGraph() {
+ henryUri.iri -- FOAF.knows --> retoUri.iri
+ }
+ Assert.assertEquals("The two graphs should be equals", expected, ez.getImmutableGraph)
+ }
+
+ @Test
+ def inverseTriple {
+ val expected = {
+ val s = new SimpleGraph
+ s.add(new TripleImpl(retoUri.iri, FOAF.knows, henryUri.iri))
+ s.getImmutableGraph
+ }
+ val ez = new EzGraph() {
+ henryUri.iri <-- FOAF.knows -- retoUri.iri
+ }
+ Assert.assertEquals("The two graphs should be equals", expected, ez.getImmutableGraph)
+ }
+
+ @Test
+ def usingAsciiArrows {
+ val ez = new EzGraph() {(
+ b_("reto").a(FOAF.Person) -- FOAF.name --> "Reto Bachman-Gmür".lang("rm")
+ -- FOAF.title --> "Mr"
+ -- FOAF.currentProject --> "http://clerezza.org/".iri
+ -- FOAF.knows --> (
+ "http://bblfish.net/#hjs".iri.a(FOAF.Person)
+ -- FOAF.name --> "Henry Story"
+ -- FOAF.currentProject --> "http://webid.info/".iri
+ -- FOAF.knows -->> List(b_("reto"), b_("danny"))
+ //one need to list properties before inverse properties, or use brackets
+ <-- identity -- (
+ bnode.a(RSAPublicKey) //. notation because of precedence of operators
+ -- modulus --> 65537
+ -- public_exponent --> (bblfishModulus^^hex) // brackets needed due to precedence
+ )
+ )
+ -- FOAF.knows --> (
+ b_("danny").a(FOAF.Person)
+ -- FOAF.name --> "Danny Ayers".lang("en")
+ -- FOAF.knows --> "http://bblfish.net/#hjs".iri //knows
+ -- FOAF.knows --> b_("reto")
+ )
+ )}
+ Assert.assertEquals("the two graphs should be of same size",tinyGraph.size,ez.size)
+ Assert.assertEquals("Both graphs should contain exactly the same triples",tinyGraph,ez.getImmutableGraph)
+ //We can add triples by creating a new anonymous instance
+ new EzGraph(ez) {(
+ "http://bblfish.net/#hjs".iri -- FOAF.name --> "William"
+ -- FOAF.name --> "Bill"
+ )}
+ Assert.assertEquals("the triple colletion has grown by one",tinyGraph.size()+2,ez.size)
+ //or by just importing it
+ import ez._
+ ez.b_("danny") -- FOAF.name --> "George"
+ Assert.assertEquals("the triple colletion has grown by one",tinyGraph.size()+3,ez.size)
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4d35191b/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/RichGraphNodeTest.scala
----------------------------------------------------------------------
diff --git a/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/RichGraphNodeTest.scala b/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/RichGraphNodeTest.scala
new file mode 100644
index 0000000..2029c03
--- /dev/null
+++ b/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/RichGraphNodeTest.scala
@@ -0,0 +1,174 @@
+/*
+ * 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.clerezza.rdf.scala.utils
+
+import org.apache.clerezza.rdf.utils._
+import org.apache.clerezza.rdf.core._
+import org.apache.clerezza.rdf.core.impl._
+import org.apache.clerezza.rdf.ontologies._
+import org.apache.commons.rdf.BlankNode
+import org.apache.commons.rdf.Graph
+import org.apache.commons.rdf.Iri
+import org.apache.commons.rdf.Language
+import org.apache.commons.rdf.Literal
+import org.apache.commons.rdf.impl.utils.PlainLiteralImpl
+import org.apache.commons.rdf.impl.utils.TripleImpl
+import org.apache.commons.rdf.impl.utils.TypedLiteralImpl
+import org.apache.commons.rdf.impl.utils.simple.SimpleGraph
+import org.junit._
+import Preamble._
+
+class RichGraphNodeTest {
+
+ private val johnUri = new Iri("http://example.org/john")
+ private val susanneUri = new Iri("http://example.org/susanne")
+ private val listUri = new Iri("http://example.org/list")
+ private val greetingsUri = new Iri("http://example.org/greetings")
+ private val billBlankNode = new BlankNode()
+ private var node : RichGraphNode = null;
+ private var mGraph = new SimpleGraph()
+
+ @Before
+ def prepare() = {
+ mGraph.add(new TripleImpl(johnUri, FOAF.name, new PlainLiteralImpl("John")));
+ mGraph.add(new TripleImpl(johnUri, FOAF.nick, new PlainLiteralImpl("johny")));
+ mGraph.add(new TripleImpl(johnUri, FOAF.name, new PlainLiteralImpl("Johnathan Guller")));
+ mGraph.add(new TripleImpl(johnUri, FOAF.knows, billBlankNode))
+ mGraph.add(new TripleImpl(johnUri, RDF.`type`, FOAF.Person));
+ mGraph.add(new TripleImpl(billBlankNode, FOAF.nick, new PlainLiteralImpl("Bill")));
+ mGraph.add(new TripleImpl(billBlankNode, FOAF.name, new PlainLiteralImpl("William")));
+ mGraph.add(new TripleImpl(billBlankNode, RDF.`type`, FOAF.Person));
+ mGraph.add(new TripleImpl(susanneUri, FOAF.knows, johnUri));
+ mGraph.add(new TripleImpl(susanneUri, FOAF.name, new PlainLiteralImpl("Susanne")));
+ mGraph.add(new TripleImpl(susanneUri, RDF.`type`, FOAF.Person));
+ val rdfList = new RdfList(listUri, mGraph);
+ rdfList.add(johnUri)
+ rdfList.add(new PlainLiteralImpl("foo"))
+ rdfList.add(new PlainLiteralImpl("bar"))
+ mGraph.add(new TripleImpl(johnUri, SKOS04.related, listUri))
+ val litEn = new PlainLiteralImpl("hello",
+ new Language("en"))
+ val litFr = new PlainLiteralImpl("satul",
+ new Language("fr"))
+ mGraph.add(new TripleImpl(greetingsUri, RDF.value, litEn))
+ mGraph.add(new TripleImpl(greetingsUri, RDF.value, litFr))
+ node = new GraphNode(johnUri, mGraph)
+ }
+
+ @Test
+ def testBaseGraph {
+ val preamble = new Preamble(mGraph)
+ import preamble._
+ def asGn(gn: GraphNode) = gn
+ val johnUriNode = asGn(johnUri)
+ Assert.assertEquals(johnUriNode, node)
+ }
+
+ @Test
+ def testSlash = {
+ val rNode = new RichGraphNode(node)
+ Assert.assertEquals(new PlainLiteralImpl("johny"), (rNode/FOAF.nick)(0).getNode)
+ Assert.assertEquals(2, (rNode/FOAF.name).length(20))
+ val stringNames = (for(name <- (rNode/FOAF.name).iterator) yield {
+ name.toString
+ }).toList
+ Assert.assertTrue(stringNames.contains("\"Johnathan Guller\""))
+ Assert.assertTrue(stringNames.contains("\"John\""))
+ }
+
+ @Test
+ def testIterate = {
+ val simple: Graph = new SimpleGraph();
+ val node = new GraphNode(new BlankNode(), simple);
+ node.addProperty(DCTERMS.provenance, new Iri("http://example.org/foo"));
+ node.addProperty(DCTERMS.language, new Iri("http://www.bluewin.ch/"));
+ simple.add(new TripleImpl(new Iri("http://www.bluewin.ch/"),RDF.`type`, RDFS.Container));
+ node.addProperty(RDF.`type`, PLATFORM.HeadedPage);
+ node.addProperty(RDF.`type`, RDFS.Class);
+ val test: CollectedIter[RichGraphNode] = node/DCTERMS.language/RDF.`type`;
+ Assert.assertEquals(1, test.length)
+ var counter = 0;
+ for(k <- test) { counter = counter + 1 }
+ Assert.assertEquals(1, counter)
+ }
+
+ @Test
+ def testInverse = {
+ val rNode = new RichGraphNode(node)
+ Assert.assertEquals(1, (rNode/-FOAF.knows).length)
+ }
+
+ @Test
+ def testMissingProperty = {
+ val rNode = new RichGraphNode(node)
+ Assert.assertEquals(0, (rNode/FOAF.thumbnail).length)
+ Assert.assertEquals("", rNode/FOAF.thumbnail*)
+
+ }
+
+ @Test
+ def testInverseImplicit = {
+ Assert.assertEquals(1, (node/-FOAF.knows).length)
+ }
+
+ @Test
+ def testPath = {
+ Assert.assertEquals(1, (node/-FOAF.knows).length)
+ Assert.assertEquals(new PlainLiteralImpl("Susanne"), node/-FOAF.knows%0/FOAF.name%0!)
+ Assert.assertEquals(new PlainLiteralImpl("Susanne"), ((node/-FOAF.knows)(0)/FOAF.name)(0)!)
+ Assert.assertEquals(new PlainLiteralImpl("Susanne"), node/-FOAF.knows/FOAF.name!)
+ Assert.assertEquals(new PlainLiteralImpl("Bill"), node/FOAF.knows/FOAF.nick!)
+ Assert.assertEquals("Bill", (node/FOAF.knows/FOAF.nick)(0)*)
+ Assert.assertEquals("Bill", node/FOAF.knows/FOAF.nick*)
+ }
+
+ @Test
+ def testLists = {
+ Assert.assertEquals(new PlainLiteralImpl("foo"),(node/SKOS04.related).asList().get(1))
+ Assert.assertEquals(new PlainLiteralImpl("foo"), (node/SKOS04.related%0!!)(1)!)
+ Assert.assertEquals(new PlainLiteralImpl("foo"),
+ (for (value <- node/SKOS04.related%0!!) yield value!).toList(1))
+ Assert.assertEquals(new PlainLiteralImpl("bar"),
+ (for (value <- node/SKOS04.related%0!!) yield value!).toList(2))
+ Assert.assertEquals(new PlainLiteralImpl("foo"), node/SKOS04.related%0%!!1!)
+ }
+
+ @Test
+ def sortProperties = {
+ Assert.assertEquals(new PlainLiteralImpl("bar"), (node/SKOS04.related%0!!).sortWith((a,b) => ((a*) < (b*)))(0)!)
+ Assert.assertEquals(johnUri, (node/SKOS04.related%0!!).sortWith((a,b) => ((a*) > (b*)))(0)!)
+ }
+
+ @Test
+ def literalAsObject = {
+ val dateLiteral = new TypedLiteralImpl("2009-01-01T01:33:58Z",
+ new Iri("http://www.w3.org/2001/XMLSchema#dateTime"))
+ val node = new GraphNode(dateLiteral, new SimpleGraph())
+ Assert.assertNotNull(node.as[java.util.Date])
+ }
+
+ @Test
+ def literalLanguage = {
+ node = new GraphNode(greetingsUri, mGraph)
+ val lang = new Language("en")
+ val enValue = (node/RDF.value).find(l=>(l!).asInstanceOf[Literal].getLanguage == lang).get
+ Assert.assertEquals("hello", enValue*)
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4d35191b/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/TypeConversionTest.scala
----------------------------------------------------------------------
diff --git a/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/TypeConversionTest.scala b/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/TypeConversionTest.scala
new file mode 100644
index 0000000..5798e86
--- /dev/null
+++ b/rdf/scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/TypeConversionTest.scala
@@ -0,0 +1,74 @@
+package org.apache.clerezza.rdf.scala.utils
+
+/*
+ * 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.
+ */
+import org.apache.commons.rdf.Iri
+import org.apache.commons.rdf.Language
+import org.apache.commons.rdf.impl.utils.PlainLiteralImpl
+import org.apache.commons.rdf.impl.utils.TripleImpl
+import org.apache.commons.rdf.impl.utils.TypedLiteralImpl
+import org.junit._
+import org.apache.clerezza.rdf.core._
+
+class TypeConversionTest {
+
+ private val literalFactory = LiteralFactory.getInstance()
+
+ import Preamble._
+
+ @Test
+ def useStringAsObject {
+ val t = new TripleImpl(new Iri(("http://example.org/subject")), new Iri(("http://example.org/predicate")), "a value")
+ Assert.assertEquals(literalFactory.createTypedLiteral("a value"), t.getObject)
+ }
+
+ /*@Test
+ def useStringWithLanguageTag {
+ val t = new TripleImpl(new Iri(("http://example.org/subject")), new Iri(("http://example.org/predicate")), "a value"("en"))
+ Assert.assertEquals(new PlainLiteralImpl("a value", new Language("en")), t.getObject)
+ }*/
+
+ @Test
+ def useStringWithLanguageTag {
+ val lit = new PlainLiteralImpl("a value", new Language("en"))
+ val t = new TripleImpl(new Iri(("http://example.org/subject")), new Iri(("http://example.org/predicate")), "a value" lang "en")
+ Assert.assertEquals(lit, t.getObject)
+ }
+
+ @Test
+ def useStringWithType {
+ val typeUri = new Iri("http://example.org/dt")
+ val t = new TripleImpl(new Iri(("http://example.org/subject")), new Iri(("http://example.org/predicate")), "a value"^^typeUri)
+ Assert.assertEquals(new TypedLiteralImpl("a value", typeUri), t.getObject)
+ }
+
+ @Test
+ def literaToString {
+ val lit = literalFactory.createTypedLiteral("a value")
+ val s: String = lit
+ Assert.assertEquals("a value", s)
+ }
+
+ @Test
+ def dotUri {
+ val t = new TripleImpl(new Iri(("http://example.org/subject")), new Iri(("http://example.org/predicate")), "http://example.org".iri)
+ Assert.assertEquals(new Iri("http://example.org"), t.getObject)
+ }
+
+}