You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by bb...@apache.org on 2011/07/08 13:20:06 UTC

svn commit: r1144265 - in /incubator/clerezza/trunk/parent/rdf.scala.utils/src: main/scala/org/apache/clerezza/rdf/scala/utils/ test/scala/org/apache/clerezza/rdf/scala/utils/

Author: bblfish
Date: Fri Jul  8 11:20:05 2011
New Revision: 1144265

URL: http://svn.apache.org/viewvc?rev=1144265&view=rev
Log:
CLEREZZA-596 - Replace LiteralBuilder with EzLiteral

Added:
    incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzLiteral.scala
Modified:
    incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala
    incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EzMGraphTest.scala

Added: incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzLiteral.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzLiteral.scala?rev=1144265&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzLiteral.scala (added)
+++ incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EzLiteral.scala Fri Jul  8 11:20:05 2011
@@ -0,0 +1,76 @@
+/*
+ * 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.clerezza.rdf.core.{Language, PlainLiteral, UriRef}
+
+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 hjs
+ * @created: 07/07/2011
+ */
+class EzLiteral(string: String) extends TypedLiteralImpl(string,XSD.string) {
+
+	/**
+	 * @return a plain literal with language specified by lang
+	 */
+	def lang(lng: String): PlainLiteral = lang(new Language(lng))
+
+	/**
+	 * @return a plain literal with language specified by lang
+	 */
+	def lang(lng: Language) = new PlainLiteralImpl(string, lng)
+
+	/**
+	 * @return a plain literal with language specified by lang symbol
+	 */
+	def lang(lng: Symbol) = lang(new Language(lng.toString()))
+
+	/**
+	 * Map to a Typed Literal of given type
+	 */
+	def ^^(typ: UriRef) = new TypedLiteralImpl(string, typ)
+
+	/**
+	 * Map to a URI of given lexical form
+	 */
+	def uri = new UriRef(string)
+
+}
\ No newline at end of file

Modified: incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala?rev=1144265&r1=1144264&r2=1144265&view=diff
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala (original)
+++ incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/Preamble.scala Fri Jul  8 11:20:05 2011
@@ -70,7 +70,7 @@ protected trait TcDependentConversions e
 	}
 }
 
-protected trait TcIndependentConversions {
+protected trait TcIndependentConversions extends EzLiteralImplicits {
 	implicit def toRichGraphNode(node: GraphNode) = {
 		new RichGraphNode(node)
 	}
@@ -85,19 +85,9 @@ protected trait TcIndependentConversions
 
 	private val litFactory = LiteralFactory.getInstance
 
-	/*a strig is dynamically converted to 3 types of objects, a Literal, a
-	  LiteralBuilder or a UriRefBuilder*/
-
-	implicit def string2litBuilder(str: String) = new TcIndependentConversions.LiteralBuilder(str)
-
-	implicit def string2uriRefBuilder(str: String) = new TcIndependentConversions.UriRefBuilder(str)
-
-	implicit def string2lit(str: String) = litFactory.createTypedLiteral(str)
 
 	implicit def lit2String(lit: Literal) = lit.getLexicalForm
 
-	implicit def litBuilder2lit(litBuilder: TcIndependentConversions.LiteralBuilder) = litFactory.createTypedLiteral(litBuilder.lexicalForm)
-
 	implicit def date2lit(date: Date) = litFactory.createTypedLiteral(date)
 
 	implicit def int2lit(int: Int) = litFactory.createTypedLiteral(int)
@@ -123,58 +113,7 @@ protected object TcIndependentConversion
 	val emptyGraph = new impl.SimpleGraph(new impl.SimpleMGraph)
 	val emptyLiteral = new RichGraphNode(new GraphNode(new impl.PlainLiteralImpl(""), emptyGraph))
 
-	/**
-	 * A Literal Builder enriches a String with methods to create a literal
-	 */
-	class LiteralBuilder(val lexicalForm: String) {
-
-		/**
-		 * Produces a PlainLiteral with the wrapped String as lexical form
-		 * and a given language
-		 *
-		 * @param lang the language tag of the literal to be created
-		 * @return a plain literal with the specified language
-		 */
-		def lang(lang: Language) = new PlainLiteralImpl(lexicalForm, lang)
-
-		/**
-		 * Produces a PlainLiteral with the wrapped String as lexical form
-		 * and a given language
-		 *
-		 * @param lang a sthe language tag of the literal to be created as String
-		 * @return a plain literal with the specified language
-		 */
-		def lang(lang: String) = new PlainLiteralImpl(lexicalForm, new Language(lang))
-
-		/**
-		 * Produces a PlainLiteral with the wrapped String as lexical form
-		 * and a given language
-		 *
-		 * @param lang a symbol of which the name is the language tag of the literal to be created
-		 * @return a plain literal with the specified language
-		 */
-		def lang(lang: Symbol) = new PlainLiteralImpl(lexicalForm, new Language(lang.name))
-
-		/**
-		 * Produces a TypedLiteral with the wrapped String as lexical form
-		 * and a data type
-		 *
-		 * @param dataType the data type
-		 * @return the TypedLiteral of the specified type
-		 */
-		def ^^(dataType: UriRef) = new TypedLiteralImpl(lexicalForm, dataType)
 
-	}
 
-	/**
-	 * A UriRef Builder enriches a String with methods to create a UriRef
-	 */
-	class UriRefBuilder(string: String) {
-
-		/**
-		 * returns a UriRef with the wrapped string as unicode representation
-		 */
-		def uri = new UriRef(string)
 
-	}
 }
\ No newline at end of file

Modified: incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EzMGraphTest.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EzMGraphTest.scala?rev=1144265&r1=1144264&r2=1144265&view=diff
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EzMGraphTest.scala (original)
+++ incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EzMGraphTest.scala Fri Jul  8 11:20:05 2011
@@ -18,11 +18,11 @@
  */
 package org.apache.clerezza.rdf.scala.utils
 
-import org.apache.clerezza.rdf.ontologies._
 import org.junit._
 import org.apache.clerezza.rdf.core._
 import impl._
 import Preamble._
+import org.apache.clerezza.rdf.ontologies._
 
 class EzMGraphTest {
 
@@ -110,41 +110,41 @@ class EzMGraphTest {
 
 	@Test
 	def usingAsciiArrows {
-		val ez = new EzMGraph() {
-			b_("reto").a(FOAF.Person) --
-			FOAF.name --> "Reto Bachman-Gmür".lang('rm) --
-			FOAF.title --> "Mr" --
-			FOAF.currentProject --> "http://clerezza.org/".uri --
-			FOAF.knows --> (
-			 "http://bblfish.net/#hjs".uri.a(FOAF.Person)
-				  -- FOAF.name --> "Henry Story"
-				  -- FOAF.currentProject --> "http://webid.info/".uri
-				  -- FOAF.knows -->> List(b_("reto"), b_("danny"))
+		val ez = new EzMGraph() {(
+			b_("reto").a(FOAF.Person) -- FOAF.name --> "Reto Bachman-Gmür".lang("rm")
+				-- FOAF.title --> "Mr"
+				-- FOAF.currentProject --> "http://clerezza.org/".uri
+				-- FOAF.knows --> (
+					"http://bblfish.net/#hjs".uri.a(FOAF.Person)
+						-- FOAF.name --> "Henry Story"
+				      -- FOAF.currentProject --> "http://webid.info/".uri
+				      -- 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".uri //knows
-					-- FOAF.knows --> b_("reto")
-			)
-		}
-		Assert.assertEquals("the two graphs should be of same size",tinyGraph.size(),ez.size())
+				)
+				-- FOAF.knows --> (
+					b_("danny").a(FOAF.Person)
+						-- FOAF.name --> "Danny Ayers".lang("en")
+				      -- FOAF.knows --> "http://bblfish.net/#hjs".uri //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.getGraph)
 		//We can add triples by creating a new anonymous instance
 		new EzMGraph(ez) {(
 			"http://bblfish.net/#hjs".uri -- FOAF.name --> "William"
 			-- FOAF.name --> "Bill"
 		)}
-		Assert.assertEquals("the triple colletion has grown by one",tinyGraph.size()+2,ez.size())
+		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())
+		Assert.assertEquals("the triple colletion has grown by one",tinyGraph.size()+3,ez.size)
 	}
 
 }