Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- import TSim.CommandException;
- import TSim.SensorEvent;
- import TSim.TSimInterface;
- public class Lab2 {
- private static int sim_speed;
- private static final int MAXSPEED = 15;
- private static TrackMonitor[] tracks;
- private TSimInterface tsi;
- public static void main(String[] args){
- new Lab2(args);
- }
- public Lab2(String[] args) {
- // Create all the track monitors
- tracks = new TrackMonitor[]{
- new TrackMonitor(new Pos[]{new Pos(1,1),new Pos(2,2)},new Pos[]{new Pos(1,1),new Pos(2,2)},new Switch(1,1,1)),
- new TrackMonitor(new Pos[]{new Pos(1,1),new Pos(2,2)},new Pos[]{new Pos(1,1),new Pos(2,2)},new Switch(1,1,1)),
- new TrackMonitor(new Pos[]{new Pos(1,1),new Pos(2,2)},new Pos[]{new Pos(1,1),new Pos(2,2)},new Switch(1,1,1)),
- new TrackMonitor(new Pos[]{new Pos(1,1),new Pos(2,2)},new Pos[]{new Pos(1,1),new Pos(2,2)},new Switch(1,1,1)),
- new TrackMonitor(new Pos[]{new Pos(1,10)},new Pos[]{new Pos(19,8)},new Switch(4,9,tsi.SWITCH_LEFT)),
- new TrackMonitor(new Pos[]{new Pos(4,13),new Pos(6,11)},new Pos[]{new Pos(7,9)},new Switch(4,9,tsi.SWITCH_LEFT)),
- new TrackMonitor(new Pos[]{new Pos(4,13),new Pos(6,11)},new Pos[]{new Pos(6,10)},new Switch(4,9,tsi.SWITCH_RIGHT)),
- new TrackMonitor(new Pos[]{new Pos(1,10)},new Pos[]{new Pos(6,11)},new Switch(3,11,tsi.SWITCH_LEFT)),
- new TrackMonitor(new Pos(4,13),new Pos(4,13),new Switch(3,11,tsi.SWITCH_RIGHT))
- };
- // Test if speeds for the trains are set, if not randomize a number between 1 and MAXSPEED
- Random random = new Random();
- if(args.length == 0){
- args = new String[1];
- args[0] = ""+(random.nextInt(14)+1);
- }
- if(args.length <= 1){
- String [] temp = args.clone();
- args = new String[args.length + 1];
- System.arraycopy(temp, 0, args, 0, temp.length);
- args[1] = ""+(random.nextInt(14)+1);
- }
- // Test if sim speed has been given, if not default it to 100.
- if(args.length <= 2){
- this.sim_speed = 100;
- }
- else{
- this.sim_speed = Integer.parseInt(args[2]);
- }
- System.err.println(args[0]);
- System.err.println(args[1]);
- System.err.println(sim_speed);
- Train train1 = new Train(args[0], 1);
- Train train2 = new Train(args[1], 2);
- (new Thread(train1)).start();
- (new Thread(train2)).start();
- }
- public class Train implements Runnable {
- private int speed, trainId;
- public Train(String speed, int trainId) {
- this.speed = Integer.parseInt(speed);
- this.trainId = trainId;
- this.tsi = TSimInterface.getInstance();
- }
- public void run(){
- try{
- tsi.setSpeed(trainId, speed);
- SensorEvent e;
- while(true){
- e = tsi.getSensor(trainId);
- for(int i = 0;i<tracks.length;i++){
- if((tracks[i].pos1.x == e.getXpos() && tracks[i].pos1.y == e.getYpos()) || (tracks[i].pos2.x == e.getXpos() && tracks[i].pos2.y == e.getYpos())){
- if(tracks[i].tryEnter(e, trainId)){
- }
- else{
- // Double track
- if(i == 1 || i == 3 ||i == 5){
- tracks[i+1].tryEnter(e, trainId);
- }
- // Set lower speed
- tsi.setSpeed(trainId, 0);
- tracks[i].enter(e, trainId);
- tsi.setSpeed(trainId, speed);
- }
- }
- tracks[i].leave(e, trainId);
- }
- }
- } catch (CommandException | InterruptedException e) {
- e.printStackTrace(); // or only e.getMessage() for the error
- System.exit(1);
- }
- }
- }
- public class Pos{
- protected int x,y;
- public Pos(int x, int y){
- this.x = x;
- this.y = y;
- }
- }
- public class Switch extends Pos{
- private int switchDir;
- public Switch(int x, int y, int switchDir){
- super(x,y);
- this.switchDir = switchDir;
- }
- }
- public class TrackPiece{
- private Pos p;
- private Switch sw;
- public TrackPiece(Pos p, Switch sw){
- this.p = p;
- this.sw = sw;
- }
- }
- public class TrackMonitor{
- private Lock l = new ReentrantLock();
- private Condition enter = l.newCondition();
- private TrackPiece[] tp1, tp2;
- private Boolean trackBusy, enteredOn1;
- private int trainId;
- public TrackMonitor(TrackPiece[] tp1, TrackPiece[] tp2){
- this.tp1= tp1;
- this.tp2 = tp2;
- trackBusy = false;
- enteredOn1 = false; // Used to keep track on which direction the train entered the monitor.
- }
- public Boolean tryEnter(SensorEvent e, int trainId) throws InterruptedException, CommandException{
- if(!trackBusy){
- l.lock();
- enter(e, trainId);
- l.unlock();
- return true;
- }
- return false;
- }
- public void enter(SensorEvent e, int trainId) throws InterruptedException, CommandException{
- l.lock();
- try{
- Boolean canEnter = false;
- int trackId;
- for(int i=0;i<tp1.length;i++){
- if(tp1[i].p.x == e.getXpos() && tp1[i].p.y == e.getYpos()){
- enteredOn1 = true;
- canEnter = true;
- trackId = i;
- }
- }
- for(int i=0;i<tp2.length;i++){
- if(tp2[i].p.x == e.getXpos() && tp2[i].p.y == e.getYpos()){
- enteredOn1 = false;
- canEnter = true;
- trackId = i;
- }
- }
- if(canEnter){
- while(trackBusy){
- enter.await();
- }
- this.trainId = trainId;
- trackBusy = true;
- // Switch the switch
- if(enteredOn1 && tp1[trackId].sw != null){
- TSimInterface.getInstance().setSwitch(tp1[trackId].sw.x, tp1[trackId].sw.y, tp1[trackId].sw.switchDir);
- }
- else if(!enteredOn1 && tp2[trackId].sw != null){
- TSimInterface.getInstance().setSwitch(tp2[trackId].sw.x, tp2[trackId].sw.y, tp2[trackId].sw.switchDir);
- }
- }
- } finally{
- l.unlock();
- }
- }
- public void leave(SensorEvent e, int trainId) throws CommandException{
- l.lock();
- try{
- if(enteredOn1){
- for(int i=0;i<tp2.length;i++){
- if(e.getXpos() == tp2[i].p.x && e.getYpos() == tp2[i].p.y && trackBusy && this.trainId == trainId){
- trackBusy = false;
- enter.signal();
- }
- }
- }
- else{
- for(int i=0;i<tp1.length;i++){
- if(e.getXpos() == tp1[i].p.x && e.getYpos() == tp1[i].p.y && trackBusy && this.trainId == trainId){
- trackBusy = false;
- enter.signal();
- }
- }
- }
- } finally{
- l.unlock();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement