forked from perabrahamsen/daisy-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_lisp.C
399 lines (357 loc) · 10.6 KB
/
action_lisp.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// action_lisp.C
//
// Copyright 1996-2001 Per Abrahamsen and Søren Hansen
// Copyright 2000-2001 KVL.
//
// This file is part of Daisy.
//
// Daisy is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// Daisy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser Public License for more details.
//
// You should have received a copy of the GNU Lesser Public License
// along with Daisy; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#define BUILD_DLL
#include "action.h"
#include "daisy.h"
#include "log.h"
#include "memutils.h"
#include "submodeler.h"
#include "librarian.h"
#include "treelog.h"
#include "frame.h"
#include "block_model.h"
// We need to initialize the Condition library.
#include "condition.h"
struct ActionNil : public Action
{
void doIt (Daisy&, const Scope&, Treelog&)
{ }
void tick (const Daisy&, const Scope&, Treelog&)
{ }
void initialize (const Daisy&, const Scope&, Treelog&)
{ }
bool check (const Daisy&, const Scope&, Treelog& err) const
{ return true; }
ActionNil (const BlockModel& al)
: Action (al)
{ }
};
struct ActionT : public Action
{
void doIt (Daisy&, const Scope&, Treelog&)
{ }
bool done (const Daisy&, const Scope&, Treelog&) const
{ return false; }
void tick (const Daisy&, const Scope&, Treelog&)
{ }
void initialize (const Daisy&, const Scope&, Treelog&)
{ }
bool check (const Daisy&, const Scope&, Treelog& err) const
{ return true; }
ActionT (const BlockModel& al)
: Action (al)
{ }
};
struct ActionProgn : public Action
{
std::vector<Action*> actions;
void tick (const Daisy& daisy, const Scope& scope, Treelog& out)
{
for (std::vector<Action*>::iterator i = actions.begin ();
i != actions.end ();
i++)
{
(*i)->tick (daisy, scope, out);
}
}
void doIt (Daisy& daisy, const Scope& scope, Treelog& out)
{
for (std::vector<Action*>::iterator i = actions.begin ();
i != actions.end ();
i++)
{
(*i)->doIt (daisy, scope, out);
}
}
bool done (const Daisy& daisy, const Scope& scope, Treelog& msg) const
{
bool all_done = true;
TREELOG_MODEL (msg);
for (std::vector<Action*>::const_iterator i = actions.begin ();
i != actions.end ();
i++)
{
if (!(*i)->done (daisy, scope, msg))
all_done = false;
}
return all_done;
}
void output (Log& log) const
{ output_list (actions, "actions", log, Action::component); }
void initialize (const Daisy& daisy, const Scope& scope, Treelog& msg)
{
for (std::vector<Action*>::iterator i = actions.begin ();
i != actions.end ();
i++)
{
(*i)->initialize (daisy, scope, msg);
}
}
bool check (const Daisy& daisy, const Scope& scope, Treelog& err) const
{
bool ok = true;
for (std::vector<Action*>::const_iterator i = actions.begin ();
i != actions.end ();
i++)
{
if (!(*i)->check (daisy, scope, err))
ok = false;
}
return ok;
}
ActionProgn (const BlockModel& al)
: Action (al),
actions (Librarian::build_vector<Action> (al, "actions"))
{ }
~ActionProgn ()
{ sequence_delete (actions.begin (), actions.end ()); }
};
struct ActionCond : public Action
{
struct clause
{
std::unique_ptr<Condition> condition;
std::vector<Action*> actions;
void output (Log& log) const
{
output_object (condition, "condition", log);
output_list (actions, "actions", log, Action::component);
}
static void load_syntax (Frame& frame)
{
frame.declare_object ("condition", Condition::component,
"Condition for performing the actions.");
frame.declare_object ("actions", Action::component,
Attribute::State, Attribute::Variable,
"Actions to perform when condition is meet.");
frame.order ("condition", "actions");
}
clause (const Block& al)
: condition (Librarian::build_item<Condition> (al, "condition")),
actions (Librarian::build_vector<Action> (al, "actions"))
{ }
~clause ()
{ sequence_delete (actions.begin (), actions.end ()); }
};
std::vector<clause*> clauses;
void tick (const Daisy& daisy, const Scope& scope, Treelog& msg)
{
for (std::vector<clause*>::iterator i = clauses.begin ();
i != clauses.end ();
i++)
{
(*i)->condition->tick (daisy, scope, msg);
std::vector<Action*>& actions = (*i)->actions;
for (unsigned int j = 0; j < actions.size (); j++)
actions[j]->tick (daisy, scope, msg);
}
}
void doIt (Daisy& daisy, const Scope& scope, Treelog& msg)
{
for (std::vector<clause*>::iterator i = clauses.begin ();
i != clauses.end ();
i++)
{
if ((*i)->condition->match (daisy, scope, msg))
{
std::vector<Action*>& actions = (*i)->actions;
for (unsigned int j = 0; j < actions.size (); j++)
actions[j]->doIt (daisy, scope, msg);
break;
}
}
}
void output (Log& log) const
{
static const symbol clauses_symbol ("clauses");
if (log.check_interior (clauses_symbol))
{
Log::Open open (log, clauses_symbol);
for (std::vector<clause*>::const_iterator item = clauses.begin ();
item != clauses.end ();
item++)
{
Log::Unnamed unnamed (log);
(*item)->output (log);
}
}
}
void initialize (const Daisy& daisy, const Scope& scope, Treelog& msg)
{
for (std::vector<clause*>::iterator i = clauses.begin ();
i != clauses.end ();
i++)
{
(*i)->condition->initialize (daisy, scope, msg);
std::vector<Action*>& actions = (*i)->actions;
for (unsigned int j = 0; j < actions.size (); j++)
actions[j]->initialize (daisy, scope, msg);
}
}
bool check (const Daisy& daisy, const Scope& scope, Treelog& err) const
{
bool ok = true;
for (std::vector<clause*>::const_iterator i = clauses.begin ();
i != clauses.end ();
i++)
{
const std::vector<Action*>& actions = (*i)->actions;
for (unsigned int j = 0; j < actions.size (); j++)
if (!actions[j]->check (daisy, scope, err))
ok = false;
}
return ok;
}
ActionCond (const BlockModel& al)
: Action (al),
clauses (map_submodel<clause> (al, "clauses"))
{ }
~ActionCond ()
{ sequence_delete (clauses.begin (), clauses.end ()); }
};
struct ActionIf : public Action
{
std::unique_ptr<Condition> if_c;
std::unique_ptr<Action> then_a;
std::unique_ptr<Action> else_a;
void tick (const Daisy& daisy, const Scope& scope, Treelog& msg)
{
if_c->tick (daisy, scope, msg);
then_a->tick (daisy, scope, msg);
else_a->tick (daisy, scope, msg);
}
void doIt (Daisy& daisy, const Scope& scope, Treelog& msg)
{
if (if_c->match (daisy, scope, msg))
then_a->doIt (daisy, scope, msg);
else
else_a->doIt (daisy, scope, msg);
}
void output (Log& log) const
{
output_object (if_c, "if", log);
output_object (then_a, "then", log);
output_object (else_a, "else", log);
}
void initialize (const Daisy& daisy, const Scope& scope, Treelog& msg)
{
if_c->initialize (daisy, scope, msg);
then_a->initialize (daisy, scope, msg);
else_a->initialize (daisy, scope, msg);
}
bool check (const Daisy& daisy, const Scope& scope, Treelog& err) const
{
bool ok = true;
if (!if_c->check (daisy, scope, err))
ok = false;
if (!then_a->check (daisy, scope, err))
ok = false;
if (!else_a->check (daisy, scope, err))
ok = false;
return ok;
}
ActionIf (const BlockModel& al)
: Action (al),
if_c (Librarian::build_item<Condition> (al, "if")),
then_a (Librarian::build_item<Action> (al, "then")),
else_a (Librarian::build_item<Action> (al, "else"))
{ }
~ActionIf ()
{ }
};
static struct ActionNilSyntax : public DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionNil (al); }
ActionNilSyntax ()
: DeclareModel (Action::component, "nil", "\
This action does nothing, always done.")
{ }
void load_frame (Frame&) const
{ }
} ActionNil_syntax;
static struct ActionTSyntax : public DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionT (al); }
ActionTSyntax ()
: DeclareModel (Action::component, "t", "\
This action does nothing, never done.")
{ }
void load_frame (Frame&) const
{ }
} ActionT_syntax;
static struct ActionPrognSyntax : public DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionProgn (al); }
ActionPrognSyntax ()
: DeclareModel (Action::component, "progn", "\
Perform all the specified actions in the sequence listed.\n\
All the actions will be performed in the same time step.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_object ("actions", Action::component,
Attribute::State, Attribute::Variable,
"List of actions to perform.");
frame.order ("actions");
}
} ActionProgn_syntax;
static struct ActionCondSyntax : public DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionCond (al); }
ActionCondSyntax ()
: DeclareModel (Action::component, "cond", "\
Perform the actions associated with the first true condition in the list.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_submodule_sequence ("clauses", Attribute::State, "\
Each clause consist of a condition and a sequence of actions.\n\
The first clause whose condition is true, will have its actions activated.",
ActionCond::clause::load_syntax);
frame.order ("clauses");
}
} ActionCond_syntax;
static struct ActionIfSyntax : public DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionIf (al); }
ActionIfSyntax ()
: DeclareModel (Action::component, "if", "\
If the condition is true, perform the first action,\n\
otherwise perform the second action.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_object ("if", Condition::component,
"Condition determining which action to perform.");
frame.declare_object ("then", Action::component,
"Action to perform if the condition is true.");
frame.declare_object ("else", Action::component,
"Action to perform if the condition is false.");
frame.order ("if", "then", "else");
frame.set ("else", "nil");
}
} ActionIf_syntax;
// action_lisp.C ends here.