1 //////////////////////////////////////////////////////////////////////////////
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
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
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
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 //////////////////////////////////////////////////////////////////////////////
37 Rete(const Rete
&); // no copy constructor
38 void operator = (const Rete
&); // no assignment
42 ///////////////////////////////////////////////////////////////////////////
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 ///////////////////////////////////////////////////////////////////////////
53 ///////////////////////////////////////////////////////////////////////////
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.
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
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 ///////////////////////////////////////////////////////////////////////////
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;