2 Copyright (C) 2003-2004 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "NGRuleContext.h"
24 #include "NGRuleModel.h"
25 #include "NSObject+Logs.h"
27 #import <EOControl/EOQualifier.h>
29 @implementation NGRuleContext
31 + (id)ruleContextWithModelInUserDefault:(NSString *)_defName {
34 if ((mod = [NGRuleModel ruleModelWithContentsOfUserDefault:_defName]) == nil)
37 return [self ruleContextWithModel:mod];
40 + (id)ruleContextWithModel:(NGRuleModel *)_model {
41 return [[[self alloc] initWithModel:_model] autorelease];
44 - (id)initWithModel:(NGRuleModel *)_model {
45 if ((self = [super init])) {
46 [self setModel:_model];
51 return [self initWithModel:nil];
55 [self->model release];
56 [self->storedValues release];
62 - (void)setModel:(NGRuleModel *)_model {
63 ASSIGN(self->model, _model);
65 - (NGRuleModel *)model {
71 - (void)takeStoredValue:(id)_value forKey:(NSString *)_key {
73 if (self->storedValues == nil)
74 self->storedValues = [[NSMutableDictionary alloc] initWithCapacity:32];
75 [self->storedValues setObject:_value forKey:_key];
78 [self->storedValues removeObjectForKey:_key];
80 - (id)storedValueForKey:(NSString *)_key {
81 return [self->storedValues objectForKey:_key];
84 - (void)takeValue:(id)_value forKey:(NSString *)_key {
85 [self takeStoredValue:_value forKey:_key];
89 [self->storedValues removeAllObjects];
94 - (id)inferredValueForKey:(NSString *)_key {
99 [self debugWithFormat:@"calculate value for key: '%@'", _key];
101 /* select candidates */
102 rules = [[self model] candidateRulesForKey:_key];
104 [self debugWithFormat:@" candidate rules: %@", rules];
106 /* check qualifiers */
107 for (i = 0, count = [rules count]; i < count; i++) {
110 rule = [rules objectAtIndex:i];
111 if ([(id<EOQualifierEvaluation>)[rule qualifier] evaluateWithObject:self]){
113 [self debugWithFormat:@" rule %i matches: %@", i, rule];
114 return [[rule action] fireInContext:self];
118 [self debugWithFormat:@" no rule matched !"];
122 - (NSArray *)allPossibleValuesForKey:(NSString *)_key {
123 NSMutableArray *values;
128 [self debugWithFormat:@"calculate all values for key: '%@'", _key];
130 /* select candidates */
131 rules = [[self model] candidateRulesForKey:_key];
133 [self debugWithFormat:@" candidate rules: %@", rules];
135 values = [NSMutableArray arrayWithCapacity:16];
137 /* check qualifiers */
138 for (i = 0, count = [rules count]; i < count; i++) {
141 rule = [rules objectAtIndex:i];
142 if ([(id<EOQualifierEvaluation>)[rule qualifier] evaluateWithObject:self]){
146 [self debugWithFormat:@" rule %i matches: %@", i, rule];
148 v = [[rule action] fireInContext:self];
149 [values addObject:(v ? v : [NSNull null])];
153 [self debugWithFormat:@" %d rules matched.", [values count]];
157 - (id)valueForKey:(NSString *)_key {
160 // TODO: add rule cache?
162 /* look for constants */
163 if ((v = [self->storedValues objectForKey:_key]) != nil)
166 /* look into rule system */
167 if ((v = [self inferredValueForKey:_key]) != nil)
173 - (NSArray *)valuesForKeyPath:(NSString *)_kp
174 takingSuccessiveValues:(NSArray *)_values
175 forKeyPath:(NSString *)_valkp
177 NSMutableArray *results;
180 count = [_values count];
181 results = [NSMutableArray arrayWithCapacity:count];
183 for (i = 0; i < count; i++) {
187 [self takeValue:[_values objectAtIndex:i] forKeyPath:_valkp];
189 /* calculate the rule value */
190 ruleValue = [self valueForKey:_kp];
191 [results addObject:ruleValue ? ruleValue : [NSNull null]];
198 - (BOOL)isDebuggingEnabled {
199 return self->debugOn;
201 - (void)setDebugEnabled:(BOOL)_flag {
202 self->debugOn = _flag;
205 @end /* NGRuleContext */