~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/jcsp-demos/altingBarriers/AltingBarrierGadget6Demo0.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import org.jcsp.demos.util.*;
 
3
import org.jcsp.lang.*;
 
4
import org.jcsp.util.*;
 
5
import org.jcsp.plugNplay.*;
 
6
 
 
7
import java.awt.Color;
 
8
import java.util.Random;
 
9
import java.util.ArrayList;
 
10
 
 
11
public class AltingBarrierGadget6Demo0 {
 
12
 
 
13
  public static void main (String[] argv) {
 
14
 
 
15
    final int width = Ask.Int ("\nwidth = ", 10, 30);
 
16
    final int depth = Ask.Int ("depth = ", 1, 30);
 
17
 
 
18
    final int span = Ask.Int ("length of arms = ", 1, width/2);
 
19
 
 
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);
 
28
    
 
29
    final Color offColour = Color.black, standbyColour = Color.lightGray;
 
30
    
 
31
    // make the buttons
 
32
 
 
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);
 
36
    }
 
37
    
 
38
    final One2OneChannel[][] configure = new One2OneChannel[depth][];
 
39
    for (int i = 0; i < depth; i++) {
 
40
      configure[i] = Channel.one2oneArray (width);
 
41
    }
 
42
 
 
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)
 
47
      );
 
48
 
 
49
    // make labels for the buttons (read-only shared by all the gadgets)
 
50
    
 
51
    final String[] label = new String[playInterval/countInterval];
 
52
    for (int i = 0; i < label.length; i++) {
 
53
      label[i] = String.valueOf (i);
 
54
    }
 
55
 
 
56
    // make the gadgets
 
57
 
 
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
 
63
      }
 
64
    }
 
65
 
 
66
    final Random random = new Random ();
 
67
 
 
68
    final Barrier bar = new Barrier (width*depth);
 
69
 
 
70
    final AltingBarrierGadget6[][] gadgets =
 
71
      new AltingBarrierGadget6[depth][width];
 
72
 
 
73
    for (int row = 0; row < depth; row++) {
 
74
      for (int col = 0; col < width; col++) {
 
75
        gadgets[row][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,
 
82
            label
 
83
          );
 
84
      }
 
85
    }
 
86
 
 
87
    // run everything
 
88
 
 
89
    new Parallel (
 
90
      new CSProcess[] {
 
91
        buttons, new Parallel (gadgets)
 
92
      }
 
93
    ).run ();
 
94
 
 
95
  }
 
96
 
 
97
  static ArrayList[] shape (
 
98
    Random random, int row, int col, int span, ArrayList[][] connect
 
99
  ) {
 
100
    switch (random.nextInt (3)) {
 
101
      case 0:
 
102
        return shapePlus (row, col, span, connect);
 
103
      case 1:
 
104
        return shapeCross (row, col, span, connect);
 
105
      case 2:
 
106
        return shapeCircle (row, col, span, connect);
 
107
      default:
 
108
        return null;
 
109
    }
 
110
  }
 
111
 
 
112
  static ArrayList[] shapePlus (
 
113
    int row, int col, int span, ArrayList[][] connect
 
114
  ) {
 
115
    final int n = 4*span;
 
116
    final ArrayList[] collect = new ArrayList[n];
 
117
    int index = 0;
 
118
    for (int i = 1; i <= span; i++) {
 
119
      collect[index] = get (connect, row-i, col);
 
120
      index++;
 
121
      collect[index] = get (connect, row, col+i);
 
122
      index++;
 
123
      collect[index] = get (connect, row+i, col);
 
124
      index++;
 
125
      collect[index] = get (connect, row, col-i);
 
126
      index++;
 
127
    }
 
128
    return collect;
 
129
  }
 
130
 
 
131
  static ArrayList[] shapeCross (
 
132
    int row, int col, int span, ArrayList[][] connect
 
133
  ) {
 
134
    final int n = 4*span;
 
135
    final ArrayList[] collect = new ArrayList[n];
 
136
    int index = 0;
 
137
    for (int i = 1; i <= span; i++) {
 
138
      collect[index] = get (connect, row-i, col-i);
 
139
      index++;
 
140
      collect[index] = get (connect, row-i, col+i);
 
141
      index++;
 
142
      collect[index] = get (connect, row+i, col-i);
 
143
      index++;
 
144
      collect[index] = get (connect, row+i, col+i);
 
145
      index++;
 
146
    }
 
147
    return collect;
 
148
  }
 
149
 
 
150
  static ArrayList[] shapeCircle (
 
151
    int row, int col, int span, ArrayList[][] connect
 
152
  ) {
 
153
    final int n = (4*span) - 1;
 
154
    final ArrayList[] collect = new ArrayList[n];
 
155
    int index = 0;
 
156
    for (int i = 0; i < (span - 1); i ++) {
 
157
      col++;
 
158
      collect[index] = get (connect, row, col);
 
159
      index++;
 
160
    }
 
161
    col++;
 
162
    for (int i = 0; i < span; i++) {
 
163
      row--;
 
164
      collect[index] = get (connect, row, col);
 
165
      index++;
 
166
    }
 
167
    row--;
 
168
    for (int i = 0; i < span; i++) {
 
169
      col--;
 
170
      collect[index] = get (connect, row, col);
 
171
      index++;
 
172
    }
 
173
    col--;
 
174
    for (int i = 0; i < span; i++) {
 
175
      row++;
 
176
      collect[index] = get (connect, row, col);
 
177
      index++;
 
178
    }
 
179
    return collect;
 
180
  }
 
181
 
 
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];
 
188
  }
 
189
 
 
190
}