Example #1
0
    public void transform(final GroupElement parent) throws InvalidPatternException {
      final List<GroupElement> orsList = new ArrayList<GroupElement>();
      // must keep order, so, using array
      final RuleConditionElement[] others = new RuleConditionElement[parent.getChildren().size()];

      // first we split children as OR or not OR
      int permutations = 1;
      int index = 0;
      for (final RuleConditionElement child : parent.getChildren()) {
        if ((child instanceof GroupElement) && ((GroupElement) child).isOr()) {
          permutations *= ((GroupElement) child).getChildren().size();
          orsList.add((GroupElement) child);
        } else {
          others[index] = child;
        }
        index++;
      }

      // transform parent into an OR
      parent.setType(GroupElement.OR);
      parent.getChildren().clear();

      // prepare arrays and indexes to calculate permutation
      final int[] indexes = new int[orsList.size()];

      // now we know how many permutations we will have, so create it
      for (int i = 1; i <= permutations; i++) {
        final GroupElement and = GroupElementFactory.newAndInstance();

        // create the actual permutations
        int mod = 1;
        for (int j = orsList.size() - 1; j >= 0; j--) {
          GroupElement or = orsList.get(j);
          // we must insert at the beginning to keep the order
          and.addChild(0, or.getChildren().get(indexes[j]).clone());
          if ((i % mod) == 0) {
            indexes[j] = (indexes[j] + 1) % or.getChildren().size();
          }
          mod *= or.getChildren().size();
        }

        // elements originally outside OR will be in every permutation, so add them
        // in their original position
        for (int j = 0; j < others.length; j++) {
          if (others[j] != null) {
            // always add clone of them to avoid offset conflicts in declarations

            // HERE IS THE MESSY PROBLEM: need to change further references to the appropriate
            // cloned ref
            and.addChild(j, others[j].clone());
          }
        }
        parent.addChild(and);
      }

      // remove duplications
      parent.pack();
    }
Example #2
0
    public void transform(final GroupElement parent) throws InvalidPatternException {

      if ((!(parent.getChildren().get(0) instanceof GroupElement))
          || (!((GroupElement) parent.getChildren().get(0)).isOr())) {
        throw new RuntimeException(
            "NotOrTransformation expected 'OR' but instead found '"
                + parent.getChildren().get(0).getClass().getName()
                + "'");
      }

      // we know a Not only ever has one child, and the previous algorithm
      // has confirmed the child is an OR

      final GroupElement or = (GroupElement) parent.getChildren().get(0);
      parent.setType(GroupElement.AND);
      parent.getChildren().clear();
      for (RuleConditionElement ruleConditionElement : or.getChildren()) {
        final GroupElement newNot = GroupElementFactory.newNotInstance();
        newNot.addChild(ruleConditionElement);
        parent.addChild(newNot);
      }
      parent.pack();
    }