package EDU.neu.ccs.demeter.tools.apstudio.graphedit;
import java.awt.*;
import java.io.*;
import java.util.*;
import EDU.neu.ccs.demeter.*;
import EDU.neu.ccs.demeter.common.tg.*;


import EDU.neu.ccs.demeter.*;
class StrategyGraph extends StrategyExpression implements StrategyGraphI, Cloneable {
  protected Vector edgeVec;
  public Vector get_edgeVec() { return edgeVec; }
  public void set_edgeVec(Vector new_edgeVec)
    { edgeVec = new_edgeVec; }
  protected SGEdge_SList edges;
  public SGEdge_SList get_edges() { return edges; }
  public void set_edges(SGEdge_SList new_edges)
    { edges = new_edges; }
  protected ClassGlobSpec sources;
  public ClassGlobSpec get_sources() { return sources; }
  public void set_sources(ClassGlobSpec new_sources)
    { sources = new_sources; }
  protected Integer_NList sourceEdges;
  public Integer_NList get_sourceEdges() { return sourceEdges; }
  public void set_sourceEdges(Integer_NList new_sourceEdges)
    { sourceEdges = new_sourceEdges; }
  protected ClassGlobSpec targets;
  public ClassGlobSpec get_targets() { return targets; }
  public void set_targets(ClassGlobSpec new_targets)
    { targets = new_targets; }
  public StrategyGraph(Vector edgeVec, SGEdge_SList edges, ClassGlobSpec sources, Integer_NList sourceEdges, ClassGlobSpec targets) {
    super();
    set_edgeVec(edgeVec);
    set_edges(edges);
    set_sources(sources);
    set_sourceEdges(sourceEdges);
    set_targets(targets);
  }
  public static StrategyGraph parse(java.io.InputStream in) throws ParseException
    { return new Parser(in)._StrategyGraph(); }
  public static StrategyGraph parse(String s) {
    try { return parse(new java.io.ByteArrayInputStream(s.getBytes())); }
    catch (ParseException e) { throw new RuntimeException(e.toString()); }
  }
  public StrategyGraph() { edgeVec = new Vector(); edges = new SGEdge_SList(); }
  StrategyGraph toGraph(ClassName source) {
    Enumeration e = edges.elements();
    while (e.hasMoreElements()) {
      edgeVec.addElement(e.nextElement());
    }
    if (sources == null) {
      if (sourceEdges != null) {
	ClassGlobSet set = new ClassGlobSet(new ClassGlob_Commalist());
	Enumeration e2 = sourceEdges.elements();
	while (e2.hasMoreElements()) {
	  Integer i = (Integer) e2.nextElement();
	  SGEdge edge = (SGEdge) edgeVec.elementAt(i.intValue());
	  set.setadd(edge.get_source());
	}
	sources = set;
      } else {
	sources = OneClassGlob.parse(source.toString());
      }
    }
    if (sourceEdges == null) {
      sourceEdges = new Integer_NList();
      sources.allClassNames(new SourceEdgeCollector(this, sourceEdges));
    }
    if (targets == null) {
      ClassGlobCollector c = new ClassGlobCollector();
      allDests(c);
      targets = c.get_spec();
    }	
    return this;
  }
  void addEdge(SGEdge edge) {
    edgeVec.addElement(edge);
    edges.addElement(edge);
  }
  public Vector getIncomingIndices(ClassNameI c) {
    SGEdgeMatcher v0 = new SGEdgeMatcher();
    v0.set_c(c);
    v0.start();
    allDests(v0);
    v0.finish();
    return v0.get_return_val();
  }
  public Vector getOutgoingIndices(ClassNameI c) {
    SGEdgeMatcher v0 = new SGEdgeMatcher();
    v0.set_c(c);
    v0.start();
    allSources(v0);
    v0.finish();
    return v0.get_return_val();
  }
  void markReachableForwardFromSources(TraversalGraph tg) {
    __V_StrategyGraph_markReachableForwardFromSources v0 = new __V_StrategyGraph_markReachableForwardFromSources();
    v0.set_tg(tg);
    v0.start();
    __trav_markReachableForwardFromSources(v0);
    v0.finish();
  }
  void markReachableBackwardFromTargets(TraversalGraph tg) {
    __V_StrategyGraph_markReachableBackwardFromTargets v0 = new __V_StrategyGraph_markReachableBackwardFromTargets();
    v0.set_tg(tg);
    v0.start();
    __trav_markReachableBackwardFromTargets(v0);
    v0.finish();
  }
  public boolean meetsConstraint(int i, TGEdge edge) {
    SGEdge sgedge = (SGEdge) edgeVec.elementAt(i);
    return sgedge.checkConstraint(edge);
  }
  boolean allSourcesAndTargetsMarked(TraversalGraph tg) {
    __V_StrategyGraph_allSourcesAndTargetsMarked v0 = new __V_StrategyGraph_allSourcesAndTargetsMarked();
    v0.set_tg(tg);
    v0.start();
    __trav_allSourcesAndTargetsMarked(v0);
    v0.finish();
    return v0.get_return_val();
  }
  String startSetCode(String indent, String setname, ClassName startt) {
    if (!sources.match(startt)) {
      System.err.println("Error: strategy cannot start at " + startt + ".");
      return "";
    }
    String code = "";
    Enumeration e = sourceEdges.elements();
    while (e.hasMoreElements()) {
      Integer i = (Integer) e.nextElement();
      code += indent + setname + ".set(" + i.intValue() + ");\n";
    }
    return code;
  }
  int size() { return edges.size(); }
  ClassName getFirst() {
    __V_StrategyGraph_getFirst v0 = new __V_StrategyGraph_getFirst();
    v0.start();
    __trav_getFirst(v0);
    v0.finish();
    return v0.get_return_val();
  }
  void universal_trv0_bef(UniversalVisitor _v_) {
    super.universal_trv0_bef(_v_);
    _v_.before(this);
  }
  void universal_trv0_aft(UniversalVisitor _v_) {
    _v_.after(this);
    super.universal_trv0_aft(_v_);
  }
  void universal_trv0(UniversalVisitor _v_) {
    universal_trv0_bef(_v_);
    if (edgeVec != null) {
      _v_.before_edgeVec(this, edgeVec);
      _v_.after_edgeVec(this, edgeVec);
    }
    _v_.before_edges(this, edges);
    edges.universal_trv0(_v_);
    _v_.after_edges(this, edges);
    if (sources != null) {
      _v_.before_sources(this, sources);
    sources.universal_trv0(_v_);
      _v_.after_sources(this, sources);
    }
    if (sourceEdges != null) {
      _v_.before_sourceEdges(this, sourceEdges);
    sourceEdges.universal_trv0(_v_);
      _v_.after_sourceEdges(this, sourceEdges);
    }
    if (targets != null) {
      _v_.before_targets(this, targets);
    targets.universal_trv0(_v_);
      _v_.after_targets(this, targets);
    }
    super.universal_trv0(_v_);
    universal_trv0_aft(_v_);
  }
  void toAllSubclasses_Program_trv_bef(ClassNameTranspVisitor cntv, EdgeVisitor aev) {
    super.toAllSubclasses_Program_trv_bef(cntv, aev);
  }
  void toAllSubclasses_Program_trv_aft(ClassNameTranspVisitor cntv, EdgeVisitor aev) {
    super.toAllSubclasses_Program_trv_aft(cntv, aev);
  }
  void toAllSubclasses_Program_trv(ClassNameTranspVisitor cntv, EdgeVisitor aev) {
    toAllSubclasses_Program_trv_bef(cntv, aev);
    edges.toAllSubclasses_Program_trv(cntv, aev);
    if (sources != null) {
    sources.toAllSubclasses_Program_trv(cntv, aev);
    }
    if (targets != null) {
    targets.toAllSubclasses_Program_trv(cntv, aev);
    }
    toAllSubclasses_Program_trv_aft(cntv, aev);
  }
  void checkStars_ProgramBehavior_trv_bef(DummyVisitor dv) {
    super.checkStars_ProgramBehavior_trv_bef(dv);
  }
  void checkStars_ProgramBehavior_trv_aft(DummyVisitor dv) {
    super.checkStars_ProgramBehavior_trv_aft(dv);
  }
  void checkStars_ProgramBehavior_trv(DummyVisitor dv) {
    checkStars_ProgramBehavior_trv_bef(dv);
    edges.checkStars_ProgramBehavior_trv(dv);
    if (sources != null) {
    sources.checkStars_ProgramBehavior_trv(dv);
    }
    if (targets != null) {
    targets.checkStars_ProgramBehavior_trv(dv);
    }
    checkStars_ProgramBehavior_trv_aft(dv);
  }
  public void allSources(GlobVisitor __v0) {
    allSources_StrategyGraph_trv(__v0);
  }
  void allSources_StrategyGraph_trv_bef(GlobVisitor __v0) {
    super.allSources_StrategyGraph_trv_bef(__v0);
  }
  void allSources_StrategyGraph_trv_aft(GlobVisitor __v0) {
    super.allSources_StrategyGraph_trv_aft(__v0);
  }
  void allSources_StrategyGraph_trv(GlobVisitor __v0) {
    allSources_StrategyGraph_trv_bef(__v0);
    edges.allSources_StrategyGraph_trv(__v0);
    allSources_StrategyGraph_trv_aft(__v0);
  }
  public void allDests(GlobVisitor __v0) {
    allDests_StrategyGraph_trv(__v0);
  }
  void allDests_StrategyGraph_trv_bef(GlobVisitor __v0) {
    super.allDests_StrategyGraph_trv_bef(__v0);
  }
  void allDests_StrategyGraph_trv_aft(GlobVisitor __v0) {
    super.allDests_StrategyGraph_trv_aft(__v0);
  }
  void allDests_StrategyGraph_trv(GlobVisitor __v0) {
    allDests_StrategyGraph_trv_bef(__v0);
    edges.allDests_StrategyGraph_trv(__v0);
    allDests_StrategyGraph_trv_aft(__v0);
  }
  public void __trav_markReachableForwardFromSources(__V_StrategyGraph_markReachableForwardFromSources __v0) {
    __trav_markReachableForwardFromSources_StrategyGraph_trv(__v0);
  }
  void __trav_markReachableForwardFromSources_StrategyGraph_trv_bef(__V_StrategyGraph_markReachableForwardFromSources __v0) {
    super.__trav_markReachableForwardFromSources_StrategyGraph_trv_bef(__v0);
    __v0.before(this);
  }
  void __trav_markReachableForwardFromSources_StrategyGraph_trv_aft(__V_StrategyGraph_markReachableForwardFromSources __v0) {
    super.__trav_markReachableForwardFromSources_StrategyGraph_trv_aft(__v0);
  }
  void __trav_markReachableForwardFromSources_StrategyGraph_trv(__V_StrategyGraph_markReachableForwardFromSources __v0) {
    __trav_markReachableForwardFromSources_StrategyGraph_trv_bef(__v0);
    if (sources != null) {
    sources.__trav_markReachableForwardFromSources_StrategyGraph_trv(__v0);
    }
    __trav_markReachableForwardFromSources_StrategyGraph_trv_aft(__v0);
  }
  public void __trav_markReachableBackwardFromTargets(__V_StrategyGraph_markReachableBackwardFromTargets __v0) {
    __trav_markReachableBackwardFromTargets_StrategyGraph_trv(__v0);
  }
  void __trav_markReachableBackwardFromTargets_StrategyGraph_trv_bef(__V_StrategyGraph_markReachableBackwardFromTargets __v0) {
    super.__trav_markReachableBackwardFromTargets_StrategyGraph_trv_bef(__v0);
    __v0.before(this);
  }
  void __trav_markReachableBackwardFromTargets_StrategyGraph_trv_aft(__V_StrategyGraph_markReachableBackwardFromTargets __v0) {
    super.__trav_markReachableBackwardFromTargets_StrategyGraph_trv_aft(__v0);
  }
  void __trav_markReachableBackwardFromTargets_StrategyGraph_trv(__V_StrategyGraph_markReachableBackwardFromTargets __v0) {
    __trav_markReachableBackwardFromTargets_StrategyGraph_trv_bef(__v0);
    if (targets != null) {
    targets.__trav_markReachableBackwardFromTargets_StrategyGraph_trv(__v0);
    }
    __trav_markReachableBackwardFromTargets_StrategyGraph_trv_aft(__v0);
  }
  public void __trav_allSourcesAndTargetsMarked(__V_StrategyGraph_allSourcesAndTargetsMarked __v0) {
    __trav_allSourcesAndTargetsMarked_StrategyGraph_trv(__v0);
  }
  void __trav_allSourcesAndTargetsMarked_StrategyGraph_trv_bef(__V_StrategyGraph_allSourcesAndTargetsMarked __v0) {
    super.__trav_allSourcesAndTargetsMarked_StrategyGraph_trv_bef(__v0);
  }
  void __trav_allSourcesAndTargetsMarked_StrategyGraph_trv_aft(__V_StrategyGraph_allSourcesAndTargetsMarked __v0) {
    super.__trav_allSourcesAndTargetsMarked_StrategyGraph_trv_aft(__v0);
  }
  void __trav_allSourcesAndTargetsMarked_StrategyGraph_trv(__V_StrategyGraph_allSourcesAndTargetsMarked __v0) {
    __trav_allSourcesAndTargetsMarked_StrategyGraph_trv_bef(__v0);
    if (sources != null) {
    sources.__trav_allSourcesAndTargetsMarked_StrategyGraph_trv(__v0);
    }
    if (targets != null) {
    targets.__trav_allSourcesAndTargetsMarked_StrategyGraph_trv(__v0);
    }
    __trav_allSourcesAndTargetsMarked_StrategyGraph_trv_aft(__v0);
  }
  public void __trav_getFirst(__V_StrategyGraph_getFirst __v0) {
    java.util.BitSet startSet = new java.util.BitSet();
    startSet.set(0);
    __trav_getFirst_StrategyGraph_trv(startSet, __v0);
  }
  void __trav_getFirst_StrategyGraph_trv_bef(__V_StrategyGraph_getFirst __v0) {
    super.__trav_getFirst_StrategyGraph_trv_bef(__v0);
  }
  void __trav_getFirst_StrategyGraph_trv_aft(__V_StrategyGraph_getFirst __v0) {
    super.__trav_getFirst_StrategyGraph_trv_aft(__v0);
  }
  void __trav_getFirst_StrategyGraph_trv(java.util.BitSet nodes, __V_StrategyGraph_getFirst __v0) {
    __trav_getFirst_StrategyGraph_trv_bef(__v0);
    { java.util.BitSet newnodes = new java.util.BitSet();
      newnodes.set(0);
      newnodes.and(nodes);
      if (!newnodes.equals(new java.util.BitSet()))
        edges.__trav_getFirst_StrategyGraph_trv(newnodes, __v0); }
    __trav_getFirst_StrategyGraph_trv_aft(__v0);
  }
  void __trav_toGraph_PathDirective_trv(__V_PathDirective_toGraph __v0) {  }
}

