package com.zy.common.utils;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.zy.core.enums.ShuttleTaskModeType;
|
|
import java.io.*;
|
import java.util.ArrayList;
|
|
/**
|
* A*算法地图获取类
|
*/
|
public class NavigateMapData {
|
|
public int[][] getData() {
|
return getData(ShuttleTaskModeType.PAK_IN.id);
|
}
|
|
public int[][] getData(Integer mapType) {
|
try {
|
String mapFilename = "";
|
if (mapType == ShuttleTaskModeType.PAK_IN.id) {
|
//入库地图
|
mapFilename = "mapIn.json";
|
}else {
|
//出库地图
|
mapFilename = "mapOut.json";
|
}
|
|
String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//获取文件路径
|
File file = new File(fileName);
|
StringBuffer stringBuffer = new StringBuffer();
|
if (file.isFile() && file.exists()) {
|
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
|
BufferedReader br = new BufferedReader(isr);
|
String lineTxt = null;
|
while ((lineTxt = br.readLine()) != null) {
|
stringBuffer.append(lineTxt);
|
}
|
br.close();
|
|
//解析json地图数据
|
ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class);
|
int[][] map = new int[arrayList.size()][];
|
int j = 0;
|
for (Object obj : arrayList) {
|
ArrayList list = JSON.parseObject(obj.toString(), ArrayList.class);
|
int[] tmp = new int[list.size()];
|
int i = 0;
|
for (Object o : list) {
|
JSONObject jsonObject = JSON.parseObject(o.toString());
|
//将数据添加进二维数组
|
tmp[i++] = Integer.parseInt(jsonObject.get("value").toString());
|
}
|
//数据添加进一维数组
|
map[j++] = tmp;
|
}
|
|
return map;
|
} else {
|
System.out.println("文件不存在!");
|
}
|
} catch (IOException ioException) {
|
ioException.printStackTrace();
|
}
|
return null;
|
}
|
|
//获取JSON格式数据
|
public ArrayList<ArrayList<JSONObject>> getJsonData(Integer mapType) {
|
try {
|
String mapFilename = "";
|
if (mapType == ShuttleTaskModeType.PAK_IN.id) {
|
//入库地图
|
mapFilename = "mapIn.json";
|
}else {
|
//出库地图
|
mapFilename = "mapOut.json";
|
}
|
|
String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//获取文件路径
|
File file = new File(fileName);
|
StringBuffer stringBuffer = new StringBuffer();
|
if (file.isFile() && file.exists()) {
|
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
|
BufferedReader br = new BufferedReader(isr);
|
String lineTxt = null;
|
while ((lineTxt = br.readLine()) != null) {
|
stringBuffer.append(lineTxt);
|
}
|
br.close();
|
|
//返回的结果集
|
ArrayList<ArrayList<JSONObject>> returnLists = new ArrayList<>();
|
|
//解析json地图数据
|
ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class);
|
for (Object obj : arrayList) {
|
ArrayList list = JSON.parseObject(obj.toString(), ArrayList.class);
|
ArrayList<JSONObject> maps = new ArrayList<>();
|
for (Object o : list) {
|
JSONObject jsonObject = JSON.parseObject(o.toString());
|
maps.add(jsonObject);
|
}
|
returnLists.add(maps);
|
}
|
|
return returnLists;
|
} else {
|
System.out.println("文件不存在!");
|
}
|
} catch (IOException ioException) {
|
ioException.printStackTrace();
|
}
|
return null;
|
}
|
|
}
|