Skip to content

Commit

Permalink
model: First shot at fluent synchronization
Browse files Browse the repository at this point in the history
Although what's really being synced is an ExogEvent for an ExogAction
that is being generated if a fluent is marked synced.
  • Loading branch information
vmatare committed Dec 3, 2019
1 parent 6562a8f commit ef78440
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/model/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ AbstractAction::AbstractAction(
const vector<unique_ptr<AbstractEffectAxiom>> &AbstractAction::effects() const
{ return effects_; }

vector<unique_ptr<AbstractEffectAxiom>> &AbstractAction::effects()
{ return effects_; }


void AbstractAction::add_effect(AbstractEffectAxiom *effect)
{
Expand Down
17 changes: 17 additions & 0 deletions src/model/effect_axiom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,21 @@ void AbstractEffectAxiom::set_condition(Expression *condition)
condition_->set_parent(this);
}




template<>
const Reference<Fluent> &EffectAxiom<Reference<Fluent>>::fluent() const
{ return lhs(); }

template<>
const Reference<Fluent> &EffectAxiom<FieldAccess>::fluent() const
{ return dynamic_cast<const Reference<Fluent> &>(lhs().subject()); }

template<>
const Reference<Fluent> &EffectAxiom<ListAccess>::fluent() const
{ return dynamic_cast<const Reference<Fluent> &>(lhs().subject()); }



} // namespace gologpp
3 changes: 3 additions & 0 deletions src/model/effect_axiom.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class AbstractEffectAxiom : public virtual AbstractLanguageElement {
const Expression &condition() const;
Expression &condition();
void set_condition(Expression *condition);
virtual const Reference<Fluent> &fluent() const = 0;

protected:
AbstractAction *action_;
Expand Down Expand Up @@ -95,6 +96,8 @@ class EffectAxiom : public AbstractEffectAxiom, public NoScopeOwner, public Lang
virtual const Scope &parent_scope() const override
{ return action().scope(); }

virtual const Reference<Fluent> &fluent() const override;


DEFINE_ATTACH_SEMANTICS_WITH_MEMBERS(*condition_, *assignment_)

Expand Down
46 changes: 31 additions & 15 deletions src/model/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ void AExecutionContext::postcompile()
{ postcompile_(); }


void AExecutionContext::sync_fluent(const Reference<Fluent> &f, const Activity &context)
{
vector<unique_ptr<Value>> sync_exog_args;
for (const unique_ptr<Expression> &arg : f.args())
sync_exog_args.emplace_back(
new Value(arg->abstract_expr_semantics().evaluate(context, history()))
);

sync_exog_args.emplace_back(
new Value(context.target()->senses()->abstract_expr_semantics().evaluate(context, history()))
);

backend().sync_event(
ExogEvent(
context.target()->senses()->target()->sync_action(),
std::move(sync_exog_args)
)
);
}




ExecutionContext::ExecutionContext(unique_ptr<SemanticsFactory> &&semantics, unique_ptr<PlatformBackend> &&exec_backend)
: AExecutionContext(std::move(semantics), std::move(exec_backend))
Expand Down Expand Up @@ -149,27 +171,21 @@ void ExecutionContext::run(Block &&program)
backend().start_activity(trans);
else if (trans->hook() == Transition::Hook::FINISH) {
shared_ptr<Activity> activity = backend().end_activity(trans);

// Sync effects if necessary
for (const auto &effect : trans->target()->effects())
if (effect->fluent()->synced())
sync_fluent(effect->fluent(), *activity);

if (trans->target()->senses()) {
history().abstract_semantics().append_sensing_result(
*activity->target()->senses(),
activity->sensing_result().get()
);

if (trans->target()->senses()->target()->synced()) {
vector<unique_ptr<Value>> sync_exog_args;
for (const unique_ptr<Expression> &arg : trans->target()->senses()->args())
sync_exog_args.emplace_back(
new Value(arg->abstract_semantics().evaluate(*activity, history()))
);

sync_exog_args.emplace_back(
trans->target()->senses()->abstract_semantics().evaluate(*activity, history());
);

backend().sync_event(
ExogEvent(trans->target()->senses())
);
}
// Sync sensed fluent if necessary
if (trans->target()->senses()->target()->synced())
sync_fluent(*trans->target()->senses(), *activity);
}
}
else
Expand Down
3 changes: 3 additions & 0 deletions src/model/execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class AExecutionContext {
PlatformBackend &backend();
History &history();

protected:
void sync_fluent(const Reference<Fluent> &f, const Activity &context);

private:
virtual void precompile_() = 0;
virtual void postcompile_() = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/model/expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const AbstractLanguageElement *Expression::parent() const
void Expression::set_parent(AbstractLanguageElement *parent)
{ parent_ = parent; }

AbstractSemantics<Expression> &Expression::abstract_expr_semantics() const
{ return dynamic_cast<AbstractSemantics<Expression> &>(abstract_semantics()); }

const string &Expression::type_name() const
{ return type().name(); }

Expand Down
2 changes: 1 addition & 1 deletion src/model/expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Expression : public virtual AbstractLanguageElement {
AbstractLanguageElement *parent();
const AbstractLanguageElement *parent() const;
void set_parent(AbstractLanguageElement *parent);

AbstractSemantics<Expression> &abstract_expr_semantics() const;
const string &type_name() const;

protected:
Expand Down
3 changes: 3 additions & 0 deletions src/model/fluent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ bool Fluent::synced() const
const ExogAction &Fluent::sync_action() const
{ return *sync_action_; }

ExogAction &Fluent::sync_action()
{ return *sync_action_; }

void Fluent::define(const vector<InitialValue *> &initial_values, bool synced)
{ define(boost::optional<vector<InitialValue *>>(initial_values), synced); }

Expand Down
1 change: 1 addition & 0 deletions src/model/fluent.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Fluent
const vector<unique_ptr<InitialValue>> &initially() const;
bool synced() const;
const ExogAction &sync_action() const;
ExogAction &sync_action();

void define(const vector<InitialValue *> &initial_values, bool synced = false);
void define(const boost::optional<vector<InitialValue *>> &initial_values, bool synced = false);
Expand Down
2 changes: 2 additions & 0 deletions src/model/gologpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class History;

template<class> class Semantics;
template<class> class AbstractSemantics;
template<> class AbstractSemantics<Expression>;
template<> class AbstractSemantics<Instruction>;

class SemanticsFactory;

Expand Down
3 changes: 3 additions & 0 deletions src/model/platform_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,8 @@ Clock::time_point DummyBackend::time() const noexcept
}


void DummyBackend::sync_event(const ExogEvent &)
{ throw Bug("Not implemented"); }


}
2 changes: 2 additions & 0 deletions src/model/platform_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class DummyBackend : public PlatformBackend {
virtual void preempt_activity(shared_ptr<Activity>) override;
virtual Clock::time_point time() const noexcept override;

virtual void sync_event(const ExogEvent &) override;

private:
virtual void execute_activity(shared_ptr<Activity> a) override;

Expand Down
11 changes: 9 additions & 2 deletions src/model/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ class ReferenceBase
)
{}

ReferenceBase(TargetT &target, vector<unique_ptr<ArgsT>> &&args)
: ReferenceBase(std::dynamic_pointer_cast<TargetT>(target.shared_from_this()), std::move(args))
{}

ReferenceBase(const string &target_name, const boost::optional<vector<ArgsT *>> &args)
: ReferenceBase(target_name, args.get_value_or({}))
{}
Expand All @@ -122,8 +126,11 @@ class ReferenceBase
TargetT &operator * () const
{ return target(); }

TargetT *operator -> () const
{ return &target(); }
const TargetT *operator -> () const
{ return target().get(); }

TargetT *operator -> ()
{ return target().get(); }

bool operator == (const ReferenceBase<TargetT, ArgsT> &other) const
{
Expand Down
1 change: 0 additions & 1 deletion src/model/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#define GOLOGPP_IMPLEMENTATION_H_

#include "gologpp.h"
#include "expressions.h"
#include <memory>

#include <boost/preprocessor/seq/for_each.hpp>
Expand Down

0 comments on commit ef78440

Please sign in to comment.