88 #include "../r_code/utils.h"
89 #include "binding_map.h"
160 _Fact(uint16 opcode,
r_code::Code *
object, Timestamp after, Timestamp before, float32 confidence, float32 psln_thr);
162 static bool MatchAtom(
Atom lhs,
Atom rhs);
163 static bool MatchStructure(
const r_code::Code* lhs, uint16 lhs_base_index, uint16 lhs_index,
const r_code::Code* rhs, uint16 rhs_index,
bool same_binding_state =
false);
164 static bool Match(
const r_code::Code* lhs, uint16 lhs_base_index, uint16 lhs_index,
const r_code::Code* rhs, uint16 rhs_index, uint16 lhs_arity,
bool same_binding_state =
false);
174 bool is_invalidated()
override;
176 bool is_fact()
const {
return (code(0).asOpcode() == Opcodes::Fact); }
177 bool is_anti_fact()
const {
return (code(0).asOpcode() == Opcodes::AntiFact); }
178 void set_opposite() {
180 code(0) = Atom::Object(Opcodes::AntiFact, FACT_ARITY);
182 code(0) = Atom::Object(Opcodes::Fact, FACT_ARITY);
184 _Fact *get_absentee()
const;
186 bool match_timings_sync(
const _Fact *evidence)
const;
187 bool match_timings_overlap(
const _Fact *evidence)
const;
188 bool match_timings_inclusive(
const _Fact *evidence)
const;
190 MatchResult is_evidence(
const _Fact *target)
const;
191 MatchResult is_timeless_evidence(
const _Fact *target)
const;
193 bool has_after()
const {
return r_code::Utils::HasTimestamp<r_code::Code>(
this, FACT_AFTER); }
194 bool has_before()
const {
return r_code::Utils::HasTimestamp<r_code::Code>(
this, FACT_BEFORE); }
199 uint16 v_index = code(FACT_AFTER).asIndex();
200 if (code(v_index) == Atom::Object(Opcodes::Var, 1))
201 return (
int)code(v_index + 1).asFloat();
208 uint16 v_index = code(FACT_BEFORE).asIndex();
209 if (code(v_index) == Atom::Object(Opcodes::Var, 1))
210 return (
int)code(v_index + 1).asFloat();
213 Timestamp get_after()
const;
214 Timestamp get_before()
const;
215 float32 get_cfd()
const {
return code(FACT_CFD).asFloat(); }
217 void set_cfd(float32 cfd) { code(FACT_CFD) = Atom::Float(cfd); }
219 Pred *get_pred()
const {
220 Code *pred = get_reference(0);
221 if (pred->code(0).asOpcode() == Opcodes::Pred)
225 Goal *get_goal()
const {
226 Code *goal = get_reference(0);
227 if (goal->code(0).asOpcode() == Opcodes::Goal)
231 Success *get_success()
const {
232 Code *success = get_reference(0);
233 if (success->code(0).asOpcode() == Opcodes::Success)
234 return (Success *)success;
253 Sim(SimMode mode, std::chrono::microseconds thz,
Fact *super_goal,
bool opposite,
Controller *root, float32 psln_thr,
Controller *solution_controller = NULL, float32 solution_cfd = 0, Timestamp solution_before = r_code::Utils::GetTimeReference());
254 bool invalidate()
override;
255 bool is_invalidated()
override;
257 SimMode get_mode()
const {
return (SimMode)(int)code(SIM_MODE).asFloat(); }
259 std::chrono::microseconds get_thz()
const {
260 return r_code::Utils::GetDuration<Code>(
this, SIM_THZ);
282 Timestamp
get_solution_before()
const {
return r_code::Utils::GetTimestamp<Code>(
this, SIM_SOLUTION_BEFORE); }
287 void set_solution_before(Timestamp before) {
return r_code::Utils::SetTimestamp<Code>(
this, SIM_SOLUTION_BEFORE, before); }
303 bool register_goal_target(
_Fact* f_obj);
305 bool is_requirement_;
318 : promoted_fact_(NULL), original_fact_(NULL), defeasible_validity_(NULL)
328 : original_fact_(original_fact), promoted_fact_(promoted_fact), defeasible_validity_(defeasible_validity)
350 std::vector<P<_Fact> > already_signalled_;
353 std::vector<P<_Fact> > goalTargets_;
362 void *
operator new(
size_t s);
366 Fact(
r_code::Code *
object, Timestamp after, Timestamp before, float32 confidence, float32 psln_thr);
373 void *
operator new(
size_t s);
377 AntiFact(
r_code::Code *
object, Timestamp after, Timestamp before, float32 confidence, float32 psln_thr);
405 construct(target, simulations, psln_thr);
413 construct(target, std::vector<
P<Sim>>(), psln_thr);
422 std::vector<P<Sim>> simulations;
423 simulations.push_back(sim);
424 construct(target, simulations, psln_thr);
433 Pred(
_Fact *target,
const Pred* simulations_source, float32 psln_thr);
441 bool is_invalidated()
override;
442 bool grounds_invalidated(
_Fact *evidence);
444 _Fact *get_target()
const {
return (
_Fact *)get_reference(0); }
446 std::vector<P<_Fact> > grounds_;
448 std::set<P<DefeasibleValidity> > defeasible_validities_;
452 bool is_simulation()
const {
return get_simulations_size() > 0; }
459 auto sim_set_index = code(PRED_SIMS).asIndex();
460 return code(sim_set_index).getAtomCount();
469 auto sim_set_index = code(PRED_SIMS).asIndex();
470 auto atom = code(sim_set_index + i);
471 return (
Sim*)get_reference(atom.asIndex());
486 bool has_simulation(
Sim* sim)
const;
500 if (!defeasible_consequence_)
502 return defeasible_consequence_;
513 void construct(
_Fact *target,
const std::vector<
P<Sim> >& simulations, float32 psln_thr);
523 bool invalidate()
override;
524 bool is_invalidated()
override;
525 bool ground_invalidated(
_Fact *evidence);
527 bool is_requirement()
const;
529 bool is_self_goal()
const;
530 bool is_drive()
const {
return (!has_sim() && is_self_goal() && get_target()->get_reference(0)->code(0).asOpcode() == Opcodes::Ent); }
531 bool is_imdl_drive()
const {
532 return (!has_sim() && is_self_goal() && get_target()->get_reference(0)->code(0).asOpcode() == Opcodes::IMdl);
535 _Fact *get_target()
const {
return (
_Fact *)get_reference(0); }
536 _Fact *get_super_goal()
const {
return get_sim()->get_f_super_goal(); }
537 r_code::Code *get_actor()
const {
return get_reference(code(GOAL_ACTR).asIndex()); }
543 bool has_sim()
const {
return code(GOAL_SIM).getDescriptor() == Atom::R_PTR; }
550 Sim*
get_sim()
const {
return has_sim() ? (
Sim*)get_reference(code(GOAL_SIM).asIndex()) : NULL; }
560 std::cerr <<
"Goal::set_sim: Error: The goal already has a Sim. Ignoring the new sim." << std::endl;
564 code(GOAL_SIM) = Atom::RPointer(references_size());
574 Sim* sim = get_sim();
575 return sim && sim->get_thz() != std::chrono::seconds(0);
581 float32 get_strength(Timestamp now)
const {
582 _Fact *target = get_target();
583 return target->get_cfd() / std::chrono::duration_cast<std::chrono::microseconds>(target->get_before() - now).count();
596 return get_reference(code(code(MK_RDX_INPUTS).asIndex() + 1).asIndex());
600 return get_reference(code(code(MK_RDX_PRODS).asIndex() + 1).asIndex());
623 bool has_evidence()
const {
return code(SUCCESS_EVD).getDescriptor() == Atom::R_PTR; }
636 return code(SUCCESS_OBJ_MK_RDX).getDescriptor() == Atom::R_PTR &&
637 get_reference(code(SUCCESS_OBJ_MK_RDX).asIndex())->code(0).asOpcode() == Opcodes::MkRdx;
651 Perf(std::chrono::microseconds reduction_job_avg_latency, std::chrono::microseconds d_reduction_job_avg_latency, std::chrono::microseconds time_job_avg_latency, std::chrono::microseconds d_time_job_avg_latency);
660 bool is_invalidated()
override;
668 bool contains(
const _Fact *component, uint16 &component_index)
const;
676 bool r_contains(
const _Fact* component)
const;
679 std::vector<P<_Fact> > components_;