2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Random;
26 * a simplified version of the AWT RobotManager example
28 public class RobotManager {
41 String processSequence(String sequence){
42 System.out.printf("robot %s processing sequence: %s\n", id, sequence);
47 class StatusAcquisitionThread extends Thread {
50 public StatusAcquisitionThread () {
56 int n = robotList.size();
57 Random random = new Random(0);
60 int idx = random.nextInt(n);
61 Robot robot = robotList.get(idx);
62 setRobotOnline(robot, !isRobotOnline(robot.id));
66 } catch (InterruptedException ix) {
71 public void terminate () {
76 List<Robot> robotList = new ArrayList<Robot>();
77 HashMap<String,Robot> onlineRobots = new HashMap<String,Robot>();
78 StatusAcquisitionThread acquisitionThread;
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"));
86 for (Robot r : robotList) {
87 setRobotOnline(r, true);
91 public void setRobotOnline (Robot robot, boolean isOnline) {
93 onlineRobots.put(robot.getId(), robot);
95 onlineRobots.remove(robot.getId());
99 public boolean isRobotOnline (String robotName) {
100 return onlineRobots.containsKey(robotName);
103 public Robot getOnlineRobot (String robotName) {
104 return onlineRobots.get(robotName);
107 public String sendSequence(Robot robot, String sequence) {
108 return robot.processSequence(sequence);
111 void startStatusAcquisitionThread (){
112 acquisitionThread = new StatusAcquisitionThread();
113 acquisitionThread.start();
116 void stopStatusAcquisitionThread(){
117 acquisitionThread.terminate();
120 void processInput (){
121 String robotName = "POGO-1";
122 String sequence = "left; go";
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);
129 System.out.print("robot not online: ");
130 System.out.println(robotName);
134 public static void main (String[] args){
135 RobotManager robotManager = new RobotManager();
136 robotManager.startStatusAcquisitionThread();
137 robotManager.processInput();
138 //robotManager.stopStatusAcquisitionThread();