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 #include <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 - (id)valueForKey:(NSString *)_key {
125 /* look for constants */
126 if ((v = [self->storedValues objectForKey:_key]))
129 /* look into rule system */
130 if ((v = [self inferredValueForKey:_key]))
136 - (NSArray *)valuesForKeyPath:(NSString *)_kp
137 takingSuccessiveValues:(NSArray *)_values
138 forKeyPath:(NSString *)_valkp
140 NSMutableArray *results;
143 count = [_values count];
144 results = [NSMutableArray arrayWithCapacity:count];
146 for (i = 0; i < count; i++) {
150 [self takeValue:[_values objectAtIndex:i] forKeyPath:_valkp];
152 /* calculate the rule value */
153 ruleValue = [self valueForKey:_kp];
154 [results addObject:ruleValue ? ruleValue : [NSNull null]];
161 - (BOOL)isDebuggingEnabled {
162 return self->debugOn;
164 - (void)setDebugEnabled:(BOOL)_flag {
165 self->debugOn = _flag;
168 @end /* NGRuleContext */