tile38/internal/server/expression.go

187 lines
4.2 KiB
Go
Raw Normal View History

package server
import (
"strings"
"github.com/tidwall/geojson"
)
type BinaryOp byte
const (
NOOP BinaryOp = iota
AND
OR
2019-06-12 03:13:33 +03:00
tokenAND = "and"
tokenOR = "or"
tokenNOT = "not"
tokenLParen = "("
tokenRParen = ")"
)
// areaExpression is either an object or operator+children
type areaExpression struct {
negate bool
obj geojson.Object
op BinaryOp
children children
}
type children []*areaExpression
// String representation, helpful in logging.
2019-06-12 03:13:33 +03:00
func (e *areaExpression) String() (res string) {
if e.obj != nil {
2019-06-12 03:13:33 +03:00
res = e.obj.String()
} else {
var chStrings []string
for _, c := range e.children {
chStrings = append(chStrings, c.String())
}
switch e.op {
case NOOP:
res = "empty operator"
case AND:
res = "(" + strings.Join(chStrings, " "+tokenAND+" ") + ")"
case OR:
res = "(" + strings.Join(chStrings, " "+tokenOR+" ") + ")"
default:
res = "unknown operator"
}
}
2019-06-12 03:13:33 +03:00
if e.negate {
res = tokenNOT + " " + res
}
2019-06-12 03:13:33 +03:00
return
}
// Return boolean value modulo negate field of the expression.
2019-06-12 03:13:33 +03:00
func (e *areaExpression) maybeNegate(val bool) bool {
if e.negate {
return !val
}
return val
}
2019-06-12 03:13:33 +03:00
// Methods for testing an areaExpression against the spatial object
func (e *areaExpression) testObject(
o geojson.Object,
objObjTest func(o1, o2 geojson.Object) bool,
exprObjTest func(ae *areaExpression, ob geojson.Object) bool,
) bool {
if e.obj != nil {
return objObjTest(e.obj, o)
}
switch e.op {
case AND:
for _, c := range e.children {
if !exprObjTest(c, o) {
2019-06-12 03:13:33 +03:00
return false
}
}
2019-06-12 03:13:33 +03:00
return true
case OR:
for _, c := range e.children {
if exprObjTest(c, o) {
2019-06-12 03:13:33 +03:00
return true
}
}
2019-06-12 03:13:33 +03:00
return false
}
2019-06-12 03:13:33 +03:00
return false
}
func (e *areaExpression) rawIntersects(o geojson.Object) bool {
return e.testObject(o, geojson.Object.Intersects, (*areaExpression).Intersects)
}
2019-06-12 03:13:33 +03:00
func (e *areaExpression) rawContains(o geojson.Object) bool {
return e.testObject(o, geojson.Object.Contains, (*areaExpression).Contains)
}
2019-06-12 03:13:33 +03:00
func (e *areaExpression) rawWithin(o geojson.Object) bool {
return e.testObject(o, geojson.Object.Within, (*areaExpression).Within)
}
2019-06-12 03:13:33 +03:00
func (e *areaExpression) Intersects(o geojson.Object) bool {
return e.maybeNegate(e.rawIntersects(o))
}
func (e *areaExpression) Contains(o geojson.Object) bool {
return e.maybeNegate(e.rawContains(o))
}
func (e *areaExpression) Within(o geojson.Object) bool {
return e.maybeNegate(e.rawWithin(o))
}
// Methods for testing an areaExpression against another areaExpression
func (e *areaExpression) testExpression(
oe *areaExpression,
exprObjTest func(ae *areaExpression, ob geojson.Object) bool,
rawExprExprTest func(ae1, ae2 *areaExpression) bool,
exprExprTest func(ae1, ae2 *areaExpression) bool,
) bool {
2019-06-12 03:13:33 +03:00
if oe.negate {
e2 := &areaExpression{negate: !e.negate, obj: e.obj, op: e.op, children: e.children}
oe2 := &areaExpression{negate: false, obj: oe.obj, op: oe.op, children: oe.children}
return exprExprTest(e2, oe2)
2019-06-12 03:13:33 +03:00
}
if oe.obj != nil {
return exprObjTest(e, oe.obj)
}
switch oe.op {
case AND:
for _, c := range oe.children {
if !rawExprExprTest(e, c) {
2019-06-12 03:13:33 +03:00
return false
}
}
2019-06-12 03:13:33 +03:00
return true
case OR:
for _, c := range oe.children {
if rawExprExprTest(e, c) {
2019-06-12 03:13:33 +03:00
return true
}
}
2019-06-12 03:13:33 +03:00
return false
}
2019-06-12 03:13:33 +03:00
return false
}
func (e *areaExpression) rawIntersectsExpr(oe *areaExpression) bool {
return e.testExpression(
oe,
(*areaExpression).rawIntersects,
(*areaExpression).rawIntersectsExpr,
(*areaExpression).IntersectsExpr)
}
2019-06-12 03:13:33 +03:00
func (e *areaExpression) rawWithinExpr(oe *areaExpression) bool {
return e.testExpression(
oe,
(*areaExpression).rawWithin,
(*areaExpression).rawWithinExpr,
(*areaExpression).WithinExpr)
}
2019-06-12 03:13:33 +03:00
func (e *areaExpression) rawContainsExpr(oe *areaExpression) bool {
return e.testExpression(
oe,
(*areaExpression).rawContains,
(*areaExpression).rawContainsExpr,
(*areaExpression).ContainsExpr)
2019-06-12 03:13:33 +03:00
}
func (e *areaExpression) IntersectsExpr(oe *areaExpression) bool {
return e.maybeNegate(e.rawIntersectsExpr(oe))
}
func (e *areaExpression) WithinExpr(oe *areaExpression) bool {
return e.maybeNegate(e.rawWithinExpr(oe))
}
func (e *areaExpression) ContainsExpr(oe *areaExpression) bool {
return e.maybeNegate(e.rawContainsExpr(oe))
}