IIIM-IS / AERA

Other
12 stars 4 forks source link

HLPBindingMap: In init_from_hlp, add packed_hlp and use it to get vars #260

Closed jefft0 closed 1 year ago

jefft0 commented 1 year ago

Background: When a new cst or model is injected, a controller is created for it. The controller has a "binding map template" which is simply a binding map with the same number of variables as the cst or model. The controller's binding map template is set up by HLPBindingMap::init_from_hlp which scans the code of the cst or model looking for variables. Consider this cst (simplified):

(cst [] []
   (fact (mk.val v0: holding []) v1: v2:)
   (fact (icst cst_same_pos [] [v0: v3: v4: sphere]) v1: v2:))

It has variables v0 through v4 so the binding map template should have these five variables.

More background: A cst or mdl has a "packed" and "unpacked" version. The packed version has all the code in one array similar to the example shown above. There can be external references such as cst_same_pos, but the relevant code is all together. This is used for evaluating guards, etc. The unpacked version is much smaller and has external references to each of the components, such as the two facts in the example above. This is used, for example, to match with an input fact during forward or backward chaining.

Currently, HLPBindingMap::init_from_hlp scans the unpacked version. This means that it uses init_from_pattern to recurse into every reference, scanning for variables. Normally, this works OK but in this case it recurses into cst_same_pos which has variables v0 through v5 . (This happens because the constant sphere is used instead of a variable v5.) Therefore, the controller's binding map template is set up incorrectly.

This pull request updates HLPBindingMap::init_from_hlp to take the parameter packed_hlp and use the packed version to scan for variables. In this case, it does not need to recurse into referenced objects so we can remove the unused recursive method init_from_pattern. Since the packed version has exactly the needed variables, the controller's binding map template is correct.