]> err.no Git - sope/blob - sope-core/NGExtensions/NGRuleEngine.subproj/NGRuleModel.m
renamed packages as discussed in the developer list
[sope] / sope-core / NGExtensions / NGRuleEngine.subproj / NGRuleModel.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 "NGRuleModel.h"
24 #include "NGRule.h"
25 #include "NGRuleParser.h"
26 #include "EOTrueQualifier.h"
27 #include <EOControl/EOControl.h>
28 #include "common.h"
29
30 // TODO: add a candidate cache
31
32 @implementation NGRuleModel
33
34 + (id)ruleModelWithPropertyList:(id)_plist {
35   static NGRuleParser *ruleParser = nil; // THREAD
36
37   if (ruleParser == nil)
38     ruleParser = [[NGRuleParser sharedRuleParser] retain];
39   
40   return [ruleParser parseRuleModelFromPropertyList:_plist];
41 }
42 + (id)ruleModelWithContentsOfUserDefault:(NSString *)_defName {
43   id plist;
44   
45   plist = [[NSUserDefaults standardUserDefaults] objectForKey:_defName];
46   if (plist == nil) return nil;
47   
48   return [self ruleModelWithPropertyList:plist];
49 }
50
51 - (id)init {
52   if ((self = [super init])) {
53     self->rules = [[NSMutableArray alloc] initWithCapacity:16];
54   }
55   return self;
56 }
57 - (id)initWithRules:(NSArray *)_rules {
58   if ((self = [self init])) {
59     [self->rules addObjectsFromArray:_rules];
60   }
61   return self;
62 }
63
64 - (id)initWithPropertyList:(id)_plist {
65   [self autorelease];
66   return [[[self class] ruleModelWithPropertyList:_plist] retain];
67 }
68
69 - (id)initWithContentsOfFile:(NSString *)_path {
70   NSString *s;
71   id plist;
72   
73   if ((s = [[NSString alloc] initWithContentsOfFile:_path])) {
74     [self release];
75     return nil;
76   }
77   plist = [s propertyList];
78   [s release];
79   return [self initWithPropertyList:plist];
80 }
81
82 - (id)initWithContentsOfUserDefault:(NSString *)_defName {
83   [self autorelease];
84   return [[[self class] ruleModelWithContentsOfUserDefault:_defName] retain];
85 }
86
87 - (void)dealloc {
88   [self->rules release];
89   [super dealloc];
90 }
91
92 /* accessors */
93
94 - (void)setRules:(NSArray *)_rules {
95   [self->rules removeAllObjects];
96   [self->rules addObjectsFromArray:_rules];
97 }
98 - (NSArray *)rules {
99   return [[self->rules shallowCopy] autorelease];
100 }
101
102 - (void)addRule:(NGRule *)_rule {
103   [self->rules addObject:_rule];
104 }
105 - (void)removeRule:(NGRule *)_rule {
106   [self->rules removeObject:_rule];
107 }
108
109 /* operations */
110
111 static int candidateSort(NGRule *rule1, NGRule *rule2, NGRuleModel *model) {
112   static Class TrueQualClass = Nil;
113   EOQualifier *q1, *q2;
114   register int pri1, pri2;
115   
116   pri1 = [rule1 priority];
117   pri2 = [rule2 priority];
118   if (pri1 != pri2)
119     return pri1 > pri2 ? NSOrderedAscending : NSOrderedDescending;
120   
121   /* check number of qualifiers (order on how specific the qualifier is) */
122
123   if (TrueQualClass == Nil) TrueQualClass = [EOTrueQualifier class];
124   q1 = [rule1 qualifier];
125   q2 = [rule2 qualifier];
126   
127   pri1 = [q1 isKindOfClass:TrueQualClass]
128     ? - 1
129     : ([q1 respondsToSelector:@selector(count)] ? [q1 count] : 0);
130   pri2 = [q2 isKindOfClass:TrueQualClass]
131     ? -1
132     : ([q2 respondsToSelector:@selector(count)] ? [q2 count] : 0);
133   
134   if (pri1 != pri2)
135     return pri1 > pri2 ? NSOrderedAscending : NSOrderedDescending;
136   
137   return NSOrderedSame;
138 }
139
140 - (NSArray *)candidateRulesForKey:(NSString *)_key {
141   NSMutableArray *candidates;
142   unsigned i, cnt;
143   
144   /* first, find all candidates */
145   candidates = nil;
146   cnt = [self->rules count];
147   for (i = 0; i < cnt; i++) {
148     NGRule *rule;
149     
150     rule = [self->rules objectAtIndex:i];
151     if ([rule isCandidateForKey:_key]) {
152       if (candidates == nil)
153         candidates = [[NSMutableArray alloc] initWithCapacity:cnt];
154       [candidates addObject:rule];
155     }
156   }
157
158   /* sort candidates */
159   [candidates sortUsingFunction:(void *)candidateSort context:self];
160   
161   return candidates;
162 }
163
164 /* representations */
165
166 @end /* NGRuleModel */