]> err.no Git - sope/blob - sope-core/NGExtensions/NGRuleEngine.subproj/NGRuleContext.m
NGLogEventFormatter.h was marked as project, not public - fixed
[sope] / sope-core / NGExtensions / NGRuleEngine.subproj / NGRuleContext.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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 // $Id$
22
23 #include "NGRuleContext.h"
24 #include "NGRule.h"
25 #include "NGRuleModel.h"
26 #include "NSObject+Logs.h"
27 #include "common.h"
28 #include <EOControl/EOQualifier.h>
29
30 @implementation NGRuleContext
31
32 + (id)ruleContextWithModelInUserDefault:(NSString *)_defName {
33   NGRuleModel *mod;
34   
35   if ((mod = [NGRuleModel ruleModelWithContentsOfUserDefault:_defName]) == nil)
36     return nil;
37   
38   return [self ruleContextWithModel:mod];
39 }
40
41 + (id)ruleContextWithModel:(NGRuleModel *)_model {
42   return [[[self alloc] initWithModel:_model] autorelease];
43 }
44
45 - (id)initWithModel:(NGRuleModel *)_model {
46   if ((self = [super init])) {
47     [self setModel:_model];
48   }
49   return self;
50 }
51 - (id)init {
52   return [self initWithModel:nil];
53 }
54
55 - (void)dealloc {
56   [self->model        release];
57   [self->storedValues release];
58   [super dealloc];
59 }
60
61 /* accessors */
62
63 - (void)setModel:(NGRuleModel *)_model {
64   ASSIGN(self->model, _model);
65 }
66 - (NGRuleModel *)model {
67   return self->model;
68 }
69
70 /* values */
71
72 - (void)takeStoredValue:(id)_value forKey:(NSString *)_key {
73   if (_value) {
74     if (self->storedValues == nil)
75       self->storedValues = [[NSMutableDictionary alloc] initWithCapacity:32];
76     [self->storedValues setObject:_value forKey:_key];
77   }
78   else
79     [self->storedValues removeObjectForKey:_key];
80 }
81 - (id)storedValueForKey:(NSString *)_key {
82   return [self->storedValues objectForKey:_key];
83 }
84
85 - (void)takeValue:(id)_value forKey:(NSString *)_key {
86   [self takeStoredValue:_value forKey:_key];
87 }
88
89 - (void)reset {
90   [self->storedValues removeAllObjects];
91 }
92
93 /* processing */
94
95 - (id)inferredValueForKey:(NSString *)_key {
96   NSArray *rules;
97   unsigned i, count;
98   
99   if (self->debugOn)
100     [self debugWithFormat:@"calculate value for key: '%@'", _key];
101   
102   /* select candidates */
103   rules = [[self model] candidateRulesForKey:_key];
104   if (self->debugOn)
105     [self debugWithFormat:@"  candidate rules: %@", rules];
106   
107   /* check qualifiers */
108   for (i = 0, count = [rules count]; i < count; i++) {
109     NGRule *rule;
110     
111     rule = [rules objectAtIndex:i];
112     if ([(id<EOQualifierEvaluation>)[rule qualifier] evaluateWithObject:self]){
113       if (self->debugOn)
114         [self debugWithFormat:@"  rule %i matches: %@", i, rule];
115       return [[rule action] fireInContext:self];
116     }
117   }
118   if (self->debugOn)
119     [self debugWithFormat:@"  no rule matched !"];
120   return nil;
121 }
122
123 - (id)valueForKey:(NSString *)_key {
124   id v;
125   
126   /* look for constants */
127   if ((v = [self->storedValues objectForKey:_key]))
128     return v;
129   
130   /* look into rule system */
131   if ((v = [self inferredValueForKey:_key]))
132     return v;
133   
134   return nil;
135 }
136
137 - (NSArray *)valuesForKeyPath:(NSString *)_kp
138   takingSuccessiveValues:(NSArray *)_values
139   forKeyPath:(NSString *)_valkp
140 {
141   NSMutableArray *results;
142   unsigned i, count;
143   
144   count   = [_values count];
145   results = [NSMutableArray arrayWithCapacity:count];
146   
147   for (i = 0; i < count; i++) {
148     id ruleValue;
149     
150     /* take the value */
151     [self takeValue:[_values objectAtIndex:i] forKeyPath:_valkp];
152
153     /* calculate the rule value */
154     ruleValue = [self valueForKey:_kp];
155     [results addObject:ruleValue ? ruleValue : [NSNull null]];
156   }
157   return results;
158 }
159
160 /* debugging */
161
162 - (BOOL)isDebuggingEnabled {
163   return self->debugOn;
164 }
165 - (void)setDebugEnabled:(BOOL)_flag {
166   self->debugOn = _flag;
167 }
168
169 @end /* NGRuleContext */