Soar Kernel  9.3.2 08-06-12
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rete.h
Go to the documentation of this file.
1 /*************************************************************************
2  * PLEASE SEE THE FILE "license.txt" (INCLUDED WITH THIS SOFTWARE PACKAGE)
3  * FOR LICENSE AND COPYRIGHT INFORMATION.
4  *************************************************************************/
5 
6 /* =======================================================================
7  rete.h
8 
9  All_wmes_in_rete is the header for a dll of all the wmes currently
10  in the rete. (This is normally equal to all of WM, except at times
11  when WM changes have been buffered but not yet done.) The wmes
12  are linked via their "rete_next" and "rete_prev" fields.
13  Num_wmes_in_rete counts how many wmes there are in the rete.
14 
15  Init_rete() initializes the rete. It should be called at startup time.
16 
17  Any_assertions_or_retractions_ready() returns TRUE iff there are any
18  pending changes to the match set. This is used to test for quiescence.
19  Get_next_assertion() retrieves a pending assertion (returning TRUE) or
20  returns FALSE is no more are available. Get_next_retraction() is
21  similar.
22 
23  Add_production_to_rete() adds a given production, with a given LHS,
24  to the rete. If "refracted_inst" is non-NIL, it should point to an
25  initial instantiation of the production. This routine returns one
26  of NO_REFRACTED_INST, REFRACTED_INST_MATCHED, etc. (see below).
27  Excise_production_from_rete() removes the given production from the
28  rete, and enqueues all its existing instantiations as pending
29  retractions.
30 
31  Add_wme_to_rete() and remove_wme_from_rete() inform the rete of changes
32  to WM.
33 
34  P_node_to_conditions_and_nots() takes a p_node and (optionally) a
35  token/wme pair, and reconstructs the (optionally instantiated) LHS
36  for the production. The firer uses this to build the instantiated
37  conditions; the printer uses it to reconstruct the LHS for printing.
38  Get_symbol_from_rete_loc() takes a token/wme pair and a location
39  specification (levels_up/field_num), examines the match (token/wme),
40  and returns the symbol at that location. The firer uses this for
41  resolving references in RHS actions to variables bound on the LHS.
42 
43  Count_rete_tokens_for_production() returns a count of the number of
44  tokens currently in use for the given production.
45 
46  Print_partial_match_information(), print_match_set(), and
47  print_rete_statistics() do printouts for various interface routines.
48 
49  Save_rete_net() and load_rete_net() are used for the fastsave/load
50  commands. They save/load everything to/from the given (already open)
51  files. They return TRUE if successful, FALSE if any error occurred.
52 ======================================================================= */
53 
54 #ifndef RETE_H
55 #define RETE_H
56 
57 #include <stdio.h> // Needed for FILE token below
58 
59 #ifdef __cplusplus
60 //extern "C"
61 //{
62 #endif
63 
64 struct not_struct;
65 
66 typedef char Bool;
67 typedef unsigned char byte;
72 typedef struct condition_struct condition;
73 typedef struct action_struct action;
74 typedef struct wme_struct wme;
75 typedef struct rete_node_struct rete_node;
76 typedef struct agent_struct agent;
77 typedef union symbol_union Symbol;
78 
79 typedef struct token_struct {
80  /* --- Note: "parent" is NIL on negative node negrm (local join result)
81  tokens, non-NIL on all other tokens including CN and CN_P stuff.
82  I put "parent" at offset 0 in the structure, so that upward scans
83  are fast (saves doing an extra integer addition in the inner loop) --- */
85  union token_a_union {
87  struct token_struct *next_in_bucket, *prev_in_bucket; /*hash bucket dll*/
88  Symbol *referent; /* referent of the hash test (thing we hashed on) */
89  } ht;
91  struct token_struct *next_negrm, *prev_negrm;/*other local join results*/
92  struct token_struct *left_token; /* token this is local join result for*/
93  } neg;
94  } a;
96  wme *w;
97  struct token_struct *first_child; /* first of dll of children */
98  struct token_struct *next_sibling, *prev_sibling; /* for dll of children */
99  struct token_struct *next_of_node, *prev_of_node; /* dll of tokens at node */
100  struct token_struct *next_from_wme, *prev_from_wme; /* tree-based remove */
101  struct token_struct *negrm_tokens; /* join results: for Neg, CN nodes only */
102 } token;
103 
104 extern void init_rete (agent* thisAgent);
105 
107 extern Bool postpone_assertion (agent* thisAgent, production **prod, struct token_struct **tok, wme **w);
108 extern void consume_last_postponed_assertion(agent* thisAgent);
109 extern void restore_postponed_assertions (agent* thisAgent);
110 extern Bool get_next_retraction (agent* thisAgent, struct instantiation_struct **inst);
111 /* REW: begin 08.20.97 */
112 /* Special routine for retractions in removed goals. See note in rete.cpp */
113 extern Bool get_next_nil_goal_retraction (agent* thisAgent, struct instantiation_struct **inst);
114 /* REW: end 08.20.97 */
115 
116 #define NO_REFRACTED_INST 0 /* no refracted inst. was given */
117 #define REFRACTED_INST_MATCHED 1 /* there was a match for the inst. */
118 #define REFRACTED_INST_DID_NOT_MATCH 2 /* there was no match for it */
119 #define DUPLICATE_PRODUCTION 3 /* the prod. was a duplicate */
120 extern byte add_production_to_rete (agent* thisAgent, production *p, condition *lhs_top,
121  instantiation *refracted_inst,
122  Bool warn_on_duplicates, Bool ignore_rhs = false);
123 extern void excise_production_from_rete (agent* thisAgent, production *p);
124 
125 extern void add_wme_to_rete (agent* thisAgent, wme *w);
126 extern void remove_wme_from_rete (agent* thisAgent, wme *w);
127 
128 extern void p_node_to_conditions_and_nots (agent* thisAgent,
129  struct rete_node_struct *p_node,
130  struct token_struct *tok,
131  wme *w,
132  condition **dest_top_cond,
133  condition **dest_bottom_cond,
134  not_struct **dest_nots,
135  action **dest_rhs);
136 extern Symbol *get_symbol_from_rete_loc (unsigned short levels_up,
137  byte field_num,
138  struct token_struct *tok, wme *w);
139 
140 extern uint64_t count_rete_tokens_for_production (agent* thisAgent, production *prod);
141 extern void print_partial_match_information (agent* thisAgent, struct rete_node_struct *p_node,
142  wme_trace_type wtt);
143 extern void xml_partial_match_information (agent* thisAgent, rete_node *p_node, wme_trace_type wtt) ;
144 
145 extern void print_match_set (agent* thisAgent, wme_trace_type wtt, ms_trace_type mst);
146 extern void xml_match_set (agent* thisAgent, wme_trace_type wtt, ms_trace_type mst);
147 extern void get_all_node_count_stats (agent* thisAgent);
148 extern int get_node_count_statistic (agent* thisAgent, char * node_type_name,
149  char * column_name,
150  uint64_t * result);
151 
152 extern Bool save_rete_net (agent* thisAgent, FILE *dest_file, Bool use_rete_net_64);
153 extern Bool load_rete_net (agent* thisAgent, FILE *source_file);
154 
155 #ifdef __cplusplus
156 //}
157 #endif
158 
159 #endif