//===========================// //== This software is ==// //== (c)opyrighted in 1993 ==// //== by Chris Koeritz ==// //== cak0l@Virginia.EDU ==// //===========================// #include "operation.h" #include "guards.h" #include // operands are stored first in the amorph: 0 .. number_of_operands-1 // consequences next: number_of_operands .. number_of_consequences // +number_of_operands-1 operation::operation() : my_consequences(0), my_operands(0) // : packable_amorph(0) { } operation::~operation() { } int operation::add_operand(operand *new_operand) { my_operands.append(new_operand); return my_operands.elements(); } int operation::add_consequence(consequence *new_consequence) { my_consequences.append(new_consequence); return my_consequences.elements(); } int operation::operands() const { return my_operands.elements(); } int operation::consequences() const { return my_consequences.elements(); } int operation::well_formed() const { if (consequences() <= 0) return FALSE; // guarantee 1. // guarantee 2... for (int i = 0; i < consequences(); i++) { const consequence *cons_i = my_consequences.get(i); if (!cons_i->well_formed()) return FALSE; } // guarantee 3... for (int j = 0; j < operands(); j++) { const operand *op_j = my_operands.get(j); if (!op_j->well_formed()) return FALSE; } if (!my_name.length()) return FALSE; // guarantee 4. return TRUE; } operand *operation::get_operand(int index) { return my_operands.borrow(index); } operand *operation::get_operand(string op_name) { for (int i = 0; i < operands(); i++) { operand *to_return_perhaps = get_operand(i); if (to_return_perhaps->name() == op_name) return to_return_perhaps; } return NIL; } consequence *operation::get_consequence(int index) { return my_consequences.borrow(index); } consequence *operation::get_consequence(string cons_name) { for (int i = 0; i < consequences(); i++) { consequence *to_return_perhaps = get_consequence(i); if (to_return_perhaps->name() == cons_name) return to_return_perhaps; } return NIL; } string operation::name() const { return my_name; } void operation::name(const string &name) { my_name = name; } /* byte *operation::pack(int &size) const { size = sizeof(operation); operation *to_hold = new operation; for (int i = 0; i < number_of_operands; i++) { int op_size; byte *opi_pack = get_operand(i)->pack(op_size); chunk *to_stuff = new chunk(OTHER_OWNED, opi_pack, op_size); to_stuff->take_possession(); to_hold->put(i, to_stuff); } for (i = 0; i < number_of_consequences; i++) { int con_size; byte *coni_pack = get_consequence(i)->pack(con_size); chunk *to_stuff = new chunk(OTHER_OWNED, coni_pack, con_size); to_stuff->take_possession(); to_hold->put(i + number_of_operands, to_stuff); } int amolen; byte *packed_amo = to_hold->packable_amorph::pack(amolen); size += amolen; byte *to_return = new byte[size]; int temp = number_of_operands; typed_copy(&temp, to_return, int); temp = number_of_consequences; typed_copy(&temp, to_return + sizeof(int), int); mcopy(packed_amo, to_return + 2 * sizeof(int), amolen); delete packed_amo; return to_return; } operation *operation_unpack(byte *packed_operation) { operation *to_return = new operation; typed_copy(packed_operation, &to_return->number_of_operands, int); typed_copy(to_return + sizeof(int), &to_return->number_of_consequences, int); byte *packed_amo = packed_operation + 2 * sizeof(int); packable_amorph *unpacked_amo = amorph_unpack(packed_amo, chunk_unpack); for (int i = 0; i < to_return->number_of_operands; i++) { byte *pack_op = unpacked_amo->get(i)->held(); operand *unpack_op = unpack_operand(pack_op); to_return->add_operand(unpack_op); } for (i = 0; i < to_return->number_of_consequences; i++) { byte *pack_con = unpacked_amo-> get(i + to_return->number_of_operands)->held(); consequence *unpack_con = consequence_unpack(pack_con); to_return->add_consequence(unpack_con); } return to_return; } */ void operation::print() const { cout << "[ operation has name=" << name() << flush; for (int i = 0; i < operands(); i++) { cout << "operand " << i << ": " << flush; const operand *to_print = my_operands.get(i); to_print->print(); } for (i = 0; i < consequences(); i++) { cout << "consequence " << i << ": " << flush; const consequence *to_print = my_consequences.get(i); to_print->print(); } cout << "]\n" << flush; }