85 #include "hlp_overlay.h"
86 #include "hlp_controller.h"
87 #include "hlp_context.h"
90 using namespace r_code;
94 bool HLPOverlay::EvaluateBWDGuards(Controller *c, HLPBindingMap *bindings) {
96 HLPOverlay o(c, bindings);
97 return o.evaluate_bwd_guards();
103 return o.evaluate_fwd_timings();
109 return o.scan_bwd_guards();
112 HLPOverlay::HLPOverlay(Controller *c, HLPBindingMap *bindings) : Overlay(c, true), bindings_(bindings) {
115 HLPOverlay::HLPOverlay(Controller *c,
const HLPBindingMap *bindings,
bool load_code) : Overlay(c, load_code) {
117 bindings_ =
new HLPBindingMap((HLPBindingMap *)bindings);
120 HLPOverlay::~HLPOverlay() {
123 Atom *HLPOverlay::get_value_code(uint16
id)
const {
125 return bindings_->get_value_code(
id);
128 uint16 HLPOverlay::get_value_code_size(uint16
id)
const {
130 return bindings_->get_value_code_size(
id);
133 inline bool HLPOverlay::evaluate_guards(uint16 guard_set_iptr_index) {
135 uint16 guard_set_index = code_[guard_set_iptr_index].asIndex();
136 uint16 guard_count = code_[guard_set_index].getAtomCount();
137 for (uint16 i = 1; i <= guard_count; ++i) {
140 HLPContext c(code_, guard_set_index + i,
this);
143 if (c.dereference()[0].isBooleanFalse())
150 bool HLPOverlay::evaluate_fwd_guards() {
152 return evaluate_guards(HLP_FWD_GUARDS);
155 bool HLPOverlay::evaluate_bwd_guards() {
157 return evaluate_guards(HLP_BWD_GUARDS);
166 bool HLPOverlay::evaluate_fwd_timings() {
168 int16 fwd_after_guard_index = -1;
169 int16 fwd_before_guard_index = -1;
171 uint16 bm_fwd_after_index = bindings_->get_fwd_after_index();
172 uint16 bm_fwd_before_index = bindings_->get_fwd_before_index();
174 uint16 guard_set_index = code_[HLP_BWD_GUARDS].asIndex();
175 uint16 guard_count = code_[guard_set_index].getAtomCount();
176 for (uint16 i = 1; i <= guard_count; ++i) {
178 uint16 index = guard_set_index + i;
179 Atom a = code_[index];
180 if (a.getDescriptor() == Atom::ASSIGN_PTR) {
182 uint16 _i = a.asAssignmentIndex();
183 if (_i == bm_fwd_after_index)
184 fwd_after_guard_index = i;
185 if (_i == bm_fwd_before_index)
186 fwd_before_guard_index = i;
191 if (!bindings_->has_fwd_before()) {
193 if (fwd_before_guard_index == -1)
196 if (!
evaluate(guard_set_index + fwd_before_guard_index))
197 #if 1 // Debug: temporary solution to handle dependecies among guards. The full solution would recurse through the guards.
200 if (fwd_after_guard_index == -1)
203 if (!
evaluate(guard_set_index + fwd_after_guard_index))
206 if (!
evaluate(guard_set_index + fwd_before_guard_index))
214 if (!bindings_->has_fwd_after()) {
216 if (fwd_after_guard_index == -1)
219 if (!
evaluate(guard_set_index + fwd_after_guard_index))
226 bool HLPOverlay::scan_bwd_guards()
const {
228 uint16 guard_set_index = code_[HLP_BWD_GUARDS].asIndex();
229 uint16 guard_count = code_[guard_set_index].getAtomCount();
230 for (uint16 i = 1; i <= guard_count; ++i) {
232 uint16 index = guard_set_index + i;
233 Atom a = code_[index];
234 switch (a.getDescriptor()) {
236 if (!scan_location(a.asIndex(), index))
239 case Atom::ASSIGN_PTR:
241 if (!scan_location(a.asIndex(), index) && !bindings_->scan_variable(a.asAssignmentIndex()))
249 bool HLPOverlay::scan_location(uint16 index, uint16 parent_guard_index)
const {
251 Atom a = code_[index];
252 switch (a.getDescriptor()) {
254 return scan_location(a.asIndex(), parent_guard_index);
255 case Atom::ASSIGN_PTR:
256 return scan_location(a.asIndex(), parent_guard_index);
258 if (bindings_->scan_variable(a.asIndex()))
261 return scan_variable(a.asIndex(), parent_guard_index);
262 case Atom::OPERATOR: {
263 uint16 atom_count = a.getAtomCount();
264 for (uint16 j = 1; j <= atom_count; ++j) {
266 if (!scan_location(index + j, parent_guard_index))
276 bool HLPOverlay::scan_variable(uint16 index, uint16 parent_guard_index)
const {
278 uint16 guard_set_index = code_[HLP_BWD_GUARDS].asIndex();
279 uint16 guard_count = code_[guard_set_index].getAtomCount();
280 for (uint16 i = 1; i <= guard_count; ++i) {
282 uint16 guard_index = guard_set_index + i;
283 Atom a = code_[guard_index];
284 switch (a.getDescriptor()) {
285 case Atom::ASSIGN_PTR:
286 if (a.asAssignmentIndex() == index) {
287 if (guard_index == parent_guard_index)
290 return scan_location(a.asIndex(), guard_index);
299 Code *HLPOverlay::get_unpacked_object()
const {
301 return ((HLPController *)controller_)->get_unpacked_object();
304 void HLPOverlay::store_evidence(_Fact *evidence,
bool prediction,
bool is_simulation) {
309 ((HLPController *)controller_)->store_predicted_evidence(evidence);
311 ((HLPController *)controller_)->store_evidence(evidence);