forked from eldersantos/community
/
Pattern.scala
211 lines (169 loc) · 7.63 KB
/
Pattern.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Copyright (c) 2002-2012 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal.commands
import expressions.Expression
import org.neo4j.graphdb.Direction
import collection.Seq
import org.neo4j.cypher.internal.symbols._
abstract class Pattern extends TypeSafe {
def optional: Boolean
def predicate: Predicate
def possibleStartPoints: Seq[(String,CypherType)]
def relTypes:Seq[String]
protected def node(name: String) = if (name.startsWith(" UNNAMED")) "()" else name
protected def left(dir: Direction) = if (dir == Direction.INCOMING) "<-" else "-"
protected def right(dir: Direction) = if (dir == Direction.OUTGOING) "->" else "-"
def rewrite( f : Expression => Expression) : Pattern
def equalOrUnnamed(name1: String, name2: String) = name1 == name2 || (name1.startsWith(" UNNAMED") && name2.startsWith(" UNNAMED"))
protected def filtered(x:Seq[String]): Seq[String] =x.filter(!_.startsWith(" UNNAMED"))
def nodes:Seq[String]
def rels:Seq[String]
}
object RelatedTo {
def apply(left: String, right: String, relName: String, relType: String, direction: Direction, optional: Boolean = false, predicate: Predicate = True()) =
new RelatedTo(left, right, relName, Seq(relType), direction, optional, predicate)
}
case class RelatedTo(left: String,
right: String,
relName: String,
relTypes: Seq[String],
direction: Direction,
optional: Boolean,
predicate: Predicate) extends Pattern {
override def toString = node(left) + left(direction) + relInfo + right(direction) + node(right)
private def relInfo: String = {
var info = relName
if (optional) info = info + "?"
if (relTypes.nonEmpty) info = info + ":" + relTypes.mkString("|")
if (info == "") "" else "[" + info + "]"
}
val possibleStartPoints: Seq[(String, MapType)] = Seq(left-> NodeType(), right-> NodeType(), relName->RelationshipType())
def rewrite(f: (Expression) => Expression) =
new RelatedTo(left, right, relName, relTypes, direction, optional, predicate.rewrite(f))
override def equals(p1: Any): Boolean = p1 match {
case null => false
case other: RelatedTo =>
equalOrUnnamed(other.left, left) &&
equalOrUnnamed(other.right, right) &&
equalOrUnnamed(other.relName, relName) &&
other.relTypes == relTypes &&
other.direction == direction &&
other.optional == optional &&
other.predicate == predicate
case _ => false
}
def nodes = Seq(left,right)
def rels = Seq(relName)
def assertTypes(symbols: SymbolTable) {
predicate.assertTypes(symbols)
}
def symbolTableDependencies = predicate.symbolTableDependencies
}
abstract class PathPattern extends Pattern {
def pathName: String
def start: String
def end: String
def cloneWithOtherName(newName: String): PathPattern
def relIterator: Option[String]
}
object VarLengthRelatedTo {
def apply(pathName: String, start: String, end: String, minHops: Option[Int], maxHops: Option[Int], relTypes: String, direction: Direction, optional: Boolean = false, predicate: Predicate = True()) =
new VarLengthRelatedTo(pathName, start, end, minHops, maxHops, Seq(relTypes), direction, None, optional, predicate)
}
case class VarLengthRelatedTo(pathName: String,
start: String,
end: String,
minHops: Option[Int],
maxHops: Option[Int],
relTypes: Seq[String],
direction: Direction,
relIterator: Option[String],
optional: Boolean,
predicate: Predicate) extends PathPattern {
override def toString: String = pathName + "=" + node(start) + left(direction) + relInfo + right(direction) + node(end)
def symbolTableDependencies = predicate.symbolTableDependencies
def cloneWithOtherName(newName: String) = copy(pathName = newName)
private def relInfo: String = {
var info = if (optional) "?" else ""
if (relTypes.nonEmpty) info = info + ":" + relTypes.mkString("|")
val hops = (minHops, maxHops) match {
case (None, None) => "*"
case (Some(min), None) => "*" + min + ".."
case (None, Some(max)) => "*" + ".." + max
case (Some(min), Some(max)) => "*" + min + ".." + max
}
info = info + hops
if (info == "") "" else "[" + info + "]"
}
def rewrite(f: (Expression) => Expression) = new VarLengthRelatedTo(pathName,start,end, minHops,maxHops,relTypes,direction,relIterator,optional,predicate.rewrite(f))
lazy val possibleStartPoints: Seq[(String, AnyType)] = Seq(start -> NodeType(), end -> NodeType(), pathName -> PathType())
override def equals(p1: Any): Boolean = p1 match {
case null => false
case other: VarLengthRelatedTo =>
equalOrUnnamed(other.pathName, pathName) &&
equalOrUnnamed(other.start, start) &&
equalOrUnnamed(other.end, end) &&
other.minHops == minHops &&
other.maxHops == maxHops &&
other.relTypes == relTypes &&
other.direction == direction &&
other.relIterator == relIterator &&
other.optional == optional &&
other.predicate == predicate
case _ => false
}
def nodes = Seq(start,end)
def rels = Seq()
def assertTypes(symbols: SymbolTable) {
predicate.assertTypes(symbols)
}
}
case class ShortestPath(pathName: String,
start: String,
end: String,
relTypes: Seq[String],
dir: Direction,
maxDepth: Option[Int],
optional: Boolean,
single: Boolean,
relIterator: Option[String],
predicate: Predicate = True())
extends PathPattern {
override def toString: String = pathName + "=" + algo + "(" + start + left(dir) + relInfo + right(dir) + end + ")"
private def algo = if (single) "singleShortestPath" else "allShortestPath"
def cloneWithOtherName(newName: String) = copy(pathName = newName)
def symbolTableDependencies = predicate.symbolTableDependencies ++ Set(start, end)
private def relInfo: String = {
var info = "["
if (optional) info = info + "?"
if (relTypes.nonEmpty) info = info + ":" + relTypes.mkString("|")
info = info + "*"
if (maxDepth.nonEmpty) info = info + ".." + maxDepth.get
info + "]"
}
lazy val possibleStartPoints: Seq[(String, NodeType)] = Seq(start-> NodeType(), end-> NodeType())
def rewrite(f: Expression => Expression) = new ShortestPath(pathName,start,end,relTypes,dir,maxDepth,optional,single,relIterator,predicate.rewrite(f))
def rels = Seq()
def nodes = Seq(start,end)
def assertTypes(symbols: SymbolTable) {
possibleStartPoints.foreach(p => symbols.evaluateType(p._1, p._2))
predicate.assertTypes(symbols)
}
}