From 5ab10f40243e049e370bf78439bc68f7b3019158 Mon Sep 17 00:00:00 2001 From: cacaodev Date: Sun, 27 Dec 2015 15:34:37 +0100 Subject: [PATCH 1/6] NEW: CPExpression +expressionForBlock:arguments: An expression that returns the result of evaluating a block. With tests. --- Foundation/CPPredicate/CPExpression.j | 1 + Foundation/CPPredicate/_CPBlockExpression.j | 83 +++++++++++++++++++++ Foundation/CPPredicate/_CPExpression.j | 25 +++++++ Tests/Foundation/CPPredicateTest.j | 20 +++++ 4 files changed, 129 insertions(+) create mode 100644 Foundation/CPPredicate/_CPBlockExpression.j diff --git a/Foundation/CPPredicate/CPExpression.j b/Foundation/CPPredicate/CPExpression.j index 06ed6b0ba..12ad45a74 100644 --- a/Foundation/CPPredicate/CPExpression.j +++ b/Foundation/CPPredicate/CPExpression.j @@ -28,3 +28,4 @@ @import "_CPAggregateExpression.j" @import "_CPSetExpression.j" @import "_CPSubqueryExpression.j" +@import "_CPBlockExpression.j" diff --git a/Foundation/CPPredicate/_CPBlockExpression.j b/Foundation/CPPredicate/_CPBlockExpression.j new file mode 100644 index 000000000..109d5d517 --- /dev/null +++ b/Foundation/CPPredicate/_CPBlockExpression.j @@ -0,0 +1,83 @@ +/* + * _CPBlockExpression.j + * + * Created by cacaodev. + * Copyright 2015. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +@import "_CPExpression.j" + +@implementation _CPBlockExpression : CPExpression +{ + Function _block; + CPArray _arguments; +} + +- (id)initWithBlock:(Function)aBlock arguments:(CPArray)arguments +{ + self = [super initWithExpressionType:CPBlockExpressionType]; + + if (self) + { + _block = aBlock; + _arguments = arguments; + } + + return self; +} + +- (BOOL)isEqual:(id)object +{ + if (self === object) + return YES; + + if (object === nil || object.isa !== self.isa || [object expressionBlock] !== _block || ![[object arguments] isEqual:_arguments]) + return NO; + + return YES; +} + +- (Function)expressionBlock +{ + return _block; +} + +- (CPArray)arguments +{ + return _arguments; +} + +- (id)expressionValueWithObject:(id)object context:(CPDictionary)context +{ + var args = []; + + if (_arguments !== nil) + { + args = _arguments.map(function(expression){ + return [expression expressionValueWithObject:object context:context]; + }); + } + + return _block(object, args, context); +} + +- (CPString)description +{ + return [CPString stringWithFormat:@"Block(0x%@)", [CPString stringWithHash:[self UID]]]; +} + +@end \ No newline at end of file diff --git a/Foundation/CPPredicate/_CPExpression.j b/Foundation/CPPredicate/_CPExpression.j index 5a9561cb7..02fb98801 100644 --- a/Foundation/CPPredicate/_CPExpression.j +++ b/Foundation/CPPredicate/_CPExpression.j @@ -65,6 +65,10 @@ CPIntersectSetExpressionType = 8; An expression that combines two nested expression results by set subtraction. */ CPMinusSetExpressionType = 9; +/*! + An expression that returns the result of evaluating a block. +*/ +CPBlockExpressionType = 10; /*! @ingroup foundation @@ -259,6 +263,27 @@ CPMinusSetExpressionType = 9; return [[_CPSubqueryExpression alloc] initWithExpression:expression usingIteratorVariable:variable predicate:predicate]; } +/*! + Returns Creates an NSExpression object that will use the Block for evaluating objects. + @param aBlock The Block is applied to the object to be evaluated. + +The Block takes three arguments and returns a value: + +evaluatedObject +The object to be evaluated. +expressions +An array of predicate expressions that evaluates to a collection. +context +A dictionary that the expression can use to store temporary state for one predicate evaluation. + +@discussion Note that context is mutable, and that it can only be accessed during the evaluation of the expression. +@param arguments An array containing NSExpression objects that will be used as parameters during the invocation of the block. +*/ ++ (CPExpression)expressionForBlock:(Function)aBlock arguments:(CPArray)args +{ + return [[_CPBlockExpression alloc] initWithBlock:aBlock arguments:args]; +} + // Getting Information About an Expression /*! Returns the expression type for the receiver. diff --git a/Tests/Foundation/CPPredicateTest.j b/Tests/Foundation/CPPredicateTest.j index f167d5513..2d6f8210a 100644 --- a/Tests/Foundation/CPPredicateTest.j +++ b/Tests/Foundation/CPPredicateTest.j @@ -79,6 +79,13 @@ var expression_minusset = [CPExpression expressionForMinusSet:set with:array]; [self assertNotNull:expression_minusset message:"MinusSet Expression should not be nil"]; + + var expression_block = [CPExpression expressionForBlock:function(obj, args, bindings) + { + return obj; + } arguments:nil]; + + [self assertNotNull:expression_block message:"Block Expression should not be nil"]; } - (void)testSetExpressionEvaluation @@ -147,6 +154,19 @@ [self assertTrue:([eval isEqual:expected]) message:"'" + [expression predicateFormat] + "' result is "+ eval + "but should be " + expected]; } +- (void)testBlockExpressionEvaluation +{ + var block = function(obj, args, bindings) + { + return [obj stringByAppendingString:[args componentsJoinedByString:"-"]]; + }; + + var expression = [CPExpression expressionForBlock:block arguments:[[CPExpression expressionForConstantValue:"A"], [CPExpression expressionForConstantValue:"B"]]]; + + var eval = [expression expressionValueWithObject:"OBJ" context:@{}]; + [self assertTrue:([eval isEqual:"OBJA-B"]) message:"'" + [expression description] + "' result is "+ eval + "but should be " + "OBJA-B"]; +} + - (void)testOptions { var pred = [[CPComparisonPredicate alloc] initWithLeftExpression:[CPExpression expressionForConstantValue:@"àa"] rightExpression:[CPExpression expressionForConstantValue:@"aà"] modifier:CPDirectPredicateModifier type:CPLikePredicateOperatorType options:3]; From 07b6e2b9f15c7e307434f98a5381f1477ea6c79c Mon Sep 17 00:00:00 2001 From: cacaodev Date: Sun, 27 Dec 2015 15:40:32 +0100 Subject: [PATCH 2/6] CPExpression +expressionForConditional:trueExpression:falseExpression: Conditional Expression : an expression using different expressions for evaluation, depending of a predicate result. Support for predicate parsing: ```TERNARY(predicate, trueExpression, falseExpression)``` With init, equal, evaluation, parsing tests. --- Foundation/CPPredicate/CPExpression.j | 1 + .../CPPredicate/_CPConditionalExpression.j | 84 +++++++++++++++++++ Foundation/CPPredicate/_CPExpression.j | 9 ++ Foundation/CPPredicate/_CPPredicate.j | 26 +++++- Tests/Foundation/CPPredicateTest.j | 23 +++++ 5 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 Foundation/CPPredicate/_CPConditionalExpression.j diff --git a/Foundation/CPPredicate/CPExpression.j b/Foundation/CPPredicate/CPExpression.j index 12ad45a74..10ca2b598 100644 --- a/Foundation/CPPredicate/CPExpression.j +++ b/Foundation/CPPredicate/CPExpression.j @@ -29,3 +29,4 @@ @import "_CPSetExpression.j" @import "_CPSubqueryExpression.j" @import "_CPBlockExpression.j" +@import "_CPConditionalExpression.j" diff --git a/Foundation/CPPredicate/_CPConditionalExpression.j b/Foundation/CPPredicate/_CPConditionalExpression.j new file mode 100644 index 000000000..6e636ec29 --- /dev/null +++ b/Foundation/CPPredicate/_CPConditionalExpression.j @@ -0,0 +1,84 @@ +/* + * _CPConditionalExpression.j + * + * Created by cacaodev. + * Copyright 2015. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +@import "_CPPredicate.j" +@import "_CPExpression.j" + +@implementation _CPConditionalExpression : CPExpression +{ + CPPredicate _predicate; + CPExpression _trueExpression; + CPExpression _falseExpression; +} + +- (id)initWithPredicate:(CPPredicate)aPredicate trueExpression:(CPExpression)trueExpression falseExpression:(CPExpression)falseExpression +{ + self = [super initWithExpressionType:CPConditionalExpressionType]; + + if (self) + { + _predicate = aPredicate; + _trueExpression = trueExpression; + _falseExpression = falseExpression; + } + + return self; +} + +- (BOOL)isEqual:(id)object +{ + if (self === object) + return YES; + + if (object === nil || object.isa !== self.isa || ![[object predicate] isEqual:_predicate] || ![[object trueExpression] isEqual:_trueExpression] || ![[object falseExpression] isEqual:_falseExpression]) + return NO; + + return YES; +} + +- (CPPredicate)predicate +{ + return _predicate; +} + +- (CPExpression)trueExpression +{ + return _trueExpression; +} + +- (CPExpression)falseExpression +{ + return _falseExpression; +} + +- (id)expressionValueWithObject:(id)object context:(CPDictionary)context +{ + var eval = [_predicate evaluateWithObject:object substitutionVariables:context]; + var exp = eval ? _trueExpression : _falseExpression; + + return [exp expressionValueWithObject:object context:context]; +} + +- (CPString)description +{ + return [CPString stringWithFormat:@"TERNARY(%@,%@,%@)", [_predicate predicateFormat], [_trueExpression description], [_falseExpression description]]; +} + +@end \ No newline at end of file diff --git a/Foundation/CPPredicate/_CPExpression.j b/Foundation/CPPredicate/_CPExpression.j index 02fb98801..b26707fc0 100644 --- a/Foundation/CPPredicate/_CPExpression.j +++ b/Foundation/CPPredicate/_CPExpression.j @@ -69,6 +69,10 @@ CPMinusSetExpressionType = 9; An expression that returns the result of evaluating a block. */ CPBlockExpressionType = 10; +/*! + An expression that returns an expression that depends on the evaluation of a predicate. +*/ +CPConditionalExpressionType = 11; /*! @ingroup foundation @@ -284,6 +288,11 @@ A dictionary that the expression can use to store temporary state for one predic return [[_CPBlockExpression alloc] initWithBlock:aBlock arguments:args]; } ++ (CPExpression)expressionForConditional:(CPPredicate)aPredicate trueExpression:(CPExpression)trueExpression falseExpression:(CPExpression)falseExpression +{ + return [[_CPConditionalExpression alloc] initWithPredicate:aPredicate trueExpression:trueExpression falseExpression:falseExpression]; +} + // Getting Information About an Expression /*! Returns the expression type for the receiver. diff --git a/Foundation/CPPredicate/_CPPredicate.j b/Foundation/CPPredicate/_CPPredicate.j index 56bfcd643..57192ab65 100644 --- a/Foundation/CPPredicate/_CPPredicate.j +++ b/Foundation/CPPredicate/_CPPredicate.j @@ -751,11 +751,35 @@ subpredicate = [self parsePredicate]; if (![self scanString:@")" intoString:NULL]) - CPRaiseParseError(self, @"expression"); + CPRaiseParseError(self, @"predicate"); return [[_CPSubqueryExpression alloc] initWithExpression:collection usingIteratorExpression:variableExpression predicate:subpredicate]; } + if ([self scanString:@"TERNARY" intoString:NULL]) + { + if (![self scanString:@"(" intoString:NULL]) + CPRaiseParseError(self, @"expression"); + + var predicate, + trueExpression, + falseExpression; + + predicate = [self parsePredicate]; + if (![self scanString:@"," intoString:NULL]) + CPRaiseParseError(self, @"predicate"); + + trueExpression = [self parseExpression]; + if (![self scanString:@"," intoString:NULL]) + CPRaiseParseError(self, @"expression"); + + falseExpression = [self parseExpression]; + if (![self scanString:@")" intoString:NULL]) + CPRaiseParseError(self, @"expression"); + + return [CPExpression expressionForConditional:predicate trueExpression:trueExpression falseExpression:falseExpression]; + } + if ([self scanString:@"FUNCTION" intoString:NULL]) { if (![self scanString:@"(" intoString:NULL]) diff --git a/Tests/Foundation/CPPredicateTest.j b/Tests/Foundation/CPPredicateTest.j index 2d6f8210a..470cbc722 100644 --- a/Tests/Foundation/CPPredicateTest.j +++ b/Tests/Foundation/CPPredicateTest.j @@ -86,6 +86,10 @@ } arguments:nil]; [self assertNotNull:expression_block message:"Block Expression should not be nil"]; + + var expression_conditional = [CPExpression expressionForConditional:[CPPredicate predicateWithValue:YES] trueExpression:expression_minusset falseExpression:expression_block]; + + [self assertNotNull:expression_conditional message:"Conditional Expression should not be nil"]; } - (void)testSetExpressionEvaluation @@ -167,6 +171,14 @@ [self assertTrue:([eval isEqual:"OBJA-B"]) message:"'" + [expression description] + "' result is "+ eval + "but should be " + "OBJA-B"]; } +- (void)testConditionalExpressionEvaluation +{ + var expression = [CPExpression expressionForConditional:[CPPredicate predicateWithFormat:"SELF = $variable"] trueExpression:[CPExpression expressionForConstantValue:"TRUE_EXP"] falseExpression:[CPExpression expressionForConstantValue:"FALSE_EXP"]]; + + var eval = [expression expressionValueWithObject:"OBJ" context:@{"variable":"OBJ"}]; + [self assertTrue:([eval isEqual:"TRUE_EXP"]) message:"'" + [expression description] + "' result is "+ eval + "but should be " + "TRUE_EXP"]; +} + - (void)testOptions { var pred = [[CPComparisonPredicate alloc] initWithLeftExpression:[CPExpression expressionForConstantValue:@"àa"] rightExpression:[CPExpression expressionForConstantValue:@"aà"] modifier:CPDirectPredicateModifier type:CPLikePredicateOperatorType options:3]; @@ -401,6 +413,9 @@ predicate = [CPPredicate predicateWithFormat:@"a UNION {'b'} = {'a','b'}"]; var left = [[predicate leftExpression] expressionValueWithObject:object context:nil]; [self assertTrue:[left isEqualToSet:result] message:"Expression eval " + left + " should be " + result]; + + predicate = [CPPredicate predicateWithFormat:@"TERNARY(Record1.Children[SIZE] = 1, 'Single', Record1.Name) = 'John'"]; + [self assertTrue:[predicate evaluateWithObject:dict] message:"Predicate " + predicate + " should evaluate to TRUE"]; } - (void)testExpressionAndPredicateIsEqual @@ -459,6 +474,10 @@ pred1 = [CPPredicate predicateWithFormat:@"a = 'a' AND b = 'b'"]; pred2 = [CPPredicate predicateWithFormat:@"a = 'a' AND b = 'b'"]; [self assert:pred1 equals:pred2]; + + pred1 = [CPPredicate predicateWithFormat:@"TERNARY(Record1.Children[FIRST] BEGINSWITH Record1.Name, 'SAME', 'DIFFERENT') = 'DIFFERENT'"]; + pred2 = [CPPredicate predicateWithFormat:@"TERNARY(Record1.Children[FIRST] BEGINSWITH Record1.Name, 'SAME', 'DIFFERENT') = 'DIFFERENT'"]; + [self assert:pred1 equals:pred2]; } - (void)testExpressionAndPredicateIsNotEqualToNil @@ -510,6 +529,10 @@ [self assert:pred1 notEqual:nil]; + pred1 = [CPPredicate predicateWithFormat:@"TERNARY(Record1.Children[FIRST] BEGINSWITH Record1.Name, 'SAME', 'DIFFERENT') = 'DIFFERENT'"]; + + [self assert:pred1 notEqual:nil]; + pred1 = [CPPredicate predicateWithFormat:@"$x CONTAINS 'a'"]; [self assert:pred1 notEqual:nil]; From e72a42fd2d205ed741bdc83123a2c7516b2926e1 Mon Sep 17 00:00:00 2001 From: cacaodev Date: Sun, 27 Dec 2015 19:28:03 +0100 Subject: [PATCH 3/6] FIXED: block & conditional expressions: support for CPPredicate -predicateWithSubstitutionVariables: --- Foundation/CPPredicate/_CPBlockExpression.j | 12 ++++++++++++ Foundation/CPPredicate/_CPConditionalExpression.j | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/Foundation/CPPredicate/_CPBlockExpression.j b/Foundation/CPPredicate/_CPBlockExpression.j index 109d5d517..4b1472e1a 100644 --- a/Foundation/CPPredicate/_CPBlockExpression.j +++ b/Foundation/CPPredicate/_CPBlockExpression.j @@ -75,6 +75,18 @@ return _block(object, args, context); } +- (CPExpression)_expressionWithSubstitutionVariables:(CPDictionary)bindings +{ + var args = []; + + [_arguments enumerateObjectsUsingBlock:function(exp, idx) + { + [args addObject:[exp _expressionWithSubstitutionVariables:bindings]]; + }]; + + return [[_CPBlockExpression alloc] initWithBlock:_block arguments:args]; +} + - (CPString)description { return [CPString stringWithFormat:@"Block(0x%@)", [CPString stringWithHash:[self UID]]]; diff --git a/Foundation/CPPredicate/_CPConditionalExpression.j b/Foundation/CPPredicate/_CPConditionalExpression.j index 6e636ec29..1f939a64e 100644 --- a/Foundation/CPPredicate/_CPConditionalExpression.j +++ b/Foundation/CPPredicate/_CPConditionalExpression.j @@ -76,6 +76,15 @@ return [exp expressionValueWithObject:object context:context]; } +- (CPExpression)_expressionWithSubstitutionVariables:(CPDictionary)bindings +{ + var s_predicate = [_predicate predicateWithSubstitutionVariables:bindings], + s_trueExp = [_trueExpression _expressionWithSubstitutionVariables:bindings], + s_falseExp = [_falseExpression _expressionWithSubstitutionVariables:bindings]; + + return [[_CPConditionalExpression alloc] initWithPredicate:s_predicate trueExpression:s_trueExp falseExpression:s_falseExp]; +} + - (CPString)description { return [CPString stringWithFormat:@"TERNARY(%@,%@,%@)", [_predicate predicateFormat], [_trueExpression description], [_falseExpression description]]; From 117ac0f5d60f0637f435d61e2bb4b0e5fd68d352 Mon Sep 17 00:00:00 2001 From: cacaodev Date: Sun, 27 Dec 2015 19:31:05 +0100 Subject: [PATCH 4/6] FIXED: replace js Array.map function with capp API Also fixed BlockExpression description to match a little more with cocoa impl. --- Foundation/CPPredicate/_CPBlockExpression.j | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Foundation/CPPredicate/_CPBlockExpression.j b/Foundation/CPPredicate/_CPBlockExpression.j index 4b1472e1a..10a007205 100644 --- a/Foundation/CPPredicate/_CPBlockExpression.j +++ b/Foundation/CPPredicate/_CPBlockExpression.j @@ -65,12 +65,10 @@ { var args = []; - if (_arguments !== nil) + [_arguments enumerateObjectsUsingBlock:function(exp, idx) { - args = _arguments.map(function(expression){ - return [expression expressionValueWithObject:object context:context]; - }); - } + [args addObject:[exp expressionValueWithObject:object context:context]]; + }]; return _block(object, args, context); } @@ -89,7 +87,7 @@ - (CPString)description { - return [CPString stringWithFormat:@"Block(0x%@)", [CPString stringWithHash:[self UID]]]; + return [CPString stringWithFormat:@"Block(function, %@)", [_arguments description]]; } @end \ No newline at end of file From f1591e593a2e62cf4ed26bc14cf140b03c0a2801 Mon Sep 17 00:00:00 2001 From: cacaodev Date: Sun, 27 Dec 2015 22:03:35 +0100 Subject: [PATCH 5/6] Coding style --- Foundation/CPPredicate/_CPConditionalExpression.j | 12 ++++++------ Foundation/CPPredicate/_CPPredicate.j | 7 +++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Foundation/CPPredicate/_CPConditionalExpression.j b/Foundation/CPPredicate/_CPConditionalExpression.j index 1f939a64e..86926e392 100644 --- a/Foundation/CPPredicate/_CPConditionalExpression.j +++ b/Foundation/CPPredicate/_CPConditionalExpression.j @@ -70,19 +70,19 @@ - (id)expressionValueWithObject:(id)object context:(CPDictionary)context { - var eval = [_predicate evaluateWithObject:object substitutionVariables:context]; - var exp = eval ? _trueExpression : _falseExpression; + var eval = [_predicate evaluateWithObject:object substitutionVariables:context], + exp = eval ? _trueExpression : _falseExpression; return [exp expressionValueWithObject:object context:context]; } - (CPExpression)_expressionWithSubstitutionVariables:(CPDictionary)bindings { - var s_predicate = [_predicate predicateWithSubstitutionVariables:bindings], - s_trueExp = [_trueExpression _expressionWithSubstitutionVariables:bindings], - s_falseExp = [_falseExpression _expressionWithSubstitutionVariables:bindings]; + var predicate = [_predicate predicateWithSubstitutionVariables:bindings], + trueExp = [_trueExpression _expressionWithSubstitutionVariables:bindings], + falseExp = [_falseExpression _expressionWithSubstitutionVariables:bindings]; - return [[_CPConditionalExpression alloc] initWithPredicate:s_predicate trueExpression:s_trueExp falseExpression:s_falseExp]; + return [[_CPConditionalExpression alloc] initWithPredicate:predicate trueExpression:trueExp falseExpression:falseExp]; } - (CPString)description diff --git a/Foundation/CPPredicate/_CPPredicate.j b/Foundation/CPPredicate/_CPPredicate.j index 57192ab65..34001c415 100644 --- a/Foundation/CPPredicate/_CPPredicate.j +++ b/Foundation/CPPredicate/_CPPredicate.j @@ -744,10 +744,12 @@ if (![self scanString:@"," intoString:NULL]) CPRaiseParseError(self, @"expression"); + variableExpression = [self parseExpression]; if (![self scanString:@"," intoString:NULL]) CPRaiseParseError(self, @"expression"); + subpredicate = [self parsePredicate]; if (![self scanString:@")" intoString:NULL]) @@ -761,19 +763,20 @@ if (![self scanString:@"(" intoString:NULL]) CPRaiseParseError(self, @"expression"); - var predicate, + var predicate = [self parsePredicate], trueExpression, falseExpression; - predicate = [self parsePredicate]; if (![self scanString:@"," intoString:NULL]) CPRaiseParseError(self, @"predicate"); trueExpression = [self parseExpression]; + if (![self scanString:@"," intoString:NULL]) CPRaiseParseError(self, @"expression"); falseExpression = [self parseExpression]; + if (![self scanString:@")" intoString:NULL]) CPRaiseParseError(self, @"expression"); From 36c417458df2f18554f8ead1e3b0ca045270089a Mon Sep 17 00:00:00 2001 From: cacaodev Date: Sun, 27 Dec 2015 22:08:30 +0100 Subject: [PATCH 6/6] STYLE: replaced expression methods with accessors Also added methods and documentation for the new methods: -trueExpression, -falseExpression and -expressionBlock. Completed documentation for -predicate and -arguments. --- .../CPPredicate/_CPAggregateExpression.j | 7 +--- Foundation/CPPredicate/_CPBlockExpression.j | 14 +------ .../CPPredicate/_CPConditionalExpression.j | 21 ++-------- .../CPPredicate/_CPConstantValueExpression.j | 7 +--- Foundation/CPPredicate/_CPExpression.j | 39 +++++++++++++++++-- .../CPPredicate/_CPFunctionExpression.j | 14 +------ Foundation/CPPredicate/_CPSetExpression.j | 14 +------ .../CPPredicate/_CPSubqueryExpression.j | 14 +------ .../CPPredicate/_CPVariableExpression.j | 7 +--- 9 files changed, 50 insertions(+), 87 deletions(-) diff --git a/Foundation/CPPredicate/_CPAggregateExpression.j b/Foundation/CPPredicate/_CPAggregateExpression.j index 688166398..2216c959c 100644 --- a/Foundation/CPPredicate/_CPAggregateExpression.j +++ b/Foundation/CPPredicate/_CPAggregateExpression.j @@ -25,7 +25,7 @@ @implementation _CPAggregateExpression : CPExpression { - CPArray _aggregate; + CPArray _aggregate @accessors(getter=collection); } - (id)initWithAggregate:(CPArray)collection @@ -48,11 +48,6 @@ return YES; } -- (id)collection -{ - return _aggregate; -} - - (id)expressionValueWithObject:(id)object context:(CPDictionary)context { var eval_array = [CPArray array], diff --git a/Foundation/CPPredicate/_CPBlockExpression.j b/Foundation/CPPredicate/_CPBlockExpression.j index 10a007205..299f250ee 100644 --- a/Foundation/CPPredicate/_CPBlockExpression.j +++ b/Foundation/CPPredicate/_CPBlockExpression.j @@ -23,8 +23,8 @@ @implementation _CPBlockExpression : CPExpression { - Function _block; - CPArray _arguments; + Function _block @accessors(getter=expressionBlock); + CPArray _arguments @accessors(getter=arguments); } - (id)initWithBlock:(Function)aBlock arguments:(CPArray)arguments @@ -51,16 +51,6 @@ return YES; } -- (Function)expressionBlock -{ - return _block; -} - -- (CPArray)arguments -{ - return _arguments; -} - - (id)expressionValueWithObject:(id)object context:(CPDictionary)context { var args = []; diff --git a/Foundation/CPPredicate/_CPConditionalExpression.j b/Foundation/CPPredicate/_CPConditionalExpression.j index 86926e392..8fc544580 100644 --- a/Foundation/CPPredicate/_CPConditionalExpression.j +++ b/Foundation/CPPredicate/_CPConditionalExpression.j @@ -23,9 +23,9 @@ @implementation _CPConditionalExpression : CPExpression { - CPPredicate _predicate; - CPExpression _trueExpression; - CPExpression _falseExpression; + CPPredicate _predicate @accessors(getter=predicate); + CPExpression _trueExpression @accessors(getter=trueExpression); + CPExpression _falseExpression @accessors(getter=falseExpression); } - (id)initWithPredicate:(CPPredicate)aPredicate trueExpression:(CPExpression)trueExpression falseExpression:(CPExpression)falseExpression @@ -53,21 +53,6 @@ return YES; } -- (CPPredicate)predicate -{ - return _predicate; -} - -- (CPExpression)trueExpression -{ - return _trueExpression; -} - -- (CPExpression)falseExpression -{ - return _falseExpression; -} - - (id)expressionValueWithObject:(id)object context:(CPDictionary)context { var eval = [_predicate evaluateWithObject:object substitutionVariables:context], diff --git a/Foundation/CPPredicate/_CPConstantValueExpression.j b/Foundation/CPPredicate/_CPConstantValueExpression.j index 617c9b10a..81fd60b2e 100644 --- a/Foundation/CPPredicate/_CPConstantValueExpression.j +++ b/Foundation/CPPredicate/_CPConstantValueExpression.j @@ -27,7 +27,7 @@ @implementation _CPConstantValueExpression : CPExpression { - id _value; + id _value @accessors(getter=constantValue); } - (id)initWithValue:(id)value @@ -51,11 +51,6 @@ return YES; } -- (id)constantValue -{ - return _value; -} - - (id)expressionValueWithObject:(id)object context:(CPDictionary)context { return _value; diff --git a/Foundation/CPPredicate/_CPExpression.j b/Foundation/CPPredicate/_CPExpression.j index b26707fc0..8a671c5b2 100644 --- a/Foundation/CPPredicate/_CPExpression.j +++ b/Foundation/CPPredicate/_CPExpression.j @@ -350,7 +350,7 @@ A dictionary that the expression can use to store temporary state for one predic /*! Returns the arguments for the receiver. - @return The arguments for the receiver—that is, the array of expressions that will be passed as parameters during invocation of the selector on the operand of a function expression. + @return The arguments for the receiver—that is, the array of expressions that will be passed as parameters during invocation of the selector on the operand of a function expression or as a parameter of the block of a block expression. This method raises an exception if it is not applicable to the receiver. */ - (CPArray)arguments @@ -371,8 +371,8 @@ A dictionary that the expression can use to store temporary state for one predic } /*! - Returns the predicate in a subquery expression. - @return The predicate in a subquery expression.. + Returns the predicate in a subquery expression or a conditional expression. + @return The predicate in a subquery expression or a conditional expression. This method raises an exception if it is not applicable to the receiver. */ - (CPPredicate)predicate @@ -414,6 +414,39 @@ A dictionary that the expression can use to store temporary state for one predic return nil; } +/*! + Returns the block of a block expression. + @return The block of a block expression. + This method raises an exception if it is not applicable to the receiver. +*/ +- (Function)expressionBlock +{ + _CPRaiseInvalidAbstractInvocation(self, _cmd); + return nil; +} + +/*! + Returns the true expression of a conditional expression. + @return The true expression of a conditional expression. + This method raises an exception if it is not applicable to the receiver. +*/ +- (CPExpression)trueExpression +{ + _CPRaiseInvalidAbstractInvocation(self, _cmd); + return nil; +} + +/*! + Returns the false expression of a conditional expression. + @return The false expression of a conditional expression. + This method raises an exception if it is not applicable to the receiver. +*/ +- (CPExpression)falseExpression +{ + _CPRaiseInvalidAbstractInvocation(self, _cmd); + return nil; +} + - (CPExpression)_expressionWithSubstitutionVariables:(CPDictionary)variables { return self; diff --git a/Foundation/CPPredicate/_CPFunctionExpression.j b/Foundation/CPPredicate/_CPFunctionExpression.j index 394c93612..32790d4e4 100644 --- a/Foundation/CPPredicate/_CPFunctionExpression.j +++ b/Foundation/CPPredicate/_CPFunctionExpression.j @@ -28,9 +28,9 @@ @implementation _CPFunctionExpression : CPExpression { - CPExpression _operand; + CPExpression _operand @accessors(getter=operand); SEL _selector; - CPArray _arguments; + CPArray _arguments @accessors(getter=arguments); int _argc; int _maxargs; } @@ -88,16 +88,6 @@ return [self _function]; } -- (CPArray)arguments -{ - return _arguments; -} - -- (CPExpression)operand -{ - return _operand; -} - - (id)expressionValueWithObject:(id)object context:(CPDictionary)context { var target = [_operand expressionValueWithObject:object context:context], diff --git a/Foundation/CPPredicate/_CPSetExpression.j b/Foundation/CPPredicate/_CPSetExpression.j index a0e9df3bb..707486b32 100644 --- a/Foundation/CPPredicate/_CPSetExpression.j +++ b/Foundation/CPPredicate/_CPSetExpression.j @@ -25,8 +25,8 @@ @implementation _CPSetExpression : CPExpression { - CPExpression _left; - CPExpression _right; + CPExpression _left @accessors(getter=leftExpression); + CPExpression _right @accessors(getter=rightExpression); } - (id)initWithType:(int)type left:(CPExpression)left right:(CPExpression)right @@ -88,16 +88,6 @@ return self; } -- (CPExpression)leftExpression -{ - return _left; -} - -- (CPExpression)rightExpression -{ - return _right; -} - - (CPString)description { var desc; diff --git a/Foundation/CPPredicate/_CPSubqueryExpression.j b/Foundation/CPPredicate/_CPSubqueryExpression.j index e8f648403..d69483d2c 100644 --- a/Foundation/CPPredicate/_CPSubqueryExpression.j +++ b/Foundation/CPPredicate/_CPSubqueryExpression.j @@ -26,9 +26,9 @@ @implementation _CPSubqueryExpression : CPExpression { - CPExpression _collection; + CPExpression _collection @accessors(getter=collection); CPExpression _variableExpression; - CPPredicate _subpredicate; + CPPredicate _subpredicate @accessors(getter=predicate); } - (id)initWithExpression:(CPExpression)collection usingIteratorVariable:(CPString)variable predicate:(CPPredicate)subpredicate @@ -81,21 +81,11 @@ return YES; } -- (CPExpression)collection -{ - return _collection; -} - - (id)copy { return [[_CPSubqueryExpression alloc] initWithExpression:[_collection copy] usingIteratorExpression:[_variableExpression copy] predicate:[_subpredicate copy]]; } -- (CPPredicate)predicate -{ - return _subpredicate; -} - - (CPString)description { return [self predicateFormat]; diff --git a/Foundation/CPPredicate/_CPVariableExpression.j b/Foundation/CPPredicate/_CPVariableExpression.j index 910ef23d5..a33911222 100644 --- a/Foundation/CPPredicate/_CPVariableExpression.j +++ b/Foundation/CPPredicate/_CPVariableExpression.j @@ -29,7 +29,7 @@ @implementation _CPVariableExpression : CPExpression { - CPString _variable; + CPString _variable @accessors(getter=variable); } - (id)initWithVariable:(CPString)variable @@ -54,11 +54,6 @@ return YES; } -- (CPString)variable -{ - return _variable; -} - - (id)expressionValueWithObject:object context:(CPDictionary)context { var expression = [self _expressionWithSubstitutionVariables:context];