-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfaspga.cpp
More file actions
253 lines (203 loc) · 5.92 KB
/
faspga.cpp
File metadata and controls
253 lines (203 loc) · 5.92 KB
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
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <cctype>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
using namespace boost::algorithm;
typedef map<string, int> mapstr;
void dumpnodes(const mapstr& nodes)
{
mapstr::const_iterator it;
for(it=nodes.begin(); it!=nodes.end(); ++it)
cout<<it->second<<" "<<it->first<<endl;
}
int main(int argc, char** argv)
{
int atype, s;
atype = atoi(argv[1]);
s = atoi(argv[2]);
int n,k, i, j;
mapstr nodes;
string lineinput;
while(cin.peek()!=-1 && !isdigit(cin.peek()))
{
getline(cin, lineinput);
cout<<lineinput<<endl;
}
cin>>n>>k;
for(i=0; i<n; i++)
{
string node1;
cin>>node1;
nodes.insert(pair<string, int>(node1, i));
}
//cout<<"n = "<<n<<endl;
//dumpnodes(nodes);
// print shows
mapstr::const_iterator it, it2;
for(it=nodes.begin(); it!=nodes.end(); ++it)
{
cout<<"#show "<<it->first<<"/2."<<endl;
//cout<<"#show line/3."<<endl;
}
cout<<"#const maxT="<<s<<"."<<endl;
cout<<"time(0..maxT)."<<endl;
for(it=nodes.begin(); it!=nodes.end(); ++it)
{
string nn = it->first;
string gnn = "g_" + nn;
cout<<gnn<<0;
for(j=1; j<=k; j++)
cout<<" | "<<gnn<<j;
cout<<"."<<endl;
for(j=1; j<=k; j++)
cout<<nn<<"(0, "<<j<<") :- "<<gnn<<j<<"."<<endl;
for(j=1; j<k; j++)
cout<<nn<<"(0, "<<j<<") :- "<<nn<<"(0, "<<j+1<<")."<<endl;
/* All the truth-hierarchical rules */
cout<<nn<<"(T, "<<0<<") :- time(T)."<<endl;
for(j=1; j<k; j++)
{
cout<<nn<<"(T, "<<j<<")"<<" :- "<<nn<<"(T, "<<j+1<<")."<<endl;
}
}
/* print all possible constants */
for(i=1; i<=k; i++)
{
for(j=1; j<=i; j++)
cout<<"const_"<<i<<"("<<j<<")."<<endl;
}
char_separator<char> sep(" ");
int cndctr = 0;
int linenum = 0;
while(getline(cin, lineinput))
{
trim_right(lineinput);
if(lineinput=="")
continue;
linenum ++;
cout<<"% line number "<<linenum<<endl;
cout <<"%"<<lineinput<<endl;
tokenizer< char_separator<char> > tokens(lineinput, sep);
vector<string> tt;
BOOST_FOREACH (const string& t, tokens)
tt.push_back(t);
int ts = tt.size()/2 - 1;
string no = tt[0];
string novstr = tt[1];
int nvo;
istringstream(novstr) >> nvo;
//cout<<"ts = "<<ts<<endl;
int startctr = cndctr;
for(i=1; i<=ts; i++)
{
//cout<<"i = "<<i<<endl;
string ni = tt[2*i];
string nvstr = tt[2*i+1];
int nv;
istringstream(nvstr) >> nv;
cout<<"% "<<ni<<" = "<<nv<<"/"<<k<<endl;
cndctr++;
/* For every input ni = nv, translate set flag cnd1 = 1 iff ni<=p */
cout<<"% cnd"<<no<<cndctr<<" :- "<<ni<<" * "<<k-nv<<"/"<<k<<"."<<endl;
cout<<"% cnd"<<no<<cndctr<<" :- "<<"cnd"<<no<<cndctr<<" + "<<"cnd"<<no<<cndctr<<"."<<endl;
int ii,jj;
for(ii=1; ii<=k; ii++)
{
for (jj=ii; jj<=k; jj++)
{
cout<<"cnd"<<no<<cndctr<<"(T, "<<ii<<")"<<" :- time(T), "<<ni<<"(T, "<<jj<<"), "<<"const_"<<k-nv<<"("<<k-jj+ii<<")."<<endl;
}
}
for(ii=1; ii<k; ii++)
{
cout<<"cnd"<<no<<cndctr<<"(T, "<<ii<<")"<<" :- time(T), "<<"cnd"<<no<<cndctr<<"(T, "<<ii+1<<")."<<endl;
cout<<"cnd"<<no<<cndctr<<"(T, "<<ii+1<<")"<<" :- time(T), "<<"cnd"<<no<<cndctr<<"(T, "<<ii<<")."<<endl;
}
cndctr++;
/* For every input ni = nv, translate set flag cnd2 = 1 iff ni>=p */
cout<<"% cnd"<<no<<cndctr<<" :- not "<<ni<<" * "<<nv<<"/"<<k<<"."<<endl;
cout<<"% cnd"<<no<<cndctr<<" :- "<<"cnd"<<no<<cndctr<<" + "<<"cnd"<<no<<cndctr<<"."<<endl;
for(ii=1; ii<=k; ii++)
{
for (jj=ii; jj<=k; jj++)
{
cout<<"cnd"<<no<<cndctr<<"(T, "<<ii<<")"<<" :- time(T), "<<"not_"<<ni<<"(T, "<<jj<<"), "<<"const_"<<nv<<"("<<k-jj+ii<<")."<<endl;
}
}
for(ii=1; ii<=k; ii++)
cout<<"not_"<<ni<<"(T, "<<ii<<") :- time(T), not "<<ni<<"(T, "<<k-ii+1<<")."<<endl;
for(ii=1; ii<k; ii++)
{
cout<<"cnd"<<no<<cndctr<<"(T, "<<ii<<")"<<" :- "<<"cnd"<<no<<cndctr<<"(T, "<<ii+1<<")."<<endl;
cout<<"cnd"<<no<<cndctr<<"(T, "<<ii+1<<")"<<" :- "<<"cnd"<<no<<cndctr<<"(T, "<<ii<<")."<<endl;
}
cout<<"line(T, "<<linenum<<", "<<i<<") :- time(T), not cnd"<<no<<cndctr<<"(T, "<<k<<"), not cnd"<<no<<cndctr-1<<"(T, "<<k<<")."<<endl;
}
/* For every line of input, create a rule specifying the input if all conditional flags are satisfied. */
/*int stopctr = cndctr;
cout<<no<<"_p("<<nvo<<")"<<" :- ";
for(i=stopctr; i>startctr+1; i--)
{
cout<<"not cnd"<<no<<i<<"("<<k<<"), ";
}
cout<<"not cnd"<<no<<startctr+1<<"("<<k<<")."<<endl;
*/
int nodeid = nodes[no];
cout<<no<<"(T, "<<nvo<<")"<<" :- up("<<nodeid<<", T-1), ";
for(i=1; i<ts; i++)
cout<<"line(T-1, "<<linenum<<", "<<i<<"), ";
cout<<"line(T-1, "<<linenum<<", "<<ts<<"), time(T), T>=1. "<<endl;
}
/* Equality constraint for nn and nn_p */
for(it=nodes.begin(); it!=nodes.end(); ++it)
{
string nn = it->first;
for(i=0; i<=k; i++)
{
cout<<" :- "<<nn<<"(0, "<<i<<")"<<", not "<<nn<<"(maxT, "<<i<<")."<<endl;
cout<<" :- "<<nn<<"(maxT, "<<i<<")"<<", not "<<nn<<"(0, "<<i<<")."<<endl;
}
}
/* Discard steady-states (where nn stays constant) */
for(it=nodes.begin(); it!=nodes.end(); ++it)
{
string nn = it->first;
for(i=0; i<=k; i++)
{
cout<<" notconst :- "<<nn<<"(0, "<<i<<")"<<", not "<<nn<<"(1, "<<i<<")."<<endl;
cout<<" notconst :- "<<nn<<"(1, "<<i<<")"<<", not "<<nn<<"(0, "<<i<<")."<<endl;
}
}
cout<<" :- not notconst."<<endl;
/* Update selection rules */
switch(atype)
{
case 0:
for(i=0; i<n; i++)
cout<<"up("<<i<<", T) :- time(T)."<<endl;
break;
case 1:
for(i=0; i<n-1; i++)
cout<<"up("<<i<<", T) |";
cout<<"up("<<n-1<<", T):- time(T)."<<endl;
break;
case 2:
for(i=0; i<n; i++)
cout<<" up("<<i<<", T) | -up("<<i<<", T) :- time(T)."<<endl;
cout<<"sup(T) :- time(T), up(_, T)."<<endl;
cout<<":- time(T), not sup(T)."<<endl;
break;
default:
cerr<<"ERROR: Async type not specified correctly!"<<endl;
return 1;
}
return 0;
}