package org.opentrafficsim.imb.demo;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nl.tudelft.simulation.language.io.URLResource;
/**
*
* Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License .
*
* $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
* initial version Oct 18, 2016
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class Convert
{
private static List nodesEB = new ArrayList<>();
private static List nodesWB = new ArrayList<>();
private static Map coordinateMap = new HashMap<>();
private static Map nodeMaxLanesMap = new HashMap<>();
private static List linksEB = new ArrayList<>();
private static Set mainNodes = new HashSet<>();
private static List linksWB = new ArrayList<>();
private static List> linesEB = new ArrayList<>();
private static List> linesWB = new ArrayList<>();
private static List parseLine(final String line)
{
String l = line;
l = l.replaceAll("\n", "");
l = l.replaceAll("\r", "");
List out = new ArrayList<>();
while (l.length() > 0)
{
if (l.charAt(0) == '[')
{
int p = l.indexOf("]", 1);
out.add(parseLine(l.substring(1, p)));
l = l.substring(p + 2).trim();
}
else if (l.charAt(0) == '"')
{
int p = l.indexOf("\"", 1);
out.add(l.substring(1, p));
l = l.substring(p + 2).trim();
}
else
{
int p = l.indexOf(",");
if (p == -1)
{
out.add(l.trim());
l = "";
}
else
{
out.add(l.substring(0, p));
l = l.substring(p + 1).trim();
}
}
}
return out;
}
private static void parseAllLines(final String[] lines, final String dir, final List> linesList)
{
boolean first = true;
for (String line : lines)
{
if (first)
{
first = false;
continue;
}
List list = parseLine(line);
linesList.add(list);
}
}
private static void writeNodes(final List> linesList, final String dir, final List nodes)
{
for (List line : linesList)
{
writeNodes(line, dir, nodes, 0, 1);
if (line.get(5).toString().equalsIgnoreCase("true"))
{
writeNodes(line, dir, nodes, 11, 12);
}
if (line.get(6).toString().equalsIgnoreCase("true"))
{
writeNodes(line, dir, nodes, 16, 17);
}
}
System.out.println();
}
private static void writeNodes(final List line, final String dir, final List nodes, final int xi,
final int yi)
{
List xc = (List) line.get(xi);
List yc = (List) line.get(yi);
Coordinate cf = new Coordinate(Double.parseDouble(xc.get(0)), Double.parseDouble(yc.get(0)));
String sf = "N" + (nodes.size() + 1) + dir;
if (!coordinateMap.keySet().contains(cf))
{
System.out.println(" ");
nodes.add(sf);
coordinateMap.put(cf, sf);
}
if (!nodeMaxLanesMap.containsKey(sf))
{
nodeMaxLanesMap.put(sf, 0);
}
Coordinate cl = new Coordinate(Double.parseDouble(xc.get(xc.size() - 1)), Double.parseDouble(yc.get(yc.size() - 1)));
String sl = "N" + (nodes.size() + 1) + dir;
if (!coordinateMap.keySet().contains(cl))
{
System.out.println(" ");
nodes.add(sl);
coordinateMap.put(cl, sl);
}
if (!nodeMaxLanesMap.containsKey(sl))
{
nodeMaxLanesMap.put(sl, 0);
}
}
private static void writeLinks(final List> linesList, final String dir, final List links)
{
for (List line : linesList)
{
writeLinks(line, dir, links, 0, 1, 2, 0.0, 0.0);
if (line.get(5).toString().equalsIgnoreCase("true"))
{
writeLinks(line, dir, links, 11, 12, 14, 0.0, -3.5); // merge
}
if (line.get(6).toString().equalsIgnoreCase("true"))
{
writeLinks(line, dir, links, 16, 17, 19, -3.5, 0.0); // diverge
}
}
}
private static void writeLinks(final List line, final String dir, final List links, final int xi,
final int yi, final int li, final double startOffset, final double endOffset)
{
List xc = (List) line.get(xi);
List yc = (List) line.get(yi);
Coordinate cf = new Coordinate(Double.parseDouble(xc.get(0)), Double.parseDouble(yc.get(0)));
Coordinate cl = new Coordinate(Double.parseDouble(xc.get(xc.size() - 1)), Double.parseDouble(yc.get(yc.size() - 1)));
String nodef = coordinateMap.get(cf);
String nodel = coordinateMap.get(cl);
String linkName = "L" + (links.size() + 1) + dir;
int nrLanes = Integer.parseInt((String) line.get(li));
int offset = xi == 0 ? Integer.parseInt((String) line.get(li + 1)) : nrLanes;
nodeMaxLanesMap.put(nodef, Math.max(nodeMaxLanesMap.get(nodef), nrLanes));
nodeMaxLanesMap.put(nodel, Math.max(nodeMaxLanesMap.get(nodel), nrLanes));
if (xi == 0)
{
mainNodes.add(nodef);
mainNodes.add(nodel);
}
System.out.print(" ");
if (xc.size() > 2)
{
System.out.print(" ");
}
else
{
System.out.println(" ");
}
if (linkName.equals("L2EB"))
{
System.out.println(" ");
System.out.println(" ");
}
if (linkName.equals("L36EB"))
{
System.out.println(" ");
System.out.println(" ");
}
if (linkName.equals("L2WB"))
{
System.out.println(" ");
System.out.println(" ");
}
if (linkName.equals("L34WB"))
{
System.out.println(" ");
System.out.println(" ");
}
System.out.println("\n");
links.add(linkName);
}
private static void writeRoutes(final String routeName, final List nodes, final boolean reverse)
{
System.out.print("= 0; --i)
{
if (mainNodes.contains(nodes.get(i)))
{
System.out.print(" " + nodes.get(i));
}
}
}
System.out.println("\" />");
}
/**
* @param args args
* @throws IOException on i/o error
* @throws URISyntaxException on illegal filename
*/
public static void main(final String[] args) throws IOException, URISyntaxException
{
String a58EB = new String(Files.readAllBytes(Paths.get(URLResource.getResource("/A58_EB.txt").toURI())));
String[] eb = a58EB.split("\n");
String a58WB = new String(Files.readAllBytes(Paths.get(URLResource.getResource("/A58_WB.txt").toURI())));
String[] wb = a58WB.split("\n");
parseAllLines(eb, "EB", linesEB);
parseAllLines(wb, "WB", linesWB);
writeNodes(linesEB, "EB", nodesEB);
writeNodes(linesWB, "WB", nodesWB);
writeLinks(linesEB, "EB", linksEB);
writeLinks(linesWB, "WB", linksWB);
writeRoutes("RouteEB", nodesEB, false);
writeRoutes("RouteWB", nodesWB, false);
System.out.println();
}
/** */
private static class Coordinate
{
final double x, y;
/**
* @param x x
* @param y y
*/
public Coordinate(double x, double y)
{
super();
this.x = x;
this.y = y;
}
/** {@inheritDoc} */
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(this.x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(this.y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/** {@inheritDoc} */
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Coordinate other = (Coordinate) obj;
if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x))
return false;
if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y))
return false;
return true;
}
/** {@inheritDoc} */
@Override
public String toString()
{
return "(" + this.x + "," + this.y + ")";
}
}
}