forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsRuleNetwork.h
899 lines (723 loc) · 29.1 KB
/
nsRuleNetwork.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Waterson <waterson@netscape.com>
* Neil Deakin <enndeakin@sympatico.ca>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
A rule discrimination network implementation based on ideas from
RETE and TREAT.
RETE is described in Charles Forgy, "Rete: A Fast Algorithm for the
Many Patterns/Many Objects Match Problem", Artificial Intelligence
19(1): pp. 17-37, 1982.
TREAT is described in Daniel P. Miranker, "TREAT: A Better Match
Algorithm for AI Production System Matching", AAAI 1987: pp. 42-47.
--
TO DO:
. nsAssignmentSet::List objects are allocated by the gallon. We
should make it so that these are always allocated from a pool,
maybe owned by the nsRuleNetwork?
*/
#ifndef nsRuleNetwork_h__
#define nsRuleNetwork_h__
#include "nsCOMPtr.h"
#include "nsCOMArray.h"
#include "nsFixedSizeAllocator.h"
#include "nsIAtom.h"
#include "nsIContent.h"
#include "nsIDOMNode.h"
#include "plhash.h"
#include "pldhash.h"
#include "nsCRT.h"
#include "nsIRDFNode.h"
class nsIRDFResource;
class nsXULTemplateResultSetRDF;
class nsXULTemplateQueryProcessorRDF;
//----------------------------------------------------------------------
/**
* A memory element that supports an instantiation. A memory element holds a
* set of nodes involved in an RDF test such as <member> or <triple> test. A
* memory element is created when a specific test matches. The query processor
* maintains a map between the memory elements and the results they eventually
* matched. When an assertion is removed from the graph, this map is consulted
* to determine which results will no longer match.
*/
class MemoryElement {
protected:
MemoryElement() { MOZ_COUNT_CTOR(MemoryElement); }
virtual ~MemoryElement() { MOZ_COUNT_DTOR(MemoryElement); }
public:
static bool Init();
static bool gPoolInited;
static nsFixedSizeAllocator gPool;
virtual void Destroy() = 0;
virtual const char* Type() const = 0;
virtual PLHashNumber Hash() const = 0;
virtual bool Equals(const MemoryElement& aElement) const = 0;
bool operator==(const MemoryElement& aMemoryElement) const {
return Equals(aMemoryElement);
}
bool operator!=(const MemoryElement& aMemoryElement) const {
return !Equals(aMemoryElement);
}
};
//----------------------------------------------------------------------
/**
* A collection of memory elements
*/
class MemoryElementSet {
public:
class ConstIterator;
friend class ConstIterator;
protected:
class List {
public:
List() { MOZ_COUNT_CTOR(MemoryElementSet::List); }
~List() {
MOZ_COUNT_DTOR(MemoryElementSet::List);
mElement->Destroy();
NS_IF_RELEASE(mNext); }
PRInt32 AddRef() { return ++mRefCnt; }
PRInt32 Release() {
PRInt32 refcnt = --mRefCnt;
if (refcnt == 0) delete this;
return refcnt; }
MemoryElement* mElement;
PRInt32 mRefCnt;
List* mNext;
};
List* mElements;
public:
MemoryElementSet() : mElements(nsnull) {
MOZ_COUNT_CTOR(MemoryElementSet); }
MemoryElementSet(const MemoryElementSet& aSet) : mElements(aSet.mElements) {
MOZ_COUNT_CTOR(MemoryElementSet);
NS_IF_ADDREF(mElements); }
MemoryElementSet& operator=(const MemoryElementSet& aSet) {
NS_IF_RELEASE(mElements);
mElements = aSet.mElements;
NS_IF_ADDREF(mElements);
return *this; }
~MemoryElementSet() {
MOZ_COUNT_DTOR(MemoryElementSet);
NS_IF_RELEASE(mElements); }
public:
class ConstIterator {
public:
ConstIterator(List* aElementList) : mCurrent(aElementList) {
NS_IF_ADDREF(mCurrent); }
ConstIterator(const ConstIterator& aConstIterator)
: mCurrent(aConstIterator.mCurrent) {
NS_IF_ADDREF(mCurrent); }
ConstIterator& operator=(const ConstIterator& aConstIterator) {
NS_IF_RELEASE(mCurrent);
mCurrent = aConstIterator.mCurrent;
NS_IF_ADDREF(mCurrent);
return *this; }
~ConstIterator() { NS_IF_RELEASE(mCurrent); }
ConstIterator& operator++() {
List* next = mCurrent->mNext;
NS_RELEASE(mCurrent);
mCurrent = next;
NS_IF_ADDREF(mCurrent);
return *this; }
ConstIterator operator++(int) {
ConstIterator result(*this);
List* next = mCurrent->mNext;
NS_RELEASE(mCurrent);
mCurrent = next;
NS_IF_ADDREF(mCurrent);
return result; }
const MemoryElement& operator*() const {
return *mCurrent->mElement; }
const MemoryElement* operator->() const {
return mCurrent->mElement; }
bool operator==(const ConstIterator& aConstIterator) const {
return mCurrent == aConstIterator.mCurrent; }
bool operator!=(const ConstIterator& aConstIterator) const {
return mCurrent != aConstIterator.mCurrent; }
protected:
List* mCurrent;
};
ConstIterator First() const { return ConstIterator(mElements); }
ConstIterator Last() const { return ConstIterator(nsnull); }
// N.B. that the set assumes ownership of the element
nsresult Add(MemoryElement* aElement);
};
//----------------------------------------------------------------------
/**
* An assignment of a value to a variable
*/
class nsAssignment {
public:
const nsCOMPtr<nsIAtom> mVariable;
nsCOMPtr<nsIRDFNode> mValue;
nsAssignment(nsIAtom* aVariable, nsIRDFNode* aValue)
: mVariable(aVariable),
mValue(aValue)
{ MOZ_COUNT_CTOR(nsAssignment); }
nsAssignment(const nsAssignment& aAssignment)
: mVariable(aAssignment.mVariable),
mValue(aAssignment.mValue)
{ MOZ_COUNT_CTOR(nsAssignment); }
~nsAssignment() { MOZ_COUNT_DTOR(nsAssignment); }
bool operator==(const nsAssignment& aAssignment) const {
return mVariable == aAssignment.mVariable && mValue == aAssignment.mValue; }
bool operator!=(const nsAssignment& aAssignment) const {
return mVariable != aAssignment.mVariable || mValue != aAssignment.mValue; }
PLHashNumber Hash() const {
// XXX I have no idea if this hashing function is good or not // XXX change this
PLHashNumber temp = PLHashNumber(NS_PTR_TO_INT32(mValue.get())) >> 2; // strip alignment bits
return (temp & 0xffff) | NS_PTR_TO_INT32(mVariable.get()); }
};
//----------------------------------------------------------------------
/**
* A collection of value-to-variable assignments that minimizes
* copying by sharing subsets when possible.
*/
class nsAssignmentSet {
public:
class ConstIterator;
friend class ConstIterator;
protected:
class List {
public:
List(const nsAssignment &aAssignment) : mAssignment(aAssignment) {
MOZ_COUNT_CTOR(nsAssignmentSet::List); }
~List() {
MOZ_COUNT_DTOR(nsAssignmentSet::List);
NS_IF_RELEASE(mNext); }
PRInt32 AddRef() { return ++mRefCnt; }
PRInt32 Release() {
PRInt32 refcnt = --mRefCnt;
if (refcnt == 0) delete this;
return refcnt; }
nsAssignment mAssignment;
PRInt32 mRefCnt;
List* mNext;
};
List* mAssignments;
public:
nsAssignmentSet()
: mAssignments(nsnull)
{ MOZ_COUNT_CTOR(nsAssignmentSet); }
nsAssignmentSet(const nsAssignmentSet& aSet)
: mAssignments(aSet.mAssignments) {
MOZ_COUNT_CTOR(nsAssignmentSet);
NS_IF_ADDREF(mAssignments); }
nsAssignmentSet& operator=(const nsAssignmentSet& aSet) {
NS_IF_RELEASE(mAssignments);
mAssignments = aSet.mAssignments;
NS_IF_ADDREF(mAssignments);
return *this; }
~nsAssignmentSet() {
MOZ_COUNT_DTOR(nsAssignmentSet);
NS_IF_RELEASE(mAssignments); }
public:
class ConstIterator {
public:
ConstIterator(List* aAssignmentList) : mCurrent(aAssignmentList) {
NS_IF_ADDREF(mCurrent); }
ConstIterator(const ConstIterator& aConstIterator)
: mCurrent(aConstIterator.mCurrent) {
NS_IF_ADDREF(mCurrent); }
ConstIterator& operator=(const ConstIterator& aConstIterator) {
NS_IF_RELEASE(mCurrent);
mCurrent = aConstIterator.mCurrent;
NS_IF_ADDREF(mCurrent);
return *this; }
~ConstIterator() { NS_IF_RELEASE(mCurrent); }
ConstIterator& operator++() {
List* next = mCurrent->mNext;
NS_RELEASE(mCurrent);
mCurrent = next;
NS_IF_ADDREF(mCurrent);
return *this; }
ConstIterator operator++(int) {
ConstIterator result(*this);
List* next = mCurrent->mNext;
NS_RELEASE(mCurrent);
mCurrent = next;
NS_IF_ADDREF(mCurrent);
return result; }
const nsAssignment& operator*() const {
return mCurrent->mAssignment; }
const nsAssignment* operator->() const {
return &mCurrent->mAssignment; }
bool operator==(const ConstIterator& aConstIterator) const {
return mCurrent == aConstIterator.mCurrent; }
bool operator!=(const ConstIterator& aConstIterator) const {
return mCurrent != aConstIterator.mCurrent; }
protected:
List* mCurrent;
};
ConstIterator First() const { return ConstIterator(mAssignments); }
ConstIterator Last() const { return ConstIterator(nsnull); }
public:
/**
* Add an assignment to the set
* @param aElement the assigment to add
* @return NS_OK if all is well, NS_ERROR_OUT_OF_MEMORY if memory
* could not be allocated for the addition.
*/
nsresult Add(const nsAssignment& aElement);
/**
* Determine if the assignment set contains the specified variable
* to value assignment.
* @param aVariable the variable for which to lookup the binding
* @param aValue the value to query
* @return true if aVariable is bound to aValue; false otherwise.
*/
bool HasAssignment(nsIAtom* aVariable, nsIRDFNode* aValue) const;
/**
* Determine if the assignment set contains the specified assignment
* @param aAssignment the assignment to search for
* @return true if the set contains the assignment, false otherwise.
*/
bool HasAssignment(const nsAssignment& aAssignment) const {
return HasAssignment(aAssignment.mVariable, aAssignment.mValue); }
/**
* Determine whether the assignment set has an assignment for the
* specified variable.
* @param aVariable the variable to query
* @return true if the assignment set has an assignment for the variable,
* false otherwise.
*/
bool HasAssignmentFor(nsIAtom* aVariable) const;
/**
* Retrieve the assignment for the specified variable
* @param aVariable the variable to query
* @param aValue an out parameter that will receive the value assigned
* to the variable, if any.
* @return true if the variable has an assignment, false
* if there was no assignment for the variable.
*/
bool GetAssignmentFor(nsIAtom* aVariable, nsIRDFNode** aValue) const;
/**
* Count the number of assignments in the set
* @return the number of assignments in the set
*/
PRInt32 Count() const;
/**
* Determine if the set is empty
* @return true if the assignment set is empty, false otherwise.
*/
bool IsEmpty() const { return mAssignments == nsnull; }
bool Equals(const nsAssignmentSet& aSet) const;
bool operator==(const nsAssignmentSet& aSet) const { return Equals(aSet); }
bool operator!=(const nsAssignmentSet& aSet) const { return !Equals(aSet); }
};
//----------------------------------------------------------------------
/**
* A collection of variable-to-value bindings, with the memory elements
* that support those bindings. Essentially, an instantiation is the
* collection of variables and values assigned to those variables for a single
* result. For each RDF rule in the rule network, each instantiation is
* examined and either extended with additional bindings specified by the RDF
* rule, or removed if the rule doesn't apply (for instance if a node has no
* children). When an instantiation gets to the last node of the rule network,
* which is always an nsInstantiationNode, a result is created for it.
*
* An instantiation object is typically created by "extending" another
* instantiation object. That is, using the copy constructor, and
* adding bindings and support to the instantiation.
*/
class Instantiation
{
public:
/**
* The variable-to-value bindings
*/
nsAssignmentSet mAssignments;
/**
* The memory elements that support the bindings.
*/
MemoryElementSet mSupport;
Instantiation() { MOZ_COUNT_CTOR(Instantiation); }
Instantiation(const Instantiation& aInstantiation)
: mAssignments(aInstantiation.mAssignments),
mSupport(aInstantiation.mSupport) {
MOZ_COUNT_CTOR(Instantiation); }
Instantiation& operator=(const Instantiation& aInstantiation) {
mAssignments = aInstantiation.mAssignments;
mSupport = aInstantiation.mSupport;
return *this; }
~Instantiation() { MOZ_COUNT_DTOR(Instantiation); }
/**
* Add the specified variable-to-value assignment to the instantiation's
* set of assignments.
* @param aVariable the variable to which is being assigned
* @param aValue the value that is being assigned
* @return NS_OK if no errors, NS_ERROR_OUT_OF_MEMORY if there
* is not enough memory to perform the operation
*/
nsresult AddAssignment(nsIAtom* aVariable, nsIRDFNode* aValue) {
mAssignments.Add(nsAssignment(aVariable, aValue));
return NS_OK; }
/**
* Add a memory element to the set of memory elements that are
* supporting the instantiation
* @param aMemoryElement the memory element to add to the
* instantiation's set of support
* @return NS_OK if no errors occurred, NS_ERROR_OUT_OF_MEMORY
* if there is not enough memory to perform the operation.
*/
nsresult AddSupportingElement(MemoryElement* aMemoryElement) {
mSupport.Add(aMemoryElement);
return NS_OK; }
bool Equals(const Instantiation& aInstantiation) const {
return mAssignments == aInstantiation.mAssignments; }
bool operator==(const Instantiation& aInstantiation) const {
return Equals(aInstantiation); }
bool operator!=(const Instantiation& aInstantiation) const {
return !Equals(aInstantiation); }
static PLHashNumber Hash(const void* aKey);
static PRIntn Compare(const void* aLeft, const void* aRight);
};
//----------------------------------------------------------------------
/**
* A collection of intantiations
*/
class InstantiationSet
{
public:
InstantiationSet();
InstantiationSet(const InstantiationSet& aInstantiationSet);
InstantiationSet& operator=(const InstantiationSet& aInstantiationSet);
~InstantiationSet() {
MOZ_COUNT_DTOR(InstantiationSet);
Clear(); }
class ConstIterator;
friend class ConstIterator;
class Iterator;
friend class Iterator;
friend class nsXULTemplateResultSetRDF; // so it can get to the List
protected:
class List {
public:
Instantiation mInstantiation;
List* mNext;
List* mPrev;
List() { MOZ_COUNT_CTOR(InstantiationSet::List); }
~List() { MOZ_COUNT_DTOR(InstantiationSet::List); }
};
List mHead;
public:
class ConstIterator {
protected:
friend class Iterator; // XXXwaterson so broken.
List* mCurrent;
public:
ConstIterator(List* aList) : mCurrent(aList) {}
ConstIterator(const ConstIterator& aConstIterator)
: mCurrent(aConstIterator.mCurrent) {}
ConstIterator& operator=(const ConstIterator& aConstIterator) {
mCurrent = aConstIterator.mCurrent;
return *this; }
ConstIterator& operator++() {
mCurrent = mCurrent->mNext;
return *this; }
ConstIterator operator++(int) {
ConstIterator result(*this);
mCurrent = mCurrent->mNext;
return result; }
ConstIterator& operator--() {
mCurrent = mCurrent->mPrev;
return *this; }
ConstIterator operator--(int) {
ConstIterator result(*this);
mCurrent = mCurrent->mPrev;
return result; }
const Instantiation& operator*() const {
return mCurrent->mInstantiation; }
const Instantiation* operator->() const {
return &mCurrent->mInstantiation; }
bool operator==(const ConstIterator& aConstIterator) const {
return mCurrent == aConstIterator.mCurrent; }
bool operator!=(const ConstIterator& aConstIterator) const {
return mCurrent != aConstIterator.mCurrent; }
};
ConstIterator First() const { return ConstIterator(mHead.mNext); }
ConstIterator Last() const { return ConstIterator(const_cast<List*>(&mHead)); }
class Iterator : public ConstIterator {
public:
Iterator(List* aList) : ConstIterator(aList) {}
Iterator& operator++() {
mCurrent = mCurrent->mNext;
return *this; }
Iterator operator++(int) {
Iterator result(*this);
mCurrent = mCurrent->mNext;
return result; }
Iterator& operator--() {
mCurrent = mCurrent->mPrev;
return *this; }
Iterator operator--(int) {
Iterator result(*this);
mCurrent = mCurrent->mPrev;
return result; }
Instantiation& operator*() const {
return mCurrent->mInstantiation; }
Instantiation* operator->() const {
return &mCurrent->mInstantiation; }
bool operator==(const ConstIterator& aConstIterator) const {
return mCurrent == aConstIterator.mCurrent; }
bool operator!=(const ConstIterator& aConstIterator) const {
return mCurrent != aConstIterator.mCurrent; }
friend class InstantiationSet;
};
Iterator First() { return Iterator(mHead.mNext); }
Iterator Last() { return Iterator(&mHead); }
bool Empty() const { return First() == Last(); }
Iterator Append(const Instantiation& aInstantiation) {
return Insert(Last(), aInstantiation); }
Iterator Insert(Iterator aBefore, const Instantiation& aInstantiation);
Iterator Erase(Iterator aElement);
void Clear();
bool HasAssignmentFor(nsIAtom* aVariable) const;
};
//----------------------------------------------------------------------
/**
* A abstract base class for all nodes in the rule network
*/
class ReteNode
{
public:
ReteNode() {}
virtual ~ReteNode() {}
/**
* Propagate a set of instantiations "down" through the
* network. Each instantiation is a partial set of
* variable-to-value assignments, along with the memory elements
* that support it.
*
* The node must evaluate each instantiation, and either 1)
* extend it with additional assignments and memory-element
* support, or 2) remove it from the set because it is
* inconsistent with the constraints that this node applies.
*
* The node must then pass the resulting instantiation set along
* to any of its children in the network. (In other words, the
* node must recursively call Propagate() on its children. We
* should fix this to make the algorithm interruptable.)
*
* See TestNode::Propagate for details about instantiation set ownership
*
* @param aInstantiations the set of instantiations to propagate
* down through the network.
* @param aIsUpdate true if updating, false for first generation
* @param aTakenInstantiations true if the ownership over aInstantiations
* has been taken from the caller. If false,
* the caller owns it.
* @return NS_OK if no errors occurred.
*/
virtual nsresult Propagate(InstantiationSet& aInstantiations,
bool aIsUpdate, bool& aTakenInstantiations) = 0;
};
//----------------------------------------------------------------------
/**
* A collection of nodes in the rule network
*/
class ReteNodeSet
{
public:
ReteNodeSet();
~ReteNodeSet();
nsresult Add(ReteNode* aNode);
nsresult Clear();
class Iterator;
class ConstIterator {
public:
ConstIterator(ReteNode** aNode) : mCurrent(aNode) {}
ConstIterator(const ConstIterator& aConstIterator)
: mCurrent(aConstIterator.mCurrent) {}
ConstIterator& operator=(const ConstIterator& aConstIterator) {
mCurrent = aConstIterator.mCurrent;
return *this; }
ConstIterator& operator++() {
++mCurrent;
return *this; }
ConstIterator operator++(int) {
ConstIterator result(*this);
++mCurrent;
return result; }
const ReteNode* operator*() const {
return *mCurrent; }
const ReteNode* operator->() const {
return *mCurrent; }
bool operator==(const ConstIterator& aConstIterator) const {
return mCurrent == aConstIterator.mCurrent; }
bool operator!=(const ConstIterator& aConstIterator) const {
return mCurrent != aConstIterator.mCurrent; }
protected:
friend class Iterator; // XXXwaterson this is so wrong!
ReteNode** mCurrent;
};
ConstIterator First() const { return ConstIterator(mNodes); }
ConstIterator Last() const { return ConstIterator(mNodes + mCount); }
class Iterator : public ConstIterator {
public:
Iterator(ReteNode** aNode) : ConstIterator(aNode) {}
Iterator& operator++() {
++mCurrent;
return *this; }
Iterator operator++(int) {
Iterator result(*this);
++mCurrent;
return result; }
ReteNode* operator*() const {
return *mCurrent; }
ReteNode* operator->() const {
return *mCurrent; }
bool operator==(const ConstIterator& aConstIterator) const {
return mCurrent == aConstIterator.mCurrent; }
bool operator!=(const ConstIterator& aConstIterator) const {
return mCurrent != aConstIterator.mCurrent; }
};
Iterator First() { return Iterator(mNodes); }
Iterator Last() { return Iterator(mNodes + mCount); }
PRInt32 Count() const { return mCount; }
protected:
ReteNode** mNodes;
PRInt32 mCount;
PRInt32 mCapacity;
};
//----------------------------------------------------------------------
/**
* A node that applies a test condition to a set of instantiations.
*
* This class provides implementations of Propagate() and Constrain()
* in terms of one simple operation, FilterInstantiations(). A node
* that is a "simple test node" in a rule network should derive from
* this class, and need only implement FilterInstantiations().
*/
class TestNode : public ReteNode
{
public:
TestNode(TestNode* aParent);
/**
* Retrieve the test node's parent
* @return the test node's parent
*/
TestNode* GetParent() const { return mParent; }
/**
* Calls FilterInstantiations() on the instantiation set, and if
* the resulting set isn't empty, propagates the new set down to
* each of the test node's children.
*
* Note that the caller of Propagate is responsible for deleting
* aInstantiations if necessary as described below.
*
* Propagate may be called in update or non-update mode as indicated
* by the aIsUpdate argument. Non-update mode is used when initially
* generating results, whereas update mode is used when the datasource
* changes and new results might be available.
*
* The last node in a chain of TestNodes is always an nsInstantiationNode.
* In non-update mode, this nsInstantiationNode will cache the results
* in the query using the SetCachedResults method. The query processor
* takes these cached results and creates a nsXULTemplateResultSetRDF
* which is the enumeration returned to the template builder. This
* nsXULTemplateResultSetRDF owns the instantiations and they will be
* deleted when the nsXULTemplateResultSetRDF goes away.
*
* In update mode, the nsInstantiationNode node will iterate over the
* instantiations itself and callback to the builder to update any matches
* and generated content. If no instantiations match, then the builder
* will never be called.
*
* Thus, the difference between update and non-update modes is that in
* update mode, the results and instantiations have been already handled
* whereas in non-update mode they are expected to be returned in an
* nsXULTemplateResultSetRDF for further processing by the builder.
*
* Regardless, aTakenInstantiations will be set to true if the
* ownership over aInstantiations has been transferred to a result set.
* If set to false, the caller is still responsible for aInstantiations.
* aTakenInstantiations will be set properly even if an error occurs.
*/
virtual nsresult Propagate(InstantiationSet& aInstantiations,
bool aIsUpdate, bool& aTakenInstantiations);
/**
* This is called by a child node on its parent to allow the
* parent's constraints to apply to the set of instantiations.
*
* A node must iterate through the set of instantiations, and for
* each instantiation, either 1) extend the instantiation by
* adding variable-to-value assignments and memory element support
* for those assignments, or 2) remove the instantiation because
* it is inconsistent.
*
* The node must then pass the resulting set of instantiations up
* to its parent (by recursive call; we should make this iterative
* & interruptable at some point.)
*
* @param aInstantiations the set of instantiations that must
* be constrained
* @return NS_OK if no errors occurred
*/
virtual nsresult Constrain(InstantiationSet& aInstantiations);
/**
* Given a set of instantiations, filter out any that are
* inconsistent with the test node's test, and append
* variable-to-value assignments and memory element support for
* those which do pass the test node's test.
*
* @param aInstantiations the set of instantiations to be
* filtered
* @param aCantHandleYet [out] true if the instantiations do not contain
* enough information to constrain the data. May be null if this
* isn't important to the caller.
* @return NS_OK if no errors occurred.
*/
virtual nsresult FilterInstantiations(InstantiationSet& aInstantiations,
bool* aCantHandleYet) const = 0;
//XXX probably better named "ApplyConstraints" or "Discrminiate" or something
/**
* Add another node as a child of this node.
* @param aNode the node to add.
* @return NS_OK if no errors occur.
*/
nsresult AddChild(ReteNode* aNode) { return mKids.Add(aNode); }
/**
* Remove all the children of this node
* @return NS_OK if no errors occur.
*/
nsresult RemoveAllChildren() { return mKids.Clear(); }
protected:
TestNode* mParent;
ReteNodeSet mKids;
};
#endif // nsRuleNetwork_h__