Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.jcsp.lang.CSProcess;
- import org.jcsp.lang.Parallel;
- import org.jcsp.lang.*;
- import java.util.ArrayList;
- public class Main {
- public static void main(String[] args) {
- Processes processes = new Processes();
- processes.execute();
- }
- }
- class Processes
- {
- private int SenderProcessesCount = 3;
- private int ReceiverProcessesCount = 2;
- Parallel proceses = new Parallel();
- Any2OneChannel SenderChannel = Channel.any2one();
- One2AnyChannel ResultsChannel = Channel.one2any();
- One2AnyChannel ContinueToSendItems = Channel.one2any();
- One2AnyChannel ContinueToReadItems = Channel.one2any();
- ArrayList<Sender> SenderProcesses = new ArrayList<Sender>();
- ArrayList<Receiver> ReceiverProcesses = new ArrayList<Receiver>();
- Manager managerProcess;
- public Processes()
- {
- for(int i = 0; i < 3; i++)
- {
- Sender process = new Sender(i+1, SenderChannel.out(), ContinueToSendItems.in());
- SenderProcesses.add(process);
- }
- for (int i = 0; i < 2; i++)
- {
- Receiver process = new Receiver(i+4, ResultsChannel.in(),ContinueToReadItems.in());
- ReceiverProcesses.add(process);
- }
- managerProcess = new Manager(SenderChannel.in(),ResultsChannel.out(),ContinueToSendItems.out(), ContinueToReadItems.out());
- }
- public void execute()
- {
- proceses.addProcess(managerProcess);
- for (int i = 0; i < SenderProcessesCount; i++)
- {
- proceses.addProcess(SenderProcesses.get(i));
- }
- for (int i = 0; i < ReceiverProcessesCount; i++)
- {
- proceses.addProcess(ReceiverProcesses.get(i));
- }
- proceses.run();
- }
- }
- class Sender implements CSProcess
- {
- private ChannelOutput outputChannel;
- private int processNumber;
- private ChannelInput ContinueToSendItems;
- private boolean Continue;
- public Sender(int processNumber, ChannelOutput outputChannel, ChannelInput continueToSendItems)
- {
- this.outputChannel = outputChannel;
- this.processNumber = processNumber;
- this.ContinueToSendItems = continueToSendItems;
- this.Continue = true;
- }
- @Override
- public void run()
- {
- while(Continue)
- {
- outputChannel.write(processNumber);
- boolean cont = (boolean) ContinueToSendItems.read();
- Continue = cont;
- }
- }
- }
- class NandM
- {
- private int n;
- private int m;
- public NandM(int n, int m)
- {
- this.n = n;
- this.m = m;
- }
- public int GetN()
- {
- return this.n;
- }
- public int GetM()
- {
- return this.m;
- }
- }
- class Receiver implements CSProcess {
- private int processNumber;
- private ChannelInput inputChannel;
- private ChannelInput ContinueToReadItems;
- private boolean Continue;
- public Receiver(int processNumber, ChannelInput inputChannel, ChannelInput continueToReadItems) {
- this.Continue = true;
- this.ContinueToReadItems = continueToReadItems;
- this.processNumber = processNumber;
- this.inputChannel = inputChannel;
- }
- @Override
- public void run()
- {
- while(Continue)
- {
- NandM obj = (NandM) inputChannel.read();
- System.out.print("ProcessNumber: ");
- System.out.print(processNumber);
- System.out.print(" n: ");
- System.out.print(obj.GetN());
- System.out.print(" m: ");
- System.out.print(obj.GetM());
- System.out.println(" ");
- boolean cont = (boolean) ContinueToReadItems.read();
- Continue = cont;
- }
- }
- }
- class Manager implements CSProcess
- {
- private int n;
- private int m;
- private int changedCount;
- private ChannelInput inputChannel;
- private ChannelOutput outputChannel;
- private ChannelOutput ContinueToSendItems;
- private ChannelOutput ContinueToReadItems;
- public Manager(ChannelInput inputChannel, ChannelOutput outputChannel, ChannelOutput continueToSendItems, ChannelOutput continueToReadItems)
- {
- n = 0;
- m = 100;
- changedCount = 0;
- this.ContinueToReadItems = continueToReadItems;
- this.ContinueToSendItems = continueToSendItems;
- this.inputChannel = inputChannel;
- this.outputChannel = outputChannel;
- }
- @Override
- public void run() {
- while (Math.abs(n - m) > 5) {
- int i = (int) inputChannel.read();
- ContinueToSendItems.write(true);
- n = n + i;
- m = m - i;
- changedCount++;
- if (changedCount == 2)
- {
- NandM obj = new NandM(n, m);
- outputChannel.write(obj);
- changedCount = 0;
- ContinueToReadItems.write(true);
- }
- }
- ContinueToReadItems.write(false);
- ContinueToSendItems.write(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment