~ fix based on feedback at http://stackoverflow.com/questions/9065343/exposing-a-path-dependent-type-coming-from-a-singleton-type
--- a/aRDF/src/main/scala/GraphIsomorphism.scala Sun Jan 29 13:52:08 2012 -0500
+++ b/aRDF/src/main/scala/GraphIsomorphism.scala Wed Feb 01 23:20:14 2012 -0500
@@ -1,7 +1,7 @@
package org.w3.rdf
-abstract class GraphIsomorphism[M <: Model] {
+abstract class GraphIsomorphism[M <: Model](val m: M) {
- def isIsomorphicWith(g1: M#Graph, g2: M#Graph): Boolean
+ def isIsomorphicWith(g1: m.Graph, g2: m.Graph): Boolean
}
--- a/aRDF/src/main/scala/GraphIsomorphismForJenaModel.scala Sun Jan 29 13:52:08 2012 -0500
+++ b/aRDF/src/main/scala/GraphIsomorphismForJenaModel.scala Wed Feb 01 23:20:14 2012 -0500
@@ -1,11 +1,10 @@
package org.w3.rdf
-import org.w3.rdf.Modules.JenaModel
import org.w3.rdf.jena._
-trait GraphIsomorphismForJenaModel extends GraphIsomorphism[JenaModel] {
+object GraphIsomorphismForJenaModel extends GraphIsomorphism[JenaModel.type](JenaModel) {
- def isIsomorphicWith(g1: JenaModel#Graph, g2: JenaModel#Graph): Boolean =
+ def isIsomorphicWith(g1: m.Graph, g2: m.Graph): Boolean =
g1.jenaGraph isIsomorphicWith g2.jenaGraph
--- a/aRDF/src/main/scala/Isomorphic.scala Sun Jan 29 13:52:08 2012 -0500
+++ b/aRDF/src/main/scala/Isomorphic.scala Wed Feb 01 23:20:14 2012 -0500
@@ -4,13 +4,13 @@
def unapply(r: R): Boolean
}
-trait Isomorphic0[R] extends Function0[R] with UnApply0[R]
+trait AlgebraicDataType0[R] extends Function0[R] with UnApply0[R]
trait UnApply1[T, R] {
def unapply(r: R): Option[T]
}
-trait Isomorphic1[T, R] extends Function1[T, R] with UnApply1[T, R]
+trait AlgebraicDataType1[T, R] extends Function1[T, R] with UnApply1[T, 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 UnApply2[T1, T2, R]
+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 Isomorphic3[T1, T2, T3, R] extends Function3[T1, T2, T3, R] with UnApply3[T1, T2, T3, R]
+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 Sun Jan 29 13:52:08 2012 -0500
+++ b/aRDF/src/main/scala/JenaModel.scala Wed Feb 01 23:20:14 2012 -0500
@@ -7,40 +7,34 @@
import org.w3.isomorphic._
-trait JenaModel extends Model {
+object JenaModel extends Model {
- case class IRI(iri:String) { override def toString = '"' + iri + '"' }
- object IRI extends Isomorphic1[String, IRI]
+ case class IRI(iri: String) { override def toString = '"' + iri + '"' }
+ object IRI extends AlgebraicDataType1[String, IRI]
class Graph(val jenaGraph: JenaGraph) extends GraphLike {
- def iterator:Iterator[Triple] = new Iterator[Triple] {
+ def iterator: Iterator[Triple] = new Iterator[Triple] {
val iterator = jenaGraph.find(JenaNode.ANY, JenaNode.ANY, JenaNode.ANY)
def hasNext = iterator.hasNext
def next = iterator.next
}
- def ++(other:Graph):Graph = {
+ def ++(other: Graph):Graph = {
val g = Factory.createDefaultGraph
iterator foreach { t => g add t }
other.iterator foreach { t => g add t }
new Graph(g)
}
-
-// new Graph(new com.hp.hpl.jena.graph.compose.Union(jenaGraph, other.jenaGraph))
-
- // {
- // for(triple <- other) jenaGraph add triple
- // this
- // }
- override def equals(o:Any):Boolean = ( o.isInstanceOf[Graph] && jenaGraph.isIsomorphicWith(o.asInstanceOf[Graph].jenaGraph) )
+ override def equals(o: Any): Boolean =
+ ( o.isInstanceOf[Graph] && jenaGraph.isIsomorphicWith(o.asInstanceOf[Graph].jenaGraph) )
}
object Graph extends GraphObject {
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 empty: Graph = new Graph(Factory.createDefaultGraph)
+ def apply(elems: Triple*): Graph = apply(elems.toIterable)
+ def apply(it: Iterable[Triple]):Graph = {
val jenaGraph = Factory.createDefaultGraph
it foreach { t => jenaGraph add t }
new Graph(jenaGraph)
@@ -48,65 +42,66 @@
}
type Triple = JenaTriple
- object Triple extends Isomorphic3[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))
+ 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 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] = if (bn.isBlank) Some(bn.getBlankNodeId.getLabelString) else None
+ 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 Node = JenaNode
type NodeIRI = Node
- object NodeIRI extends Isomorphic1[IRI, NodeIRI] {
+ 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
- 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
+ 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 Isomorphic1[Node, SubjectNode] {
- def apply(node:Node):SubjectNode = node
- def unapply(node:SubjectNode):Option[Node] = Some(node)
+ object SubjectNode extends AlgebraicDataType1[Node, SubjectNode] {
+ def apply(node: Node): SubjectNode = node
+ def unapply(node: SubjectNode): Option[Node] = Some(node)
}
type Predicate = JenaNode
type PredicateIRI = JenaNode
- object PredicateIRI extends Isomorphic1[IRI, PredicateIRI] {
+ 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 Isomorphic1[Node, ObjectNode] {
- def apply(node:Node):ObjectNode = node
- def unapply(node:ObjectNode):Option[Node] =
+ 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 Isomorphic1[Literal, ObjectLiteral] {
- def apply(literal:Literal):ObjectLiteral = literal
- def unapply(node:ObjectLiteral):Option[Literal] =
+ 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
- object Literal extends Isomorphic3[String, Option[LangTag], Option[IRI], 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)
+ langtagOption map { _.s } getOrElse null,
+ datatypeOption map { i => mapper.getTypeByName(i.iri) } getOrElse null
).asInstanceOf[Literal]
}
def unapply(literal: Literal): Option[(String, Option[LangTag], Option[IRI])] =
@@ -119,7 +114,7 @@
None
}
- case class LangTag(s:String)
- object LangTag extends Isomorphic1[String, LangTag]
+ case class LangTag(s: String)
+ object LangTag extends AlgebraicDataType1[String, LangTag]
}
--- a/aRDF/src/main/scala/Model.scala Sun Jan 29 13:52:08 2012 -0500
+++ b/aRDF/src/main/scala/Model.scala Wed Feb 01 23:20:14 2012 -0500
@@ -6,7 +6,7 @@
type IRI
trait GraphLike extends Iterable[Triple] { self =>
- def ++(other:Graph):Graph
+ def ++(other: Graph):Graph
}
type Graph <: GraphLike
type Triple
@@ -24,37 +24,31 @@
type Literal
type LangTag
- val IRI : Isomorphic1[String, IRI]
+ val IRI : AlgebraicDataType1[String, IRI]
trait GraphObject {
def empty: Graph
- def apply(elems:Triple*):Graph
- def apply(it:Iterable[Triple]):Graph
+ def apply(elems: Triple*): Graph
+ def apply(it: Iterable[Triple]): Graph
}
- val Graph : GraphObject
-
- val Triple : Isomorphic3[Subject, Predicate, Object, Triple]
-
- val BNode : Isomorphic1[String, BNode]
-
- val NodeIRI : Isomorphic1[IRI, NodeIRI]
- val NodeBNode : Isomorphic1[BNode, NodeBNode]
+ val Graph: GraphObject
- val SubjectNode : Isomorphic1[Node, SubjectNode]
-
- val PredicateIRI : Isomorphic1[IRI, PredicateIRI]
-
- val ObjectNode : Isomorphic1[Node, ObjectNode]
- val ObjectLiteral : Isomorphic1[Literal, ObjectLiteral]
+ val Triple: AlgebraicDataType3[Subject, Predicate, Object, Triple]
- val Literal : Isomorphic3[String, Option[LangTag], Option[IRI], Literal]
- val LangTag : Isomorphic1[String, LangTag]
+ val BNode: AlgebraicDataType1[String, BNode]
- // val StringDatatype = IRI("http://www.w3.org/2001/XMLSchema#string")
- // val IntegerDatatype = IRI("http://www.w3.org/2001/XMLSchema#integer")
- // val FloatDatatype = IRI("http://www.w3.org/2001/XMLSchema#float")
- // val DateDatatype = IRI("http://www.w3.org/2001/XMLSchema#date")
- // val DateTimeDatatype = IRI("http://www.w3.org/2001/XMLSchema#dateTime")
+ val NodeIRI: AlgebraicDataType1[IRI, NodeIRI]
+ val NodeBNode: AlgebraicDataType1[BNode, NodeBNode]
+
+ val SubjectNode: AlgebraicDataType1[Node, SubjectNode]
+
+ val PredicateIRI: AlgebraicDataType1[IRI, PredicateIRI]
+
+ val ObjectNode: AlgebraicDataType1[Node, ObjectNode]
+ val ObjectLiteral: AlgebraicDataType1[Literal, ObjectLiteral]
+
+ val Literal: AlgebraicDataType3[String, Option[LangTag], Option[IRI], Literal]
+ val LangTag: AlgebraicDataType1[String, LangTag]
}
--- a/aRDF/src/main/scala/Modules.scala Sun Jan 29 13:52:08 2012 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-package org.w3.rdf.Modules
-
-import org.w3.rdf.jena._
-import org.w3.rdf._
-
-object ScalaModel extends ScalaModel
-
-object JenaModel extends JenaModel
-
-object ScalaToJena extends Transformer[ScalaModel, JenaModel](ScalaModel, JenaModel)
-
-object JenaToScala extends Transformer[JenaModel, ScalaModel](JenaModel, ScalaModel)
-
-object GraphIsomorphismForJenaModel extends GraphIsomorphismForJenaModel
-
-
-
-
-object PimpMyRDF {
-
- // TODO
-
-}
\ No newline at end of file
--- a/aRDF/src/main/scala/ScalaModel.scala Sun Jan 29 13:52:08 2012 -0500
+++ b/aRDF/src/main/scala/ScalaModel.scala Wed Feb 01 23:20:14 2012 -0500
@@ -2,53 +2,51 @@
import org.w3.isomorphic._
-trait ScalaModel extends Model {
+object ScalaModel extends Model {
case class IRI(iri:String) { override def toString = '"' + iri + '"' }
- object IRI extends Isomorphic1[String, IRI]
+ object IRI extends AlgebraicDataType1[String, IRI]
case class Graph(triples:Set[Triple]) extends GraphLike {
def iterator = triples.iterator
- def ++(other:Graph):Graph = Graph(triples ++ other.triples)
+ def ++(other: Graph): Graph = Graph(triples ++ other.triples)
}
object Graph extends GraphObject {
- def empty:Graph = Graph(Set[Triple]())
- def apply(elems:Triple*):Graph = Graph(Set[Triple](elems:_*))
- def apply(it:Iterable[Triple]):Graph = Graph(it.toSet)
+ 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 Isomorphic3[Subject, Predicate, Object, Triple]
+ 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 Isomorphic1[String, BNode]
+ case class BNode(label: String)
+ object BNode extends AlgebraicDataType1[String, BNode]
sealed trait Node
- case class NodeIRI(i:IRI) extends Node
- object NodeIRI extends Isomorphic1[IRI, NodeIRI]
- case class NodeBNode(b:BNode) extends Node
- object NodeBNode extends Isomorphic1[BNode, NodeBNode]
+ 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 Isomorphic1[Node, SubjectNode]
+ case class SubjectNode(n: Node) extends Subject
+ object SubjectNode extends AlgebraicDataType1[Node, SubjectNode]
sealed trait Predicate
- case class PredicateIRI(i:IRI) extends Predicate
- object PredicateIRI extends Isomorphic1[IRI, PredicateIRI]
+ case class PredicateIRI(i: IRI) extends Predicate
+ object PredicateIRI extends AlgebraicDataType1[IRI, PredicateIRI]
sealed trait Object
- case class ObjectNode(n:Node) extends Object
- object ObjectNode extends Isomorphic1[Node, ObjectNode]
- case class ObjectLiteral (n:Literal) extends Object
- object ObjectLiteral extends Isomorphic1[Literal, ObjectLiteral]
+ 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 Literal(lexicalForm: String, langtag: Option[LangTag], datatype: Option[IRI])
- object Literal extends Isomorphic3[String, Option[LangTag], Option[IRI], Literal]
+ object Literal extends AlgebraicDataType3[String, Option[LangTag], Option[IRI], Literal]
- case class LangTag(s:String)
- object LangTag extends Isomorphic1[String, LangTag]
+ case class LangTag(s: String)
+ object LangTag extends AlgebraicDataType1[String, LangTag]
}
-
-object ScalaModel extends ScalaModel
--- a/aRDF/src/main/scala/Transformer.scala Sun Jan 29 13:52:08 2012 -0500
+++ b/aRDF/src/main/scala/Transformer.scala Wed Feb 01 23:20:14 2012 -0500
@@ -1,10 +1,9 @@
package org.w3.rdf
-
class Transformer[ModelA <: Model, ModelB <: Model](val a: ModelA, val b: ModelB) {
-
+
def transform(graph: a.Graph): b.Graph =
- b.Graph(graph map (transformTriple _))
+ b.Graph(graph map transformTriple)
def transformTriple(t: a.Triple): b.Triple = {
val a.Triple(s, p, o) = t
@@ -56,3 +55,8 @@
}
+import org.w3.rdf.jena.JenaModel
+
+object ScalaToJena extends Transformer[ScalaModel.type, JenaModel.type](ScalaModel, JenaModel)
+
+object JenaToScala extends Transformer[JenaModel.type, ScalaModel.type](JenaModel, ScalaModel)
--- a/aRDF/src/test/scala/ModelSpec.scala Sun Jan 29 13:52:08 2012 -0500
+++ b/aRDF/src/test/scala/ModelSpec.scala Wed Feb 01 23:20:14 2012 -0500
@@ -7,7 +7,6 @@
import com.hp.hpl.jena.rdf.model._
import com.hp.hpl.jena.graph._
import org.w3.rdf.jena._
-import org.w3.rdf.Modules._
class ModelSpec {
@@ -17,12 +16,12 @@
val model = ModelFactory.createDefaultModel()
model.getReader("TURTLE").read(model, new FileReader("src/test/resources/card.ttl"), "http://www.w3.org/People/Berners-Lee/card")
- val jenaGraph: JenaModel#Graph = JenaModel.Graph.fromJena(model.getGraph)
+ val jenaGraph = JenaModel.Graph.fromJena(model.getGraph)
// println(jenaGraph)
- val scalaGraph: ScalaModel#Graph = JenaToScala.transform(jenaGraph)
+ val scalaGraph = JenaToScala.transform(jenaGraph)
- val jenaGraphAgain: JenaModel#Graph = ScalaToJena.transform(scalaGraph)
+ val jenaGraphAgain: JenaModel.Graph = ScalaToJena.transform(scalaGraph)
// println(jenaGraphAgain)