]> err.no Git - sope/blob - sope-core/NGExtensions/NGRuleEngine.subproj/NGRuleContext.m
changed #includes into #imports - does compile against MulleEOF out of the box now
[sope] / sope-core / NGExtensions / NGRuleEngine.subproj / NGRuleContext.m
1 /*
2   Copyright (C) 2003-2004 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "NGRuleContext.h"
23 #include "NGRule.h"
24 #include "NGRuleModel.h"
25 #include "NSObject+Logs.h"
26 #include "common.h"
27 #import <EOControl/EOQualifier.h>
28
29 @implementation NGRuleContext
30
31 + (id)ruleContextWithModelInUserDefault:(NSString *)_defName {
32   NGRuleModel *mod;
33   
34   if ((mod = [NGRuleModel ruleModelWithContentsOfUserDefault:_defName]) == nil)
35     return nil;
36   
37   return [self ruleContextWithModel:mod];
38 }
39
40 + (id)ruleContextWithModel:(NGRuleModel *)_model {
41   return [[[self alloc] initWithModel:_model] autorelease];
42 }
43
44 - (id)initWithModel:(NGRuleModel *)_model {
45   if ((self = [super init])) {
46     [self setModel:_model];
47   }
48   return self;
49 }
50 - (id)init {
51   return [self initWithModel:nil];
52 }
53
54 - (void)dealloc {
55   [self->model        release];
56   [self->storedValues release];
57   [super dealloc];
58 }
59
60 /* accessors */
61
62 - (void)setModel:(NGRuleModel *)_model {
63   ASSIGN(self->model, _model);
64 }
65 - (NGRuleModel *)model {
66   return self->model;
67 }
68
69 /* values */
70
71 - (void)takeStoredValue:(id)_value forKey:(NSString *)_key {
72   if (_value) {
73     if (self->storedValues == nil)
74       self->storedValues = [[NSMutableDictionary alloc] initWithCapacity:32];
75     [self->storedValues setObject:_value forKey:_key];
76   }
77   else
78     [self->storedValues removeObjectForKey:_key];
79 }
80 - (id)storedValueForKey:(NSString *)_key {
81   return [self->storedValues objectForKey:_key];
82 }
83
84 - (void)takeValue:(id)_value forKey:(NSString *)_key {
85   [self takeStoredValue:_value forKey:_key];
86 }
87
88 - (void)reset {
89   [self->storedValues removeAllObjects];
90 }
91
92 /* processing */
93
94 - (id)inferredValueForKey:(NSString *)_key {
95   NSArray  *rules;
96   unsigned i, count;
97   
98   if (self->debugOn)
99     [self debugWithFormat:@"calculate value for key: '%@'", _key];
100   
101   /* select candidates */
102   rules = [[self model] candidateRulesForKey:_key];
103   if (self->debugOn)
104     [self debugWithFormat:@"  candidate rules: %@", rules];
105   
106   /* check qualifiers */
107   for (i = 0, count = [rules count]; i < count; i++) {
108     NGRule *rule;
109     
110     rule = [rules objectAtIndex:i];
111     if ([(id<EOQualifierEvaluation>)[rule qualifier] evaluateWithObject:self]){
112       if (self->debugOn)
113         [self debugWithFormat:@"  rule %i matches: %@", i, rule];
114       return [[rule action] fireInContext:self];
115     }
116   }
117   if (self->debugOn)
118     [self debugWithFormat:@"  no rule matched !"];
119   return nil;
120 }
121
122 - (NSArray *)allPossibleValuesForKey:(NSString *)_key {
123   NSMutableArray *values;
124   NSArray  *rules;
125   unsigned i, count;
126   
127   if (self->debugOn)
128     [self debugWithFormat:@"calculate all values for key: '%@'", _key];
129   
130   /* select candidates */
131   rules = [[self model] candidateRulesForKey:_key];
132   if (self->debugOn)
133     [self debugWithFormat:@"  candidate rules: %@", rules];
134   
135   values = [NSMutableArray arrayWithCapacity:16];
136   
137   /* check qualifiers */
138   for (i = 0, count = [rules count]; i < count; i++) {
139     NGRule *rule;
140     
141     rule = [rules objectAtIndex:i];
142     if ([(id<EOQualifierEvaluation>)[rule qualifier] evaluateWithObject:self]){
143       id v;
144       
145       if (self->debugOn)
146         [self debugWithFormat:@"  rule %i matches: %@", i, rule];
147       
148       v = [[rule action] fireInContext:self];
149       [values addObject:(v ? v : [NSNull null])];
150     }
151   }
152   if (self->debugOn)
153     [self debugWithFormat:@"  %d rules matched.", [values count]];
154   return values;
155 }
156
157 - (id)valueForKey:(NSString *)_key {
158   id v;
159
160   // TODO: add rule cache?
161   
162   /* look for constants */
163   if ((v = [self->storedValues objectForKey:_key]) != nil)
164     return v;
165   
166   /* look into rule system */
167   if ((v = [self inferredValueForKey:_key]) != nil)
168     return v;
169   
170   return nil;
171 }
172
173 - (NSArray *)valuesForKeyPath:(NSString *)_kp
174   takingSuccessiveValues:(NSArray *)_values
175   forKeyPath:(NSString *)_valkp
176 {
177   NSMutableArray *results;
178   unsigned i, count;
179   
180   count   = [_values count];
181   results = [NSMutableArray arrayWithCapacity:count];
182   
183   for (i = 0; i < count; i++) {
184     id ruleValue;
185     
186     /* take the value */
187     [self takeValue:[_values objectAtIndex:i] forKeyPath:_valkp];
188
189     /* calculate the rule value */
190     ruleValue = [self valueForKey:_kp];
191     [results addObject:ruleValue ? ruleValue : [NSNull null]];
192   }
193   return results;
194 }
195
196 /* debugging */
197
198 - (BOOL)isDebuggingEnabled {
199   return self->debugOn;
200 }
201 - (void)setDebugEnabled:(BOOL)_flag {
202   self->debugOn = _flag;
203 }
204
205 @end /* NGRuleContext */