summaryrefslogtreecommitdiff
path: root/impl/main.cpp
blob: 65464449281abf9138a1ada28828ccb620d63961 (about) (plain)
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
#include <iostream>
#include <vector>
#include <sstream>
#include "Complete.hpp"
#include "Expression.hpp"
#include "Operator.hpp"
#include "EquationSystem.hpp"
#include "MaxStrategy.hpp"
#include "FixpointAlgorithm.hpp"

extern "C" {
#include "EquationSystemParser.h"
#include "EquationSystemLexer.h"
}

using namespace std;

template<typename T>
Expression<T>& treeToExpression(pANTLR3_BASE_TREE node, ConcreteEquationSystem<T>& system) {
  ANTLR3_UINT32 num = node->getChildCount(node);
  string name = (char*) node->getText(node)->chars;

  if (num == 0) {
    // leaf node -> constant or variable
    if (name == "inf") {
      return system.constant(infinity<T>());
    } else {
      stringstream stream(name);
      T output;
      if (stream >> output) {
        return system.constant(output);
      } else {
        return system.variable(name);
      }
    }
  }

  // anything that's not a constant/variable
  std::vector<Expression<T>*> args;
  pANTLR3_BASE_TREE childNode;
  for (unsigned int i = 0; i < num; ++i) {
    childNode = (pANTLR3_BASE_TREE) node->getChild(node, i);
    args.push_back(&treeToExpression(childNode, system));
  }

  if (name == "max") {
    return system.maxExpression(args);
  } else {
    Operator<T>* op = NULL;
    if (name == "min") {
      op = new Minimum<T>();
    } else if (name == "+") {
      op = new Addition<T>();
    } else if (name == "-") {
      if (args.size() == 1) {
        op = new Negation<T>();
      } else {
        op = new Subtraction<T>();
      }
    } else if (name == ";") {
      op = new Comma<T>();
    } else if (name == "GUARD") {
      op = new Guard<T>();
    } else {
      throw "Parse error: Unknown operator";
    }
    return system.expression(op, args);
  }
}

template<typename T>
void treeToSystem(pANTLR3_BASE_TREE node, ConcreteEquationSystem<T>& system) {
  ANTLR3_UINT32 num = node->getChildCount(node);

  if (num % 2 == 1)
    throw "Big problem here.";

  pANTLR3_BASE_TREE varNode;
  pANTLR3_BASE_TREE exprNode;
  for (unsigned int i = 0; i < num; i += 2) {
    varNode = (pANTLR3_BASE_TREE) node->getChild(node, i);
    exprNode = (pANTLR3_BASE_TREE) node->getChild(node, i+1);

    string varName = (char*) varNode->getText(varNode)->chars;
    Variable<T>& var = system.variable(varName);

    vector<Expression<T>*> args;
    args.push_back(&system.constant(-infinity<T>()));
    args.push_back(&treeToExpression(exprNode, system));
    system[var] = &system.maxExpression(args);
  }
}

typedef Complete<int> ZBar;
int main (int argc, char* argv[]) {
  if (argc <= 3) {
    cout << "Usage: " << argv[0] << " filename naive|smart naive|repeat" << endl;
    exit(1);
  }
  FixpointAlgorithm<ZBar>* algorithm = NULL;
  if (!strcmp(argv[2], "naive")) {
    algorithm = new NaiveFixpointAlgorithm<ZBar>();
    cout << "Naive fixpoint" << endl;
  } else if (!strcmp(argv[2], "smart")) {
    algorithm = new SmartFixpointAlgorithm<ZBar>();
    cout << "Smart fixpoint" << endl;
  } else {
    cout << "Unknown fixpoint algorithm." << endl;
  }
  MaxStrategy<ZBar>::ImprovementOperator* naiveImprovement = new MaxStrategy<ZBar>::NaiveImprovementOperator();
  MaxStrategy<ZBar>::ImprovementOperator* improvement = NULL;
  if (!strcmp(argv[3], "repeat")) {
    improvement = new MaxStrategy<ZBar>::RepeatedImprovementOperator(*naiveImprovement);
    cout << "Repeated strategy improvement" << endl;
  } else if (!strcmp(argv[3], "naive")) {
    improvement = naiveImprovement;
    cout << "Naive strategy improvement" << endl;
  } else {
    cout << "Unknown strategy improvement algorithm." << endl;
  }
  if (!improvement || !algorithm) {
    exit(1);
  }

  pANTLR3_INPUT_STREAM input;
  pEquationSystemLexer lex;
  pANTLR3_COMMON_TOKEN_STREAM tokens;
  pEquationSystemParser parser;

  input = antlr3FileStreamNew((pANTLR3_UINT8)argv[1], ANTLR3_ENC_8BIT);
  lex = EquationSystemLexerNew(input);
  tokens = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lex));
  parser = EquationSystemParserNew(tokens);

  EquationSystemParser_equation_system_return ret = parser -> equation_system(parser);

  ConcreteEquationSystem<ZBar> system;
  treeToSystem<ZBar>(ret.tree, system);
  // DO MORE HERE

  cout << system << endl;
  VariableAssignment<ZBar>* result = NULL;
  MaxStrategy<ZBar> strategy(system);
  bool changed;
  do {
    if (result) delete result;
    result = algorithm->maxFixpoint(strategy);
    /*for (unsigned int i = 0, size = system.variableCount(); i < size; ++i) {
      Variable<ZBar>& var = system.variable(i);
      cout << var.name() << " = " << (*result)[var] << ", ";
    }
    cout << endl;*/
    changed = strategy.improve(*improvement, *result);
  } while(changed);

  VariableAssignment<ZBar>* rho = result;

  cout << endl;
  for (unsigned int i = 0, size = system.variableCount(); i < size; ++i) {
    Variable<ZBar>& var = system.variable(i);
    cout << var.name() << " = " << (*rho)[var] << endl;
  }

  delete result;
  delete algorithm;
  if (naiveImprovement != improvement)
    delete improvement;
  delete naiveImprovement;

  parser -> free(parser);
  tokens -> free(tokens);
  lex -> free(lex);
  input -> close(input);

  return 0;
}