ChanUseEnv.java 9.19 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
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
package jcircus.environment;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
import java.util.Vector;

import jcircus.exceptions.ChanUseUnificationException;
import jcircus.exceptions.MoreThanOneWriterException;
import jcircus.newutil.ChannelUtil;
import jcircus.util.ChanUse;
import net.sourceforge.czt.base.ast.ListTerm;
import net.sourceforge.czt.circus.ast.ProcessPara;
import net.sourceforge.czt.z.ast.Name;
import net.sourceforge.czt.z.ast.Spec;
import net.sourceforge.czt.z.ast.ZExprList;
import net.sourceforge.czt.z.ast.ZNameList;



/**
* ChanUseEnv.java
*
* @author Angela Freitas
*/
public class ChanUseEnv {
/**
* Needs to be String because the key in TreeMap has to implement Comparable.
* String (channelName) -> ChanUse
*/
private TreeMap<String, ChanUse> treeMap_;
/**
* Constructor
*/
public ChanUseEnv() {
this.treeMap_ = new TreeMap<String, ChanUse>();
}
/**
*
* @param channelName
* @param javaType
* @throws ChannelAlreadyDefinedException
*/
public void put(String channelName, ChanUse chanUse) {
if (chanUse == null)
throw new NullPointerException();
this.treeMap_.put(channelName.toString(), chanUse);
}

/**
*
* @param channelName
* @return
*/
public ChanUse get(String channelName) {
return treeMap_.get(channelName);
}
/**
*
* @param channelName
*/
public void remove(String channelName) {
this.treeMap_.remove(channelName);
}
/**
*
* @return
*/
public int size() {
return this.treeMap_.size();
}
/**
*
* @param channelName
* @return
*/
public boolean containsKey(String channelName) {
return this.treeMap_.containsKey(channelName);
}
/**
*
* @param channelUseEnvironment
* @throws ChannelAlreadyDefinedException
*/
private void putAll(ChanUseEnv channelUseEnvironment) {
Iterator<String> iterator = channelUseEnvironment.iteratorKeys();
String channelName;
ChanUse javaTypeChannel;
while(iterator.hasNext()) {
channelName = iterator.next();
javaTypeChannel = channelUseEnvironment.get(channelName);
this.put(channelName, javaTypeChannel);
}
}
/**
*
* @param channelUseEnvironment
*/
public void removeAll(ChanUseEnv channelUseEnvironment) {
Iterator<String> iterator = channelUseEnvironment.iteratorKeys();
String channelName;
while(iterator.hasNext()) {
channelName = iterator.next();
this.remove(channelName);
}
}

public ChanUseEnv merge(ChanUseEnv other, boolean isParallel)
/*throws ChanUseUnificationException, MoreThanOneWriterException*/ {

// Creates a new env, which will be returned
ChanUseEnv r = new ChanUseEnv();
// Inserts all the channels of this env
r.putAll(this);
// Iterates over the second env
Iterator<String> iterator = other.iteratorKeys();
while(iterator.hasNext()) {
String chanName = iterator.next();
ChanUse otherChanUse = other.get(chanName);
ChanUse newChanUse = otherChanUse;

// If the channel exists in both envs
if (r.containsKey(chanName)) {
ChanUse thisChanUse = r.get(chanName);

if (isParallel) {
// If this merge is a parallelism, permits if they are of
// complementary types
if (thisChanUse.equals(ChanUse.Input) && otherChanUse.equals(ChanUse.Output) ||
thisChanUse.equals(ChanUse.Output) && otherChanUse.equals(ChanUse.Input)) {

// In this case the resulting type is mixed, to indicate
// that it is used in a parallelism
newChanUse = ChanUse.Mixed;
// If they are equal
} else if (otherChanUse.equals(thisChanUse)) {
// The resulting remains the same
newChanUse = otherChanUse;
// If one is mixed and the other is input
} else if (thisChanUse.equals(ChanUse.Mixed) && otherChanUse.equals(ChanUse.Input) ||
thisChanUse.equals(ChanUse.Input) && otherChanUse.equals(ChanUse.Mixed)) {
// Remains mixed
newChanUse = ChanUse.Mixed;
// If one is mixed and the other is output
} /*else if (thisChanUse.equals(ChanUse.Mixed) && otherChanUse.equals(ChanUse.Output) ||
thisChanUse.equals(ChanUse.Output) && otherChanUse.equals(ChanUse.Mixed)) {
// Error because there is more than one writer in the
// parallelism
throw new MoreThanOneWriterException();

// If one is undefined and the other is input
}*/ else if (thisChanUse.equals(ChanUse.Undefined) && otherChanUse.equals(ChanUse.Input) ||
thisChanUse.equals(ChanUse.Input) && otherChanUse.equals(ChanUse.Undefined)) {
// Input
newChanUse = ChanUse.Input;

// If one is undefined and the other is output
} else if (thisChanUse.equals(ChanUse.Undefined) && otherChanUse.equals(ChanUse.Output) ||
thisChanUse.equals(ChanUse.Output) && otherChanUse.equals(ChanUse.Undefined)) {
// Output
newChanUse = ChanUse.Output;
// All other cases is a situation that I did not expected
} else {

// so throw an exception to find out what is happening!
/*throw new ChanUseUnificationException(
chanName, thisChanUse, otherChanUse);*/
}
} else {

// If this is not a composition of a parallelism, then we need
// to have the same types
if (otherChanUse.equals(thisChanUse)) {
newChanUse = otherChanUse;
} else {
// throw new ChanUseUnificationException(chanName, thisChanUse, otherChanUse);
}
}
}/*if (r.containsKey(chanName))*/
r.put(chanName, newChanUse);
}
return r;
}
/**
*
* @param newChannels
* @param oldChannels
* @return
* @throws ChannelAlreadyDefinedException
*/
public ChanUseEnv substitute(ZExprList/*ListTerm*/ newChannels, ZNameList/*ListTerm*/ oldChannels) {
ChanUseEnv r = new ChanUseEnv();
String channelName;
String newChannelName;
Name name;
ChanUse javaType;
int index;
r.putAll(this);
for (int i = 0; i < oldChannels.size(); i++) {
name = (Name) oldChannels.get(i);
newChannelName = ((Name) newChannels.get(i)).toString();
if (r.containsKey(name.toString())) {
javaType = r.get(name.toString());
r.remove(name.toString());
r.put(newChannelName, javaType);
}
}
return r;
}
/**
*
* @return
*/
public Iterator<String> iteratorKeys() {
return this.treeMap_.keySet().iterator();
}
public Set keysFromMainAction (ProcessPara processPara, Spec spec) {
Set keys = this.treeMap_.keySet();
Set <String> keysFromMainAction = new HashSet <String> ();
Vector <String> visibleChannelsFromMainAction = ChannelUtil.getVisibleChannelsFromMainAction(processPara, spec);
keysFromMainAction.addAll(visibleChannelsFromMainAction);
return keysFromMainAction;
}
public Iterator iteratorKeysFromMainAction (ProcessPara processPara, Spec spec) {
return keysFromMainAction (processPara, spec).iterator();
}

/**
*
*/
public void print() {
Iterator<String> it = this.iteratorKeys();
System.out.println("---------------------------");
while(it.hasNext()) {
String chanName = it.next();
ChanUse chanUse = this.get(chanName);
System.out.println(chanName + " " + chanUse);
}
}
}