gremlin-scala-examples
gremlin-scala-examples copied to clipboard
Example of Multiple Values
Hi there,
Can you add an example of multiple values for a vertex ? I know you did it for TinkerPop. I'm trying to add one using java.util.List or Set but I keep getting error
which database do you refer to? can you share what you tried, and what error you're getting?
Janus. I will send the Error
Here is an example
import java.util
import gremlin.scala._
import org.apache.tinkerpop.gremlin.process.traversal.{P => JavaP}
import org.janusgraph.core.schema.JanusGraphManagement
import org.janusgraph.core.{Cardinality, JanusGraph, JanusGraphVertex}
import org.scalatest.{Matchers, WordSpec}
import scala.collection.JavaConverters._
trait InMemoryConnectScala {
def connect(): JanusGraph = {
import org.apache.commons.configuration.BaseConfiguration
val conf = new BaseConfiguration()
conf.setProperty("storage.backend", "inmemory")
val jg = JanusGraphFactory.open(conf)
jg
}
}
/* test cases copied from TP3 PTest.java */
class PSpec extends WordSpec with Matchers with InMemoryConnectScala {
val graph: JanusGraph = connect()
val mgmt: JanusGraphManagement = graph.openManagement()
val atomEnergy = createProperty(ACM.atomEnergy.toString, Cardinality.LIST)
val acm: JanusGraphVertex = graph.addVertex()
val prop = new util.ArrayList[String]()
prop.add("2")
prop.add("3")
acm.property("atomEnergy", List("1,2,3").asJava)
val c = acm.properties("atomEnergy")
c.forEachRemaining(a => println(a))
println(
"Quering" + graph
.V()
.has(Key[List[String]]("atomEnergy"), P.within(List("2", "3")))
.has(Key[String]("test"), "Harjit")
.toList()
.head
.property("atomEnergy"))
def createProperty(str: String, cardinality: Cardinality = Cardinality.SET) = {
val prop = mgmt.makePropertyKey(str).dataType(classOf[String]).cardinality(cardinality).make
prop
}
case class TestCase[A](predicate: JavaP[A], value: A, expected: Boolean)
}
I get this error
/media/hsingh/ExtraDrive1/projects/deciphernow/dungeoner/src/test/scala/PSpec.scala:34: type mismatch;
[error] found : gremlin.scala.Key[List[String]]
[error] required: gremlin.scala.Key[java.io.Serializable]
[error] Note: List[String] <: java.io.Serializable, but class Key is invariant in type A.
[error] You may wish to define A as +A instead. (SLS 4.5)
[error] .has(Key[List[String]](ACM.atomEnergy.toString), P.within(List("2", "3")))
[error] ^
[error] /media/hsingh/ExtraDrive1/projects/deciphernow/dungeoner/src/test/scala/PSpec.scala:34: type mismatch;
[error] found : org.apache.tinkerpop.gremlin.process.traversal.P[String]
[error] required: org.apache.tinkerpop.gremlin.process.traversal.P[java.io.Serializable]
[error] Note: String <: java.io.Serializable, but Java-defined class P is invariant in type V.
[error] You may wish to investigate a wildcard type such as `_ <: java.io.Serializable`. (SLS 3.2.10)
[error] .has(Key[List[String]](ACM.atomEnergy.toString), P.within(List("2", "3")))
Please let me know if I'm doing anything wrong.
I don't have time to fix incomplete code listings. Please share the missing pieces to fix the below compilation errors.
[error] /home/mp/Projects/tinkerpop/gremlin-scala-examples/janusgraph/src/test/scala/PSpec.scala:11:49: not found: type InMemoryConnectScala
[error] class PSpec extends WordSpec with Matchers with InMemoryConnectScala {
[error] ^
[error] /home/mp/Projects/tinkerpop/gremlin-scala-examples/janusgraph/src/test/scala/PSpec.scala:13:27: not found: value connect
[error] val graph: JanusGraph = connect()
[error] ^
[error] /home/mp/Projects/tinkerpop/gremlin-scala-examples/janusgraph/src/test/scala/PSpec.scala:16:35: not found: value ACM
[error] val atomEnergy = createProperty(ACM.atomEnergy.toString, Cardinality.LIST)
[error] ^
[error] /home/mp/Projects/tinkerpop/gremlin-scala-examples/janusgraph/src/test/scala/PSpec.scala:24:16: not found: value ACM
[error] acm.property(ACM.atomEnergy.toString, List("1,2,3").asJava)
[error] ^
[error] /home/mp/Projects/tinkerpop/gremlin-scala-examples/janusgraph/src/test/scala/PSpec.scala:26:26: not found: value ACM
[error] val c = acm.properties(ACM.atomEnergy.toString)
[error] ^
[error] /home/mp/Projects/tinkerpop/gremlin-scala-examples/janusgraph/src/test/scala/PSpec.scala:33:30: not found: value ACM
[error] .has(Key[List[String]](ACM.atomEnergy.toString), P.within(List("2", "3")))
[error] ^
[error] 6 errors found
[error] (Test / compileIncremental) Compilation failed
Sorry about that. Try now . I have fixed it
Ok, the main thing is to replace
.has(Key[List[String]]("atomEnergy"), P.within(List("2", "3")))
with
.has(Key[String]("atomEnergy"), P.within(List("2", "3")))
.
Then, it compiles. I haven't executed it to verify if it's doing the right thing, but there's some working examples e.g. in https://github.com/mpollmeier/gremlin-scala/blob/master/gremlin-scala/src/test/scala/gremlin/scala/ElementSpec.scala#L44