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{ @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(a1info2.GlassIndex) return -1; return 0; } } private Info[] toInfo(ResultSet set,boolean canRotate) throws SQLException{ List list=new ArrayList(); 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[] getPiece(Connection con,boolean canRotate) throws SQLException{ Statement sm= con.createStatement(); ResultSet rs= sm.executeQuery(this.peice_sql); ArrayList> pss=new ArrayList>(); 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 pl=new ArrayList(); pl.add(p); pss.add(pl); } } for(int i=0;i list=pss.get(i); for(int j=0;j p=new ArrayList(); List[] ret=(List[])Array.newInstance(p.getClass(), pss.size()); for(int i=0;i=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[] 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