/*
 * $Log: SetPairConstraint.java,v $
 * Revision 1.5  1996/07/11 01:24:37  hma
 * intermediate working version
 *
# Revision 1.4  1996/06/08  05:59:45  hma
# change SwitchSet to Set.
#
# Revision 1.3  1996/06/08  05:06:22  hma
# in setBValue(), check if noSetB is true.
#
# Revision 1.2  1996/06/08  04:46:42  hma
# added method setBValue( Set ).
#
# Revision 1.1  1996/06/05  18:12:16  hma
# Initial revision
#
# Revision 1.1  1996/03/18  19:01:31  rlj1
# Initial revision
#
 */

package network;

import java.util.*;
import general.*;
import network.SwitchSet;


public class SetPairConstraint {
  String name;

  // set of switches
  Set a, b;
  // in is traffic from b to a,
  // out is traffic from a to b.
  double in, out;
  // for some constraint, there is only one set, a, and no b. 
  // b should be the complement of set a,
  // which should be computed after new switches are added.
  //
  // when a is a single switch,
  // this kind of constraint specifies alpha and omega,
  boolean noSetB = false;
  
public SetPairConstraint(String Name, Set A, Set B, double In, double Out) {
  name = Name;
  a = A;
  b = B;
  in = In;
  out = Out;
  noSetB = false;
}
/* not working yet. set a, b are sets of switches, not sets of strings
public SetPairConstraint(StreamTokenizer stk)
throws IOException, ClassNotFoundException, GenericException {
  int tokenType;
  Set swtSet1, swtSet2 = null;
  double out, in;

  // read the starting '{'
  while (stk.nextToken() != '{');

  // read the constraint name;
  while (stk.nextToken() != StreamTokenizer.TT_WORD);
  this.name = stk.sval;

  this.b = null;

  // read the first set in the constraint
  this.a = readSwitchSet(stk);
  
  while ( (tokenType = stk.nextToken()) != StreamTokenizer.TT_NUMBER 
	 && tokenType != '{' );
  
  // read the second set in the constraint, if there is a second set
  if (tokenType == '{') {
    stk.pushBack();
    this.b = readSwitchSet(stk);
  }
  else // we have over read the next value, it should be "out", push it back
    stk.pushBack();
  
  // get the value of out flow
  while ( stk.nextToken() != StreamTokenizer.TT_NUMBER );
  out = stk.nval;

  // get the value of in flow
  while ( stk.nextToken() != StreamTokenizer.TT_NUMBER );
  in = stk.nval;

  // read the closing "}"
  while (stk.nextToken() != '}');
  
  if (a.size() == 0)
    throw (new GenericException("set size zero in SetPairConstraint"));

  if(this.b == null) {
    noSetB = true;
    // setBValue(switches);
  }
}
*/

  public void setA(Set A) {
    a = A;
  }

  public void setB(Set B) {
    b = B;
  }

  public void setIn(double In) {
    in = In;
  }

  public void setOut(double Out) {
    out = Out;
  }

  public Set getA() {
    return a;
  }

  public Set getB() {
    return b;
  }

  public double getIn() {
    return in;
  }

  public double getOut() {
    return out;
  }

  public String toString() {
    String s;
    s =  "{" + name + ",{";

    if (a != null) {
      for (Enumeration e = a.elements(); e.hasMoreElements();)
	s += ((Switch)e.nextElement()).getName() + ",";
    }
    s += "},{";

    if (b != null) {
      for (Enumeration e = b.elements(); e.hasMoreElements();)
	s += ((Switch)e.nextElement()).getName() + ",";
    }
    s += "}," + out + "," + in + "}";
    return s;
  }

  /**
   * it's no longer a pair, only one set.
   * b should be given a value from setBValue()
   */
  public SetPairConstraint(String Name, Set A, double In, double Out) {
    name = Name;
    a = A;
    b = null;
    in = In;
    out = Out;
    noSetB = true;
  }

  public synchronized void update(Set omega) {
    a = omega.intersection(a);
    if (noSetB == true)
      b = omega.complement(a);
    else
      b = omega.intersection(b);
  }

  public String getName() {
    return name;
  }

  public void setName(String Name) {
    name = Name;
  }
}


