|
| 1 | +--- |
| 2 | +source: https://soar.eecs.umich.edu/articles/articles/technical-documentation/198-basic-kernel-terminology |
| 3 | +date: 2014-10-07 |
| 4 | +authors: |
| 5 | + - soar |
| 6 | +tags: |
| 7 | + - kernel programming |
| 8 | +--- |
| 9 | + |
| 10 | +# Basic Kernel Terminology |
| 11 | + |
| 12 | +This is a document that defines some of the basic data structures, files and |
| 13 | +terminology used in the Soar kernel code. It is very incomplete but a good |
| 14 | +starting point for understanding the kernel. |
| 15 | + |
| 16 | +"But where can I start?" |
| 17 | + |
| 18 | +In a nutshell: The Soar Kernel is a very object-oriented, structured set of |
| 19 | +code. If you don't understand the basic Soar execution cycle and its phases, the |
| 20 | +source code won't help you. You should start by reading the introductory |
| 21 | +material in the [Soar Tutorials](../tutorials/soar_tutorial/index.md) |
| 22 | +that are bundled with the releases (in the Documents directory). Then read the |
| 23 | +first four chapters of the [Soar Manual](../soar_manual/index.md), |
| 24 | +"Introduction" thru "Learning" Basic code execution |
| 25 | + |
| 26 | +## Data Structures |
| 27 | + |
| 28 | +All of the structures are well-commented in the header files. In earlier |
| 29 | +versions of Soar (up thru 8.3.5), the single header file "soarkernel.h" defined |
| 30 | +all the common structures used throughout the code. I still find it the easiest |
| 31 | +place to search for information even though it's a very large file as code goes. |
| 32 | +**In 8.6. and later, the header file was separated by function into many smaller |
| 33 | +files which can be found in "Core/SoarKernel/include".** |
| 34 | + |
| 35 | +The `agent_struct` defined in agent.h includes all the system parameters, |
| 36 | +counters, variables, and pointers to all the other structures used by an agent |
| 37 | +(WMEs, productions, hash tables, memory_pools, formats, etc etc). It's a BIG |
| 38 | +structure, tough to read, and includes a lot of detailed comments. But if it |
| 39 | +isn't defined or allocated here, the agent doesn't know about it. |
| 40 | + |
| 41 | +Chances are you will never modify any structures in the rete, lexer, hash |
| 42 | +tables, or backtracing facilities, but you should know that they exist. If you |
| 43 | +do start to muck with these structures, you better know what you are doing. |
| 44 | +Changes here can greatly impact performance and even whether or not Soar will |
| 45 | +run properly. Several structures require their members to be defined in specific |
| 46 | +order and are commented appropriately. |
| 47 | + |
| 48 | +Structures that you should familiarize yourself with are symbols (a typedef'd |
| 49 | +union!!), wmes, productions, instantiations, preferences, and (eventually) |
| 50 | +memory pools. |
| 51 | + |
| 52 | +- symbol_struct is in symtab.h, everything in soar boils down to |
| 53 | + some kind of symbol. See Development/ Symbol for details. |
| 54 | +- wme_struct is in wmem.h, defines the working memory elements of an agent |
| 55 | +- production_struct is in production.h, these are the productions as loaded into |
| 56 | + Soar and stored in the rete. instantiations are in instantiation.h, these store |
| 57 | + the bindings of productions as they are matched in the rete. Instantiations |
| 58 | + whose conditions all |
| 59 | +- match are fired during specific phases - meaning their actions are executed: |
| 60 | + preferences are asserted to create wmes, and/or RHS actions are executed. |
| 61 | +- preference_struct is defined in gdatastructs.h store a pointer to the |
| 62 | + instantiation that created them, and when the instantiation no longer matches, |
| 63 | + the preference is retracted. |
| 64 | + |
| 65 | +## I want to add a new link! |
| 66 | + |
| 67 | +Existing links include input/output and reward links. Instructions on how to |
| 68 | +make your own are [HowTo: IO and reward links](../how_to/IOAndRewardLinks.md). |
| 69 | + |
| 70 | +## Things to add |
| 71 | + |
| 72 | +A lot of topics are in the [Soar FAQ](../how_to/SoarTechnicalFAQ.md) |
| 73 | + |
| 74 | +- Basic structure of how critical code works (e.g. decision procedure is a big |
| 75 | + switch statement, how printing/XML generation works) |
| 76 | +- Locations of critical code (e.g. decision procedure, preference procedure, scheduler) |
| 77 | +- Union basics (most people don't know what unions are) |
| 78 | + - see Kernigan and Ritchie |
| 79 | + - Unions are a data structure that can have multiple data-dependent ways of |
| 80 | + storing and accessing information, much like (but better than) overloading |
| 81 | + methods in C++. |
| 82 | +- Explain how sysparams work (e.g. how they are set/used, how to add a new one) |
| 83 | +- sysparams are just an array of agent parameters that store settings for |
| 84 | + runtime switches. Most of the sysparams are either True/False, or can take on |
| 85 | + some enum value. Setting a sysparam is easy, cf. init_soar.c for |
| 86 | + initializing and setting values. Search the code for "set_sysparams" to see |
| 87 | + examples. |
| 88 | +- To add a sysparam, see gsysparams.h (although that file MUST be renamed or |
| 89 | + folded into another header when gSKI removed). The code depends on looping over |
| 90 | + HIGHEST_SYSPARAM, so make sure it's always equal to the last one in the set of |
| 91 | + definitions. |
| 92 | +- When is it a sysparam, and when is it part of the agent struct? Depends what |
| 93 | + you are using it for, and whether it needs to be exposed for the user interface. |
| 94 | + If its a user-controlled setting, it should definitely be a sysparam. |
| 95 | +- What is a slot |
| 96 | + |
| 97 | +From John: |
| 98 | + |
| 99 | +> "We used to select more than just the operator (state, problem space, and goal) |
| 100 | +> and all together this was the context. Slots were for things that can be |
| 101 | +> selected, so there was a slot for each of those. Now there is just a slot for |
| 102 | +> the operator and although some of that language might have bled over to |
| 103 | +> selection of values for non-operator attributes. In general they are an out of |
| 104 | +> date terminology." |
| 105 | +
|
| 106 | +- Basics of how wme changes are done in parallel (i.e. explain do_buffered_wm_and_ownership_changes) |
| 107 | +- Difference between wme and input_wme (and any other wmes there might be) |
| 108 | +- Where/how to add new links (e.g. ep-mem link, RL link, etc) |
| 109 | +- Explain memory pool basics |
| 110 | + |
| 111 | +- Basics of bit manipulations that are used (unless this is rete-only, in which |
| 112 | + case don't bother) |
| 113 | + - I think the rete is the only place bit manipulations occur. Bit |
| 114 | + manipulations are extremely fast. If you can guarantee your raw data |
| 115 | + structure, you can shift registers instead of calling complex instructions |
| 116 | + to go very fast. Compilers hide this from you, but don't always know when |
| 117 | + they can optimize. |
| 118 | +- Explain transitive closure and tc_num |
| 119 | +- What all the Soar kernel STL-like data structures are (e.g. lists, hash |
| 120 | +tables, growable strings, others?) and how to use them. |
| 121 | +Ref counting (link to Tracking down memory leaks) |
| 122 | + |
| 123 | +## Some Definitions |
| 124 | + |
| 125 | +### slot |
| 126 | + |
| 127 | +From John Laird: |
| 128 | + |
| 129 | +> We used to select more than just the operator (state, problem space, and goal) |
| 130 | +> all together this was the context. Slots were for things that can be selected, |
| 131 | +> so there was a slot for each of those. Now there is just a slot for the |
| 132 | +> operator, although some of that language might have bled over to selection of |
| 133 | +> values for non-operator attributes. In general they are an out of date |
| 134 | +> terminology. |
| 135 | +
|
| 136 | +Slots are contained by identifiers, and hold all the preferences associated with |
| 137 | +the identifier, including acceptable wme preferences. Each identifier can have |
| 138 | +multiple slots, which can be accessed via the prev and next fields in the slot |
| 139 | +structure. Operator preferences are held incontext slots, which are identified |
| 140 | +by the isa_context_slot flag. |
| 141 | + |
| 142 | +### tc |
| 143 | + |
| 144 | +transitive closure |
| 145 | + |
| 146 | +### match goal |
| 147 | + |
| 148 | +The lowest goal (biggest number after the "S") that an instantiation of the LHS |
| 149 | +of a production matches on. Part of the instantiation structure. |
| 150 | + |
| 151 | +### potential |
| 152 | + |
| 153 | +Also referred to as _backtracing_. |
| 154 | + |
| 155 | +condition whose id is instantiated on a symbol linked from both the current goal |
| 156 | +and a higher goal, and is tested for in the production's LHS using a path from |
| 157 | +the current goal. |
| 158 | + |
| 159 | +For example, if the following wmes are in working memory |
| 160 | + |
| 161 | +```Soar |
| 162 | +(S1 ^foo F1) |
| 163 | +(S2 ^bar F1) |
| 164 | +(F1 ^baz B1) |
| 165 | +(S2 ^superstate S1) |
| 166 | +
|
| 167 | +and this production was backtraced through |
| 168 | +Code: |
| 169 | +sp {example |
| 170 | + (<s> ^bar <b>) |
| 171 | + (<b> ^baz <z>) |
| 172 | +--> |
| 173 | + ...} |
| 174 | +``` |
| 175 | + |
| 176 | +then `(<b> ^baz <z>)` would be a potential condition. |
| 177 | + |
| 178 | +### potential (life) |
| 179 | + |
| 180 | +abstract invented concept that actually has no real meaning. |
| 181 | + |
| 182 | +### tm |
| 183 | + |
| 184 | +temporary memory. I believe that any preference that is currently valid in Soar |
| 185 | +(either they are o-supported or the instantiation that generated them still |
| 186 | +matches) is in temporary memory. Once a preference is no longer valid, it is |
| 187 | +taken out of temporary memory (which involves setting the in_tm flag to false, |
| 188 | +and taking them off the preferences array and all_preferences lists on the slot |
| 189 | +they're on). | |
| 190 | + |
| 191 | +### clone (preference) |
| 192 | + |
| 193 | +a copy of a preference that is the result of a subgoal. While the inst pointer |
| 194 | +of the original preference points to the instantiation of the rule that fired in |
| 195 | +the subgoal to create the result, the inst pointer of the clone points to the |
| 196 | +newly created chunk or justification. Therefore, the preference and its clone |
| 197 | +exist on different match goals, and hence different match goal levels. |
| 198 | +| |
| 199 | + |
| 200 | +### instantiation |
| 201 | + |
| 202 | +a particular match of a production in working memory, and the preferences |
| 203 | +generated |
| 204 | +| |
| 205 | + |
| 206 | +### DEBUG_MEMORY |
| 207 | + |
| 208 | +if this flag is defined, the contents of freed memory locations in the memory |
| 209 | +pools are memset to `0xBB` |
0 commit comments