|
| 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