Skip to content

Commit 7b97a66

Browse files
committed
Add AQL() convenience mLang for SimpleQueryParser.
1 parent cd6936a commit 7b97a66

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/foam/mlang/pom.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ foam.POM({
9090
{ name: "Expressions", flags: "js|java" },
9191
{ name: "ExpressionsSingleton", flags: "js|java" },
9292
{ name: "predicate/RegExp", flags: "js|java" },
93+
{ name: "predicate/AQLExpr", flags: "js|java" },
9394
{ name: "predicate/MQLExpr", flags: "js|java" },
9495
{ name: "predicate/FScript", flags: "js|java" },
9596
{ name: "predicate/FScriptPredicate", flags: "js|java" },
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* @license
3+
* Copyright 2026 The FOAM Authors. All Rights Reserved.
4+
* http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
foam.CLASS({
8+
package: 'foam.mlang.predicate',
9+
name: 'AQLExpr',
10+
extends: 'foam.mlang.predicate.AbstractPredicate',
11+
implements: [ 'foam.lang.Serializable' ],
12+
13+
documentation: 'Stores AQL query as a property and converts it into predicate when f() is called.',
14+
15+
requires: [
16+
'foam.parse.SimpleQueryParser'
17+
],
18+
19+
javaImports: [
20+
'foam.lang.ClassInfo',
21+
'foam.lang.FObject',
22+
'foam.lib.parse.PStream',
23+
'foam.lib.parse.ParserContext',
24+
'foam.lib.parse.ParserContextImpl',
25+
'foam.lib.parse.StringPStream',
26+
'foam.mlang.Constant',
27+
'foam.parse.SimpleQueryParser',
28+
'java.util.Map',
29+
'java.util.concurrent.ConcurrentHashMap'
30+
],
31+
32+
javaCode: `
33+
protected final static Map map__ = new ConcurrentHashMap();
34+
public static AQLExpr create(String query) {
35+
AQLExpr p = (AQLExpr) map__.get(query);
36+
37+
if ( p == null ) {
38+
p = new AQLExpr();
39+
p.setQuery(query);
40+
map__.put(query, p);
41+
}
42+
43+
return p;
44+
}
45+
`,
46+
47+
axioms: [
48+
foam.pattern.Multiton.create({property: 'query'})
49+
],
50+
51+
properties: [
52+
{
53+
class: 'Map',
54+
name: 'specializations_',
55+
transient: true,
56+
factory: function() { return {}; },
57+
javaFactory: 'return new java.util.concurrent.ConcurrentHashMap<ClassInfo, foam.mlang.predicate.Predicate>();'
58+
},
59+
{
60+
class: 'String',
61+
name: 'query'
62+
}
63+
],
64+
65+
methods: [
66+
{
67+
name: 'f',
68+
code: function(o) {
69+
return this.specialization(o.model_).f(o);
70+
},
71+
javaCode: `
72+
if ( ! ( obj instanceof FObject ) )
73+
return false;
74+
75+
return specialization(((FObject)obj).getClassInfo()).f(obj);
76+
`
77+
},
78+
{
79+
name: 'specialization',
80+
args: [ { name: 'model', type: 'ClassInfo' } ],
81+
type: 'Predicate',
82+
code: function(model) {
83+
return this.specializations_[model.name] ||
84+
( this.specializations_[model.name] = this.specialize(model) );
85+
},
86+
javaCode: `
87+
if ( getSpecializations_().get(model) == null ) getSpecializations_().put(model, specialize(model));
88+
return (Predicate) getSpecializations_().get(model);
89+
`
90+
},
91+
{
92+
name: 'specialize',
93+
args: [ { name: 'model', type: 'ClassInfo' } ],
94+
type: 'Predicate',
95+
code: function(model) {
96+
var qp = this.SimpleQueryParser.create({of: model.id});
97+
var pred = qp.parseString(this.query);
98+
return pred ? pred.partialEval() : foam.mlang.predicate.False.create();
99+
},
100+
javaCode: `
101+
SimpleQueryParser parser = new SimpleQueryParser(model);
102+
103+
Object val = parser.parseString(getQuery());
104+
if ( val == null )
105+
return new False();
106+
107+
return ((foam.mlang.predicate.Nary) val).partialEval();
108+
`
109+
},
110+
{
111+
name: 'toString',
112+
code: function toString() {
113+
return '(' + this.query + ')';
114+
},
115+
javaCode: `
116+
return "(" + getQuery() + ")";
117+
`
118+
}
119+
]
120+
});

0 commit comments

Comments
 (0)