Skip to content

Instantly share code, notes, and snippets.

@PhilAndrew
Created November 16, 2014 13:35
Show Gist options
  • Save PhilAndrew/6a4621cfaeceeabc0b99 to your computer and use it in GitHub Desktop.
Save PhilAndrew/6a4621cfaeceeabc0b99 to your computer and use it in GitHub Desktop.
import java.io.FileOutputStream
import javax.script.{ScriptContext, SimpleScriptContext, ScriptEngineManager}
import com.tinkerpop.gremlin.process.Traverser
import com.tinkerpop.gremlin.process.traversers.SimpleTraverser
import com.tinkerpop.gremlin.scala.{ScalaGraph, GremlinScala}
import com.tinkerpop.gremlin.structure.io.kryo.KryoWriter
import org.dynjs.Config
import org.dynjs.runtime.DynJS
import org.scalatest._
import org.scalatest.matchers.ShouldMatchers
import com.tinkerpop.gremlin.structure._
import com.tinkerpop.gremlin.tinkergraph.structure._
import org.scalatest.FunSpec
class HelloSpec extends FlatSpec with ShouldMatchers {
"Hello" should "have tests" in {
// Test from here: https://github.com/mpollmeier/gremlin-scala/blob/tinkerpop3/src/test/scala/com/tinkerpop/gremlin/scala/ElementSpec.scala
val g: TinkerGraph = TinkerGraph.open()
val gs: ScalaGraph = GremlinScala(g)
val v1 = gs.addVertex()
val v2 = gs.addVertex()
v2.setProperty("testkey", "testValue")
gs.v(v1.id) should be(Some(v1))
val result = gs.v(v2.id).get.property[String]("testkey").value
result should be("testValue")
// Javascript
val m = new ScriptEngineManager()
val engine = m.getEngineByName("dynjs")
val context = new SimpleScriptContext()
val bindings = context.getBindings(ScriptContext.ENGINE_SCOPE)
bindings.put("g", g) // g is the Java version of the graph
bindings.put("gs", gs) // gs is the Scala version of the graph
bindings.put("v2", v2)
//g.v(v2.id).filter( (e:_) => { true; }).next().property("testkey")
val t1 = engine.eval(
"""g.v(v2.id()).filter(
| function(e) {
| return (e.get().property("testkey").value() == "testValue");
| } ).count().next()""".stripMargin, context)
//val ttt: SimpleTraverser[_] = t1.asInstanceOf[SimpleTraverser[_]]
//println(ttt.get().asInstanceOf[TinkerVertex].property("testkey").value())
println("Result from Java version of graph should be 1 == " + t1)
// val t2 = engine.eval(
// """g.v(v2.id()).filter(function(e) { return false; } ).count().next()""".stripMargin, context)
val t2 = engine.eval(
"""g.v(v2.id()).filter(
| function(e) {
| return (e.get().property("testkey").value() == "NOT testValue");
| } ).count().next()""".stripMargin, context)
println("Result from Java version of graph should be 0 == " + t2)
//val os = new FileOutputStream("tinkerpop-classic.gio")
//KryoWriter.build().create().writeGraph(os, g)
gs.V.toList.size should be(2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment