package ng.Algorithm.Layouts;
|
|
|
import java.lang.reflect.Array;
|
import java.sql.*;
|
import java.util.*;
|
|
import org.json.JSONArray;
|
import org.json.JSONException;
|
import org.json.JSONObject;
|
|
import ng.Algorithm.Layouts.Heuristic.RowResult;
|
import ng.Algorithm.Layouts.LayoutAlgorithm.AlgorithmParams;
|
|
public class AlgorithmDBInterface {
|
|
private String connectionString;
|
private int workId;
|
public AlgorithmDBInterface(String connectionString,int WorkId){
|
this.connectionString=connectionString;
|
this.workId=WorkId;
|
param_sql=String.format("SELECT `width`,`length`,`xinterval`,`yinterval` from gmms_furnace where id=%d", WorkId);
|
this.peice_sql=String.format("select id,processcard,position,front,width,height,glass_idx,thickness,color,layout_mode,w,h from tempere_task_glass where work_id=%d", WorkId);
|
}
|
|
private String param_sql;
|
private String peice_sql;
|
|
public int Line;
|
|
public boolean canr=true;
|
|
|
private AlgorithmParams getParam(Connection con) throws SQLException{
|
Statement sm= con.createStatement();
|
ResultSet rs= sm.executeQuery(param_sql);
|
rs.next();
|
AlgorithmParams p=new AlgorithmParams();
|
p.width=rs.getInt(1);
|
p.length=rs.getInt(2);
|
p.h_interval=rs.getInt(3);
|
p.v_interval=rs.getInt(4);
|
p.method="Heuristic";
|
rs.close();
|
sm.close();
|
return p;
|
}
|
|
class Info {
|
public long id;
|
public int thicknss;
|
public String color;
|
public int width;
|
public int height;
|
public Piece.LayoutMode layout_mode;
|
public int FrameNumber;
|
public boolean IsFront;
|
public int GlassIndex;
|
public String Processcard;
|
public int GlassNumber;
|
public double w,h;
|
@Override
|
public String toString(){
|
return String.format("%d.%d[%d]",FrameNumber,GlassNumber,id);
|
}
|
|
}
|
|
compare piececompare=new compare();
|
|
|
class compare implements java.util.Comparator<Piece>{
|
|
@Override
|
public int compare(Piece arg0, Piece arg1) {
|
// TODO Auto-generated method stub
|
Info info1=(Info)arg0.Source;
|
Info info2=(Info)arg1.Source;
|
int a1=info1.IsFront?0:1;
|
int a2=info2.IsFront?0:1;
|
if(a1>a2)
|
return -1;
|
if(a1<a2)
|
return 1;
|
if(info1.GlassIndex<info2.GlassIndex)
|
return 1;
|
if(info1.GlassIndex>info2.GlassIndex)
|
return -1;
|
return 0;
|
}
|
|
}
|
|
|
|
private Info[] toInfo(ResultSet set,boolean canRotate) throws SQLException{
|
List<Info> list=new ArrayList<Info>();
|
while(set.next()){
|
Info info=new Info();
|
info.id=set.getLong("id");
|
info.thicknss= (int)set.getDouble("thickness");
|
info.color=set.getString("color");
|
info.width=(int)set.getDouble("width");
|
info.height=(int)set.getDouble("height");
|
info.Processcard=set.getString("processcard");
|
double w=set.getDouble("w");
|
double h=set.getDouble("h");
|
info.w=w;
|
info.h=h;
|
if(canRotate==true){
|
info.layout_mode=Piece.LayoutMode.Both;
|
}
|
else{
|
info.layout_mode=w<h?Piece.LayoutMode.Zhong:Piece.LayoutMode.Heng;
|
}
|
|
/*
|
int mode=set.getInt("layout_mode");
|
switch(mode){
|
case 1:
|
info.layout_mode= Piece.LayoutMode.Zhong;
|
break;
|
case 2:
|
info.layout_mode=Piece.LayoutMode.Heng;
|
break;
|
default:
|
info.layout_mode=Piece.LayoutMode.Both;
|
break;
|
}
|
*/
|
info.FrameNumber=set.getInt("position");
|
info.IsFront=set.getInt("front")==1;
|
info.GlassIndex=set.getInt("glass_idx");
|
list.add(info);
|
; }
|
Info[] ret=new Info[list.size()];
|
return list.toArray(ret);
|
}
|
|
|
|
private List<Piece>[] getPiece(Connection con,boolean canRotate) throws SQLException{
|
Statement sm= con.createStatement();
|
ResultSet rs= sm.executeQuery(this.peice_sql);
|
ArrayList<ArrayList<Piece>> pss=new ArrayList<ArrayList<Piece>>();
|
Info[] infos=toInfo(rs,canRotate);
|
if(infos==null)
|
return null;
|
if(infos.length==0)
|
return null;
|
rs.close();
|
sm.close();
|
for(int i=0;i<infos.length;i++){
|
Info cur=infos[i];
|
Piece p=new Piece();
|
p.width=cur.width;
|
p.height=cur.height;
|
p.Mode=cur.layout_mode;
|
p.Source=cur;
|
p.w=cur.w;
|
p.h=cur.h;
|
boolean ok=false;
|
for(int j=0;j<pss.size();j++){
|
Piece pp=pss.get(j).get(0);
|
Info info=(Info)pp.Source;
|
if(info.FrameNumber==cur.FrameNumber){
|
pss.get(j).add(p);
|
ok=true;
|
break;
|
}
|
}
|
if(ok==false){
|
ArrayList<Piece> pl=new ArrayList<Piece>();
|
pl.add(p);
|
pss.add(pl);
|
}
|
}
|
for(int i=0;i<pss.size();i++){
|
pss.get(i).sort(this.piececompare);
|
List<Piece> list=pss.get(i);
|
for(int j=0;j<list.size();j++){
|
Info info= (Info)list.get(j).Source;
|
info.GlassNumber=j+1;
|
}
|
}
|
List<Piece> p=new ArrayList<Piece>();
|
List<Piece>[] ret=(List<Piece>[])Array.newInstance(p.getClass(), pss.size());
|
for(int i=0;i<ret.length;i++){
|
ret[i]=pss.get(i);
|
}
|
return ret;
|
}
|
|
|
|
|
|
|
|
private void saveLayout(Connection con,LayoutResult result,AlgorithmParams param) throws SQLException {
|
int step=0;
|
try {
|
con.setAutoCommit(false);
|
int number=result.Number;
|
step=1;
|
CallableStatement call= con.prepareCall("{call insert_tempere_layout(?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
|
boolean ok=true;
|
for(int i=0;i<result.layout.length;i++){
|
Rect r=result.layout[i];
|
Info info=(Info)r.getSource().Source;
|
int move=0;
|
int pendulum=1;
|
if(i==result.layout.length-1){
|
pendulum=3;
|
}
|
else{
|
if(result.layout[i].Row!=result.layout[i+1].Row)
|
pendulum=2;
|
}
|
|
|
if(pendulum!=1){
|
move=param.width-result.layout[i].x;
|
}
|
else{
|
move=result.layout[i+1].x-result.layout[i].x;
|
}
|
|
call.setInt(1,this.workId);
|
call.setInt(2,number);
|
call.setDouble(3,result.rate);
|
call.setInt(4,i+1);
|
call.setInt(5,r.x);
|
call.setInt(6,r.y);
|
if(r.getLength()==r.getWidth()){
|
call.setInt(7,0);
|
}else{
|
call.setInt(7,r.isHeng?0:1);
|
}
|
|
call.setInt(8, pendulum);
|
call.setInt(9,move);
|
call.setInt(10, r.Row);
|
call.setInt(11,r.Col);
|
call.setLong(12,info.id);
|
call.setString(13, null);
|
call.registerOutParameter(14,java.sql.Types.INTEGER);
|
call.execute();
|
if(call.getInt(14)!=1){
|
ok=false;
|
break;
|
}
|
|
}
|
if(ok){
|
con.commit();
|
}
|
else{
|
con.rollback();
|
}
|
con.close();
|
|
} catch (SQLException e) {
|
if(step>=1)
|
con.rollback();
|
con.close();
|
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
|
}
|
|
public LayoutResult lastResult;
|
|
public class ComputeResult{
|
public Exception Error;
|
public boolean Success;
|
public int Number;
|
public boolean isNothing;
|
public LayoutResult Result;
|
public String toJson(){
|
|
try{
|
JSONArray arr=new JSONArray();
|
for(int i=0;i<Result.layout.length;i++){
|
Rect r=Result.layout[i];
|
JSONObject obj=new JSONObject();
|
obj.put("x",r.x);
|
obj.put("y",r.y);
|
obj.put("w",r.getXSize());
|
obj.put("h",r.getYSize());
|
obj.put("size",""+r.getSource().w+"*"+r.getSource().h);
|
int k=0;
|
if(r.canHeng()){
|
k=2;
|
}
|
if(r.canZhong()){
|
k+=1;
|
}
|
obj.put("mode",k);
|
arr.put(obj);
|
}
|
return arr.toString();
|
}
|
catch(Exception e){
|
return "{}";
|
}
|
}
|
}
|
|
|
|
int getNextLayoutNumber(Connection con) throws SQLException{
|
CallableStatement sm= con.prepareCall("{call get_next_tempere_number(?,?)}");
|
sm.setInt(1,this.workId);
|
sm.registerOutParameter(2,java.sql.Types.INTEGER);
|
|
int ret=0;
|
sm.execute();
|
ret=sm.getInt(2);
|
sm.close();
|
return ret;
|
}
|
|
|
public ComputeResult ComputeOnce(boolean canRotate){
|
Connection con=null;
|
lastResult=null;
|
try {
|
con = java.sql.DriverManager.getConnection(this.connectionString);
|
AlgorithmParams param=getParam(con);
|
List<Piece>[] ps=getPiece(con,canRotate);
|
if(ps==null){
|
ComputeResult ret= new ComputeResult();
|
ret.Success=false;
|
ret.Number=0;
|
ret.Error=null;
|
ret.isNothing=true;
|
con.close();
|
return ret;
|
}
|
LayoutResult result= LayoutAlgorithm.Compute(param,ps);
|
int number= getNextLayoutNumber(con);
|
result.WorkId=this.workId;
|
result.Number=number;
|
if(result!=null){
|
this.saveLayout(con, result,param);
|
this.lastResult=result;
|
ComputeResult ret= new ComputeResult();
|
ret.Success=true;
|
ret.Number=number;
|
ret.Error=null;
|
ret.isNothing=false;
|
ret.Result=result;
|
return ret;
|
}
|
ComputeResult ret1= new ComputeResult();
|
ret1.Success=false;
|
ret1.Number=0;
|
ret1.Error=null;
|
ret1.isNothing=true;
|
con.close();
|
return ret1;
|
|
} catch (SQLException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
ComputeResult ret= new ComputeResult();
|
ret.Success=false;
|
ret.Number=0;
|
ret.Error=e;
|
ret.isNothing=false;
|
return ret;
|
}
|
finally{
|
try {
|
con.close();
|
} catch (SQLException e1) {
|
// TODO Auto-generated catch block
|
e1.printStackTrace();
|
}
|
}
|
|
|
}
|
private static AlgorithmDBInterface[] als;
|
public static void initAlgorithm(String connectionString,int[] lines){
|
als=new AlgorithmDBInterface[lines.length];
|
for(int i=0;i<lines.length;i++){
|
als[i]=new AlgorithmDBInterface(connectionString,lines[i]);
|
als[i].Line=lines[i];
|
}
|
}
|
|
public static int getCanRotate(int line){
|
for(int i=0;i<als.length;i++){
|
if(als[i].Line==line)
|
return als[i].canr?1:0;
|
}
|
return -1;
|
}
|
|
public static boolean setCanRotate(int line,boolean rotate){
|
for(int i=0;i<als.length;i++){
|
if(als[i].Line==line){
|
als[i].canr=rotate;
|
return true;
|
}
|
}
|
return false;
|
}
|
|
|
public static ComputeResult ComputeOnce(int line){
|
|
for(int i=0;i<als.length;i++){
|
if(als[i].Line==line)
|
return als[i].ComputeOnce(als[i].canr);
|
}
|
return null;
|
}
|
}
|