Exemplo n.º 1
0
 /**
  * Test if all of the <CODE>NumberField</CODE>s on this <CODE>Floor</CODE> are superfields of
  * <CODE>n</CODE>
  *
  * @param n A number field for which we wish to determine if all members of this floor are
  *     superfields
  * @return <CODE>true</CODE> iff all number fields belonging to this floor are subfields of
  *     <CODE>n</CODE>; <CODE>false</CODE> otherwise
  */
 public boolean allSuperFieldsOf(NumberField n) {
   Component[] all = getComponents();
   for (int i = 0; i < all.length; i++) {
     NumberField maybeSuper = (NumberField) all[i];
     // DEBUG:
     // System.err.println("\tSuperfield Test:" + maybeSuper.getPoly() + ", " + n.getPoly());
     if (!maybeSuper.hasSubfield(n)) return false;
   }
   return true;
 }
Exemplo n.º 2
0
 /**
  * Where to insert <CODE>n</CODE>, based only on degree, in the current tower. In <CODE>
  * addField()</CODE> we further test to see if the floors above, and/or at, and/or below are
  * sub-/superfields.
  *
  * @param n The NumberField whose insertion index we are intersted in
  * @return The index where <CODE>n</CODE> should be inserted into <CODE>floors</CODE>
  * @see #floors
  */
 private int findInsertionIndex(NumberField n) {
   int i = 0;
   for (i = 0; i < floors.size(); i++) {
     Floor f = (Floor) floors.get(i);
     if (f.getDegree() <= n.getDegree()) break;
   }
   return i;
 }
Exemplo n.º 3
0
      /**
       * Test if all of the <CODE>NumberField</CODE>s on this <CODE>Floor</CODE> are subfields of
       * <CODE>n</CODE>
       *
       * @param n A number field for which we wish to determine if all members of this floor are
       *     subfields
       * @return <CODE>true</CODE> iff all number fields belonging to this floor are subfields of
       *     <CODE>n</CODE>; <CODE>false</CODE> otherwise
       */
      public boolean allSubfieldsOf(NumberField n) {

        Component[] all = getComponents();
        for (int i = 0; i < all.length; i++) {
          NumberField maybeSub = (NumberField) all[i];

          if (!n.hasSubfield(maybeSub)) return false;
        }
        return true;
      }
Exemplo n.º 4
0
      /**
       * Creates a new <CODE>Floor</CODE> with the given inhabitant
       *
       * @param n A <CODE>NumberField</CODE> that should live on the new <CODE>Floor</CODE>
       */
      private Floor(NumberField n) {
        super();
        // DEBUG use
        /*
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                floorMouseEntered(evt);
            }
        });*/
        setLayout(new BoxLayout(this, javax.swing.BoxLayout.X_AXIS));
        setFocusable(false);
        setOpaque(false);

        degree = n.getDegree();
        add(n);
      }
Exemplo n.º 5
0
  /**
   * Determine the subfields of <CODE>n</CODE> and store the results there as well.
   *
   * @param n The NumberField whose subfields we are after
   */
  private void determineSubfields(NumberField n) {
    // Progress Bar
    GiANT.gui.appendConsoleText(
        "\nDetermining sub- and superfield relations for new field" + ELL, false);
    GiANT.gui.showProgressBar(true);

    // DEBUG
    // System.err.println("subfields start for " + n.getName());
    int i = 0;
    final float size = (float) allFields.size();
    final int max = GiANT.gui.getProgressMax();

    Iterator it = allFields.iterator();
    while (it.hasNext()) {
      // Progress Bar
      float index = (float) i++;
      int progress = (int) (max * (index / size));
      // DEBUG
      // System.err.println("\tprogress=" + progress);
      GiANT.gui.setProgress(progress);

      NumberField test = (NumberField) it.next();

      if (n.hasSubfield(test) || test.hasSubfield(n)) {
        // do nothing
      } else if (GiANT.gui.kash.isSubfield(test, n)) {
        n.addSubfield(test); // also adds all subfields of test to subfields of this
        if (test.getDegree() == n.getDegree()) {
          test.addSubfield(n);
        }
      } else if (GiANT.gui.kash.isSubfield(n, test)) {
        test.addSubfield(n); // also adds all subfields of n to subfields of this
      }
    }
    // Progress Bar
    GiANT.gui.showProgressBar(false);
    GiANT.gui.appendConsoleText("done.\n", false);
    // System.err.println("subfields end for " + n.getName());
  }
Exemplo n.º 6
0
  /**
   * Add <CODE>newguy</CODE> to the list of all fields in this city
   *
   * @param newguy the NumberField to add
   * @see #allFields
   */
  protected void addField(NumberField newguy) {
    boolean fatal = false;
    if (population >= MAX_POPULATION) {
      GiANT.gui.appendConsoleText(
          "\nUnable to add numberfield; maximum population of " + MAX_POPULATION + " reached.",
          true);
      fatal = true;
    } else if (allFields.contains(newguy)) {
      GiANT.gui.appendConsoleText(
          "\nUnable to add numberfield. It duplicates one already displayed.", true);
      fatal = true;
    }

    if (fatal) return;
    // we will need to know all subfields of this cat...
    determineSubfields(newguy);
    // System.err.println("entering addField()");
    // TODO do not allow duplicate (isomorphic) numberfields to be added

    boolean homeless = true; // we have yet to find a place for newguy
    boolean redraw = false;
    // update maxDegree
    final int newDeg = newguy.getDegree();
    if (newDeg > maxDegree) {
      maxDegree = newDeg;
      redraw = true;
    }

    // search all towers, left to right
    towers:
    for (int i = 0; i < towers.size(); i++) {
      Tower t = (Tower) towers.get(i);

      // boolean flags for existence of [proper] superfields / subfields
      boolean hasSuper = false;
      boolean hasSub = false;

      final int insert = t.findInsertionIndex(newguy);

      boolean hasEqual = false;
      Tower.Floor f = null;
      if (insert == t.getHeight()) {
        // nothing to check; we are outside of the current tower array
      } else {
        f = t.getFloor(insert);
        if (f.getDegree() == newDeg) hasEqual = true;
      }

      Tower.Floor above = null;
      Tower.Floor below = null;
      if (insert == 0) { // we are at the top of the tower
        hasSuper = true; // super is C
        int fetch = 0;

        if (hasEqual) fetch = 1;

        if (fetch >= t.getHeight()) {
          hasSub = true; // there are no towers below, sub is Q
        } else {
          below = t.getFloor(fetch);
          if (below.allSubfieldsOf(newguy)) hasSub = true;
        }
      } else if (insert == t.getHeight()) { // we are at the very bottom of the tower
        hasSub = true; // sub is Q itself

        above = t.getFloor(t.getHeight() - 1);

        if (above.allSuperFieldsOf(newguy)) hasSuper = true;
      } else { // we are somewhere WITHIN the tower
        if (hasEqual) {
          above = t.getFloor(insert - 1);
          if (insert == t.getHeight() - 1) hasSub = true; // sub is Q
          else below = t.getFloor(insert + 1);
        } else {
          above = t.getFloor(insert - 1);
          below = t.getFloor(insert);
        }

        if (above.allSuperFieldsOf(newguy)) hasSuper = true;
        if (hasSub) {
          // do nothing
        } else { // test for the same
          if (below.allSubfieldsOf(newguy)) hasSub = true;
        }
      }

      if (hasSub && hasSuper) {
        if (hasEqual) // kkl
        if (!f.allSubfieldsOf(
              newguy)) // could also use allSuperfieldsOf(), we need to make sure all fields this
                       // floor are isomorphic to newguy...
          continue towers; //
        homeless = false;
        t.add(insert, hasEqual, newguy);
        break towers;
      }
    }

    if (homeless) { // then build a new tower for newguy
      Tower newT = newTower();
      newT.add(0, false, newguy);
    }

    population++; // one more citizen!
    allFields.add(newguy); // add him to the set of all fields
    // if(redraw) //OPTIM
    redraw();
    GiANT.gui.repaint();
    // System.err.println("exiting addField()");
  }
Exemplo n.º 7
0
  /**
   * Draw the lines connecting fields on the desktop. These lines represent super-/subfield
   * relations.
   *
   * @param g The Graphics context on which to draw the lines
   * @see #linesLayer
   */
  private void drawLines(Graphics g) {
    java.awt.Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(
        java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON);

    final Point Q = getCenterLocation(QLabel);

    for (int i = 0; i < towers.size(); i++) {
      Tower t = (Tower) towers.get(i);
      g2.setColor(COLORS[i % COLORS.length]);
      boolean bottom = false;
      for (int j = 0; j < t.floors.size(); j++) {
        if (j == t.floors.size() - 1) bottom = true;
        Tower.Floor f = (Tower.Floor) t.getFloor(j);
        // iterate over all fields this floor
        Component[] fields = f.getComponents();
        for (int k = 0; k < fields.length; k++) {
          NumberField a = (NumberField) fields[k]; // line origin
          Point p1 = getCenterLocation(a.getCenterComponent()); // get center

          for (int l = 0; l < towers.size(); l++) {
            if (i == l) // use different colors for drawing within/without towers
            g2.setStroke(IN_LINE);
            else g2.setStroke(OUT_LINE);
            Tower t2 = (Tower) towers.get(l);
            // pen color changes according to destination tower
            // that way all 'sets of subsets' are monochrome
            // g2.setColor(COLORS[l % COLORS.length]);
            floors:
            for (int m = 0; m < t2.floors.size(); m++) {
              Tower.Floor f2 = (Tower.Floor) t2.getFloor(m);
              // iterate over all fields this floor
              boolean done = false;
              Component[] fields2 = f2.getComponents();
              for (int n = 0; n < fields2.length; n++) {
                NumberField b = (NumberField) fields2[n]; // line destination
                if (i == l & j == m) { // we are within a floor{
                  if (k == n) ; // do nothing (don't draw to yourself)
                  else { // to do, maybe have different lines for isomorphy?
                    Point p2 = getCenterLocation(b.getCenterComponent()); // get center
                    g2.drawLine(p1.x, p1.y, p2.x, p2.y);
                    done = true;
                  }
                } else if (a.hasSubfield(b) && !a.subfieldHasProperSubfield(b)) {
                  Point p2 = getCenterLocation(b.getCenterComponent()); // get center
                  g2.drawLine(p1.x, p1.y, p2.x, p2.y);
                  done = true;
                }
              }
              // go to the next tower; only need to draw to ONE floor per tower
              if (done) break floors;
            }
          }
          if (bottom && a.hasNoProperSubfields()) { // bottom of tower, no lines out of tower...
            g2.setColor(COLORS[i % COLORS.length]);
            g2.setStroke(IN_LINE);
            g2.drawLine(p1.x, p1.y, Q.x, Q.y); // then draw to Q!
          }
        }
      }
    }
  }