private void labelRow(final int row, final int column, final String value) {
    final ActionType actionType = getActionForColumn(row, column);

    if (!value.trim().equals("")
        && (actionType.getCode() == Code.ACTION || actionType.getCode() == Code.CONDITION)) {
      this._cellComments.put(new Integer(column), value);
    } else {
      this._cellComments.put(
          new Integer(column), "From cell: " + RuleSheetParserUtil.rc2name(row, column));
    }
  }
  private void processRuleCell(
      final int row, final int column, final String value, final int mergedColStart) {
    String trimVal = value.trim();
    String testVal = trimVal.toLowerCase();
    if (testVal.startsWith(RULE_TABLE_TAG)) {
      finishRuleTable();
      initRuleTable(row, column, trimVal);
      return;
    }

    // Ignore any comments cells preceding the first rule table column
    if (column < this._ruleStartColumn) {
      return;
    }

    // Ignore any further cells from the rule def row
    if (row == this._ruleStartRow) {
      return;
    }

    switch (row - this._ruleStartRow) {
      case ACTION_ROW:
        ActionType.addNewActionType(this._actions, trimVal, column, row);
        break;

      case OBJECT_TYPE_ROW:
        objectTypeRow(row, column, trimVal, mergedColStart);
        break;

      case CODE_ROW:
        codeRow(row, column, trimVal);
        break;

      case LABEL_ROW:
        labelRow(row, column, trimVal);
        break;

      default:
        nextDataCell(row, column, trimVal);
        break;
    }
  }
Example #3
0
  @Test
  public void testChooseActionType() {

    Map<Integer, ActionType> actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "C", 0, 1);
    ActionType type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.CONDITION, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "CONDITION", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.CONDITION, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "A", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.ACTION, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "ACTION", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.ACTION, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "N", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.NAME, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "NAME", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.NAME, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "I", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.DESCRIPTION, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "DESCRIPTION", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.DESCRIPTION, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "P", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.SALIENCE, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "PRIORITY", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.SALIENCE, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "D", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.DURATION, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "DURATION", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.DURATION, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "T", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.TIMER, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "TIMER", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.TIMER, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "E", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.CALENDARS, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "CALENDARS", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.CALENDARS, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "U", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.NOLOOP, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "NO-LOOP", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.NOLOOP, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "L", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.LOCKONACTIVE, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "LOCK-ON-ACTIVE", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.LOCKONACTIVE, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "F", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.AUTOFOCUS, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "AUTO-FOCUS", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.AUTOFOCUS, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "X", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.ACTIVATIONGROUP, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "ACTIVATION-GROUP", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.ACTIVATIONGROUP, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "G", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.AGENDAGROUP, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "AGENDA-GROUP", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.AGENDAGROUP, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "R", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.RULEFLOWGROUP, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "RULEFLOW-GROUP", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.RULEFLOWGROUP, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "V", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.DATEEFFECTIVE, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "DATE-EFFECTIVE", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.DATEEFFECTIVE, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "Z", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.DATEEXPIRES, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "DATE-EXPIRES", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.DATEEXPIRES, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "@", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.METADATA, type.getCode());

    actionTypeMap = new HashMap<Integer, ActionType>();
    ActionType.addNewActionType(actionTypeMap, "METADATA", 0, 1);
    type = (ActionType) actionTypeMap.get(new Integer(0));
    assertEquals(Code.METADATA, type.getCode());
  }
  private void nextDataCell(final int row, final int column, final String value) {
    final ActionType actionType = getActionForColumn(row, column);

    if (row - this._ruleRow > 1) {
      // Encountered a row gap from the last rule.
      // This is not part of the ruleset.
      finishRuleTable();
      processNonRuleCell(row, column, value);
      return;
    }

    if (row > this._ruleRow) {
      // In a new row/rule
      String headCell = RuleSheetParserUtil.rc2name(this._ruleStartRow, this._ruleStartColumn);
      String ruleCell = RuleSheetParserUtil.rc2name(row, this._ruleStartColumn);
      this._currentRule = createNewRuleForRow(row, headCell, ruleCell);
      this._ruleList.add(this._currentRule);
      this._ruleRow++;
    }

    switch (actionType.getCode()) {
      case CONDITION:
      case ACTION:
      case METADATA:
        if (actionType.getSourceBuilder() == null) {
          throw new DecisionTableParseException(
              "Data cell "
                  + RuleSheetParserUtil.rc2name(row, column)
                  + " has an empty column header.");
        }
        actionType.addCellValue(row, column, value, _currentEscapeQuotesFlag);
        break;
      case SALIENCE:
        // Only if rule set is not sequential!
        if (!this._currentSequentialFlag) {
          if (value.startsWith("(") && value.endsWith(")")) {
            this._currentRule.setSalience(value);
          } else {
            try {
              this._currentRule.setSalience(new Integer(value));
            } catch (NumberFormatException nfe) {
              throw new DecisionTableParseException(
                  "Priority is not an integer literal, in cell "
                      + RuleSheetParserUtil.rc2name(row, column));
            }
          }
        }
        break;
      case NAME:
        this._currentRule.setName(value);
        break;
      case DESCRIPTION:
        this._currentRule.setDescription(value);
        break;
      case ACTIVATIONGROUP:
        this._currentRule.setActivationGroup(value);
        break;
      case AGENDAGROUP:
        this._currentRule.setAgendaGroup(value);
        break;
      case RULEFLOWGROUP:
        this._currentRule.setRuleFlowGroup(value);
        break;
      case NOLOOP:
        this._currentRule.setNoLoop(RuleSheetParserUtil.isStringMeaningTrue(value));
        break;
      case LOCKONACTIVE:
        this._currentRule.setLockOnActive(RuleSheetParserUtil.isStringMeaningTrue(value));
        break;
      case AUTOFOCUS:
        this._currentRule.setAutoFocus(RuleSheetParserUtil.isStringMeaningTrue(value));
        break;
      case DURATION:
        try {
          this._currentRule.setDuration(new Long(value));
        } catch (NumberFormatException nfe) {
          throw new DecisionTableParseException(
              "Duration is not an integer literal, in cell "
                  + RuleSheetParserUtil.rc2name(row, column));
        }
        break;
      case TIMER:
        this._currentRule.setTimer(value);
        break;
      case ENABLED:
        this._currentRule.setEnabled(RuleSheetParserUtil.isStringMeaningTrue(value));
        break;
      case CALENDARS:
        this._currentRule.setCalendars(value);
        break;
      case DATEEFFECTIVE:
        this._currentRule.setDateEffective(value);
        break;
      case DATEEXPIRES:
        this._currentRule.setDateExpires(value);
        break;
    }
  }
  private void codeRow(final int row, final int column, final String value) {
    final ActionType actionType = getActionForColumn(row, column);
    if (actionType.getSourceBuilder() == null) {
      if (actionType.getCode() == Code.CONDITION) {
        actionType.setSourceBuilder(new LhsBuilder(row - 2, column, null));
        this.sourceBuilders.add(actionType.getSourceBuilder());
      } else if (actionType.getCode() == Code.ACTION) {
        actionType.setSourceBuilder(new RhsBuilder(Code.ACTION, row - 2, column, null));
        this.sourceBuilders.add(actionType.getSourceBuilder());
      } else if (actionType.getCode() == Code.SALIENCE) {
        actionType.setSourceBuilder(new LhsBuilder(row - 2, column, null));
        this.sourceBuilders.add(actionType.getSourceBuilder());
      } else if (actionType.getCode() == Code.METADATA) {
        actionType.setSourceBuilder(new RhsBuilder(Code.METADATA, row - 2, column, null));
        this.sourceBuilders.add(actionType.getSourceBuilder());
      }
    }

    if (value.trim().equals("")
        && (actionType.getCode() == Code.ACTION
            || actionType.getCode() == Code.CONDITION
            || actionType.getCode() == Code.METADATA)) {
      throw new DecisionTableParseException(
          "Code description in cell "
              + RuleSheetParserUtil.rc2name(row, column)
              + " does not contain any code specification. It should!");
    }

    actionType.addTemplate(row, column, value);
  }
  /**
   * This is for handling a row where an object declaration may appear, this is the row immediately
   * above the snippets. It may be blank, but there has to be a row here.
   *
   * <p>Merged cells have "special meaning" which is why this is so freaking hard. A future refactor
   * may be to move away from an "event" based listener.
   */
  private void objectTypeRow(
      final int row, final int column, final String value, final int mergedColStart) {
    if (value.indexOf("$param") > -1 || value.indexOf("$1") > -1) {
      throw new DecisionTableParseException(
          "It looks like you have snippets in the row that is "
              + "meant for object declarations."
              + " Please insert an additional row before the snippets, "
              + "at cell "
              + RuleSheetParserUtil.rc2name(row, column));
    }
    ActionType action = getActionForColumn(row, column);
    if (mergedColStart == RuleSheetListener.NON_MERGED) {
      if (action.getCode() == Code.CONDITION) {
        SourceBuilder src = new LhsBuilder(row - 1, column, value);
        action.setSourceBuilder(src);
        this.sourceBuilders.add(src);

      } else if (action.getCode() == Code.ACTION) {
        SourceBuilder src = new RhsBuilder(Code.ACTION, row - 1, column, value);
        action.setSourceBuilder(src);
        this.sourceBuilders.add(src);
      }
    } else {
      if (column == mergedColStart) {
        if (action.getCode() == Code.CONDITION) {
          action.setSourceBuilder(new LhsBuilder(row - 1, column, value));
          this.sourceBuilders.add(action.getSourceBuilder());
        } else if (action.getCode() == Code.ACTION) {
          action.setSourceBuilder(new RhsBuilder(Code.ACTION, row - 1, column, value));
          this.sourceBuilders.add(action.getSourceBuilder());
        }
      } else {
        ActionType startOfMergeAction = getActionForColumn(row, mergedColStart);
        action.setSourceBuilder(startOfMergeAction.getSourceBuilder());
      }
    }
  }