@Test
 public void checkStitchCountsAndKnittingShapeRestored() throws Exception {
   engine.startNewRow();
   assertEquals(Direction.BACKWARDS, engine.getDirection());
   engine.knit(10);
   for (int i = 0; i < 10; i++) {
     engine.knitTwoTogether();
   }
   engine.knit(10);
   assertEquals(2, engine.getCurrentRowNumber());
   assertEquals(30, engine.getTotalNumberOfStitchesInRow());
   engine.endRow();
   assertEquals(2, engine.getTotalRowsCompleted());
   engine.declareRoundKnitting();
   assertEquals(KnittingShape.ROUND, engine.getKnittingShape());
   engine.startNewRow();
   engine.knit(5);
 }
Esempio n. 2
0
  protected Row analyzeRow(Row originalRow) {
    KnittingEngine engine = renderingContext.getEngine();
    // can't handle short rows
    if (originalRow.isShortRow()) {
      return null;
    }
    // can't handle colors (right now)
    if (originalRow.getYarnIdRef() != null) {
      return null;
    }

    List<InlineOperation> newOperations = new ArrayList<InlineOperation>();
    Row newRow = new Row(originalRow, newOperations);
    this.currentRowInfo = new RowInfo();

    // Begin the row for the engine
    if (dynamicFirstRowCastOn) {
      // if this is an instruction definition, infer the intended shape
      // from what is specified by the first row's shape.
      // If nothing is specified, flat knitting is assumed.
      if (originalRow.getType() != null) {
        this.shape = originalRow.getType();
      }
      // cast on one stitch so that we can start a new row below
      engine.castOn(1, false);
    }
    engine.startNewRow();
    if (this.startingSide == null) {
      this.startingSide = Side.RIGHT;
      if (dynamicFirstRowCastOn && originalRow.getSide() == Side.WRONG) {
        this.startingSide = Side.WRONG;
        engine.knit();
        engine.endRow();
        engine.startNewRow();
      } else if (engine.getDirection() == Direction.BACKWARDS) {
        this.startingSide = Side.WRONG;
      }
    }

    // Walk through all of the row's operations and see how it affects the
    // counts
    for (InlineOperation operation : originalRow.getOperations()) {
      InlineOperation newOperation = handle(operation);
      // if null is returned, we cannot chart, so return null up the chain
      if (newOperation == null) {
        return null;
      }
      newOperations.add(newOperation);
    }

    // End the row for the engine
    if (dynamicFirstRowCastOn) {
      // remove the "extra" stitch we created in order to start the row.
      engine.reverseSlip(1);
      // don't swallow the real stitch's nature
      StitchNature stitchNature = engine.peekAtNextStitch().getCurrentNature();
      if (stitchNature == StitchNature.PURL) {
        engine.purlTwoTogether();
      } else {
        engine.knitTwoTogether();
      }
      // now pretend like all is normal
      dynamicFirstRowCastOn = false;
    }
    engine.endRow();

    // if this row pushed the bounds of the previously recorded maximum
    // width, record the new max width
    if (currentRowInfo.getRowWidth() > maxWidth) {
      maxWidth = currentRowInfo.getRowWidth();
    }

    // if (!repeatInCurrentRow) {
    // resetGlobalRepeatTracker();
    // }

    this.rowInfos.add(currentRowInfo);
    return newRow;
  }