/*
 *$Log$
 */
package network;
import java.util.*;
import java.awt.*;

import general.*;
import gui.*;
import network.*;

public class CalcNetworkLowerBound extends Thread {
  GeneralProperties gpData;
  String sdata;
  public CalcNetworkLowerBound(String sdata) {
    //gpData = gp;
    this.sdata = sdata;
  }
  public CalcNetworkLowerBound(String sdata, GeneralProperties gpData) {
    this.gpData = gpData;
    this.sdata = sdata;
  }
  public void run() {
    //Network n = (Network)gpData.get("Network");
    //Thread.currentThread().setPriority(MIN_PRIORITY);
    dOptimizeLowerBound dolb = 
      (dOptimizeLowerBound)gpData.get("dOptimizeLowerBound");
    dolb.lbLowerBound.setText("calculating lower bound... ");
    // dolb.layout();
    dolb.show();
    String rdata = null;
    double lowerBound = 0;
    int retry = 0;
    do {
      rdata = CGI.linearProgramming(sdata);
      lowerBound = LP.parseObjectiveValue(rdata);
      ++retry;
    }
    while (lowerBound < 0 && retry < 20);
    if (lowerBound >= 0) {
      StringBuffer sbuf = new StringBuffer();
      String line;
      StringTokenizer stk = new StringTokenizer(rdata, "\n");
      while (stk.hasMoreTokens()) {
	line = stk.nextToken();
	if (line.startsWith("From_") && !line.endsWith(" 0"))
	  sbuf.append(line + "\n");
      }
      rdata = sbuf.toString();
      
      dolb.tx.setText(rdata);
      dolb.lbLowerBound.setText("Lower Bound = " + lowerBound);
      dolb.layout();
    }
    else {
      dolb.tx.setText("");
      dolb.lbLowerBound.setText("can not get Lower Bound");
      dolb.layout();
    }
  }
}


