expression

package
v1.0.0-alpha.39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package expression - Expression evaluator implementation

Package expression - Regex pattern caching using framework's cache package

Package expression - Simple DSL for rule condition evaluation

Index

Constants

View Source
const (
	// Numeric operators
	OpEqual            = "eq"
	OpNotEqual         = "ne"
	OpLessThan         = "lt"
	OpLessThanEqual    = "lte"
	OpGreaterThan      = "gt"
	OpGreaterThanEqual = "gte"
	OpBetween          = "between"

	// String operators
	OpContains   = "contains"
	OpStartsWith = "starts_with"
	OpEndsWith   = "ends_with"
	OpRegexMatch = "regex"

	// Array operators
	OpIn            = "in"
	OpNotIn         = "not_in"
	OpLengthEq      = "length_eq"
	OpLengthGt      = "length_gt"
	OpLengthLt      = "length_lt"
	OpArrayContains = "array_contains"
)

Supported operators by field type

View Source
const (
	LogicAnd = "and"
	LogicOr  = "or"
)

Logic operators

Variables

This section is empty.

Functions

This section is empty.

Types

type ConditionExpression

type ConditionExpression struct {
	Field    string      `json:"field"`    // Predicate field (e.g., "robotics.battery.level")
	Operator string      `json:"operator"` // Comparison operator (e.g., "lte", "eq", "contains")
	Value    interface{} `json:"value"`    // Comparison value (20.0, "active", true)
	Required bool        `json:"required"` // If false, missing field doesn't fail evaluation
}

ConditionExpression represents a single field/operator/value condition

type EvaluationError

type EvaluationError struct {
	Field    string
	Operator string
	Message  string
	Err      error
}

EvaluationError represents an error during expression evaluation

func (*EvaluationError) Error

func (e *EvaluationError) Error() string

func (*EvaluationError) Unwrap

func (e *EvaluationError) Unwrap() error

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator processes expressions against entity state

func NewExpressionEvaluator

func NewExpressionEvaluator() *Evaluator

NewExpressionEvaluator creates a new expression evaluator with all supported operators

func (*Evaluator) Distance

func (e *Evaluator) Distance(entity1, entity2 *gtypes.EntityState) (float64, error)

Distance calculates the great-circle distance between two entities in meters using the Haversine formula. Returns error if either entity lacks position data. Position is now extracted from geo.location.* triples (single source of truth).

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(entityState *gtypes.EntityState, expr LogicalExpression) (bool, error)

Evaluate evaluates a logical expression against an entity state

func (*Evaluator) EvaluateWithStateFields

func (e *Evaluator) EvaluateWithStateFields(entityState *gtypes.EntityState, stateFields StateFields, expr LogicalExpression) (bool, error)

EvaluateWithStateFields evaluates a logical expression against entity state and optional match state fields. The stateFields parameter provides $state.* pseudo-field values (e.g., "$state.iteration", "$state.max_iterations") for conditions that reference rule execution state rather than entity triples.

func (*Evaluator) GetOutgoing

func (e *Evaluator) GetOutgoing(entity *gtypes.EntityState, predicate string) []string

GetOutgoing returns a list of entity IDs for outgoing relationships with the given predicate. Only returns valid entity IDs (4-part dotted notation), not literal values. Returns empty slice if entity is nil or no relationships found.

func (*Evaluator) HasTriple

func (e *Evaluator) HasTriple(entity *gtypes.EntityState, predicate string) bool

HasTriple checks if an entity has a triple with the given predicate. Returns false if entity is nil or predicate not found.

type FieldType

type FieldType int

FieldType represents the detected type of a field

const (
	// FieldTypeUnknown represents an unknown or unsupported field type
	FieldTypeUnknown FieldType = iota
	// FieldTypeFloat64 represents a floating point number field
	FieldTypeFloat64
	// FieldTypeString represents a string field
	FieldTypeString
	// FieldTypeBool represents a boolean field
	FieldTypeBool
	// FieldTypeArray represents an array field
	FieldTypeArray
)

func (FieldType) String

func (f FieldType) String() string

type LogicalExpression

type LogicalExpression struct {
	Conditions []ConditionExpression `json:"conditions"`
	Logic      string                `json:"logic"` // "and", "or"
}

LogicalExpression combines multiple conditions with logic operators

type OperatorFunc

type OperatorFunc func(fieldValue, compareValue interface{}) (bool, error)

OperatorFunc defines the signature for operator implementations

type StateFields

type StateFields map[string]interface{}

StateFields provides rule match state values for $state.* pseudo-field resolution in condition expressions. Keys are the full field names (e.g., "$state.iteration"). This avoids circular dependencies between the expression and rule packages.

type TypeDetector

type TypeDetector interface {
	GetFieldValue(entityState *gtypes.EntityState, field string) (value interface{}, exists bool, err error)
	DetectFieldType(value interface{}) FieldType
}

TypeDetector determines field type and extracts values from entity state

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL