changeset 346:eed565c0c0c2 no-hierarchy

+ union operation on graphs
author Alexandre Bertails <bertails@w3.org>
date Mon, 14 Feb 2011 18:34:47 -0500
parents 57ecde6ce678
children b92cd4aa8aa0
files jena/src/main/scala/JenaModel.scala rdf/src/main/scala/RDF.scala
diffstat 2 files changed, 17 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/jena/src/main/scala/JenaModel.scala	Mon Feb 14 14:38:46 2011 -0500
+++ b/jena/src/main/scala/JenaModel.scala	Mon Feb 14 18:34:47 2011 -0500
@@ -23,13 +23,18 @@
   case class IRI(iri:String) { override def toString = '"' + iri + '"' }
   object IRI extends Isomorphic1[String, IRI]
 
-  class Graph(val jenaGraph:JenaGraph) extends Iterable[Triple] {
+  class Graph(val jenaGraph:JenaGraph) extends GraphLike {
     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 = 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) )
 
   }
--- a/rdf/src/main/scala/RDF.scala	Mon Feb 14 14:38:46 2011 -0500
+++ b/rdf/src/main/scala/RDF.scala	Mon Feb 14 18:34:47 2011 -0500
@@ -5,7 +5,10 @@
 trait Model {
 
   type IRI
-  type Graph <: Iterable[Triple]
+  trait GraphLike extends Iterable[Triple] { self =>
+    def ++(other:Graph):Graph
+  }
+  type Graph <: GraphLike
   type Triple
   type BNode
   type Node
@@ -80,11 +83,14 @@
   case class IRI(iri:String) { override def toString = '"' + iri + '"' }
   object IRI extends Isomorphic1[String, IRI]
 
-  type Graph = Set[Triple]
+  case class Graph(triples:Set[Triple]) extends GraphLike {
+    def iterator = triples.iterator
+    def ++(other:Graph):Graph = Graph(triples ++ other.triples)
+  }
   object Graph extends GraphObject {
-    def empty:Graph = Set[Triple]()
-    def apply(elems:Triple*):Graph = Set[Triple](elems:_*)
-    def apply(it:Iterable[Triple]):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)
Set up and maintained by W3C Systems Team, please report bugs to sysreq@w3.org.

W3C would like to thank Microsoft who donated the server that allows us to run this service.