successor_generator.cpp
Go to the documentation of this file.
00001 #include "globals.h"
00002 #include "operator.h"
00003 #include "state.h"
00004 #include "successor_generator.h"
00005 
00006 #include <cstdlib>
00007 #include <iostream>
00008 #include <vector>
00009 using namespace std;
00010 
00011 class SuccessorGeneratorSwitch : public SuccessorGenerator
00012 {
00013     int switch_var;
00014     SuccessorGenerator *immediate_ops;
00015     vector<SuccessorGenerator *> generator_for_value;
00016     SuccessorGenerator *default_generator;
00017     public:
00018         SuccessorGeneratorSwitch(istream &in);
00019         virtual void generate_applicable_ops(const TimeStampedState &curr,
00020             vector<const Operator *> &ops);
00021         virtual void _dump(string indent);
00022 };
00023 
00024 class SuccessorGeneratorGenerate : public SuccessorGenerator
00025 {
00026     vector<const Operator *> op;
00027     public:
00028         SuccessorGeneratorGenerate(istream &in);
00029         virtual void generate_applicable_ops(const TimeStampedState &curr,
00030             vector<const Operator *> &ops);
00031         virtual void _dump(string indent);
00032 };
00033 
00034 SuccessorGeneratorSwitch::SuccessorGeneratorSwitch(istream &in)
00035 {
00036     cin >> switch_var;
00037     immediate_ops = read_successor_generator(in);
00038     for(int i = 0; i < g_variable_domain[switch_var]; i++)
00039         generator_for_value.push_back(read_successor_generator(in));
00040     default_generator = read_successor_generator(in);
00041 }
00042 
00043 void SuccessorGeneratorSwitch::generate_applicable_ops(
00044         const TimeStampedState &curr, vector<const Operator *> &ops)
00045 {
00046     immediate_ops->generate_applicable_ops(curr, ops);
00047     generator_for_value[static_cast<int>(curr[switch_var])]->generate_applicable_ops(curr, ops);
00048     default_generator->generate_applicable_ops(curr, ops);
00049 }
00050 
00051 void SuccessorGeneratorSwitch::_dump(string indent)
00052 {
00053     cout << indent << "switch on " << g_variable_name[switch_var] << endl;
00054     cout << indent << "immediately:" << endl;
00055     immediate_ops->_dump(indent + "  ");
00056     for(int i = 0; i < g_variable_domain[switch_var]; i++) {
00057         cout << indent << "case " << i << ":" << endl;
00058         generator_for_value[i]->_dump(indent + "  ");
00059     }
00060     cout << indent << "always:" << endl;
00061     default_generator->_dump(indent + "  ");
00062 }
00063 
00064 void SuccessorGeneratorGenerate::generate_applicable_ops(
00065         const TimeStampedState &, vector<const Operator *> &ops)
00066 {
00067     ops.insert(ops.end(), op.begin(), op.end());
00068 }
00069 
00070 SuccessorGeneratorGenerate::SuccessorGeneratorGenerate(istream &in)
00071 {
00072     int count;
00073     cin >> count;
00074     for(int i = 0; i < count; i++) {
00075         int op_index;
00076         in >> op_index;
00077         op.push_back(&g_operators[op_index]);
00078     }
00079 }
00080 
00081 void SuccessorGeneratorGenerate::_dump(string indent)
00082 {
00083     for(int i = 0; i < op.size(); i++) {
00084         cout << indent;
00085         op[i]->dump();
00086     }
00087 }
00088 
00089 SuccessorGenerator *read_successor_generator(istream &in)
00090 {
00091     string type;
00092     in >> type;
00093     if(type == "switch") {
00094         return new SuccessorGeneratorSwitch(in);
00095     } else if(type == "check") {
00096         return new SuccessorGeneratorGenerate(in);
00097     }
00098     cout << "Illegal successor generator statement!" << endl;
00099     cout << "Expected 'switch' or 'check', got '" << type << "'." << endl;
00100     exit(1);
00101 }
00102 


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Mon Oct 6 2014 07:52:06