Skip to content

Commit ef78440

Browse files
committed
model: First shot at fluent synchronization
Although what's really being synced is an ExogEvent for an ExogAction that is being generated if a fluent is marked synced.
1 parent 6562a8f commit ef78440

14 files changed

Lines changed: 81 additions & 19 deletions

src/model/action.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ AbstractAction::AbstractAction(
5151
const vector<unique_ptr<AbstractEffectAxiom>> &AbstractAction::effects() const
5252
{ return effects_; }
5353

54+
vector<unique_ptr<AbstractEffectAxiom>> &AbstractAction::effects()
55+
{ return effects_; }
56+
5457

5558
void AbstractAction::add_effect(AbstractEffectAxiom *effect)
5659
{

src/model/effect_axiom.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,21 @@ void AbstractEffectAxiom::set_condition(Expression *condition)
4949
condition_->set_parent(this);
5050
}
5151

52+
53+
54+
55+
template<>
56+
const Reference<Fluent> &EffectAxiom<Reference<Fluent>>::fluent() const
57+
{ return lhs(); }
58+
59+
template<>
60+
const Reference<Fluent> &EffectAxiom<FieldAccess>::fluent() const
61+
{ return dynamic_cast<const Reference<Fluent> &>(lhs().subject()); }
62+
63+
template<>
64+
const Reference<Fluent> &EffectAxiom<ListAccess>::fluent() const
65+
{ return dynamic_cast<const Reference<Fluent> &>(lhs().subject()); }
66+
67+
68+
5269
} // namespace gologpp

src/model/effect_axiom.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class AbstractEffectAxiom : public virtual AbstractLanguageElement {
4747
const Expression &condition() const;
4848
Expression &condition();
4949
void set_condition(Expression *condition);
50+
virtual const Reference<Fluent> &fluent() const = 0;
5051

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

99+
virtual const Reference<Fluent> &fluent() const override;
100+
98101

99102
DEFINE_ATTACH_SEMANTICS_WITH_MEMBERS(*condition_, *assignment_)
100103

src/model/execution.cpp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ void AExecutionContext::postcompile()
110110
{ postcompile_(); }
111111

112112

113+
void AExecutionContext::sync_fluent(const Reference<Fluent> &f, const Activity &context)
114+
{
115+
vector<unique_ptr<Value>> sync_exog_args;
116+
for (const unique_ptr<Expression> &arg : f.args())
117+
sync_exog_args.emplace_back(
118+
new Value(arg->abstract_expr_semantics().evaluate(context, history()))
119+
);
120+
121+
sync_exog_args.emplace_back(
122+
new Value(context.target()->senses()->abstract_expr_semantics().evaluate(context, history()))
123+
);
124+
125+
backend().sync_event(
126+
ExogEvent(
127+
context.target()->senses()->target()->sync_action(),
128+
std::move(sync_exog_args)
129+
)
130+
);
131+
}
132+
133+
134+
113135

114136
ExecutionContext::ExecutionContext(unique_ptr<SemanticsFactory> &&semantics, unique_ptr<PlatformBackend> &&exec_backend)
115137
: AExecutionContext(std::move(semantics), std::move(exec_backend))
@@ -149,27 +171,21 @@ void ExecutionContext::run(Block &&program)
149171
backend().start_activity(trans);
150172
else if (trans->hook() == Transition::Hook::FINISH) {
151173
shared_ptr<Activity> activity = backend().end_activity(trans);
174+
175+
// Sync effects if necessary
176+
for (const auto &effect : trans->target()->effects())
177+
if (effect->fluent()->synced())
178+
sync_fluent(effect->fluent(), *activity);
179+
152180
if (trans->target()->senses()) {
153181
history().abstract_semantics().append_sensing_result(
154182
*activity->target()->senses(),
155183
activity->sensing_result().get()
156184
);
157185

158-
if (trans->target()->senses()->target()->synced()) {
159-
vector<unique_ptr<Value>> sync_exog_args;
160-
for (const unique_ptr<Expression> &arg : trans->target()->senses()->args())
161-
sync_exog_args.emplace_back(
162-
new Value(arg->abstract_semantics().evaluate(*activity, history()))
163-
);
164-
165-
sync_exog_args.emplace_back(
166-
trans->target()->senses()->abstract_semantics().evaluate(*activity, history());
167-
);
168-
169-
backend().sync_event(
170-
ExogEvent(trans->target()->senses())
171-
);
172-
}
186+
// Sync sensed fluent if necessary
187+
if (trans->target()->senses()->target()->synced())
188+
sync_fluent(*trans->target()->senses(), *activity);
173189
}
174190
}
175191
else

src/model/execution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class AExecutionContext {
7171
PlatformBackend &backend();
7272
History &history();
7373

74+
protected:
75+
void sync_fluent(const Reference<Fluent> &f, const Activity &context);
76+
7477
private:
7578
virtual void precompile_() = 0;
7679
virtual void postcompile_() = 0;

src/model/expressions.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const AbstractLanguageElement *Expression::parent() const
4040
void Expression::set_parent(AbstractLanguageElement *parent)
4141
{ parent_ = parent; }
4242

43+
AbstractSemantics<Expression> &Expression::abstract_expr_semantics() const
44+
{ return dynamic_cast<AbstractSemantics<Expression> &>(abstract_semantics()); }
45+
4346
const string &Expression::type_name() const
4447
{ return type().name(); }
4548

src/model/expressions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Expression : public virtual AbstractLanguageElement {
4545
AbstractLanguageElement *parent();
4646
const AbstractLanguageElement *parent() const;
4747
void set_parent(AbstractLanguageElement *parent);
48-
48+
AbstractSemantics<Expression> &abstract_expr_semantics() const;
4949
const string &type_name() const;
5050

5151
protected:

src/model/fluent.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ bool Fluent::synced() const
113113
const ExogAction &Fluent::sync_action() const
114114
{ return *sync_action_; }
115115

116+
ExogAction &Fluent::sync_action()
117+
{ return *sync_action_; }
118+
116119
void Fluent::define(const vector<InitialValue *> &initial_values, bool synced)
117120
{ define(boost::optional<vector<InitialValue *>>(initial_values), synced); }
118121

src/model/fluent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Fluent
7878
const vector<unique_ptr<InitialValue>> &initially() const;
7979
bool synced() const;
8080
const ExogAction &sync_action() const;
81+
ExogAction &sync_action();
8182

8283
void define(const vector<InitialValue *> &initial_values, bool synced = false);
8384
void define(const boost::optional<vector<InitialValue *>> &initial_values, bool synced = false);

src/model/gologpp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class History;
124124

125125
template<class> class Semantics;
126126
template<class> class AbstractSemantics;
127+
template<> class AbstractSemantics<Expression>;
128+
template<> class AbstractSemantics<Instruction>;
127129

128130
class SemanticsFactory;
129131

0 commit comments

Comments
 (0)