Skip to content

Instantly share code, notes, and snippets.

@pstutz
Last active November 8, 2017 12:13
Show Gist options
  • Save pstutz/988ff61da3e8c1d1529568bf72afe089 to your computer and use it in GitHub Desktop.
Save pstutz/988ff61da3e8c1d1529568bf72afe089 to your computer and use it in GitHub Desktop.
More powerful Graph API
trait PropertyGraph[R <: Records] {
def bind(p: Pattern): R
}
case class Pattern(
topology: Set[Connection] = Set.empty,
nodeConstraints: Map[Node, Set[NodeConstraint]] = Map.empty,
relConstraints: Map[Rel, Set[RelConstraint]] = Map.empty,
projections: Set[NamedVar] = Set.empty
)
case class Connection(s: Node, r: Rel, t: Node)
object Example extends App {
val cypherPattern =
"""
| MATCH (h:Person {name: 'Henry'})-->(:Person {name: 'George'})
| RETURN h
"""
// Below is the pattern representation of the above Cypher query:
val henry = Node("h")
val george = Node()
val manualPattern = Pattern(
topology = Set(Connection(henry, Rel(), george)),
nodeConstraints = Map(
henry -> Set(HasLabels("Person"), HasProperty("name", "Henry")),
george -> Set(HasLabels("Person"), HasProperty("name", "George"))),
projections = Set(henry)
)
println(manualPattern)
//Pattern(
// Set(Connection(
// NamedNode(h),
// org.opencypher.caps.api.graph.Rel@3e57cd70,
// org.opencypher.caps.api.graph.Node@9a7504c)
// ),
// Map(
// NamedNode(h) -> Set(HasLabels(Set(Person))),
// org.opencypher.caps.api.graph.Node@9a7504c -> Set(HasLabels(Set(Person)))
// ),
// Map(),
// Set(NamedNode(h))
//)
}
trait Records
trait EntityConstraint extends NodeConstraint with RelConstraint
case class HasProperty(name: String, value: Any) extends EntityConstraint
trait NodeConstraint
case class HasLabels(labels: Set[String] = Set.empty) extends NodeConstraint
object HasLabels {
def apply(ls: String*): HasLabels = HasLabels(ls.toSet)
}
trait RelConstraint
case class HasType(relTypes: Set[String] = Set.empty)
trait Var
trait NamedVar {
def name: String
}
class Node extends Var
class Rel extends Var
class Prop extends Var
case class NamedProp(name: String) extends Prop with NamedVar
case class NamedNode(name: String) extends Node with NamedVar
case class NamedRel(name: String) extends Rel with NamedVar
object Node {
def apply() = new Node
def apply(name: String) = NamedNode(name)
}
object Rel {
def apply() = new Rel
def apply(name: String) = NamedRel(name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment