public void testPatternPossibilityRedundancy() throws Exception {
    StatelessSession session =
        getStatelessSession(this.getClass().getResourceAsStream("Possibilities.drl"));

    session.setAgendaFilter(new RuleNameMatchesAgendaFilter("Find pattern possibility redundancy"));

    Collection<Object> data = new ArrayList<Object>();

    VerifierResult result = VerifierResultFactory.createVerifierResult();
    session.setGlobal("result", result);

    String ruleName1 = "Rule 1";
    String ruleName2 = "Rule 2";

    Pattern p1 = new Pattern();
    p1.setRuleName(ruleName1);
    Pattern p2 = new Pattern();
    p2.setRuleName(ruleName2);

    LiteralRestriction lr1 = new LiteralRestriction();
    lr1.setRuleName(ruleName1);
    LiteralRestriction lr2 = new LiteralRestriction();
    lr2.setRuleName(ruleName2);

    PatternPossibility pp1 = new PatternPossibility();
    pp1.setPatternId(p1.getId());
    pp1.setRuleName(ruleName1);
    pp1.add(lr1);

    PatternPossibility pp2 = new PatternPossibility();
    pp2.setPatternId(p2.getId());
    pp2.setRuleName(ruleName2);
    pp2.add(lr2);

    Redundancy r1 = new Redundancy(lr1, lr2);
    Redundancy r2 = new Redundancy(p1, p2);

    data.add(p1);
    data.add(p2);
    data.add(lr1);
    data.add(lr2);
    data.add(pp1);
    data.add(pp2);
    data.add(r1);
    data.add(r2);

    StatelessSessionResult sessionResult = session.executeWithResults(data);

    Map<String, Set<String>> map = createRedundancyMap(sessionResult.iterateObjects());

    assertTrue(TestBase.mapContains(map, ruleName1, ruleName2));
    assertTrue(TestBase.mapContains(map, ruleName2, ruleName1));

    if (!map.isEmpty()) {
      fail("More redundancies than was expected.");
    }
  }
  @SuppressWarnings("rawtypes")
  boolean evalRules(Object asset) {
    StatelessSession session = ruleBase.newStatelessSession();
    StatelessSessionResult result = session.executeWithResults(asset);

    java.util.Iterator objects = result.iterateObjects();
    while (objects.hasNext()) {
      if (objects.next() instanceof Allow) {
        return true;
      }
    }
    return false;
  }
  @Test
  public void testCopyIdentifierGlobalExporterTwoValues() throws Exception {
    StatelessSession session = getSession();

    session.setGlobalExporter(new CopyIdentifiersGlobalExporter(new String[] {"list", "cheesery"}));

    StatelessSessionResult result = session.executeWithResults((Object) null);

    assertSame(this.list, result.getGlobal("list"));

    // cheesery should be null
    assertSame(this.cheesery, result.getGlobal("cheesery"));

    assertNotSame(this.globalResolver, result.getGlobalResolver());
  }
  @Test
  public void testReferenceOriginalGlobalExporter() throws Exception {
    StatelessSession session = getSession();

    // I've not specified any identifiers, so it should do them alll
    session.setGlobalExporter(new ReferenceOriginalGlobalExporter());

    StatelessSessionResult result = session.executeWithResults((Object) null);

    assertSame(this.list, result.getGlobal("list"));

    // cheesery should be null
    assertSame(this.cheesery, result.getGlobal("cheesery"));

    assertSame(this.globalResolver, result.getGlobalResolver());
  }
  public void testRulePossibilityRedundancy() throws Exception {
    StatelessSession session =
        getStatelessSession(this.getClass().getResourceAsStream("Possibilities.drl"));

    session.setAgendaFilter(new RuleNameMatchesAgendaFilter("Find rule possibility redundancy"));

    Collection<Object> data = new ArrayList<Object>();

    VerifierResult result = VerifierResultFactory.createVerifierResult();
    session.setGlobal("result", result);

    /*
     * First rules. These are redundant,
     */
    String ruleName1 = "Rule 1";
    String ruleName2 = "Rule 2";

    VerifierRule r1 = new VerifierRule();
    r1.setRuleName(ruleName1);
    VerifierRule r2 = new VerifierRule();
    r2.setRuleName(ruleName2);

    PatternPossibility pp1 = new PatternPossibility();
    pp1.setRuleName(ruleName1);
    PatternPossibility pp2 = new PatternPossibility();
    pp2.setRuleName(ruleName2);

    RulePossibility rp1 = new RulePossibility();
    rp1.setRuleId(r1.getId());
    rp1.setRuleName(ruleName1);
    rp1.add(pp1);

    RulePossibility rp2 = new RulePossibility();
    rp2.setRuleId(r2.getId());
    rp2.setRuleName(ruleName2);
    rp2.add(pp2);

    Redundancy possibilityredundancy = new Redundancy(RedundancyType.STRONG, pp1, pp2);
    Redundancy ruleRedundancy = new Redundancy(r1, r2);

    data.add(r1);
    data.add(r2);
    data.add(pp1);
    data.add(pp2);
    data.add(possibilityredundancy);
    data.add(ruleRedundancy);
    data.add(rp1);
    data.add(rp2);

    /*
     * These two rules are not redundant
     */
    String ruleName3 = "Rule 3";
    String ruleName4 = "Rule 4";

    VerifierRule r3 = new VerifierRule();
    r3.setRuleName(ruleName3);
    VerifierRule r4 = new VerifierRule();
    r4.setRuleName(ruleName4);

    PatternPossibility pp3 = new PatternPossibility();
    pp3.setRuleId(r3.getId());
    pp3.setRuleName(ruleName3);
    PatternPossibility pp4 = new PatternPossibility();
    pp4.setRuleId(r4.getId());
    pp4.setRuleName(ruleName4);
    // This possibility makes them different
    PatternPossibility pp5 = new PatternPossibility();
    pp5.setRuleId(r4.getId());
    pp5.setRuleName(ruleName4);

    RulePossibility rp3 = new RulePossibility();
    rp3.setRuleId(r3.getId());
    rp3.setRuleName(ruleName3);
    rp3.add(pp3);

    RulePossibility rp4 = new RulePossibility();
    rp4.setRuleId(r4.getId());
    rp4.setRuleName(ruleName4);
    rp4.add(pp4);
    rp4.add(pp5);

    Redundancy possibilityredundancy2 = new Redundancy(RedundancyType.STRONG, pp3, pp4);
    Redundancy ruleRedundancy2 = new Redundancy(r3, r4);

    data.add(r3);
    data.add(r4);
    data.add(pp3);
    data.add(pp4);
    data.add(pp5);
    data.add(possibilityredundancy2);
    data.add(ruleRedundancy2);
    data.add(rp3);
    data.add(rp4);

    StatelessSessionResult sessionResult = session.executeWithResults(data);

    Map<Cause, Set<Cause>> map =
        createRedundancyCauseMap(CauseType.RULE_POSSIBILITY, sessionResult.iterateObjects());

    assertTrue(TestBase.causeMapContains(map, rp1, rp2));
    assertFalse(TestBase.causeMapContains(map, rp3, rp4));

    if (!map.isEmpty()) {
      fail("More redundancies than was expected.");
    }
  }
Exemple #6
0
  @Test
  public void testPatternPossibilities() throws Exception {
    StatelessSession session =
        getStatelessSession(this.getClass().getResourceAsStream("Rules.drl"));

    session.setAgendaFilter(
        new RuleNameMatchesAgendaFilter("Rule possibility that is always true"));

    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<Object> data = new ArrayList<Object>();

    session.setGlobal("result", result);

    // This rule is always true.
    VerifierRule rule1 = VerifierComponentMockFactory.createRule1();
    Pattern pattern1 = VerifierComponentMockFactory.createPattern1();

    SubRule rp1 = new SubRule(rule1, 0);
    SubPattern pp1 = new SubPattern(pattern1, 0);
    AlwaysTrue alwaysTrue1 = new AlwaysTrue(pp1);
    SubPattern pp2 = new SubPattern(pattern1, 1);
    AlwaysTrue alwaysTrue2 = new AlwaysTrue(pp2);

    rp1.add(pp1);
    rp1.add(pp2);

    // This rule is okay.
    VerifierRule rule2 = VerifierComponentMockFactory.createRule2();
    Pattern pattern2 = VerifierComponentMockFactory.createPattern2();

    SubRule rp2 = new SubRule(rule2, 0);
    SubPattern pp3 = new SubPattern(pattern2, 0);
    SubPattern pp4 = new SubPattern(pattern2, 1);
    AlwaysTrue alwaysTrue4 = new AlwaysTrue(pp4);

    rp2.add(pp3);
    rp2.add(pp4);

    data.add(rule1);
    data.add(rp1);
    data.add(pp1);
    data.add(pp2);
    data.add(alwaysTrue1);
    data.add(alwaysTrue2);

    data.add(rule2);
    data.add(rp2);
    data.add(pp3);
    data.add(pp4);
    data.add(alwaysTrue4);

    StatelessSessionResult sessionResult = session.executeWithResults(data);
    Iterator iter = sessionResult.iterateObjects();

    boolean rp1true = false;
    boolean rp2true = false;
    boolean rp3true = false;
    boolean rp4true = false;
    while (iter.hasNext()) {
      Object o = (Object) iter.next();
      if (o instanceof AlwaysTrue) {
        AlwaysTrue alwaysTrue = (AlwaysTrue) o;
        if (!rp1true) {
          rp1true = alwaysTrue.getCause().equals(pp1);
        }
        if (!rp2true) {
          rp2true = alwaysTrue.getCause().equals(pp2);
        }
        if (!rp3true) {
          rp3true = alwaysTrue.getCause().equals(pp3);
        }
        if (!rp4true) {
          rp4true = alwaysTrue.getCause().equals(pp4);
        }
      }
    }

    assertTrue(rp1true);
    assertTrue(rp2true);
    assertFalse(rp3true);
    assertTrue(rp4true);
  }