]> err.no Git - sope/blob - sope-core/NGExtensions/NGRuleEngine.subproj/NGRuleContext.m
added some NGRule documentation
[sope] / sope-core / NGExtensions / NGRuleEngine.subproj / NGRuleContext.m
1 /*
2   Copyright (C) 2003-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
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 #include <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 - (id)valueForKey:(NSString *)_key {
123   id v;
124   
125   /* look for constants */
126   if ((v = [self->storedValues objectForKey:_key]))
127     return v;
128   
129   /* look into rule system */
130   if ((v = [self inferredValueForKey:_key]))
131     return v;
132   
133   return nil;
134 }
135
136 - (NSArray *)valuesForKeyPath:(NSString *)_kp
137   takingSuccessiveValues:(NSArray *)_values
138   forKeyPath:(NSString *)_valkp
139 {
140   NSMutableArray *results;
141   unsigned i, count;
142   
143   count   = [_values count];
144   results = [NSMutableArray arrayWithCapacity:count];
145   
146   for (i = 0; i < count; i++) {
147     id ruleValue;
148     
149     /* take the value */
150     [self takeValue:[_values objectAtIndex:i] forKeyPath:_valkp];
151
152     /* calculate the rule value */
153     ruleValue = [self valueForKey:_kp];
154     [results addObject:ruleValue ? ruleValue : [NSNull null]];
155   }
156   return results;
157 }
158
159 /* debugging */
160
161 - (BOOL)isDebuggingEnabled {
162   return self->debugOn;
163 }
164 - (void)setDebugEnabled:(BOOL)_flag {
165   self->debugOn = _flag;
166 }
167
168 @end /* NGRuleContext */