2
import org.jcsp.demos.util.*;
3
import org.jcsp.lang.*;
4
import org.jcsp.util.*;
5
import org.jcsp.plugNplay.*;
8
import java.util.Random;
9
import java.util.ArrayList;
11
public class AltingBarrierGadget6Demo0 {
13
public static void main (String[] argv) {
15
final int width = Ask.Int ("\nwidth = ", 10, 30);
16
final int depth = Ask.Int ("depth = ", 1, 30);
18
final int span = Ask.Int ("length of arms = ", 1, width/2);
20
final int offInterval =
21
Ask.Int ("off interval (millisecs) = ", 100, 10000);
22
final int standbyInterval =
23
Ask.Int ("standby interval (millisecs) = ", 100, 20000);
24
final int playInterval =
25
Ask.Int ("play interval (millisecs) = ", 1000, 1000000000);
26
final int countInterval =
27
Ask.Int ("count interval (millisecs) = ", 10, 10000);
29
final Color offColour = Color.black, standbyColour = Color.lightGray;
33
final One2OneChannel[][] click = new One2OneChannel[depth][];
34
for (int i = 0; i < depth; i++) {
35
click[i] = Channel.one2oneArray (width, new OverWriteOldestBuffer (1), width);
38
final One2OneChannel[][] configure = new One2OneChannel[depth][];
39
for (int i = 0; i < depth; i++) {
40
configure[i] = Channel.one2oneArray (width);
43
final FramedButtonGrid buttons =
44
new FramedButtonGrid (
45
"AltingBarier: Gadget 6, Demo 0", depth, width,
46
20 + (depth*50), width*50, Util.get2DInputArray(configure), Util.get2DOutputArray(click)
49
// make labels for the buttons (read-only shared by all the gadgets)
51
final String[] label = new String[playInterval/countInterval];
52
for (int i = 0; i < label.length; i++) {
53
label[i] = String.valueOf (i);
58
final ArrayList[][] connect = new ArrayList [depth][width];
59
for (int row = 0; row < depth; row++) {
60
ArrayList[] connectRow = connect[row];
61
for (int col = 0; col < width; col++) {
62
connectRow[col] = new ArrayList (8*span); // estimate initial size
66
final Random random = new Random ();
68
final Barrier bar = new Barrier (width*depth);
70
final AltingBarrierGadget6[][] gadgets =
71
new AltingBarrierGadget6[depth][width];
73
for (int row = 0; row < depth; row++) {
74
for (int col = 0; col < width; col++) {
76
new AltingBarrierGadget6 (
77
connect[row][col], shape (random, row, col, span, connect), bar,
78
click[row][col].in(), configure[row][col].out(),
79
offColour, standbyColour,
80
offInterval, standbyInterval,
81
playInterval, countInterval,
91
buttons, new Parallel (gadgets)
97
static ArrayList[] shape (
98
Random random, int row, int col, int span, ArrayList[][] connect
100
switch (random.nextInt (3)) {
102
return shapePlus (row, col, span, connect);
104
return shapeCross (row, col, span, connect);
106
return shapeCircle (row, col, span, connect);
112
static ArrayList[] shapePlus (
113
int row, int col, int span, ArrayList[][] connect
115
final int n = 4*span;
116
final ArrayList[] collect = new ArrayList[n];
118
for (int i = 1; i <= span; i++) {
119
collect[index] = get (connect, row-i, col);
121
collect[index] = get (connect, row, col+i);
123
collect[index] = get (connect, row+i, col);
125
collect[index] = get (connect, row, col-i);
131
static ArrayList[] shapeCross (
132
int row, int col, int span, ArrayList[][] connect
134
final int n = 4*span;
135
final ArrayList[] collect = new ArrayList[n];
137
for (int i = 1; i <= span; i++) {
138
collect[index] = get (connect, row-i, col-i);
140
collect[index] = get (connect, row-i, col+i);
142
collect[index] = get (connect, row+i, col-i);
144
collect[index] = get (connect, row+i, col+i);
150
static ArrayList[] shapeCircle (
151
int row, int col, int span, ArrayList[][] connect
153
final int n = (4*span) - 1;
154
final ArrayList[] collect = new ArrayList[n];
156
for (int i = 0; i < (span - 1); i ++) {
158
collect[index] = get (connect, row, col);
162
for (int i = 0; i < span; i++) {
164
collect[index] = get (connect, row, col);
168
for (int i = 0; i < span; i++) {
170
collect[index] = get (connect, row, col);
174
for (int i = 0; i < span; i++) {
176
collect[index] = get (connect, row, col);
182
static ArrayList get (ArrayList[][] connect, int row, int col) {
183
final int depth = connect.length;
184
row = (row + depth)%depth;
185
final int width = connect[row].length;
186
col = (col + width)%width;
187
return connect[row][col];