simple-graph-query 3.2.0 → 3.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LANGUAGE.md +21 -1
- package/README.md +17 -0
- package/dist/ForgeExprEvaluator.d.ts +26 -1
- package/dist/ForgeExprFreeVariableFinder.d.ts +9 -0
- package/dist/ForgeExprStaticAnalyzer.d.ts +3 -0
- package/dist/NumericConstraintOptimizer.d.ts +4 -15
- package/dist/cartesianProduct.d.ts +2 -0
- package/dist/simple-graph-query.bundle.js +1 -1
- package/docs/sgq-language.json +3 -2
- package/package.json +2 -1
package/LANGUAGE.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
This library evaluates a single **expression** (the `parseExpr` grammar entry
|
|
9
9
|
point) against a data instance. The language is the expression fragment of
|
|
10
10
|
[Forge](https://forge-fm.org) (itself a dialect of Alloy), with a few
|
|
11
|
-
extensions (string literals, label access) and deliberate omissions.
|
|
11
|
+
extensions (string literals, label access, string comparison) and deliberate omissions.
|
|
12
12
|
|
|
13
13
|
**Everything is a set of tuples.** A sig name denotes the set of its atoms, a
|
|
14
14
|
relation name the set of its tuples, and a scalar (one atom, one number, one
|
|
@@ -101,12 +101,32 @@ Called with square brackets, e.g. `add[1, 2]`.
|
|
|
101
101
|
| Builtins | Arity | Notes |
|
|
102
102
|
|----------|-------|-------|
|
|
103
103
|
| `add`, `subtract`, `multiply`, `divide`, `remainder` | 2 | Integer/real arithmetic. `divide` is real division. |
|
|
104
|
+
| `lexCompare` | 2 | String comparison (SGQ extension), returning -1, 0, or 1. |
|
|
104
105
|
| `abs`, `sign`, `floor`, `ceil` | 1 | |
|
|
105
106
|
| `min`, `max`, `sum` | set | Aggregate over a set; numeric strings resolve to numbers. `sum` also has a quantifier form `sum x: S \| intExpr`. |
|
|
106
107
|
|
|
107
108
|
Builtin names are **not** reserved words: a data instance that names an
|
|
108
109
|
entity `add` shadows the builtin (see issue #59).
|
|
109
110
|
|
|
111
|
+
### String comparison
|
|
112
|
+
|
|
113
|
+
`lexCompare[a, b]` returns -1 if `a` precedes `b`, 0 if they are equal,
|
|
114
|
+
and 1 if `a` follows `b`. Each argument must be a string or a singleton
|
|
115
|
+
unary string relation; empty relations, multiple values, higher-arity tuples,
|
|
116
|
+
numbers, and booleans are rejected. No implicit conversion or label lookup occurs.
|
|
117
|
+
|
|
118
|
+
Ordering uses JavaScript's `<` and `>`: case-sensitive, locale-independent
|
|
119
|
+
lexicographic comparison of UTF-16 code units, with no Unicode normalization.
|
|
120
|
+
A proper prefix comes first (`""` precedes `"a"`, which precedes `"aa"`);
|
|
121
|
+
numeric-looking strings remain strings (`"10"` precedes `"2"`).
|
|
122
|
+
Use label extraction explicitly when ordering atoms by their displayed labels:
|
|
123
|
+
|
|
124
|
+
`{a, b: Person | lexCompare[@:a, @:b] < 0}`
|
|
125
|
+
|
|
126
|
+
Atom IDs are also represented as strings, so `lexCompare[a, b]` compares IDs
|
|
127
|
+
when given atoms. Distinct atoms with identical labels compare equal after
|
|
128
|
+
label extraction. The infix ordering operators remain numeric.
|
|
129
|
+
|
|
110
130
|
## Grammar
|
|
111
131
|
|
|
112
132
|
The expression grammar, with token literals substituted in. Pattern tokens
|
package/README.md
CHANGED
|
@@ -43,6 +43,23 @@ stands for itself.
|
|
|
43
43
|
There is no implicit conversion: `"12" = 12` and `"true" = true` are both false.
|
|
44
44
|
Use `@num:` / `@bool:` to convert explicitly.
|
|
45
45
|
|
|
46
|
+
### Lexicographic comparison
|
|
47
|
+
|
|
48
|
+
`lexCompare[a, b]` returns `-1`, `0`, or `1` for strings that precede, equal,
|
|
49
|
+
or follow one another, using JavaScript's case-sensitive UTF-16 string ordering
|
|
50
|
+
(`a < b` / `a > b`). It has no locale rules, Unicode normalization, or numeric
|
|
51
|
+
conversion: `"10"` precedes `"2"`, and a proper prefix comes first.
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
lexCompare["apple", "banana"] < 0
|
|
55
|
+
{a, b: Person | lexCompare[@:a, @:b] < 0}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Each argument must be a string or a singleton unary string relation. Empty or
|
|
59
|
+
multi-value relations, higher-arity tuples, numbers, and booleans are rejected.
|
|
60
|
+
Atom IDs are represented as strings too; use `@:` explicitly to compare labels.
|
|
61
|
+
The infix `<`, `>`, `<=`, and `>=` operators continue to require numbers.
|
|
62
|
+
|
|
46
63
|
### Embedding in YAML
|
|
47
64
|
|
|
48
65
|
`@` is a reserved indicator in YAML, so an expression beginning with `@:` cannot
|
|
@@ -2,9 +2,13 @@ import { AbstractParseTreeVisitor } from "antlr4ts/tree/AbstractParseTreeVisitor
|
|
|
2
2
|
import { ForgeVisitor } from "./forge-antlr/ForgeVisitor";
|
|
3
3
|
import { ExprContext, Expr1Context, Expr1_5Context, Expr2Context, Expr3Context, Expr4Context, Expr5Context, Expr6Context, Expr7Context, Expr8Context, Expr9Context, Expr10Context, Expr11Context, Expr12Context, Expr13Context, Expr14Context, Expr15Context, Expr17Context, Expr18Context, ExprListContext, NameContext, PredDeclContext, BlockContext, QualNameContext, QuantDeclListContext, NameListContext, QuantDeclContext, CompareOpContext } from "./forge-antlr/ForgeParser";
|
|
4
4
|
import { IDataInstance } from "./types";
|
|
5
|
+
import { ParseTree } from "antlr4ts/tree/ParseTree";
|
|
6
|
+
import { RuleNode } from "antlr4ts/tree/RuleNode";
|
|
5
7
|
export type SingleValue = string | number | boolean;
|
|
6
8
|
export type Tuple = SingleValue[];
|
|
7
9
|
export type EvalResult = SingleValue | Tuple[];
|
|
10
|
+
/** JavaScript's case-sensitive, locale-independent UTF-16 string ordering. */
|
|
11
|
+
export declare function compareStrings(a: string, b: string): -1 | 0 | 1;
|
|
8
12
|
export declare function areTupleArraysEqual(a: Tuple[], b: Tuple[]): boolean;
|
|
9
13
|
export declare const SUPPORTED_BUILTINS: string[];
|
|
10
14
|
/**
|
|
@@ -49,13 +53,19 @@ export declare function unquoteStringLiteral(text: string): string;
|
|
|
49
53
|
* This visitor walks the parse tree and prints the type of operation encountered.
|
|
50
54
|
*/
|
|
51
55
|
export declare class ForgeExprEvaluator extends AbstractParseTreeVisitor<EvalResult> implements ForgeVisitor<EvalResult> {
|
|
56
|
+
private precedenceTargets;
|
|
52
57
|
private environmentStack;
|
|
53
58
|
private freeVariableFinder;
|
|
54
59
|
private freeVariables;
|
|
55
60
|
private cachedResults;
|
|
56
61
|
private instanceData;
|
|
62
|
+
private labelLookups;
|
|
63
|
+
private labelsById?;
|
|
57
64
|
private relationCache;
|
|
58
|
-
private
|
|
65
|
+
private globalNameCache;
|
|
66
|
+
private unresolvedNames;
|
|
67
|
+
private closedResults;
|
|
68
|
+
private unresolvedReportCount;
|
|
59
69
|
private diagnostics;
|
|
60
70
|
constructor(datum: IDataInstance);
|
|
61
71
|
/** Diagnostics recorded since the last `resetDiagnostics()`. */
|
|
@@ -77,6 +87,7 @@ export declare class ForgeExprEvaluator extends AbstractParseTreeVisitor<EvalRes
|
|
|
77
87
|
private buildRelationCache;
|
|
78
88
|
private updateFreeVariables;
|
|
79
89
|
private constructFreeVariableKey;
|
|
90
|
+
evaluate(tree: ParseTree): EvalResult;
|
|
80
91
|
private getLabelForValue;
|
|
81
92
|
private getLabelAsString;
|
|
82
93
|
private getLabelAsBoolean;
|
|
@@ -88,6 +99,20 @@ export declare class ForgeExprEvaluator extends AbstractParseTreeVisitor<EvalRes
|
|
|
88
99
|
private dotJoin;
|
|
89
100
|
private cacheResult;
|
|
90
101
|
private getIden;
|
|
102
|
+
/**
|
|
103
|
+
* Evaluate a node, reusing the value of closed operator nodes.
|
|
104
|
+
*
|
|
105
|
+
* `visitExpr` caches per free-variable binding, but only for nodes of the
|
|
106
|
+
* `expr` rule. A closed subexpression that is not itself an `expr` -- the
|
|
107
|
+
* `^edge` in `{x: Node | some x.^edge}` -- was therefore recomputed for every
|
|
108
|
+
* binding of the enclosing quantifier. Such a node has no free variables, so
|
|
109
|
+
* its value depends only on the instance and can be computed once.
|
|
110
|
+
*
|
|
111
|
+
* Only nodes with more than one child are considered: the single-child nodes
|
|
112
|
+
* that make up the precedence chain do no work of their own.
|
|
113
|
+
*/
|
|
114
|
+
visit(tree: ParseTree): EvalResult;
|
|
115
|
+
visitChildren(node: RuleNode): EvalResult;
|
|
91
116
|
protected aggregateResult(aggregate: EvalResult, nextResult: EvalResult): EvalResult;
|
|
92
117
|
protected defaultResult(): EvalResult;
|
|
93
118
|
visitPredDecl(ctx: PredDeclContext): EvalResult;
|
|
@@ -39,5 +39,14 @@ export declare class ForgeExprFreeVariableFinder extends AbstractParseTreeVisito
|
|
|
39
39
|
visitExpr17(ctx: Expr17Context): FreeVariables;
|
|
40
40
|
visitExpr18(ctx: Expr18Context): FreeVariables;
|
|
41
41
|
visitExprList(ctx: ExprListContext): FreeVariables;
|
|
42
|
+
/**
|
|
43
|
+
* Whether `identifier`, used at `ctx`, is in the scope of a quantifier or
|
|
44
|
+
* comprehension that binds it.
|
|
45
|
+
*
|
|
46
|
+
* A binder's variables are in scope in its body but not in its own
|
|
47
|
+
* declaration list: the domains are evaluated before anything is bound, so
|
|
48
|
+
* in `{Node: Node | ...}` the domain `Node` is still the type.
|
|
49
|
+
*/
|
|
50
|
+
private isBoundByEnclosingBinder;
|
|
42
51
|
visitName(ctx: NameContext): FreeVariables;
|
|
43
52
|
}
|
|
@@ -1,26 +1,15 @@
|
|
|
1
1
|
import { ExprContext } from "./forge-antlr/ForgeParser";
|
|
2
|
-
import { Tuple } from "./ForgeExprEvaluator";
|
|
2
|
+
import type { Tuple } from "./ForgeExprEvaluator";
|
|
3
3
|
/**
|
|
4
4
|
* Analyzes set comprehension constraints to detect simple numeric comparison patterns
|
|
5
5
|
* that can be optimized by generating only valid combinations instead of filtering.
|
|
6
6
|
*/
|
|
7
7
|
export interface NumericConstraintPattern {
|
|
8
|
-
type: 'less_than' | 'greater_than' | 'less_equal' | 'greater_equal' | 'not_equal' | 'none';
|
|
8
|
+
type: 'equal' | 'less_than' | 'greater_than' | 'less_equal' | 'greater_equal' | 'not_equal' | 'none';
|
|
9
9
|
leftVar: string;
|
|
10
10
|
rightVar: string;
|
|
11
11
|
}
|
|
12
|
-
/**
|
|
13
|
-
* Detects if a constraint expression is a simple numeric comparison between two variables.
|
|
14
|
-
* Currently detects patterns like: a < b, a > b, a <= b, a >= b, a != b
|
|
15
|
-
*
|
|
16
|
-
* Note: Uses string matching on expression text which is simple but may miss expressions
|
|
17
|
-
* with extra whitespace or parentheses. This is intentionally conservative - we prefer
|
|
18
|
-
* to fall back to the standard approach rather than risk incorrect optimization.
|
|
19
|
-
*
|
|
20
|
-
* @param constraintExpr The constraint expression to analyze
|
|
21
|
-
* @param varNames The variable names in scope
|
|
22
|
-
* @returns Pattern info if detected, null otherwise
|
|
23
|
-
*/
|
|
12
|
+
/** Match the entire predicate before authorizing the evaluator to skip its body. */
|
|
24
13
|
export declare function detectNumericComparisonPattern(constraintExpr: ExprContext, varNames: string[]): NumericConstraintPattern | null;
|
|
25
14
|
/**
|
|
26
15
|
* Checks if all values in the sets are numbers (or tuples containing single numbers).
|
|
@@ -36,4 +25,4 @@ export declare function areAllNumericSets(sets: Tuple[][]): boolean;
|
|
|
36
25
|
* @param pattern The detected numeric comparison pattern
|
|
37
26
|
* @returns Only the tuples that satisfy the constraint
|
|
38
27
|
*/
|
|
39
|
-
export declare function generateOptimizedNumericCombinations(varNames: string[], quantifiedSets: Tuple[][], pattern: NumericConstraintPattern): Tuple
|
|
28
|
+
export declare function generateOptimizedNumericCombinations(varNames: string[], quantifiedSets: Tuple[][], pattern: NumericConstraintPattern): Generator<Tuple>;
|