package jcircus.discardedparallelism;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import jcircus.complementaryenvs.CallProc2ChannelSetEnv;
import jcircus.complementaryenvs.PId2PNameEnv;
import jcircus.complementaryenvs.ProcProcessParaEnv;
import jcircus.environment.ChanInfoEnv;
import jcircus.environment.ProcChanUseEnv;
import jcircus.util.ChanUse;
import jcircus.util.Constants;
import jcircus.util.Util;
public class DiscardedParallelism {
//Versão do método instChannelSimple para compartilhamento de front-end;
public static String instChannelSimple(String channelName, ProcChanUseEnv chanMsEnv,
Integer procId, boolean isMain, ProcProcessParaEnv procProcessParaEnv, PId2PNameEnv id2name, CallProc2ChannelSetEnv cp2cse) {
String code = "";
//List<List <Integer>> ids = resolveUndefinedChannels(chanMsEnv, isMain, channelName, procProcessParaEnv); //By Angela Freitas
List<List <Integer>> ids = resolveUndefinedChannels(chanMsEnv, isMain, channelName, procProcessParaEnv, id2name, cp2cse);
// Declaration of chanInfo
code += "\n" + Constants.CLS_CHINFO + " chanInfo_" + channelName + " = new " + Constants.CLS_CHINFO + "();";
// Initialization of chanInfo
/*for (int i = 0; i < ids.size(); i++) { //By Angela Freitas
Integer subProcId = (Integer) ids.get(i);
code = code + "\nchanInfo_" + channelName +
".put(new Integer(" + subProcId + "), new Integer(" + i + "));";
}*/
for (int i = 0; i < ids.size(); i++) { //By Samuel Barrocas, 16/12/2010, 18:14hs
for (int j = 0; j < ids.get(i).size(); j++) {
Integer subProcId = (Integer) ids.get(i).get(j);
code = code + "\nchanInfo_" + channelName +
".put(new Integer(" + subProcId + "), new Integer(" + i + "));";
}
}
// Instantiation of GeneralChannel
/*code += "\nthis." + channelName + " = new GeneralChannel(" +
"new Any2OneChannel(), chanInfo_" + channelName +
", new Integer(" + procId.intValue() + "));";*/ //Angela's, JCSP antigo
code += "\nthis." + channelName + " = new /*IV*/ GeneralChannel(" +
"org.jcsp.lang.Channel.any2one (), chanInfo_" + channelName +
", new Integer(" + procId.intValue() + "));"; //Sam's, JCSP novo
return code;
}
//VERSÃO DO MÉTODO resolveUndefinedChannels para compartilhamento de front-end
//Este método originalmente foi implementado por Angela Freitas,
//mas foi refeito com a intenção de permitir a implementação do paralelismo com compartilhamento de front-end...
public static List<List <Integer>> resolveUndefinedChannels(ProcChanUseEnv chanMSEnv,
boolean isMain, String channelName, ProcProcessParaEnv procProcessParaEnv, PId2PNameEnv id2name, CallProc2ChannelSetEnv cp2ces) {
// Creates an array
List <Integer> [] array;
List <Integer> processesOnChannel = new ArrayList <Integer> ();// = new List <Integer> ();
//processesOnChannel.add(new Integer (0)); //De olho nesta linha, pode ser que ela gere erro, acrescentei para tirar o IndexOutOfBouds que deu em Translator2Java, linha 656
List <List <Integer>> processesNotOnChannel = new ArrayList <List <Integer>> ();
processesNotOnChannel.add(new ArrayList <Integer> ());
boolean writerDefined = false;
int index = 0;
boolean shared = false;
if (!chanMSEnv.isSync()) {
// There is no synchronization involving the channel
// Will take part in gui, in case isMain is true
if (!isMain) {
System.out.println ("SITUAÇÃO 1");
array = new List [chanMSEnv.size()];
// If this is being called from a constructor of a process (that is,
// the channel is hidden, then there is no concern about the gui.
// We just add all the processes in chanMSEnv to any position
// of the array.
Iterator it = chanMSEnv.iteratorKeys();
while(it.hasNext()) {
Integer procId = (Integer) it.next();
//Abaixo, by Samuel Barrocas
//TODO retirar este trecho de código (Abaixo..., e Acima...),
//pois neste caso não temos sincronização.
//Como não temos sincronização (!chanMSEnv.isSync()), não temos interleaving,
//e portanto não há a necessidade de compartilhar front-end.
ChanInfoEnv chanInfoEnv = Util.getChannelMSEnvAnn(procProcessParaEnv.get(id2name.get(procId))); //(procId);
Iterator itChannels = chanInfoEnv.iteratorKeys();
boolean containsChannel = false;
while (itChannels.hasNext()) {
if (itChannels.toString().equals(channelName)) {
containsChannel = true;
}
}
if (containsChannel) {
processesOnChannel.add(procId);
array [0].add(procId);
}
//Acima, by Samuel Barrocas
processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
processesNotOnChannel.get(index).add(procId);
array[index] = processesNotOnChannel.get(index);
index++;
}
} else {
System.out.println ("SITUAÇÃO 2");
array = new List [chanMSEnv.size()+1]; // +1 for the gui
// Checks if the process will be input or output
ChanUse chanUseNotSync = chanMSEnv.getChanUseGuiNotSyncChannel();
if(chanUseNotSync.equals(ChanUse.Input)) { //TESTADO
System.out.println ("SITUAÇÃO 2.1");
// gui will be output
List <Integer> l_int = new ArrayList <Integer> (); //By Samuel Barrocas
l_int.add(new Integer (-1)); //By Samuel Barrocas
array[0] = l_int; //By Samuel Barrocas
//array[0] = new Integer(-1); //By Angela Freitas
// iterates over the array, inserting the other positions
index = 1;
Iterator it = chanMSEnv.iteratorKeys();
while(it.hasNext()) {
Integer procId = (Integer) it.next();
ChanUse chanUse = chanMSEnv.get(procId);
processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
processesNotOnChannel.get(index).add(procId); //By Samuel Barrocas
array[index] = processesNotOnChannel.get(index); //By Samuel Barrocas
//array[index] = procId; //By Angela Freitas
index++;
}
} else {
System.out.println ("SITUAÇÃO 2.2"); //TESTADO
// gui will be input
List <Integer> l_int = new ArrayList <Integer> (); //By Samuel Barrocas
l_int.add(new Integer (-1)); //By Samuel Barrocas
array[1] = l_int; //By Samuel Barrocas
//array[1] = new Integer(-1); //By Angela Freitas
index = 2;
writerDefined = false;
Iterator it = chanMSEnv.iteratorKeys();
while(it.hasNext()) {
Integer procId = (Integer) it.next();
ChanUse chanUse = chanMSEnv.get(procId);
// The first Undefined found goes to the writer position.
// There will be a bug if there is more than one Undefined
// because then only one of them will go to the writer
// position, therefore the others will try to read the
// channel instead of write.
if (chanUse.equals(ChanUse.Undefined) && !writerDefined) { //TODO? //Samuel Barrocas
processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
processesNotOnChannel.get(0).add(procId); //By Samuel Barrocas
array[0] = processesNotOnChannel.get(0); //By Samuel Barrocas
//array[0] = procId; //By Angela Freitas
writerDefined = true;
} else { //TODO?
if (index == chanMSEnv.size() + 1) {
// Reached the end and the writer had not been defined
processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
processesNotOnChannel.get(0).add(procId); //By Samuel Barrocas
array[0] = processesNotOnChannel.get(0); //By Samuel Barrocas
//array[0] = procId; //By Angela Freitas
}
}
}
}
}
} else {
System.out.println ("SITUAÇÃO 3");
array = new List [chanMSEnv.size()];
for (int i = 0; i < chanMSEnv.size(); i++) {
array [i] = new ArrayList <Integer> ();
}
// There is simple, or multiple synchronization involving the channel
index = 1;
writerDefined = false;
Iterator it = chanMSEnv.iteratorKeys();
while(it.hasNext()) {
Integer procId = (Integer) it.next();
ChanUse chanUse = chanMSEnv.get(procId);
if (chanUse.equals(ChanUse.Output) && !writerDefined) {
System.out.println ("SITUAÇÃO 3.1");
// Output channel
writerDefined = true;
//Abaixo, by Samuel Barrocas
ChanInfoEnv chanInfoEnv = Util.getChannelMSEnvAnn(procProcessParaEnv.get(id2name.get(procId)));
Iterator itChannels = chanInfoEnv.iteratorKeys();
boolean containsChannel = false;
while (itChannels.hasNext()) {
if (itChannels.next().toString().equals(channelName)) {
containsChannel = true;
}
}
if (containsChannel && !cp2ces.get(procId).contains(channelName)) {
processesOnChannel.add(procId);
array [0].add(procId);
shared = true;
System.out.println ("array[0].add (" + procId + ")");
}
else {
if (index == array.length && !shared)
array [0].add(procId);
else {
array [index].add(procId);
index++;
}
System.out.println ("array [index++].add (" + procId + ")");
}
//Acima, by Samuel Barrocas
//processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
//processesNotOnChannel.get(0).add(procId); //By Samuel Barrocas
//array[0] = processesNotOnChannel.get(0); //By Samuel Barrocas
//array[0] = procId; //By Angela Freitas
} else {
System.out.println ("SITUAÇÃO 3.2"); //TESTADO
// Input channel or Undefined channel
//Abaixo, by Samuel Barrocas
ChanInfoEnv chanInfoEnv = Util.getChannelMSEnvAnn(procProcessParaEnv.get(id2name.get(procId)));
Iterator itChannels = chanInfoEnv.iteratorKeys();
boolean containsChannel = false;
while (itChannels.hasNext()) {
if (itChannels.next().toString().equals(channelName)) {
containsChannel = true;
}
}
if (containsChannel && !cp2ces.get(procId).contains(channelName)) {
processesOnChannel.add(procId);
array [0].add(procId);
shared = true;
System.out.println ("array[0].add (" + procId + ")");
}
else {
if (index == array.length && !shared)
array [0].add(procId);
else {
array [index].add(procId);
index++;
}
System.out.println ("array [index++].add (" + procId + ")");
}
//Acima, by Samuel Barrocas
//index++; //By Angela Freitas, comentado em 16/12/2010, às 20:30hs, pouco depois de eu colar o trecho de código acima, delimitado por "Acima...", e "Abaixo..."
// This is the last element and there is any writer in the array
/*if (index == array.length)
index = 0;*/
processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
processesNotOnChannel.get(index).add(procId); //By Samuel Barrocas
array[index] = processesNotOnChannel.get(0); //By Samuel Barrocas
//array[index] = procId; //By Angela Freitas
}
}
}
// Transforms the array into a list
List<List <Integer>> list = new ArrayList<List <Integer>>();
for (int i=0; i<array.length; i++) {
list.add(array[i]);
}
return list;
}
}