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 "NGRuleModel.h"
25 #include "NGRuleParser.h"
26 #include "EOTrueQualifier.h"
27 #include <EOControl/EOControl.h>
30 // TODO: add a candidate cache
32 @implementation NGRuleModel
34 + (id)ruleModelWithPropertyList:(id)_plist {
35 static NGRuleParser *ruleParser = nil; // THREAD
37 if (ruleParser == nil)
38 ruleParser = [[NGRuleParser sharedRuleParser] retain];
40 return [ruleParser parseRuleModelFromPropertyList:_plist];
42 + (id)ruleModelWithContentsOfUserDefault:(NSString *)_defName {
45 plist = [[NSUserDefaults standardUserDefaults] objectForKey:_defName];
46 if (plist == nil) return nil;
48 return [self ruleModelWithPropertyList:plist];
52 if ((self = [super init])) {
53 self->rules = [[NSMutableArray alloc] initWithCapacity:16];
57 - (id)initWithRules:(NSArray *)_rules {
58 if ((self = [self init])) {
59 [self->rules addObjectsFromArray:_rules];
64 - (id)initWithPropertyList:(id)_plist {
66 return [[[self class] ruleModelWithPropertyList:_plist] retain];
69 - (id)initWithContentsOfFile:(NSString *)_path {
73 if ((s = [[NSString alloc] initWithContentsOfFile:_path])) {
77 plist = [s propertyList];
79 return [self initWithPropertyList:plist];
82 - (id)initWithContentsOfUserDefault:(NSString *)_defName {
84 return [[[self class] ruleModelWithContentsOfUserDefault:_defName] retain];
88 [self->rules release];
94 - (void)setRules:(NSArray *)_rules {
95 [self->rules removeAllObjects];
96 [self->rules addObjectsFromArray:_rules];
99 return [[self->rules shallowCopy] autorelease];
102 - (void)addRule:(NGRule *)_rule {
103 [self->rules addObject:_rule];
105 - (void)removeRule:(NGRule *)_rule {
106 [self->rules removeObject:_rule];
111 static int candidateSort(NGRule *rule1, NGRule *rule2, NGRuleModel *model) {
112 static Class TrueQualClass = Nil;
113 EOQualifier *q1, *q2;
114 register int pri1, pri2;
116 pri1 = [rule1 priority];
117 pri2 = [rule2 priority];
119 return pri1 > pri2 ? NSOrderedAscending : NSOrderedDescending;
121 /* check number of qualifiers (order on how specific the qualifier is) */
123 if (TrueQualClass == Nil) TrueQualClass = [EOTrueQualifier class];
124 q1 = [rule1 qualifier];
125 q2 = [rule2 qualifier];
127 pri1 = [q1 isKindOfClass:TrueQualClass]
129 : ([q1 respondsToSelector:@selector(count)] ? [q1 count] : 0);
130 pri2 = [q2 isKindOfClass:TrueQualClass]
132 : ([q2 respondsToSelector:@selector(count)] ? [q2 count] : 0);
135 return pri1 > pri2 ? NSOrderedAscending : NSOrderedDescending;
137 return NSOrderedSame;
140 - (NSArray *)candidateRulesForKey:(NSString *)_key {
141 NSMutableArray *candidates;
144 /* first, find all candidates */
146 cnt = [self->rules count];
147 for (i = 0; i < cnt; i++) {
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];
158 /* sort candidates */
159 [candidates sortUsingFunction:(void *)candidateSort context:self];
164 /* representations */
166 @end /* NGRuleModel */