-
Notifications
You must be signed in to change notification settings - Fork 5
/
expressions.cc
413 lines (345 loc) · 11.9 KB
/
expressions.cc
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Copyright 2003-2005 Carnegie Mellon University and Rutgers University
* Copyright 2007 Håkan Younes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "expressions.h"
#include <stdexcept>
/* ====================================================================== */
/* Expression */
/* Output operator for expressions. */
std::ostream& operator<<(std::ostream& os, const Expression& e) {
e.print(os);
return os;
}
/* ====================================================================== */
/* Value */
/* Returns the value of this expression in the given state. */
Rational Value::value(const ValueMap& values) const {
return value_;
}
/* Returns an instantiation of this expression. */
const Value& Value::instantiation(const SubstitutionMap& subst,
const ValueMap& values) const {
return *this;
}
/* Prints this object on the given stream. */
void Value::print(std::ostream& os) const {
os << value_;
}
/* ====================================================================== */
/* Fluent */
/* Table of fluents. */
Fluent::FluentTable Fluent::fluents;
/* Comparison function. */
bool Fluent::FluentLess::operator()(const Fluent* f1, const Fluent* f2) const {
if (f1->function() < f2->function()) {
return true;
} else if (f2->function() < f1->function()) {
return false;
} else {
return f1->terms() < f2->terms();
}
}
/* Returns a fluent with the given function and terms. */
const Fluent& Fluent::make(const Function& function, const TermList& terms) {
Fluent* fluent = new Fluent(function);
bool ground = true;
for (TermList::const_iterator ti = terms.begin(); ti != terms.end(); ti++) {
fluent->add_term(*ti);
if (ground && (*ti).variable()) {
ground = false;
}
}
if (!ground) {
return *fluent;
} else {
std::pair<FluentTable::const_iterator, bool> result =
fluents.insert(fluent);
if (!result.second) {
delete fluent;
return **result.first;
} else {
return *fluent;
}
}
}
/* Deletes this fluent. */
Fluent::~Fluent() {
FluentTable::const_iterator fi = fluents.find(this);
if (*fi == this) {
fluents.erase(fi);
}
}
/* Returns the value of this expression in the given state. */
Rational Fluent::value(const ValueMap& values) const {
ValueMap::const_iterator vi = values.find(this);
if (vi != values.end()) {
return (*vi).second;
} else {
throw std::logic_error("value of fluent is undefined");
}
}
/* Returns this fluent subject to the given substitution. */
const Fluent& Fluent::substitution(const SubstitutionMap& subst) const {
TermList inst_terms;
bool substituted = false;
for (TermList::const_iterator ti = terms().begin();
ti != terms().end(); ti++) {
SubstitutionMap::const_iterator si =
(*ti).variable() ? subst.find((*ti).as_variable()) : subst.end();
if (si != subst.end()) {
inst_terms.push_back((*si).second);
substituted = true;
} else {
inst_terms.push_back(*ti);
}
}
if (substituted) {
return make(function(), inst_terms);
} else {
return *this;
}
}
/* Returns an instantiation of this expression. */
const Expression& Fluent::instantiation(const SubstitutionMap& subst,
const ValueMap& values) const {
if (terms().empty()) {
if (FunctionTable::static_function(function())) {
ValueMap::const_iterator vi = values.find(this);
if (vi != values.end()) {
return *new Value((*vi).second);
} else {
std::cerr<<"fluent: "<<*this<<std::endl;
throw std::logic_error("value of static fluent is undefined");
}
} else {
return *this;
}
} else {
TermList inst_terms;
bool substituted = false;
size_t objects = 0;
for (TermList::const_iterator ti = terms().begin();
ti != terms().end(); ti++) {
SubstitutionMap::const_iterator si =
(*ti).variable() ? subst.find((*ti).as_variable()) : subst.end();
if (si != subst.end()) {
inst_terms.push_back((*si).second);
substituted = true;
objects++;
} else {
inst_terms.push_back(*ti);
if ((*ti).object()) {
objects++;
}
}
}
if (substituted) {
const Fluent& inst_fluent = make(function(), inst_terms);
if (FunctionTable::static_function(function())
&& objects == inst_terms.size()) {
ValueMap::const_iterator vi = values.find(&inst_fluent);
if (vi != values.end()) {
return *new Value((*vi).second);
} else {
std::cerr<<"fluent: "<<inst_fluent<<std::endl;
throw std::logic_error("value of static fluent is undefined");
}
} else {
return inst_fluent;
}
} else {
return *this;
}
}
}
/* Prints this object on the given stream. */
void Fluent::print(std::ostream& os) const {
os << '(' << function();
for (TermList::const_iterator ti = terms().begin();
ti != terms().end(); ti++) {
os << ' ' << *ti;
}
os << ')';
}
/* ====================================================================== */
/* Computation */
/* Constructs a computation. */
Computation::Computation(const Expression& operand1,
const Expression& operand2)
: operand1_(&operand1), operand2_(&operand2) {
ref(operand1_);
ref(operand2_);
}
/* Deletes this computation. */
Computation::~Computation() {
destructive_deref(operand1_);
destructive_deref(operand2_);
}
/* ====================================================================== */
/* Addition */
/* Returns an addition of the two expressions. */
const Expression& Addition::make(const Expression& term1,
const Expression& term2) {
const Value* v1 = dynamic_cast<const Value*>(&term1);
if (v1 != 0) {
const Value* v2 = dynamic_cast<const Value*>(&term2);
if (v2 != 0) {
const Value& value = *new Value(v1->value() + v2->value());
ref(v1);
ref(v2);
destructive_deref(v1);
destructive_deref(v2);
return value;
}
}
return *new Addition(term1, term2);
}
/* Returns the value of this expression in the given state. */
Rational Addition::value(const ValueMap& values) const {
return operand1().value(values) + operand2().value(values);
}
/* Returns an instantiation of this expression. */
const Expression& Addition::instantiation(const SubstitutionMap& subst,
const ValueMap& values) const {
const Expression& inst_term1 = operand1().instantiation(subst, values);
const Expression& inst_term2 = operand2().instantiation(subst, values);
if (&inst_term1 == &operand1() && &inst_term2 == &operand2()) {
return *this;
} else {
return make(inst_term1, inst_term2);
}
}
/* Prints this object on the given stream. */
void Addition::print(std::ostream& os) const {
os << "(+ " << operand1() << ' ' << operand2() << ")";
}
/* ====================================================================== */
/* Subtraction */
/* Returns a subtraction of the two expressions. */
const Expression& Subtraction::make(const Expression& term1,
const Expression& term2) {
const Value* v1 = dynamic_cast<const Value*>(&term1);
if (v1 != 0) {
const Value* v2 = dynamic_cast<const Value*>(&term2);
if (v2 != 0) {
const Value& value = *new Value(v1->value() - v2->value());
ref(v1);
ref(v2);
destructive_deref(v1);
destructive_deref(v2);
return value;
}
}
return *new Subtraction(term1, term2);
}
/* Returns the value of this expression in the given state. */
Rational Subtraction::value(const ValueMap& values) const {
return operand1().value(values) - operand2().value(values);
}
/* Returns an instantiation of this expression. */
const Expression& Subtraction::instantiation(const SubstitutionMap& subst,
const ValueMap& values) const {
const Expression& inst_term1 = operand1().instantiation(subst, values);
const Expression& inst_term2 = operand2().instantiation(subst, values);
if (&inst_term1 == &operand1() && &inst_term2 == &operand2()) {
return *this;
} else {
return make(inst_term1, inst_term2);
}
}
/* Prints this object on the given stream. */
void Subtraction::print(std::ostream& os) const {
os << "(- " << operand1() << ' ' << operand2() << ")";
}
/* ====================================================================== */
/* Multiplication */
/* Returns a multiplication of the two expressions. */
const Expression& Multiplication::make(const Expression& factor1,
const Expression& factor2) {
const Value* v1 = dynamic_cast<const Value*>(&factor1);
if (v1 != 0) {
const Value* v2 = dynamic_cast<const Value*>(&factor2);
if (v2 != 0) {
const Value& value = *new Value(v1->value() * v2->value());
ref(v1);
ref(v2);
destructive_deref(v1);
destructive_deref(v2);
return value;
}
}
return *new Multiplication(factor1, factor2);
}
/* Returns the value of this expression in the given state. */
Rational Multiplication::value(const ValueMap& values) const {
return operand1().value(values) * operand2().value(values);
}
/* Returns an instantiation of this expression. */
const Expression& Multiplication::instantiation(const SubstitutionMap& subst,
const ValueMap& values) const {
const Expression& inst_factor1 = operand1().instantiation(subst, values);
const Expression& inst_factor2 = operand2().instantiation(subst, values);
if (&inst_factor1 == &operand1() && &inst_factor2 == &operand2()) {
return *this;
} else {
return make(inst_factor1, inst_factor2);
}
}
/* Prints this object on the given stream. */
void Multiplication::print(std::ostream& os) const {
os << "(* " << operand1() << ' ' << operand2() << ")";
}
/* ====================================================================== */
/* Division */
/* Returns a division of the two expressions. */
const Expression& Division::make(const Expression& factor1,
const Expression& factor2) {
const Value* v1 = dynamic_cast<const Value*>(&factor1);
if (v1 != 0) {
const Value* v2 = dynamic_cast<const Value*>(&factor2);
if (v2 != 0) {
if (v2->value() == 0) {
throw std::logic_error("division by zero");
}
const Value& value = *new Value(v1->value() / v2->value());
ref(v1);
ref(v2);
destructive_deref(v1);
destructive_deref(v2);
return value;
}
}
return *new Division(factor1, factor2);
}
/* Returns the value of this expression in the given state. */
Rational Division::value(const ValueMap& values) const {
return operand1().value(values) / operand2().value(values);
}
/* Returns an instantiation of this expression. */
const Expression& Division::instantiation(const SubstitutionMap& subst,
const ValueMap& values) const {
const Expression& inst_factor1 = operand1().instantiation(subst, values);
const Expression& inst_factor2 = operand2().instantiation(subst, values);
if (&inst_factor1 == &operand1() && &inst_factor2 == &operand2()) {
return *this;
} else {
return make(inst_factor1, inst_factor2);
}
}
/* Prints this object on the given stream. */
void Division::print(std::ostream& os) const {
os << "(/ " << operand1() << ' ' << operand2() << ")";
}