~ now able to go back and forth ScalaModel and JenaModel
authorAlexandre Bertails <bertails@w3.org>
Sun, 22 Jan 2012 16:59:52 -0500
changeset 7 58319cf8d0fa
parent 6 fe13ab2c1213
child 8 0de43190d051
~ now able to go back and forth ScalaModel and JenaModel
aRDF/project/build.scala
aRDF/src/main/scala/Isomorphic.scala
aRDF/src/main/scala/JenaModel.scala
aRDF/src/main/scala/Test.scala
aRDF/src/main/scala/Transformer.scala
--- a/aRDF/project/build.scala	Sun Jan 08 19:05:57 2012 -0500
+++ b/aRDF/project/build.scala	Sun Jan 22 16:59:52 2012 -0500
@@ -47,7 +47,8 @@
   
   val mySettings = Seq(
     resolvers += "apache-repo-releases" at "http://repository.apache.org/content/repositories/releases/",
-    libraryDependencies += "org.apache.jena" % "jena-arq" % "2.9.0-incubating"
+    libraryDependencies += "org.apache.jena" % "jena-arq" % "2.9.0-incubating",
+    libraryDependencies += "com.novocode" % "junit-interface" % "0.8" % "test"
   )
   
   lazy val project = Project(
--- a/aRDF/src/main/scala/Isomorphic.scala	Sun Jan 08 19:05:57 2012 -0500
+++ b/aRDF/src/main/scala/Isomorphic.scala	Sun Jan 22 16:59:52 2012 -0500
@@ -1,18 +1,18 @@
 package org.w3.isomorphic
 
-trait PatternMatching0[R] {
+trait UnApply0[R] {
   def unapply(r: R): Boolean
 }
 
-trait Isomorphic0[R] extends Function0[R] with PatternMatching0[R]
+trait Isomorphic0[R] extends Function0[R] with UnApply0[R]
 
-trait PatternMatching1[T, R] {
+trait UnApply1[T, R] {
   def unapply(r: R): Option[T]
 }
 
-trait Isomorphic1[T, R] extends Function1[T, R] with PatternMatching1[T, R]
+trait Isomorphic1[T, R] extends Function1[T, R] with UnApply1[T, R]
 
-trait PatternMatching2[T1, T2, R] {
+trait UnApply2[T1, T2, R] {
   def unapply(r: R): Option[(T1, T2)]
 }
 
@@ -21,11 +21,11 @@
  *   def apply(t1:T1, t2:T2):R
  *   def unapply(r:R):Option[(T1, T2)]
  */
-trait Isomorphic2[T1, T2, R] extends Function2[T1, T2, R] with PatternMatching2[T1, T2, R]
+trait Isomorphic2[T1, T2, R] extends Function2[T1, T2, R] with UnApply2[T1, T2, R]
 
-trait PatternMatching3[T1, T2, T3, R] {
+trait UnApply3[T1, T2, T3, R] {
   def unapply(r: R): Option[(T1, T2, T3)]
 }
 
-trait Isomorphic3[T1, T2, T3, R] extends Function3[T1, T2, T3, R] with PatternMatching3[T1, T2, T3, R]
+trait Isomorphic3[T1, T2, T3, R] extends Function3[T1, T2, T3, R] with UnApply3[T1, T2, T3, R]
 
--- a/aRDF/src/main/scala/JenaModel.scala	Sun Jan 08 19:05:57 2012 -0500
+++ b/aRDF/src/main/scala/JenaModel.scala	Sun Jan 22 16:59:52 2012 -0500
@@ -7,17 +7,6 @@
 
 import org.w3.isomorphic._
 
-object Util {
-  def tryopt[T](b: => T):Option[T] =
-    try {
-      Some(b)
-    } catch {
-      case e => None
-    }
-}
-
-import Util._
-
 trait JenaModel extends Model {
 
   case class IRI(iri:String) { override def toString = '"' + iri + '"' }
@@ -63,23 +52,22 @@
     def unapply(t:Triple):Option[(Subject, Predicate, Object)] = Some((t.getSubject, t.getPredicate, t.getObject))
   }
 
-  type BNode = Node_Blank
+  type BNode = JenaNode
   object BNode extends Isomorphic1[String, BNode] {
     def apply(label:String):BNode = { val id = AnonId.create(label) ; JenaNode.createAnon(id).asInstanceOf[Node_Blank] }
-    def unapply(bn:BNode):Option[String] = tryopt(bn.getBlankNodeId.getLabelString)
+    def unapply(bn:BNode):Option[String] = if (bn.isBlank) Some(bn.getBlankNodeId.getLabelString) else None
   }
 
   type Node = JenaNode
-  type NodeIRI = Node_URI
+  type NodeIRI = Node
   object NodeIRI extends Isomorphic1[IRI, NodeIRI] {
-    def apply(iri:IRI):NodeIRI = { val IRI(s) = iri ; JenaNode.createURI(s).asInstanceOf[Node_URI] }
-    def unapply(node:NodeIRI):Option[IRI] = tryopt(IRI(node.getURI))
+    def apply(iri: IRI): NodeIRI = { val IRI(s) = iri ; JenaNode.createURI(s).asInstanceOf[Node_URI] }
+    def unapply(node: NodeIRI): Option[IRI] = if (node.isURI) Some(IRI(node.getURI)) else None
   }
-  type NodeBNode = Node_Blank
+  type NodeBNode = JenaNode
   object NodeBNode extends Isomorphic1[BNode, NodeBNode] {
     def apply(node:BNode):NodeBNode = node
-    def unapply(node:NodeBNode):Option[BNode] =
-      if (node.isBlank) Some(node) else None
+    def unapply(node:NodeBNode):Option[BNode] = if (node.isBlank) Some(node) else None
   }
 
   type Subject = JenaNode
@@ -92,8 +80,8 @@
   type Predicate = JenaNode
   type PredicateIRI = JenaNode
   object PredicateIRI extends Isomorphic1[IRI, PredicateIRI] {
-    def apply(iri:IRI):PredicateIRI = { val IRI(s) = iri ; JenaNode.createURI(s) }
-    def unapply(node:PredicateIRI):Option[IRI] = tryopt(IRI(node.getURI))
+    def apply(iri: IRI): PredicateIRI = { val IRI(s) = iri ; JenaNode.createURI(s) }
+    def unapply(node: PredicateIRI): Option[IRI] = if (node.isURI) Some(IRI(node.getURI)) else None
   }
 
   type Object = JenaNode
@@ -111,7 +99,7 @@
   }
 
   lazy val mapper = TypeMapper.getInstance
-  type Literal = Node_Literal
+  type Literal = JenaNode
   object Literal extends Isomorphic3[String, Option[LangTag], Option[IRI], Literal] {
     def apply(lit: String, langtagOption: Option[LangTag], datatypeOption: Option[IRI]): Literal = {
       JenaNode.createLiteral(
@@ -120,13 +108,14 @@
         datatypeOption.map{i => mapper.getTypeByName(i.iri)}.getOrElse(null)
       ).asInstanceOf[Literal]
     }
-    def unapply(literal: Literal): Option[(String, Option[LangTag], Option[IRI])] = {
-      tryopt {(
-        literal.getLiteralValue.toString,
-        { val l = literal.getLiteralLanguage; if (l != "") Some(LangTag(l)) else None },
-        Option(literal.getLiteralDatatype).map{typ => IRI(typ.getURI)}
-      )}
-    }
+    def unapply(literal: Literal): Option[(String, Option[LangTag], Option[IRI])] =
+      if (literal.isLiteral)
+        Some((
+          literal.getLiteralLexicalForm.toString,
+          { val l = literal.getLiteralLanguage; if (l != "") Some(LangTag(l)) else None },
+          Option(literal.getLiteralDatatype).map{typ => IRI(typ.getURI)}))
+      else
+        None
   }
   
   case class LangTag(s:String)
--- a/aRDF/src/main/scala/Test.scala	Sun Jan 08 19:05:57 2012 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-package org.w3.rdf
-
-import org.w3.rdf.jena._
-
-class AbstractTest[M <: Model](m: M) {
-  
-  def test() = {
-    
-    import m._
-    
-    val t = Triple(
-        SubjectNode(NodeIRI(IRI("http://www.w3.org/"))), 
-        PredicateIRI(IRI("http://www.w3.org/predicate")),
-        ObjectLiteral(Literal("toto", None, None))
-    )
-    
-    val g: Graph = Graph(t)
-    
-    t match {
-      case Triple(SubjectNode(NodeIRI(IRI(s))), PredicateIRI(IRI(p)), ObjectLiteral(Literal(o, _, _))) => println(s.toString + p.toString + o.toString)
-      
-      
-    }
-    
-    Literal("toto", None, None) match { case Literal(t, None, None) => () }
-    
-    println(g)
-    
-  }
-  
-}
-
-object Test {
-  
-  def main(args: Array[String]): Unit = {
-    
-    val scalaTest = new AbstractTest[ScalaModel](ScalaModel)
-    val jenaTest = new AbstractTest[JenaModel](JenaModel)
-    
-    val tests = Seq(scalaTest, jenaTest)
-    tests.foreach { _.test() }
-    
-    import com.hp.hpl.jena._
-    import com.hp.hpl.jena.rdf.model._
-    import com.hp.hpl.jena.graph._
-    
-    val model = ModelFactory.createDefaultModel()
-//    model.read("http://www.w3.org/People/Berners-Lee/card")
-    model.getReader("TURTLE").read(model, "file:///tmp/card")
-    
-    val jenaGraph = new JenaModel.Graph(model.getGraph)
-    
-    val scalaGraph: ScalaModel#Graph = JenaToScala.transform(jenaGraph)
-    
-    println(scalaGraph)
-    
-  }
-  
-}
\ No newline at end of file
--- a/aRDF/src/main/scala/Transformer.scala	Sun Jan 08 19:05:57 2012 -0500
+++ b/aRDF/src/main/scala/Transformer.scala	Sun Jan 22 16:59:52 2012 -0500
@@ -27,8 +27,8 @@
   
   def transformNode(n: a.Node): b.Node = {
     n match {
+      case a.NodeBNode(label) => b.NodeBNode(transformBNode(label))
       case a.NodeIRI(iri) => b.NodeIRI(transformIRI(iri))
-      case a.NodeBNode(label) => b.NodeBNode(transformBNode(label))
     }
   }
     
@@ -62,4 +62,6 @@
 
 import org.w3.rdf.jena._
 
+object ScalaToJena extends Transformer[ScalaModel, JenaModel](ScalaModel, JenaModel)
+
 object JenaToScala extends Transformer[JenaModel, ScalaModel](JenaModel, ScalaModel)