~ give up on Tim and others:
authorAlexandre Bertails <bertails@gmail.com>
Sun, 05 Feb 2012 16:54:43 -0500
changeset 13 b8ee566618a1
parent 12 963e40ee047f
child 14 fd480bff1f40
~ give up on Tim and others:
triple ::= (node, iri, node)
node ::= iri | blanknode | literal
aRDF/src/main/scala/Algebraic.scala
aRDF/src/main/scala/Isomorphic.scala
aRDF/src/main/scala/JenaModel.scala
aRDF/src/main/scala/Model.scala
aRDF/src/main/scala/ScalaModel.scala
aRDF/src/main/scala/Transformer.scala
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aRDF/src/main/scala/Algebraic.scala	Sun Feb 05 16:54:43 2012 -0500
@@ -0,0 +1,31 @@
+package org.w3.algebraic
+
+trait UnApply0[R] {
+  def unapply(r: R): Boolean
+}
+
+trait AlgebraicDataType0[R] extends Function0[R] with UnApply0[R]
+
+trait UnApply1[T, R] {
+  def unapply(r: R): Option[T]
+}
+
+trait AlgebraicDataType1[T, R] extends Function1[T, R] with UnApply1[T, R]
+
+trait UnApply2[T1, T2, R] {
+  def unapply(r: R): Option[(T1, T2)]
+}
+
+/**
+ * basically, you have to implement both following functions
+ *   def apply(t1:T1, t2:T2):R
+ *   def unapply(r:R):Option[(T1, T2)]
+ */
+trait AlgebraicDataType2[T1, T2, R] extends Function2[T1, T2, R] with UnApply2[T1, T2, R]
+
+trait UnApply3[T1, T2, T3, R] {
+  def unapply(r: R): Option[(T1, T2, T3)]
+}
+
+trait AlgebraicDataType3[T1, T2, T3, R] extends Function3[T1, T2, T3, R] with UnApply3[T1, T2, T3, R]
+
--- a/aRDF/src/main/scala/Isomorphic.scala	Wed Feb 01 23:20:14 2012 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-package org.w3.isomorphic
-
-trait UnApply0[R] {
-  def unapply(r: R): Boolean
-}
-
-trait AlgebraicDataType0[R] extends Function0[R] with UnApply0[R]
-
-trait UnApply1[T, R] {
-  def unapply(r: R): Option[T]
-}
-
-trait AlgebraicDataType1[T, R] extends Function1[T, R] with UnApply1[T, R]
-
-trait UnApply2[T1, T2, R] {
-  def unapply(r: R): Option[(T1, T2)]
-}
-
-/**
- * basically, you have to implement both following functions
- *   def apply(t1:T1, t2:T2):R
- *   def unapply(r:R):Option[(T1, T2)]
- */
-trait AlgebraicDataType2[T1, T2, R] extends Function2[T1, T2, R] with UnApply2[T1, T2, R]
-
-trait UnApply3[T1, T2, T3, R] {
-  def unapply(r: R): Option[(T1, T2, T3)]
-}
-
-trait AlgebraicDataType3[T1, T2, T3, R] extends Function3[T1, T2, T3, R] with UnApply3[T1, T2, T3, R]
-
--- a/aRDF/src/main/scala/JenaModel.scala	Wed Feb 01 23:20:14 2012 -0500
+++ b/aRDF/src/main/scala/JenaModel.scala	Sun Feb 05 16:54:43 2012 -0500
@@ -5,14 +5,11 @@
 import com.hp.hpl.jena.rdf.model.{AnonId}
 import com.hp.hpl.jena.datatypes.{RDFDatatype, TypeMapper}
 
-import org.w3.isomorphic._
+import org.w3.algebraic._
 
 object JenaModel extends Model {
 
-  case class IRI(iri: String) { override def toString = '"' + iri + '"' }
-  object IRI extends AlgebraicDataType1[String, IRI]
-
-  class Graph(val jenaGraph: JenaGraph) extends GraphLike {
+  class Graph(val jenaGraph: JenaGraph) extends GraphInterface {
     def iterator: Iterator[Triple] = new Iterator[Triple] {
       val iterator = jenaGraph.find(JenaNode.ANY, JenaNode.ANY, JenaNode.ANY)
       def hasNext = iterator.hasNext
@@ -25,16 +22,13 @@
       new Graph(g)
     }
 
-    override def equals(o: Any): Boolean =
-      ( o.isInstanceOf[Graph] && jenaGraph.isIsomorphicWith(o.asInstanceOf[Graph].jenaGraph) )
-
   }
 
-  object Graph extends GraphObject {
+  object Graph extends GraphCompanionObject {
     def fromJena(jenaGraph: JenaGraph): Graph = new Graph(jenaGraph)
     def empty: Graph = new Graph(Factory.createDefaultGraph)
     def apply(elems: Triple*): Graph = apply(elems.toIterable)
-    def apply(it: Iterable[Triple]):Graph = {
+    def apply(it: Iterable[Triple]): Graph = {
       val jenaGraph = Factory.createDefaultGraph
       it foreach { t => jenaGraph add t }
       new Graph(jenaGraph)
@@ -42,67 +36,99 @@
   }
 
   type Triple = JenaTriple
-  object Triple extends AlgebraicDataType3[Subject, Predicate, Object, Triple] {
-    def apply(s: Subject, p: Predicate, o: Object): Triple = JenaTriple.create(s, p, o)
-    def unapply(t: Triple): Option[(Subject, Predicate, Object)] = Some((t.getSubject, t.getPredicate, t.getObject))
-  }
-
-  type BNode = JenaNode
-  object BNode extends AlgebraicDataType1[String, BNode] {
-    def apply(label: String): BNode = { val id = AnonId.create(label) ; JenaNode.createAnon(id).asInstanceOf[Node_Blank] }
-    def unapply(bn: BNode): Option[String] = if (bn.isBlank) Some(bn.getBlankNodeId.getLabelString) else None
+  object Triple extends AlgebraicDataType3[Node, IRI, Node, Triple] {
+    def apply(s: Node, p: IRI, o: Node): Triple = {
+      val predicate = NodeIRI(p)
+      JenaTriple.create(s, predicate, o)
+    }
+    def unapply(t: Triple): Option[(Node, IRI, Node)] =
+      (t.getSubject, t.getPredicate, t.getObject) match {
+        case (Node(s), NodeIRI(p), Node(o)) => Some((s, p, o))
+        case _ => None
+      }
   }
 
   type Node = JenaNode
-  type NodeIRI = Node
+  object Node {
+    def unapply(node: JenaNode): Option[Node] =
+      if (node.isURI || node.isBlank || node.isLiteral) Some(node) else None
+  }
+
+  type NodeIRI = Node_URI
   object NodeIRI extends AlgebraicDataType1[IRI, NodeIRI] {
     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 = JenaNode
+
+  type NodeBNode = Node_Blank
   object NodeBNode extends AlgebraicDataType1[BNode, NodeBNode] {
-    def apply(node: BNode):NodeBNode = node
-    def unapply(node: NodeBNode): Option[BNode] = if (node.isBlank) Some(node) else None
-  }
-
-  type Subject = JenaNode
-  type SubjectNode = JenaNode
-  object SubjectNode extends AlgebraicDataType1[Node, SubjectNode] {
-    def apply(node: Node): SubjectNode = node
-    def unapply(node: SubjectNode): Option[Node] = Some(node)
+    def apply(node: BNode): NodeBNode = node
+    def unapply(node: NodeBNode): Option[BNode] = if (node.isBlank) Some(node.asInstanceOf[Node_Blank]) else None
   }
 
-  type Predicate = JenaNode
-  type PredicateIRI = JenaNode
-  object PredicateIRI extends AlgebraicDataType1[IRI, PredicateIRI] {
-    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
-  type ObjectNode = JenaNode
-  object ObjectNode extends AlgebraicDataType1[Node, ObjectNode] {
-    def apply(node: Node): ObjectNode = node
-    def unapply(node: ObjectNode): Option[Node] =
-      if (node.isURI || node.isBlank) Some(node) else None
-  }
-  type ObjectLiteral = JenaNode
-  object ObjectLiteral extends AlgebraicDataType1[Literal, ObjectLiteral] {
-    def apply(literal: Literal): ObjectLiteral = literal
-    def unapply(node: ObjectLiteral): Option[Literal] =
+  type NodeLiteral = Node_Literal
+  object NodeLiteral extends AlgebraicDataType1[Literal, NodeLiteral] {
+    def apply(literal: Literal): NodeLiteral = literal
+    def unapply(node: NodeLiteral): Option[Literal] =
       if (node.isLiteral) Some(node.asInstanceOf[Node_Literal]) else None
   }
 
+  case class IRI(iri: String) { override def toString = '"' + iri + '"' }
+  object IRI extends AlgebraicDataType1[String, IRI]
+
+  type BNode = Node_Blank
+  object BNode extends AlgebraicDataType1[String, BNode] {
+    def apply(label: String): BNode = {
+      val id = AnonId.create(label)
+      JenaNode.createAnon(id).asInstanceOf[Node_Blank]
+    }
+    def unapply(bn: BNode): Option[String] =
+      if (bn.isBlank) Some(bn.getBlankNodeId.getLabelString) else None
+  }
+
+//  type Subject = JenaNode
+//  type SubjectNode = JenaNode
+//  object SubjectNode extends AlgebraicDataType1[Node, SubjectNode] {
+//    def apply(node: Node): SubjectNode = node
+//    def unapply(node: SubjectNode): Option[Node] = Some(node)
+//  }
+//  type SubjectLiteral = JenaNode
+//  object SubjectLiteral extends AlgebraicDataType1[Node, SubjectLiteral] {
+//    def apply(node: Node): SubjectLiteral = node
+//    def unapply(node: SubjectLiteral): Option[Node] = Some(node)
+//  }
+//
+//  type Predicate = JenaNode
+//  type PredicateIRI = JenaNode
+//  object PredicateIRI extends AlgebraicDataType1[IRI, PredicateIRI] {
+//    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
+//  type ObjectNode = JenaNode
+//  object ObjectNode extends AlgebraicDataType1[Node, ObjectNode] {
+//    def apply(node: Node): ObjectNode = node
+//    def unapply(node: ObjectNode): Option[Node] =
+//      if (node.isURI || node.isBlank) Some(node) else None
+//  }
+//  type ObjectLiteral = JenaNode
+//  object ObjectLiteral extends AlgebraicDataType1[Literal, ObjectLiteral] {
+//    def apply(literal: Literal): ObjectLiteral = literal
+//    def unapply(node: ObjectLiteral): Option[Literal] =
+//      if (node.isLiteral) Some(node.asInstanceOf[Node_Literal]) else None
+//  }
+
   lazy val mapper = TypeMapper.getInstance
   
-  type Literal = JenaNode
+  type Literal = Node_Literal
   object Literal extends AlgebraicDataType3[String, Option[LangTag], Option[IRI], Literal] {
     def apply(lit: String, langtagOption: Option[LangTag], datatypeOption: Option[IRI]): Literal = {
       JenaNode.createLiteral(
         lit,
         langtagOption map { _.s } getOrElse null,
         datatypeOption map { i => mapper.getTypeByName(i.iri) } getOrElse null
-      ).asInstanceOf[Literal]
+      ).asInstanceOf[Node_Literal]
     }
     def unapply(literal: Literal): Option[(String, Option[LangTag], Option[IRI])] =
       if (literal.isLiteral)
--- a/aRDF/src/main/scala/Model.scala	Wed Feb 01 23:20:14 2012 -0500
+++ b/aRDF/src/main/scala/Model.scala	Sun Feb 05 16:54:43 2012 -0500
@@ -1,53 +1,43 @@
 package org.w3.rdf
 
-import org.w3.isomorphic._
+import org.w3.algebraic._
 
 trait Model {
 
-  type IRI
-  trait GraphLike extends Iterable[Triple] { self =>
-    def ++(other: Graph):Graph
+  trait GraphInterface extends Iterable[Triple] { self =>
+    def ++(other: Graph): Graph
   }
-  type Graph <: GraphLike
+  type Graph <: GraphInterface
   type Triple
-  type BNode
   type Node
   type NodeIRI <: Node
   type NodeBNode <: Node
-  type Subject
-  type SubjectNode <: Subject
-  type Predicate
-  type PredicateIRI <: Predicate
-  type Object
-  type ObjectNode <: Object
-  type ObjectLiteral <: Object
+  type NodeLiteral <: Node
+  type IRI
+  type BNode
   type Literal
   type LangTag
 
-  val IRI : AlgebraicDataType1[String, IRI]
-
-  trait GraphObject {
+  trait GraphCompanionObject {
     def empty: Graph
     def apply(elems: Triple*): Graph
     def apply(it: Iterable[Triple]): Graph
   }
-  val Graph: GraphObject
 
-  val Triple: AlgebraicDataType3[Subject, Predicate, Object, Triple]
+  val Graph: GraphCompanionObject
 
-  val BNode: AlgebraicDataType1[String, BNode]
+  val Triple: AlgebraicDataType3[Node, IRI, Node, Triple]
 
   val NodeIRI: AlgebraicDataType1[IRI, NodeIRI]
   val NodeBNode: AlgebraicDataType1[BNode, NodeBNode]
-
-  val SubjectNode: AlgebraicDataType1[Node, SubjectNode]
+  val NodeLiteral: AlgebraicDataType1[Literal, NodeLiteral]
+  
+  val IRI : AlgebraicDataType1[String, IRI]
 
-  val PredicateIRI: AlgebraicDataType1[IRI, PredicateIRI]
-
-  val ObjectNode: AlgebraicDataType1[Node, ObjectNode]
-  val ObjectLiteral: AlgebraicDataType1[Literal, ObjectLiteral]
+  val BNode: AlgebraicDataType1[String, BNode]
 
   val Literal: AlgebraicDataType3[String, Option[LangTag], Option[IRI], Literal]
+
   val LangTag: AlgebraicDataType1[String, LangTag]
 
 }
--- a/aRDF/src/main/scala/ScalaModel.scala	Wed Feb 01 23:20:14 2012 -0500
+++ b/aRDF/src/main/scala/ScalaModel.scala	Sun Feb 05 16:54:43 2012 -0500
@@ -1,47 +1,40 @@
 package org.w3.rdf
 
-import org.w3.isomorphic._
+import org.w3.algebraic._
 
 object ScalaModel extends Model {
 
-  case class IRI(iri:String) { override def toString = '"' + iri + '"' }
-  object IRI extends AlgebraicDataType1[String, IRI]
 
-  case class Graph(triples:Set[Triple]) extends GraphLike {
+  case class Graph(triples: Set[Triple]) extends GraphInterface {
     def iterator = triples.iterator
     def ++(other: Graph): Graph = Graph(triples ++ other.triples)
   }
-  object Graph extends GraphObject {
+
+  object Graph extends GraphCompanionObject {
     def empty: Graph = Graph(Set[Triple]())
     def apply(elems: Triple*): Graph = Graph(Set[Triple](elems:_*))
     def apply(it: Iterable[Triple]): Graph = Graph(it.toSet)
   }
 
-  case class Triple (s: Subject, p: Predicate, o: Object)
-  object Triple extends AlgebraicDataType3[Subject, Predicate, Object, Triple]
-
-  case class BNode(label: String)
-  object BNode extends AlgebraicDataType1[String, BNode]
+  case class Triple (s: Node, p: IRI, o: Node)
+  object Triple extends AlgebraicDataType3[Node, IRI, Node, Triple]
 
   sealed trait Node
+
   case class NodeIRI(i: IRI) extends Node
   object NodeIRI extends AlgebraicDataType1[IRI, NodeIRI]
+
   case class NodeBNode(b: BNode) extends Node
   object NodeBNode extends AlgebraicDataType1[BNode, NodeBNode]
 
-  sealed trait Subject
-  case class SubjectNode(n: Node) extends Subject
-  object SubjectNode extends AlgebraicDataType1[Node, SubjectNode]
+  case class NodeLiteral(lit: Literal) extends Node
+  object NodeLiteral extends AlgebraicDataType1[Literal, NodeLiteral]
 
-  sealed trait Predicate
-  case class PredicateIRI(i: IRI) extends Predicate
-  object PredicateIRI extends AlgebraicDataType1[IRI, PredicateIRI]
+  case class IRI(iri: String) { override def toString = '"' + iri + '"' }
+  object IRI extends AlgebraicDataType1[String, IRI]
 
-  sealed trait Object
-  case class ObjectNode(n: Node) extends Object
-  object ObjectNode extends AlgebraicDataType1[Node, ObjectNode]
-  case class ObjectLiteral(n: Literal) extends Object
-  object ObjectLiteral extends AlgebraicDataType1[Literal, ObjectLiteral]
+  case class BNode(label: String)
+  object BNode extends AlgebraicDataType1[String, BNode]
 
   case class Literal(lexicalForm: String, langtag: Option[LangTag], datatype: Option[IRI])
   object Literal extends AlgebraicDataType3[String, Option[LangTag], Option[IRI], Literal]
--- a/aRDF/src/main/scala/Transformer.scala	Wed Feb 01 23:20:14 2012 -0500
+++ b/aRDF/src/main/scala/Transformer.scala	Sun Feb 05 16:54:43 2012 -0500
@@ -7,9 +7,15 @@
     
   def transformTriple(t: a.Triple): b.Triple = {
     val a.Triple(s, p, o) = t
-    b.Triple(transformSubject(s), transformPredicate(p), transformObject(o))
+    b.Triple(transformNode(s), transformIRI(p), transformNode(o))
   }
   
+  def transformNode(n: a.Node): b.Node = n match {
+    case a.NodeIRI(iri) => b.NodeIRI(transformIRI(iri))
+    case a.NodeBNode(label) => b.NodeBNode(transformBNode(label))
+    case a.NodeLiteral(literal) => b.NodeLiteral(transformLiteral(literal))
+  }
+
   def transformIRI(iri: a.IRI): b.IRI = {
     val a.IRI(i) = iri
     b.IRI(i)
@@ -20,30 +26,6 @@
     b.BNode(label)
   }
   
-  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))
-    }
-  }
-    
-  def transformSubject(s: a.Subject): b.Subject = {
-    val a.SubjectNode(node) = s
-    b.SubjectNode(transformNode(node))
-  }
-  
-  def transformPredicate(p: a.Predicate): b.Predicate = {
-    val a.PredicateIRI(iri) = p
-    b.PredicateIRI(transformIRI(iri))
-  }
-
-  def transformObject(o: a.Object): b.Object = {
-    o match {
-      case a.ObjectNode(node) => b.ObjectNode(transformNode(node))
-      case a.ObjectLiteral(lit) => b.ObjectLiteral(transformLiteral(lit))
-    }
-  }
-  
   def transformLiteral(literal: a.Literal): b.Literal = {
     import a._
     val a.Literal(lit: String, langtagOption: Option[LangTag], datatypeOption: Option[IRI]) = literal