2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGRuleContext.h"
25 #include "NGRuleModel.h"
26 #include "NSObject+Logs.h"
28 #include <EOControl/EOQualifier.h>
30 @implementation NGRuleContext
32 + (id)ruleContextWithModelInUserDefault:(NSString *)_defName {
35 if ((mod = [NGRuleModel ruleModelWithContentsOfUserDefault:_defName]) == nil)
38 return [self ruleContextWithModel:mod];
41 + (id)ruleContextWithModel:(NGRuleModel *)_model {
42 return [[[self alloc] initWithModel:_model] autorelease];
45 - (id)initWithModel:(NGRuleModel *)_model {
46 if ((self = [super init])) {
47 [self setModel:_model];
52 return [self initWithModel:nil];
56 [self->model release];
57 [self->storedValues release];
63 - (void)setModel:(NGRuleModel *)_model {
64 ASSIGN(self->model, _model);
66 - (NGRuleModel *)model {
72 - (void)takeStoredValue:(id)_value forKey:(NSString *)_key {
74 if (self->storedValues == nil)
75 self->storedValues = [[NSMutableDictionary alloc] initWithCapacity:32];
76 [self->storedValues setObject:_value forKey:_key];
79 [self->storedValues removeObjectForKey:_key];
81 - (id)storedValueForKey:(NSString *)_key {
82 return [self->storedValues objectForKey:_key];
85 - (void)takeValue:(id)_value forKey:(NSString *)_key {
86 [self takeStoredValue:_value forKey:_key];
90 [self->storedValues removeAllObjects];
95 - (id)inferredValueForKey:(NSString *)_key {
100 [self debugWithFormat:@"calculate value for key: '%@'", _key];
102 /* select candidates */
103 rules = [[self model] candidateRulesForKey:_key];
105 [self debugWithFormat:@" candidate rules: %@", rules];
107 /* check qualifiers */
108 for (i = 0, count = [rules count]; i < count; i++) {
111 rule = [rules objectAtIndex:i];
112 if ([(id<EOQualifierEvaluation>)[rule qualifier] evaluateWithObject:self]){
114 [self debugWithFormat:@" rule %i matches: %@", i, rule];
115 return [[rule action] fireInContext:self];
119 [self debugWithFormat:@" no rule matched !"];
123 - (id)valueForKey:(NSString *)_key {
126 /* look for constants */
127 if ((v = [self->storedValues objectForKey:_key]))
130 /* look into rule system */
131 if ((v = [self inferredValueForKey:_key]))
137 - (NSArray *)valuesForKeyPath:(NSString *)_kp
138 takingSuccessiveValues:(NSArray *)_values
139 forKeyPath:(NSString *)_valkp
141 NSMutableArray *results;
144 count = [_values count];
145 results = [NSMutableArray arrayWithCapacity:count];
147 for (i = 0; i < count; i++) {
151 [self takeValue:[_values objectAtIndex:i] forKeyPath:_valkp];
153 /* calculate the rule value */
154 ruleValue = [self valueForKey:_kp];
155 [results addObject:ruleValue ? ruleValue : [NSNull null]];
162 - (BOOL)isDebuggingEnabled {
163 return self->debugOn;
165 - (void)setDebugEnabled:(BOOL)_flag {
166 self->debugOn = _flag;
169 @end /* NGRuleContext */