Skip to content

Commit

Permalink
Model convenience function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkohl committed Jul 9, 2018
1 parent 0980a25 commit 858e0d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/scala/jena/scala/ScalaModel.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package jena.scala

import org.apache.jena.rdf.model.{ModelFactory => MF}

case class ScalaModel(model: Model) {
def size: Long = model.size
def createResource(uri: String): Resource = model.createResource(uri)
def createProperty(nameSpace: String, localName: String): Property = model.createProperty(nameSpace, localName)
def createLiteral(v: String, language: String): Literal = model.createLiteral(v, language)
}

object ScalaModel {
def apply[T <: Iterable[Statement]](statements: T): ScalaModel = {
val model: Model = MF.createDefaultModel
statements foreach model.add
new ScalaModel(model)
}
}
16 changes: 15 additions & 1 deletion src/test/scala/jena/scala/ModelSpec.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package jena.scala

import org.apache.jena.rdf.model.{ModelFactory => MF}
import org.apache.jena.rdf.model.{ResourceFactory => RF, ModelFactory => MF}
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.{Matchers, WordSpec}
Expand All @@ -19,9 +19,23 @@ class ModelSpec extends WordSpec with Matchers {

(model should have) size 1
}
"build from an iterable of statements" in new Fixture {
val m = ScalaModel(Set(s1, s2))
(m should have) size 2
}
}

trait Fixture {
val model: ScalaModel = MF.createDefaultModel.asScala
val s1: Statement = RF.createStatement(
RF.createResource("http://example.org/s1"),
RF.createProperty("http://example.org/p1"),
RF.createResource("http://example.org/o1")
)
val s2: Statement = RF.createStatement(
RF.createResource("http://example.org/s2"),
RF.createProperty("http://example.org/p2"),
RF.createResource("http://example.org/o2")
)
}
}

0 comments on commit 858e0d5

Please sign in to comment.