New file |
| | |
| | | package Optimize; |
| | | |
| | | import java.util.Dictionary; |
| | | import java.util.Hashtable; |
| | | import java.util.UUID; |
| | | |
| | | import org.json.JSONArray; |
| | | import org.json.JSONException; |
| | | import org.json.JSONObject; |
| | | |
| | | import com.jacob.activeX.ActiveXComponent; |
| | | import com.jacob.com.Dispatch; |
| | | import java.util.concurrent.Semaphore; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import ng.db.*; |
| | | |
| | | public class OptimizeTag implements Runnable { |
| | | private String id; |
| | | private com.jacob.activeX.ActiveXComponent com; |
| | | private com.jacob.com.Dispatch dispatch; |
| | | private int running; |
| | | private java.util.Date date,lastdate; |
| | | private OptimizationInput input; |
| | | private java.lang.Thread thread; |
| | | public String flag; |
| | | public Object Token; |
| | | private static Semaphore sem; |
| | | |
| | | private Dictionary<String,Object> dic=new Hashtable<String,Object>(); |
| | | |
| | | |
| | | |
| | | public Object getProperty(String s){ |
| | | synchronized(dic){ |
| | | return dic.get(s); |
| | | } |
| | | } |
| | | |
| | | public void setProperty(String s,Object obj){ |
| | | synchronized(dic){ |
| | | dic.put(s,obj); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | public OptimizationInput getInput(){ |
| | | return input; |
| | | } |
| | | |
| | | public JSONObject infoToJson() throws JSONException{ |
| | | JSONObject obj=new JSONObject(); |
| | | obj.put("id", id); |
| | | String s="free"; |
| | | if(running==1){ |
| | | s="running"; |
| | | } |
| | | if(running==2){ |
| | | s="complated"; |
| | | } |
| | | obj.put("state", s); |
| | | obj.put("createTime",NGFunction.dateFormat("yyyy-MM-dd HH:mm:ss", date)); |
| | | obj.put("lastTime",NGFunction.dateFormat("yyyy-MM-dd HH:mm:ss",this.lastdate)); |
| | | s=""; |
| | | OptimizationInput in=this.input; |
| | | if(in!=null){ |
| | | s=in.GongCheng; |
| | | } |
| | | obj.put("lastGongCheng", s); |
| | | return obj; |
| | | } |
| | | |
| | | boolean getExe(){ |
| | | |
| | | try { |
| | | if(sem.tryAcquire(0, TimeUnit.MILLISECONDS)){ |
| | | return true; |
| | | } |
| | | return false; |
| | | } catch (InterruptedException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | public boolean BeginOptimize(OptimizationInput input){ |
| | | if(sem==null){ |
| | | sem=new Semaphore(1); |
| | | } |
| | | |
| | | if(running==0 || running==2){ |
| | | if(getExe()){ |
| | | running=1; |
| | | this.input=input; |
| | | thread=new Thread(this); |
| | | thread.start(); |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | public String saveOpt(String Gongcheng, String BtContent, String type, String path){ |
| | | this.lastdate=new java.util.Date(); |
| | | return Dispatch.call(this.dispatch,"saveOpt",Gongcheng,BtContent,type,path).toString(); |
| | | } |
| | | |
| | | public Object[] getResultByBT(String Gongcheng, String BtContent){ |
| | | String s= Dispatch.call(this.dispatch, "btToResult",Gongcheng,BtContent).toString(); |
| | | try { |
| | | JSONArray obj=new JSONArray(s); |
| | | JSONObject ob=obj.getJSONObject(1); |
| | | OptimizationResult a=new OptimizationResult(ob); |
| | | JSONObject b=obj.getJSONObject(0); |
| | | JSONArray ar=new JSONArray(); |
| | | Object[] ret=new Object[2]; |
| | | ret[0]=b; |
| | | ret[1]=a; |
| | | return ret; |
| | | } catch (JSONException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static OptimizeTag create(){ |
| | | ActiveXComponent dotnetCom = new ActiveXComponent("CLSID:97D686F9-F577-4E41-B5BF-CB8CC5BC73CB"); |
| | | Dispatch myCom = (Dispatch) dotnetCom.getObject(); |
| | | OptimizeTag tag=new OptimizeTag(); |
| | | tag.id=null; |
| | | tag.com=dotnetCom; |
| | | tag.dispatch=myCom; |
| | | |
| | | tag.date=new java.util.Date(); |
| | | tag.lastdate=new java.util.Date(); |
| | | return tag; |
| | | } |
| | | |
| | | public static OptimizeTag create(String id){ |
| | | ActiveXComponent dotnetCom = new ActiveXComponent("CLSID:97D686F9-F577-4E41-B5BF-CB8CC5BC73CB"); |
| | | Dispatch myCom = (Dispatch) dotnetCom.getObject(); |
| | | OptimizeTag tag=new OptimizeTag(); |
| | | tag.id=id; |
| | | tag.com=dotnetCom; |
| | | tag.dispatch=myCom; |
| | | tag.date=new java.util.Date(); |
| | | tag.lastdate=new java.util.Date(); |
| | | return tag; |
| | | } |
| | | public OptimizationState getState() throws JSONException{ |
| | | this.lastdate=new java.util.Date(); |
| | | String s= Dispatch.call(this.dispatch,"getState").toString(); |
| | | if("null".equals(s)) |
| | | return null; |
| | | |
| | | JSONObject obj=new JSONObject(s); |
| | | |
| | | OptimizationState r=new OptimizationState(obj); |
| | | |
| | | if(this.running==1){ |
| | | r.State="busy"; |
| | | } |
| | | return r; |
| | | } |
| | | |
| | | public OptimizationResult getResult() throws JSONException{ |
| | | this.lastdate=new java.util.Date(); |
| | | if(running!=2) |
| | | return null; |
| | | String s= Dispatch.call(this.dispatch,"getResult").toString(); |
| | | if("null".equals(s)) |
| | | return null; |
| | | JSONObject obj=new JSONObject(s); |
| | | return new OptimizationResult(obj); |
| | | } |
| | | |
| | | |
| | | public String getId(){ |
| | | return this.id; |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | this.lastdate=new java.util.Date(); |
| | | this.running=1; |
| | | // TODO Auto-generated method stub |
| | | String s; |
| | | try { |
| | | Dispatch.call(this.dispatch,"Optima",this.input.toJson().toString()).toString(); |
| | | } catch (JSONException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | } |
| | | sem.release(); |
| | | this.running=2; |
| | | this.thread=null; |
| | | } |
| | | |
| | | |
| | | } |