initial
[prop.git] / include / AD / rete / newrete.h
blob6b77573be7c55f0b9db8e05759b45b5248461ff5
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are welcomed to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef RETE_matching_network_h
26 #define RETE_matching_network_h
28 #include <AD/rete/fact.h>
29 #include <AD/memory/mem.h>
31 //////////////////////////////////////////////////////////////////////////////
32 // Class Rete represents a RETE matching network.
33 //////////////////////////////////////////////////////////////////////////////
35 class Rete {
37 Rete(const Rete&); // no copy constructor
38 void operator = (const Rete&); // no assignment
40 public:
42 ///////////////////////////////////////////////////////////////////////////
43 // Various types.
44 ///////////////////////////////////////////////////////////////////////////
45 typedef int NodeId; // node number
46 typedef int RuleId; // rule number
47 typedef int Priority; // rule priority
48 typedef Fact * Token; // a token is an array of facts
49 typedef int (*Join)(const Fact * []); // join test
51 ///////////////////////////////////////////////////////////////////////////
52 // A network node.
53 ///////////////////////////////////////////////////////////////////////////
54 struct Node {
55 enum { And, Not, Bot } kind; // kind of node
56 NodeId child; // child node (or rule id)
57 Join join; // join test (if any)
58 int left_arity; // length of token formed so far.
59 int arity; // length of the whole token.
62 protected:
64 ///////////////////////////////////////////////////////////////////////////
65 // Internal data structure
66 ///////////////////////////////////////////////////////////////////////////
67 Mem& mem; // internal memory manager
68 int number_of_nodes; // network nodes count
69 const Node * network; // the network in array form
70 class AlphaMemory * alpha_mem; // alpha memory in array form
71 class BetaMemory * beta_mem; // beta memory in array form
73 public:
75 ///////////////////////////////////////////////////////////////////////////
76 // Constructor and destructor
77 ///////////////////////////////////////////////////////////////////////////
78 Rete(int N, const Node net[]); // create the network
79 virtual ~Rete(); // destroy the network
81 ///////////////////////////////////////////////////////////////////////////
82 // Method to clears the method of facts and alpha/beta memories.
83 ///////////////////////////////////////////////////////////////////////////
84 virtual void clear();
86 ///////////////////////////////////////////////////////////////////////////
87 // Methods to assert and retract facts from the network
88 ///////////////////////////////////////////////////////////////////////////
89 inline void assert_fact (Fact & fact) { fact.assert_fact(*this); }
90 inline void assert_fact (Fact * fact) { fact->assert_fact(*this); }
91 inline void retract_fact (Fact & fact) { fact.retract_fact(*this); }
92 inline void retract_fact (Fact * fact) { fact->retract_fact(*this); }
94 ///////////////////////////////////////////////////////////////////////////
95 // Methods to insert tokens into the left and right sides of the net.
96 ///////////////////////////////////////////////////////////////////////////
97 void insert_left (NodeId, Fact * []);
98 void insert_right (NodeId, Fact *);
99 void remove_left (NodeId, Fact * []);
100 void remove_right (NodeId, Fact *);
102 ///////////////////////////////////////////////////////////////////////////
103 // Method to execute a rule, must be defined by derive class.
104 ///////////////////////////////////////////////////////////////////////////
105 virtual void fire (Fact * []) = 0;