Mercurial > hg > Members > kono > jpf-core
comparison src/examples/RobotManager.java @ 0:61d41facf527
initial v8 import (history reset)
author | Peter Mehlitz <Peter.C.Mehlitz@nasa.gov> |
---|---|
date | Fri, 23 Jan 2015 10:14:01 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:61d41facf527 |
---|---|
1 /* | |
2 * Copyright (C) 2014, United States Government, as represented by the | |
3 * Administrator of the National Aeronautics and Space Administration. | |
4 * All rights reserved. | |
5 * | |
6 * The Java Pathfinder core (jpf-core) platform is licensed under the | |
7 * Apache License, Version 2.0 (the "License"); you may not use this file except | |
8 * in compliance with the License. You may obtain a copy of the License at | |
9 * | |
10 * http://www.apache.org/licenses/LICENSE-2.0. | |
11 * | |
12 * Unless required by applicable law or agreed to in writing, software | |
13 * distributed under the License is distributed on an "AS IS" BASIS, | |
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 * See the License for the specific language governing permissions and | |
16 * limitations under the License. | |
17 */ | |
18 | |
19 | |
20 import java.util.ArrayList; | |
21 import java.util.HashMap; | |
22 import java.util.List; | |
23 import java.util.Random; | |
24 | |
25 /** | |
26 * a simplified version of the AWT RobotManager example | |
27 */ | |
28 public class RobotManager { | |
29 | |
30 static class Robot { | |
31 String id; | |
32 | |
33 Robot (String id){ | |
34 this.id = id; | |
35 } | |
36 | |
37 String getId(){ | |
38 return id; | |
39 } | |
40 | |
41 String processSequence(String sequence){ | |
42 System.out.printf("robot %s processing sequence: %s\n", id, sequence); | |
43 return "Ok"; | |
44 } | |
45 } | |
46 | |
47 class StatusAcquisitionThread extends Thread { | |
48 boolean done; | |
49 | |
50 public StatusAcquisitionThread () { | |
51 setDaemon(true); | |
52 } | |
53 | |
54 @Override | |
55 public void run () { | |
56 int n = robotList.size(); | |
57 Random random = new Random(0); | |
58 | |
59 while (!done) { | |
60 int idx = random.nextInt(n); | |
61 Robot robot = robotList.get(idx); | |
62 setRobotOnline(robot, !isRobotOnline(robot.id)); | |
63 | |
64 try { | |
65 Thread.sleep(3000); | |
66 } catch (InterruptedException ix) { | |
67 } | |
68 } | |
69 } | |
70 | |
71 public void terminate () { | |
72 done = true; | |
73 } | |
74 } | |
75 | |
76 List<Robot> robotList = new ArrayList<Robot>(); | |
77 HashMap<String,Robot> onlineRobots = new HashMap<String,Robot>(); | |
78 StatusAcquisitionThread acquisitionThread; | |
79 | |
80 public RobotManager() { | |
81 robotList.add( new Robot("RATS-1")); | |
82 robotList.add( new Robot("RATS-2")); | |
83 robotList.add( new Robot("RCAT-1")); | |
84 robotList.add( new Robot("POGO-1")); | |
85 | |
86 for (Robot r : robotList) { | |
87 setRobotOnline(r, true); | |
88 } | |
89 } | |
90 | |
91 public void setRobotOnline (Robot robot, boolean isOnline) { | |
92 if (isOnline) { | |
93 onlineRobots.put(robot.getId(), robot); | |
94 } else { | |
95 onlineRobots.remove(robot.getId()); | |
96 } | |
97 } | |
98 | |
99 public boolean isRobotOnline (String robotName) { | |
100 return onlineRobots.containsKey(robotName); | |
101 } | |
102 | |
103 public Robot getOnlineRobot (String robotName) { | |
104 return onlineRobots.get(robotName); | |
105 } | |
106 | |
107 public String sendSequence(Robot robot, String sequence) { | |
108 return robot.processSequence(sequence); | |
109 } | |
110 | |
111 void startStatusAcquisitionThread (){ | |
112 acquisitionThread = new StatusAcquisitionThread(); | |
113 acquisitionThread.start(); | |
114 } | |
115 | |
116 void stopStatusAcquisitionThread(){ | |
117 acquisitionThread.terminate(); | |
118 } | |
119 | |
120 void processInput (){ | |
121 String robotName = "POGO-1"; | |
122 String sequence = "left; go"; | |
123 | |
124 if (isRobotOnline(robotName)){ | |
125 Robot robot = getOnlineRobot( robotName); | |
126 String result = robot.processSequence(sequence); | |
127 System.out.printf("sent sequence \"%s\" to robot %s => %s\n", sequence, robotName, result); | |
128 } else { | |
129 System.out.print("robot not online: "); | |
130 System.out.println(robotName); | |
131 } | |
132 } | |
133 | |
134 public static void main (String[] args){ | |
135 RobotManager robotManager = new RobotManager(); | |
136 robotManager.startStatusAcquisitionThread(); | |
137 robotManager.processInput(); | |
138 //robotManager.stopStatusAcquisitionThread(); | |
139 } | |
140 } |