package edu.hpc.its.area.test.core;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import edu.hpc.its.area.Constant;
import edu.hpc.its.area.Cross;
import edu.hpc.its.area.core.Direction;
import edu.hpc.its.area.core.StandardArea;
import edu.hpc.its.area.core.StandardCar;
import edu.hpc.its.area.core.StandardCross;
import edu.hpc.its.area.core.StandardLane;
import edu.hpc.its.area.core.StandardLight;
import edu.hpc.its.area.core.StandardRoad;
import edu.hpc.its.area.core.StandardRoute;
import edu.hpc.its.area.dao.mysql.ServiceProxy;
import edu.hpc.its.area.service.StandardAreaService;
import edu.hpc.its.area.service.StandardCarService;
import edu.hpc.its.area.service.StandardCrossService;
import edu.hpc.its.area.service.StandardLaneService;
import edu.hpc.its.area.service.StandardLightService;
import edu.hpc.its.area.service.StandardRoadService;
import edu.hpc.its.area.service.StandardRouteService;
import edu.hpc.its.area.service.Impl.StandardAreaServiceImpl;
import edu.hpc.its.area.service.Impl.StandardCarServiceImpl;
import edu.hpc.its.area.service.Impl.StandardCrossServiceImpl;
import edu.hpc.its.area.service.Impl.StandardLaneServiceImpl;
import edu.hpc.its.area.service.Impl.StandardLightServiceImpl;
import edu.hpc.its.area.service.Impl.StandardRoadServiceImpl;
import edu.hpc.its.area.service.Impl.StandardRouteServiceImpl;
public class InitDatabase {
/******************* service **********************/
private StandardAreaService areaService = null;
private StandardCrossService crossService = null;
private StandardRoadService roadService = null;
private StandardLaneService laneService = null;
private StandardLightService lightService = null;
private StandardRouteService routeService = null;
private StandardCarService carService = null;
/******************* cache **********************/
private Map<String, Cross> crossCache = new HashMap<>();
/******************* main **********************/
public static void main(String[] args) {
InitDatabase init = new InitDatabase();
StandardArea area = new StandardArea();
init.initArea(area);
init.initCross(area);
init.initRoute(area);
init.initCar(area);
}
/**
* 初始化车
*
* @timestamp Feb 24, 2016 1:43:57 PM
* @param area
*/
private void initCar(StandardArea area) {
for (char a = 'a'; a <= 'e'; a++) {
StandardCar car = new StandardCar();
String image = "/image/" + String.valueOf(a) + ".png";
car.setImage(image);
car.setAreaId(area.getId());
carService.insertCar(car);
}
}
/**
* 初始路线
*
* @timestamp Feb 23, 2016 10:21:32 PM
*/
private void initRoute(StandardArea area) {
int[] num1 = new int[Constant.HORIZONTALNUM];
int[] num2 = new int[Constant.VERTICALNUM];
///////////////////////////// 路线图第一部分
for (int i = 1; i <= Constant.HORIZONTALNUM; i++) {
StringBuilder sbH = new StringBuilder();
for (int j = 1; j <= Constant.VERTICALNUM + 1; j++) {
sbH.append("H" + i + j + ",");
if (i == 1 && j <= (Constant.VERTICALNUM)) {
num2[j - 1] = i * 10 + j;
}
}
String h = sbH.toString().substring(0, sbH.toString().length() - 1);
reverse(h, area);
}
///////////////////////////// 路线图第二部分
for (int i = 1; i <= Constant.VERTICALNUM; i++) {
StringBuilder sbN = new StringBuilder();
for (int j = 1; j <= Constant.HORIZONTALNUM + 1; j++) {
sbN.append("N" + j + i + ",");
if (i == 1 && j <= Constant.HORIZONTALNUM) {
num1[j - 1] = i + j * 10;
}
}
String n = sbN.toString().substring(0, sbN.toString().length() - 1);
reverse(n, area);
}
///////////////////////////// 路线图第三部分
Random r = new Random();
for (int i : num1) {
StringBuilder sbX = new StringBuilder();
sbX.append("H" + i + ",");
while (i % 10 != (Constant.VERTICALNUM + 1) && i / 10 != (Constant.HORIZONTALNUM + 1)) {
if (r.nextInt(2) == 0) {
i++;
sbX.append("H");
} else {
i = i + 10;
sbX.append("N");
}
sbX.append(i + ",");
}
String h = sbX.toString().substring(0, sbX.toString().length() - 1);
reverse(h, area);
}
///////////////////////////// 路线图第四部分
for (int i : num2) {
StringBuilder sbX = new StringBuilder();
sbX.append("N" + i + ",");
while (i / 10 != (Constant.HORIZONTALNUM + 1) && i % 10 != (Constant.VERTICALNUM + 1)) {
if (r.nextInt(2) == 0) {
i++;
sbX.append("H");
} else {
i = i + 10;
sbX.append("N");
}
sbX.append(i + ",");
}
String h = sbX.toString().substring(0, sbX.toString().length() - 1);
reverse(h, area);
}
}
/**
* 翻转线路,生成逆向行驶数组,并保存
*
* @timestamp Feb 23, 2016 11:41:56 PM
* @param h
*/
private void reverse(String h, StandardArea area) {
String[] rs = h.split(",");
List<String> list = new ArrayList<>();
Collections.addAll(list, rs);
String one = list.toString()//
.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(" ", "");
Collections.reverse(list);
String two = list.toString()//
.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(" ", "");
for (int i = 30; i <= 100; i = i + 10) {// 30KM/H到100KM/H不等
StandardRoute route = new StandardRoute();
route.setSpeed(i / 3.6 * 100);// 速度单位-->cm/s
route.setRouteTable(one);
route.setAreaId(area.getId());
routeService.insertRoute(route);
route = new StandardRoute();
route.setSpeed(i / 3.6 * 100);// 速度单位-->cm/s
route.setRouteTable(two);
route.setAreaId(area.getId());
routeService.insertRoute(route);
}
}
/**
* 初始化交叉路口
*
* @timestamp Feb 19, 2016 9:41:44 PM
*/
private void initCross(StandardArea area) {
for (int i = 1; i <= Constant.VERTICALNUM; i++) {
for (int j = 1; j <= Constant.HORIZONTALNUM; j++) {
StandardCross cross = new StandardCross();
cross.setHorizontalNum(j);
cross.setOrdinateNum(i);
cross.setStandardArea(area);
cross.setXxPoint(j * Constant.ROADREALITYLENGTH);
cross.setYyPoint(i * Constant.ROADREALITYLENGTH);
crossService.insertCross(cross);
crossCache.put("" + i + j, cross);
initRoad(cross, i, j);
initLight(cross, i, j);
}
}
}
/**
* 初始化灯
*
* @timestamp Feb 20, 2016 11:06:41 AM
* @param cross
* @param i
* 纵向第几个
* @param j
* 横向第几个
*/
private void initLight(StandardCross cross, int i, int j) {
for (Direction d : Direction.values()) {
StandardLight light = new StandardLight();
light.setStandardCross(cross);
light.setRed(Constant.READTIME);
light.setGreen(Constant.GREENTIME);
light.setSize(Constant.LANENUM == 1 //
? Constant.LANEWIDE / Constant.COMPRESS * Constant.ONECM //
: (Constant.LANEWIDE / Constant.COMPRESS * Constant.ONECM) * (Constant.LANENUM - 1));
light.setDirection(d);
light.setLightState(d.getDirection() > 2 ? 1 : 0);
double part = Constant.LANENUM * (Constant.LANEWIDE / Constant.COMPRESS * Constant.ONECM);
if (d == Direction.NORTH) {// 北
light.setCenterX(cross.getXxPoint());
light.setCenterY(cross.getYyPoint() - part);
} else if (d == Direction.SOUTH) {// 南
light.setCenterX(cross.getXxPoint());
light.setCenterY(cross.getYyPoint() + part);
} else if (d == Direction.WEST) {// 西
light.setCenterX(cross.getXxPoint() - part);
light.setCenterY(cross.getYyPoint());
} else if (d == Direction.EAST) {// 东
light.setCenterX(cross.getXxPoint() + part);
light.setCenterY(cross.getYyPoint());
}
lightService.insertLight(light);
}
}
/**
* 初始化路
*
* @timestamp Feb 19, 2016 11:12:44 PM
* @param cross
* @param i
* 纵向第几个
* @param j
* 横向第几个
*/
private void initRoad(StandardCross cross, int i, int j) {
//////////// 横向路
StandardRoad hRoad