/** * printBrick Prints a String representation of a Brick with its subblocks to the error * printstream */ public void printBrick() { String brickName = this.getName(); if (!this.getVariant().isEmpty()) { brickName += "(" + this.getVariant() + ")"; } String brickKey = BrickLibrary.keyNumToName(this.getKey()); long brickDur = this.getDuration(); String brickType = this.getType(); System.out.println( "Printing brick " + brickName + " " + brickType + " " + brickKey + " " + brickDur); ArrayList<Block> subBlockList = this.getSubBlocks(); Iterator<Block> blockIter = subBlockList.iterator(); while (blockIter.hasNext()) { Block currentBlock = blockIter.next(); if (currentBlock instanceof Brick) { Brick currentBrick = (Brick) currentBlock; String currentBrickName = currentBrick.getName(); Long currentBrickKey = currentBrick.getKey(); String currentBrickKeyString = BrickLibrary.keyNumToName(currentBrickKey); long dur = currentBrick.getDuration(); System.out.println( "Printing brick " + currentBrickName + " " + currentBrickKeyString + " " + dur); } else if (currentBlock instanceof ChordBlock) { ChordBlock currentChord = (ChordBlock) currentBlock; String currentChordName = currentChord.getName(); int currentDuration = currentChord.getDuration(); System.out.println("Printing Chord" + currentChordName + " " + currentDuration); } } }
/** * setDuration Changes the duration to be as close as possible to the newly specified duration * * @param newDuration, an int duration. */ @Override public void setDuration(int newDuration) { float newDurFloat = newDuration; float ratio = (newDurFloat / this.getDuration()); List<Block> currentSubBlocks = this.getSubBlocks(); Iterator<Block> subBlockIter = currentSubBlocks.iterator(); ArrayList<Block> adjustedSubBlocks = new ArrayList<Block>(); while (subBlockIter.hasNext()) { Block currentBlock = subBlockIter.next(); if (currentBlock instanceof ChordBlock) { ((ChordBlock) currentBlock).changeChordDuration(ratio); adjustedSubBlocks.add(currentBlock); } else if (currentBlock instanceof Brick) { Brick adjustedSubBrick = (Brick) currentBlock; int newDur = Math.round(ratio * adjustedSubBrick.getDuration()); adjustedSubBrick.setDuration(newDur); adjustedSubBlocks.add(adjustedSubBrick); } } this.subBlocks = adjustedSubBlocks; this.updateDuration(); }
/** * Brick / 1 Copy constructor for a brick. Makes a deep copy. * * @param brick, a Brick */ public Brick(Brick brick) { super(brick.name, brick.getKey()); variant = brick.getVariant(); subBlocks = new ArrayList<Block>(); // Loop through all the subblocks, making copies of each ListIterator blockIter = brick.getSubBlocks().listIterator(); while (blockIter.hasNext()) { Block block = (Block) blockIter.next(); if (block.isOverlap()) { if (block.isBrick()) { Brick overlapBrick = new Brick((Brick) block); subBlocks.addAll(overlapBrick.flattenBlock()); } } else if (block instanceof Brick) { Brick nextBrick = new Brick((Brick) block); String subName = nextBrick.getName(); if (subName.contains(LAUNCHER_KEYWORD)) { String newName = subName.replaceAll(" \\(Launcher\\)", ""); newName = newName.replaceAll(LAUNCHER_KEYWORD, APPROACH_KEYWORD); nextBrick.setName(newName); } subBlocks.add(nextBrick); } else { subBlocks.add(new ChordBlock((ChordBlock) block)); } } type = brick.getType(); this.updateDuration(); mode = brick.getMode(); endValue = getSectionEnd(); }
/** * Make a Brick from a Polylist in the external representation of a RoadMap * * @param blockPolylist * @return */ public static Block fromPolylist(Polylist blockPolylist) { // Need to populate sub-blocks before calling constructor! Polylist temp; temp = blockPolylist.assoc("name"); String name = (String) temp.second(); temp = blockPolylist.assoc("variant"); String variant = temp.rest().nonEmpty() ? (String) temp.second() : DEFAULT_VARIANT; temp = blockPolylist.assoc("type"); String type = (String) temp.second(); temp = blockPolylist.assoc("key"); String key = (String) temp.second(); temp = blockPolylist.assoc("duration"); int duration = ((Number) temp.second()).intValue(); temp = blockPolylist.assoc("overlap"); boolean overlap = temp.second().equals("true"); temp = blockPolylist.assoc("end"); int endValue = ((Number) temp.second()).intValue(); temp = blockPolylist.assoc("mode"); String mode = (String) temp.second(); temp = blockPolylist.assoc("blocks"); ArrayList<Block> blocks = new ArrayList<Block>(); Polylist polyBlocks = temp.rest(); while (polyBlocks.nonEmpty()) { Polylist polyBlock = (Polylist) polyBlocks.first(); Block block = Block.fromPolylist(polyBlock); blocks.add(block); polyBlocks = polyBlocks.rest(); } Brick brick = new Brick(name, variant, BrickLibrary.keyNameToNum(key), type, blocks, mode); brick.setOverlap(overlap); brick.setSectionEnd(endValue); // Above, we are targeting this constructor: // // public Brick(String brickName, // long brickKey, // String type, // List<Block> brickList) return brick; }
/** * addSubBlocks / 3 Constructs the subblocks of a brick by reading in a PolyList and using a * BrickLibrary to convert it to bricks with appropriate subbricks. * * @param contents, a PolyList of subbricks * @param bricks, a BrickLibrary */ private void addSubBlocks( Polylist contents, BrickLibrary bricks, LinkedHashMap<String, LinkedList<Polylist>> polymap) { List<Block> subBlockList = new ArrayList<Block>(); while (contents.nonEmpty()) { Object obj = contents.first(); contents = contents.rest(); if (obj instanceof Polylist) { Polylist pList = (Polylist) obj; String blockType = pList.first().toString(); pList = pList.rest(); // If a subblock is a brick, split it into components and then // look up the corresponding brick in the library to construct // the necessary new brick. if (blockType.equals(BRICK_KEYWORD) && (pList.length() == 3 || pList.length() == 4)) { // determine the information about the name, variant, etc. String subBrickName = BrickLibrary.dashless(pList.first().toString()); pList = pList.rest(); String subBrickVariant = ""; if (pList.first() instanceof Polylist) { subBrickVariant = ((Polylist) pList.first()).toStringSansParens(); pList = pList.rest(); } String subBrickKeyString = pList.first().toString(); pList = pList.rest(); // Workaround added by RK for error reporting // in case of missing duration in sub-brick Object durObj = DEFAULT_SUBRICK_DURATION; if (pList.isEmpty()) { ErrorLog.log( ErrorLog.WARNING, "Missing Sub-Brick Duration in " + subBrickName + ", using 1"); } else { durObj = pList.first(); // pList = pList.rest(); } // when all data members are initialized, find the correct // brick scaled appropriately boolean starFlag = isStar(durObj); if (durObj instanceof Long || starFlag) { int dur = starFlag ? 0 : Arith.long2int((Long) durObj); long subBrickKeyNum = BrickLibrary.keyNameToNum(subBrickKeyString); Brick subBrick = null; // if the subbrick already exists in the dictionary if (bricks.hasBrick(subBrickName)) { if (!subBrickVariant.equals("")) { subBrick = bricks.getBrick(subBrickName, subBrickVariant, subBrickKeyNum, dur); } else { subBrick = bricks.getBrick(subBrickName, subBrickKeyNum, dur); } } // if the subbrick has yet to be initialized in the // dictionary, make one to use for now else if (polymap.containsKey(subBrickName)) { // find the appropriate definition to use to assemble // the subbrick LinkedList<Polylist> tokenList = polymap.get(subBrickName); Polylist tokens = null; if (subBrickVariant.equals("")) { tokens = tokenList.getFirst(); } else { for (Polylist p : tokenList) { Object variant = p.rest().rest().first(); if (variant instanceof Polylist && ((Polylist) variant).toStringSansParens().equals(subBrickVariant)) { tokens = p; break; } } if (tokens == null) { ErrorLog.log( ErrorLog.SEVERE, "Dictionary does not contain " + subBrickName + subBrickVariant); } } // find the elements of the subbrick String brickName = BrickLibrary.dashless(subBrickName); tokens = tokens.rest(); tokens = tokens.rest(); String brickVariant = ""; if (tokens.first() instanceof Polylist) { brickVariant = ((Polylist) tokens.first()).toStringSansParens(); tokens = tokens.rest(); } String brickMode = tokens.first().toString(); tokens = tokens.rest(); String brickType = tokens.first().toString(); tokens = tokens.rest(); String brickKeyString = tokens.first().toString(); tokens = tokens.rest(); long brickKeyNum = BrickLibrary.keyNameToNum(brickKeyString); // construct the subbrick subBrick = new Brick( brickName, brickVariant, brickKeyNum, brickType, tokens, bricks, brickMode, polymap); subBrick.transpose(Arith.long2int(subBrickKeyNum - brickKeyNum)); subBrick.setDuration(dur); } else { ErrorLog.log( ErrorLog.SEVERE, "Dictionary does " + "not contain " + subBrickName, true); } subBlockList.add(subBrick); } else { ErrorLog.log( ErrorLog.FATAL, subBrickName + ": " + "Duration not of type long: " + obj, true); } } // If a subblock is a chord, make an appropriate Chord object else if (blockType.equals(CHORD_KEYWORD) && pList.length() == 2) { String chordName = pList.first().toString(); pList = pList.rest(); Object durObj = pList.first(); // pList = pList.rest(); boolean starFlag = isStar(durObj); if (durObj instanceof Long || starFlag) { int dur = starFlag ? 0 : Arith.long2int((Long) durObj); ChordBlock subBlockChord = new ChordBlock(chordName, dur); subBlockList.add(subBlockChord); } else { ErrorLog.log( ErrorLog.FATAL, chordName + ": " + "Duration not of type long: " + durObj, true); } } else { ErrorLog.log( ErrorLog.WARNING, "Incorrect subblock of " + name + ": " + blockType + " " + pList.toStringSansParens()); } } } subBlocks.addAll(subBlockList); }