-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOperationCall.java
273 lines (257 loc) · 9.12 KB
/
BOperationCall.java
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
import java.util.Vector;
/* Package: B */
/******************************
* Copyright (c) 2003,2019 Kevin Lano
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
* *****************************/
public class BOperationCall extends BStatement
{ private String opName;
private Vector arguments = new Vector(); // BExpression
public BOperationCall(String op, Vector args)
{ opName = op;
arguments = args;
}
public String toString()
{ String res = opName;
int pc = arguments.size();
if (pc > 0)
{ res = res + "(";
for (int i = 0; i < pc; i++)
{ res = res + arguments.get(i);
if (i < pc - 1)
{ res = res + ","; }
}
res = res + ")";
}
return res;
}
public BStatement substituteEq(String oldE, BExpression newE)
{ // if (oldE.equals(toString()))
// { return newE; }
Vector nargs = new Vector();
for (int i = 0; i < nargs.size(); i++)
{ BExpression be = (BExpression) nargs.get(i);
BExpression nbe = be.substituteEq(oldE,newE);
nargs.add(nbe);
}
return new BOperationCall(opName,nargs);
}
public BExpression updateExpression(String var)
{ // if it is setAllvar(S,v), return S*{v}, etc
if (opName.equals("setAll" + var))
{ BExpression set = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
BSetExpression valset = new BSetExpression();
valset.addElement(val);
return new BBinaryExpression("*",set,valset);
}
if (opName.equals("set" + var))
{ BExpression set = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
BSetExpression valset = new BSetExpression();
valset.addElement(new BBinaryExpression("|->",set,val));
return valset;
}
if (opName.equals("update" + var))
{ return (BExpression) arguments.get(0); }
if (opName.equals("add" + var))
{ BExpression ex = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
// var := var <+ { ex |-> var(ex) \/ { val }}
BSetExpression valset = new BSetExpression();
valset.addElement(val);
BApplyExpression fex =
new BApplyExpression(var,ex);
BBinaryExpression union =
new BBinaryExpression("\\/",fex,valset);
BBinaryExpression mapsto =
new BBinaryExpression("|->",ex,union);
BSetExpression maplet = new BSetExpression();
maplet.addElement(mapsto);
return maplet;
}
else if (opName.equals("remove" + var))
{ BExpression ex = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
// var := var <+ { ex |-> var(ex) - { val }}
BSetExpression valset = new BSetExpression();
valset.addElement(val);
BApplyExpression fex =
new BApplyExpression(var,ex);
BBinaryExpression minus =
new BBinaryExpression("-",fex,valset);
BBinaryExpression mapsto =
new BBinaryExpression("|->",ex,minus);
BSetExpression maplet = new BSetExpression();
maplet.addElement(mapsto);
return maplet;
}
else if (opName.equals("union" + var))
{ BExpression ex = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
// var := var <+ { ex |-> var(ex) \/ val }
BApplyExpression fex =
new BApplyExpression(var,ex);
BBinaryExpression union =
new BBinaryExpression("\\/",fex,val);
BBinaryExpression mapsto =
new BBinaryExpression("|->",ex,union);
BSetExpression maplet = new BSetExpression();
maplet.addElement(mapsto);
return maplet;
}
else if (opName.equals("subtract" + var))
{ BExpression ex = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
// var := var <+ { ex |-> var(ex) - val }
BApplyExpression fex =
new BApplyExpression(var,ex);
BBinaryExpression minus =
new BBinaryExpression("-",fex,val);
BBinaryExpression mapsto =
new BBinaryExpression("|->",ex,minus);
BSetExpression maplet = new BSetExpression();
maplet.addElement(mapsto);
return maplet;
}
else if (opName.equals("addAll" + var))
{ BExpression ex = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
// var := var <+ { exx,varx | exx : ex &
// varx = var(exx) \/ { val } }
String exx = "e_xx";
String varx = var + "_x";
Vector args = new Vector();
args.add(exx);
args.add(varx);
BBasicExpression exxbe =
new BBasicExpression(exx);
BApplyExpression fex =
new BApplyExpression(var,exxbe);
BBasicExpression varxbe =
new BBasicExpression(varx);
BSetExpression valset = new BSetExpression();
valset.addElement(val);
BBinaryExpression union =
new BBinaryExpression("\\/",fex,valset);
BBinaryExpression eq =
new BBinaryExpression("=",varxbe,union);
BBinaryExpression range =
new BBinaryExpression(":",exxbe,ex);
BExpression pred =
new BBinaryExpression("&",range,eq);
BSetComprehension res =
new BSetComprehension(args,pred);
return res;
}
else if (opName.equals("removeAll" + var))
{ BExpression ex = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
// var := var <+ { exx,varx | exx : ex &
// varx = var(exx) - { val } }
String exx = "e_xx";
String varx = var + "_x";
Vector args = new Vector();
args.add(exx);
args.add(varx);
BBasicExpression exxbe =
new BBasicExpression(exx);
BApplyExpression fex =
new BApplyExpression(var,exxbe);
BBasicExpression varxbe =
new BBasicExpression(varx);
BSetExpression valset = new BSetExpression();
valset.addElement(val);
BBinaryExpression minus =
new BBinaryExpression("-",fex,valset);
BBinaryExpression eq =
new BBinaryExpression("=",varxbe,minus);
BBinaryExpression range =
new BBinaryExpression(":",exxbe,ex);
BExpression pred =
new BBinaryExpression("&",range,eq);
BSetComprehension res =
new BSetComprehension(args,pred);
return res;
}
else if (opName.equals("unionAll" + var))
{ BExpression ex = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
// var := var <+ { exx,varx | exx : ex &
// varx = var(exx) \/ val }
String exx = "e_xx";
String varx = var + "_x";
Vector args = new Vector();
args.add(exx);
args.add(varx);
BBasicExpression exxbe =
new BBasicExpression(exx);
BApplyExpression fex =
new BApplyExpression(var,exxbe);
BBasicExpression varxbe =
new BBasicExpression(varx);
BBinaryExpression union =
new BBinaryExpression("\\/",fex,val);
BBinaryExpression eq =
new BBinaryExpression("=",varxbe,union);
BBinaryExpression range =
new BBinaryExpression(":",exxbe,ex);
BExpression pred =
new BBinaryExpression("&",range,eq);
BSetComprehension res =
new BSetComprehension(args,pred);
return res;
}
else if (opName.equals("subtractAll" + var))
{ BExpression ex = (BExpression) arguments.get(0);
BExpression val = (BExpression) arguments.get(1);
// var := var <+ { exx,varx | exx : ex &
// varx = var(exx) - val }
String exx = "e_xx";
String varx = var + "_x";
Vector args = new Vector();
args.add(exx);
args.add(varx);
BBasicExpression exxbe =
new BBasicExpression(exx);
BApplyExpression fex =
new BApplyExpression(var,exxbe);
BBasicExpression varxbe =
new BBasicExpression(varx);
BBinaryExpression minus =
new BBinaryExpression("-",fex,val);
BBinaryExpression eq =
new BBinaryExpression("=",varxbe,minus);
BBinaryExpression range =
new BBinaryExpression(":",exxbe,ex);
BExpression pred =
new BBinaryExpression("&",range,eq);
BSetComprehension res =
new BSetComprehension(args,pred);
return res;
}
return null;
}
public BStatement simplify()
{ return this; }
public BStatement seq2parallel()
{ return this; }
public BStatement normalise()
{ return this; }
public Vector rd()
{ Vector res = new Vector();
for (int i = 0; i < arguments.size(); i++)
{ BExpression arg = (BExpression) arguments.get(i);
res = VectorUtil.union(res,arg.rd());
}
return res;
}
public Vector wr()
{ Vector res = new Vector();
return res;
}
}