egisss633

JavaJCP

Jan 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. import org.jcsp.lang.CSProcess;
  2. import org.jcsp.lang.Parallel;
  3. import org.jcsp.lang.*;
  4.  
  5. import java.util.ArrayList;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. Processes processes = new Processes();
  10. processes.execute();
  11.  
  12. }
  13. }
  14. class Processes
  15. {
  16. private int SenderProcessesCount = 3;
  17. private int ReceiverProcessesCount = 2;
  18. Parallel proceses = new Parallel();
  19. Any2OneChannel SenderChannel = Channel.any2one();
  20. One2AnyChannel ResultsChannel = Channel.one2any();
  21. One2AnyChannel ContinueToSendItems = Channel.one2any();
  22. One2AnyChannel ContinueToReadItems = Channel.one2any();
  23.  
  24. ArrayList<Sender> SenderProcesses = new ArrayList<Sender>();
  25. ArrayList<Receiver> ReceiverProcesses = new ArrayList<Receiver>();
  26. Manager managerProcess;
  27. public Processes()
  28. {
  29. for(int i = 0; i < 3; i++)
  30. {
  31. Sender process = new Sender(i+1, SenderChannel.out(), ContinueToSendItems.in());
  32. SenderProcesses.add(process);
  33. }
  34. for (int i = 0; i < 2; i++)
  35. {
  36. Receiver process = new Receiver(i+4, ResultsChannel.in(),ContinueToReadItems.in());
  37. ReceiverProcesses.add(process);
  38. }
  39. managerProcess = new Manager(SenderChannel.in(),ResultsChannel.out(),ContinueToSendItems.out(), ContinueToReadItems.out());
  40. }
  41. public void execute()
  42. {
  43. proceses.addProcess(managerProcess);
  44. for (int i = 0; i < SenderProcessesCount; i++)
  45. {
  46. proceses.addProcess(SenderProcesses.get(i));
  47. }
  48. for (int i = 0; i < ReceiverProcessesCount; i++)
  49. {
  50. proceses.addProcess(ReceiverProcesses.get(i));
  51. }
  52. proceses.run();
  53. }
  54.  
  55. }
  56. class Sender implements CSProcess
  57. {
  58. private ChannelOutput outputChannel;
  59. private int processNumber;
  60. private ChannelInput ContinueToSendItems;
  61. private boolean Continue;
  62. public Sender(int processNumber, ChannelOutput outputChannel, ChannelInput continueToSendItems)
  63. {
  64. this.outputChannel = outputChannel;
  65. this.processNumber = processNumber;
  66. this.ContinueToSendItems = continueToSendItems;
  67. this.Continue = true;
  68. }
  69. @Override
  70. public void run()
  71. {
  72. while(Continue)
  73. {
  74. outputChannel.write(processNumber);
  75. boolean cont = (boolean) ContinueToSendItems.read();
  76. Continue = cont;
  77. }
  78. }
  79. }
  80. class NandM
  81. {
  82. private int n;
  83. private int m;
  84. public NandM(int n, int m)
  85. {
  86. this.n = n;
  87. this.m = m;
  88. }
  89. public int GetN()
  90. {
  91. return this.n;
  92. }
  93. public int GetM()
  94. {
  95. return this.m;
  96. }
  97. }
  98. class Receiver implements CSProcess {
  99. private int processNumber;
  100. private ChannelInput inputChannel;
  101. private ChannelInput ContinueToReadItems;
  102. private boolean Continue;
  103.  
  104. public Receiver(int processNumber, ChannelInput inputChannel, ChannelInput continueToReadItems) {
  105. this.Continue = true;
  106. this.ContinueToReadItems = continueToReadItems;
  107. this.processNumber = processNumber;
  108. this.inputChannel = inputChannel;
  109. }
  110.  
  111. @Override
  112. public void run()
  113. {
  114. while(Continue)
  115. {
  116. NandM obj = (NandM) inputChannel.read();
  117.  
  118. System.out.print("ProcessNumber: ");
  119. System.out.print(processNumber);
  120. System.out.print(" n: ");
  121. System.out.print(obj.GetN());
  122. System.out.print(" m: ");
  123. System.out.print(obj.GetM());
  124. System.out.println(" ");
  125. boolean cont = (boolean) ContinueToReadItems.read();
  126. Continue = cont;
  127. }
  128. }
  129. }
  130. class Manager implements CSProcess
  131. {
  132.  
  133. private int n;
  134. private int m;
  135. private int changedCount;
  136. private ChannelInput inputChannel;
  137. private ChannelOutput outputChannel;
  138. private ChannelOutput ContinueToSendItems;
  139. private ChannelOutput ContinueToReadItems;
  140. public Manager(ChannelInput inputChannel, ChannelOutput outputChannel, ChannelOutput continueToSendItems, ChannelOutput continueToReadItems)
  141. {
  142. n = 0;
  143. m = 100;
  144. changedCount = 0;
  145. this.ContinueToReadItems = continueToReadItems;
  146. this.ContinueToSendItems = continueToSendItems;
  147. this.inputChannel = inputChannel;
  148. this.outputChannel = outputChannel;
  149. }
  150. @Override
  151. public void run() {
  152. while (Math.abs(n - m) > 5) {
  153. int i = (int) inputChannel.read();
  154. ContinueToSendItems.write(true);
  155. n = n + i;
  156. m = m - i;
  157. changedCount++;
  158.  
  159. if (changedCount == 2)
  160. {
  161. NandM obj = new NandM(n, m);
  162. outputChannel.write(obj);
  163. changedCount = 0;
  164. ContinueToReadItems.write(true);
  165.  
  166. }
  167. }
  168. ContinueToReadItems.write(false);
  169. ContinueToSendItems.write(false);
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment