UNPKG

foam-framework

Version:
232 lines (178 loc) 6.21 kB
/** * @license * Copyright 2014 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ CLASS({ name: 'MaxExpr', extends: 'UNARY', properties: [ { name: 'max', type: 'int', help: 'Maximum value.', defaultValue: undefined } ], methods: { maximum: function(o1, o2) { return o1.compareTo(o2) > 0 ? o1 : o2; }, reduce: function(other) { return MaxExpr.create({max: this.maximum(this.max, other.max)}); }, reduceI: function(other) { this.max = this.maximum(this.max, other.max); }, pipe: function(sink) { sink.put(this); }, put: function(obj) { var v = this.arg1.f(obj); this.max = this.max === undefined ? v : this.maximum(this.max, v); }, remove: function(obj) { }, toString: function() { return this.max; } } }); function MAX(expr) { return MaxExpr.create({arg1: expr}); } CLASS({ name: 'InExpr', extends: 'BINARY', documentation: 'Binary expression which is true if its first argument is EQ to any element of its second argument, which is an array.', properties: [ { name: 'arg2', label: 'Argument', type: 'Expr', help: 'Sub-expression', postSet: function() { this.valueSet_ = undefined; } } ], methods: { partialEval: function() { if ( this.arg2.length == 1 ) return EQ(this.arg1, this.arg2[0]); return this; }, valueSet: function() { if ( ! this.valueSet_ ) { var s = {}; for ( var i = 0 ; i < this.arg2.length ; i++ ) s[this.arg2[i]] = true; this.valueSet_ = s; } return this.valueSet_; }, toSQL: function() { return this.arg1.toSQL() + ' IN ' + this.arg2; }, toMQL: function() { return this.arg1.toMQL() + '=' + this.arg2.join(',') }, toBQL: function() { return this.arg1.toBQL() + ':(' + this.arg2.join('|') + ')' }, f: function(obj) { return this.valueSet().hasOwnProperty(this.arg1.f(obj)); } } }); function IN(arg1, arg2) { return InExpr.create({arg1: compile_(arg1), arg2: arg2 }); } CLASS({ name: 'LtExpr', extends: 'BINARY', abstract: true, methods: { toSQL: function() { return this.arg1.toSQL() + '<' + this.arg2.toSQL(); }, toMQL: function() { return this.arg1.toMQL() + '-before:' + this.arg2.toMQL(); }, toBQL: function() { return this.arg1.toBQL() + '<' + this.arg2.toBQL(); }, partialEval: function() { var newArg1 = this.arg1.partialEval(); var newArg2 = this.arg2.partialEval(); if ( ConstantExpr.isInstance(newArg1) && ConstantExpr.isInstance(newArg2) ) return compile_(this.f()); return this.arg1 !== newArg1 || this.arg2 != newArg2 ? LtExpr.create({arg1: newArg1, arg2: newArg2}) : this ; }, f: function(obj) { return compare(this.arg1.f(obj), this.arg2.f(obj)) < 0; } } }); function LT(arg1, arg2) { return LtExpr.create({arg1: compile_(arg1), arg2: compile_(arg2)}); } CLASS({ name: 'GtExpr', extends: 'BINARY', abstract: true, methods: { toSQL: function() { return this.arg1.toSQL() + '>' + this.arg2.toSQL(); }, toMQL: function() { return this.arg1.toMQL() + '-after:' + this.arg2.toMQL(); }, toBQL: function() { return this.arg1.toBQL() + '>' + this.arg2.toBQL(); }, partialEval: function() { var newArg1 = this.arg1.partialEval(); var newArg2 = this.arg2.partialEval(); if ( ConstantExpr.isInstance(newArg1) && ConstantExpr.isInstance(newArg2) ) return compile_(this.f()); return this.arg1 !== newArg1 || this.arg2 != newArg2 ? GtExpr.create({arg1: newArg1, arg2: newArg2}) : this ; }, f: function(obj) { return compare(this.arg1.f(obj), this.arg2.f(obj)) > 0; } } }); function GT(arg1, arg2) { return GtExpr.create({arg1: compile_(arg1), arg2: compile_(arg2)}); } CLASS({ name: 'LteExpr', extends: 'BINARY', abstract: true, methods: { toSQL: function() { return this.arg1.toSQL() + '<=' + this.arg2.toSQL(); }, toMQL: function() { return this.arg1.toMQL() + '-before:' + this.arg2.toMQL(); }, toBQL: function() { return this.arg1.toBQL() + '<=' + this.arg2.toBQL(); }, partialEval: function() { var newArg1 = this.arg1.partialEval(); var newArg2 = this.arg2.partialEval(); if ( ConstantExpr.isInstance(newArg1) && ConstantExpr.isInstance(newArg2) ) return compile_(this.f()); return this.arg1 !== newArg1 || this.arg2 != newArg2 ? LtExpr.create({arg1: newArg1, arg2: newArg2}) : this ; }, f: function(obj) { return compare(this.arg1.f(obj), this.arg2.f(obj)) <= 0; } } }); function LTE(arg1, arg2) { return LteExpr.create({arg1: compile_(arg1), arg2: compile_(arg2)}); } CLASS({ name: 'GteExpr', extends: 'BINARY', abstract: true, methods: { toSQL: function() { return this.arg1.toSQL() + '>=' + this.arg2.toSQL(); }, toMQL: function() { return this.arg1.toMQL() + '-after:' + this.arg2.toMQL(); }, toBQL: function() { return this.arg1.toBQL() + '>=' + this.arg2.toBQL(); }, partialEval: function() { var newArg1 = this.arg1.partialEval(); var newArg2 = this.arg2.partialEval(); if ( ConstantExpr.isInstance(newArg1) && ConstantExpr.isInstance(newArg2) ) return compile_(this.f()); return this.arg1 !== newArg1 || this.arg2 != newArg2 ? GtExpr.create({arg1: newArg1, arg2: newArg2}) : this ; }, f: function(obj) { return compare(this.arg1.f(obj), this.arg2.f(obj)) >= 0; } } }); function GTE(arg1, arg2) { return GteExpr.create({arg1: compile_(arg1), arg2: compile_(arg2)}); }