Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Intersection buildIntersection()throws IOException{
- //defining some stuff aye
- String ts_name;
- String theLine = br.readLine();
- String expectedTag = Tag.INTERSECTION;
- int indexCounter = 0;
- ArrayList<TrafficStream> TS_array = new ArrayList<>();
- ArrayList<String> TS_names = new ArrayList<>();
- Intersection inter;
- ArrayList<PretimedPhasePlan> ppArray = new ArrayList<>();
- ArrayList<Phase> phaseArray = new ArrayList<>();
- String states;
- String trafString;
- TrafficDirection trafDir;
- TrafficDirection trafOri;
- int sigIndex;
- int sigFace;
- String trafString2;
- int trafIndex;
- int k;
- int j;
- Scanner sc;
- try {
- while ((theLine = br.readLine()) != null){
- //skip empty lines and comments
- if (theLine.startsWith("//")||theLine.isEmpty()){
- continue;
- }
- if (theLine == Tag.INTERSECTION && expectedTag == Tag.INTERSECTION){
- theLine = br.readLine();
- if(theLine.startsWith("//")||theLine.isEmpty()){
- continue;
- }
- else{
- sc = new Scanner(theLine);
- sc.useDelimiter("\t");
- inter = new Intersection(sc.next(), sc.next());
- }
- }
- expectedTag = Tag.TRAFFIC_STREAMS;
- continue;
- //Traffic Stream
- if (theLine == Tag.TRAFFIC_STREAMS && expectedTag == Tag.TRAFFIC_STREAMS){
- theLine = br.readLine();
- if(theLine.startsWith("//")||theLine.isEmpty()){
- continue;
- }else{
- while(!(theLine == Tag.END_TRAFFIC_STREAMS)){
- sc = new Scanner(theLine);
- sc.useDelimiter("\t");
- ts_name = sc.next();
- TS_names.add(ts_name);
- TS_array.add(new TrafficStream(ts_name,sc.next()));
- theLine = br.readLine();
- }
- }
- }
- expectedTag = Tag.PHASEPLAN;
- continue;
- //PhasePlan
- if (theLine == Tag.PHASEPLAN && expectedTag == Tag.PHASEPLAN){
- expectedTag = Tag.PHASES;
- theLine = br.readLine();
- ppArray.add(new PretimedPhasePlan());
- if(theLine.startsWith("//")||theLine.isEmpty()){
- continue;
- }
- }
- //Phases
- if (theLine == Tag.PHASES && expectedTag == Tag.PHASES){
- theLine = br.readLine();
- if(theLine.startsWith("//")||theLine.isEmpty()){
- continue;
- }
- else{
- while(!(theLine == Tag.END_PHASES)){
- sc = new Scanner(theLine);
- sc.useDelimiter("\t");
- phaseArray.add(new Phase(sc.next(),sc.next()));
- states = sc.next();
- for (int i = 0; i<states.length(); i++){
- switch (states.charAt(i)){
- case 'R': (phaseArray.get(indexCounter)).addStream(TS_array.get(i), State.RED); break;
- case 'Y': (phaseArray.get(indexCounter)).addStream(TS_array.get(i), State.YELLOW); break;
- case 'G': (phaseArray.get(indexCounter)).addStream(TS_array.get(i), State.GREEN); break;
- case 'X': (phaseArray.get(indexCounter)).addStream(TS_array.get(i), State.OFF); break;
- }
- }
- phaseArray.get(indexCounter).setMinGreenInterval(sc.nextInt());
- indexCounter++;
- theLine = br.readLine();
- }
- expectedTag = Tag.END_PHASEPLAN;
- for (k = 0; k<ppArray.size()+1; k++){
- for (j = 0; j<phaseArray.size()+1; j++){
- ppArray.get(k).add(phaseArray.get(j));
- }
- }
- theLine = br.readLine();
- indexCounter = 0;
- }
- }
- //PhasePlan-End
- if (theLine == Tag.END_PHASEPLAN && expectedTag == Tag.END_PHASEPLAN){
- expectedTag = Tag.SIGNAL_FACES;
- theLine = br.readLine();
- }
- //Signal Faces
- if (theLine == Tag.SIGNAL_FACES && expectedTag == Tag.SIGNAL_FACES){
- theLine = br.readLine();
- if(theLine.startsWith("//")||theLine.isEmpty()){
- continue;
- }else{
- while(!(theLine == Tag.END_SIGNAL_FACES)){
- if(theLine.startsWith("//")||theLine.isEmpty()){
- continue;}
- sc = new Scanner(theLine);
- sc.useDelimiter("\t");
- trafString = sc.next();
- if (trafString.length() == 1){
- switch (trafString.charAt(0)){
- case 'N': trafDir = TrafficDirection.NORTH; break;
- case 'S': trafDir = TrafficDirection.SOUTH; break;
- case 'W': trafDir = TrafficDirection.WEST; break;
- case 'E': trafDir = TrafficDirection.EAST; break;
- }
- }else{
- if (trafString.equals("NE")){
- trafDir = TrafficDirection.NORTHEAST;}
- if (trafString.equals("NW")){
- trafDir = TrafficDirection.NORTHWEST;}
- if (trafString.equals("SE")){
- trafDir = TrafficDirection.SOUTHEAST;}
- if (trafString.equals("SW")){
- trafDir = TrafficDirection.SOUTHWEST;}
- }
- switch (sc.next().charAt(0)){
- case 'N': trafOri = TrafficDirection.NORTH; break;
- case 'S': trafOri = TrafficDirection.SOUTH; break;
- case 'W': trafOri = TrafficDirection.WEST; break;
- case 'E': trafOri = TrafficDirection.EAST; break;
- }
- switch (sc.next().charAt(0)){
- case 'S': sigFace = SignalFace.STANDARD; break;
- case 'L': sigFace = SignalFace.LEFT_ARROW; break;
- case 'R': sigFace = SignalFace.RIGHT_ARROW; break;
- }
- trafString2 = sc.next();
- for(k=0;k<TS_names.size()+1;k++){
- if(trafString2 == TS_names.get(k)){
- trafIndex = k;
- }
- }
- TS_array.get(trafIndex).addObserver(inter.addSignalFace(trafDir,trafOri,sigFace));
- theLine = br.readLine();
- }
- }
- }
- return inter;
- }
- }catch(TrafficSyntaxException e){
- System.err.println("Traffic Syntax Exception: "+ e.getMessage());
- }catch (TrafficIOException e){
- System.err.println("IOException: "+ e.getMessage());
- }catch (TrafficException e){
- System.err.println("Exception at: "+ expectedTag);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement