Example #1
0
  @Test
  public void testRuleRender() {
    final Rule rule = new Rule("myrule", new Integer(42), 1);
    rule.setComment("rule comments");

    final Condition cond = new Condition();
    cond.setComment("cond comment");
    cond.setSnippet("cond snippet");
    rule.addCondition(cond);

    final Consequence cons = new Consequence();
    cons.setComment("cons comment");
    cons.setSnippet("cons snippet;");
    rule.addConsequence(cons);
    rule.addConsequence(cons);

    final DRLOutput out = new DRLOutput();
    rule.renderDRL(out);
    final String drl = out.getDRL();
    assertNotNull(drl);

    assertTrue(drl.indexOf("cond snippet") != -1);
    assertTrue(drl.indexOf("cons snippet") != -1);
    assertTrue(drl.indexOf("salience 42") != -1);
    assertTrue(drl.indexOf("salience 42") < drl.indexOf("when"));
    assertTrue(drl.indexOf("cond snippet") < drl.indexOf("then"));
    assertTrue(drl.indexOf("cons snippet;") > drl.indexOf("then"));
    assertTrue(drl.indexOf("rule") != -1);
    assertTrue(drl.indexOf("end") > drl.indexOf("rule "));
    assertTrue(drl.indexOf("//rule comments") > -1);
  }
 @Test
 public void testGetStylesheet() {
   Rule instance = new Rule(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
   Stylesheet expResult = new Stylesheet();
   expResult.getRules().add(instance);
   Stylesheet result = instance.getStylesheet();
   assertEquals(expResult, result);
 }
  private static Style createStyle(String stylesheetText) {

    Stylesheet stylesheet = CSSParser.getInstance().parse(stylesheetText);
    Rule rule = stylesheet.getRules().get(0);
    Selector sel = rule.getUnobservedSelectorList().get(0);
    Declaration decl = rule.getUnobservedDeclarationList().get(0);
    return new Style(sel, decl);
  }
 @Test
 public void testGetSelectors() {
   List<Selector> expResult = new ArrayList<Selector>();
   expResult.add(Selector.createSelector("One.two#three"));
   expResult.add(Selector.createSelector("Four.five#six"));
   Rule instance = new Rule(expResult, Collections.EMPTY_LIST);
   List result = instance.getSelectors();
   assertEquals(expResult, result);
 }
 @Ignore
 @Test
 public void testWriteBinary() throws Exception {
   System.out.println("writeBinary");
   DataOutputStream os = null;
   StringStore stringStore = null;
   Rule instance = null;
   instance.writeBinary(os, stringStore);
   fail("The test case is a prototype.");
 }
 @Ignore
 @Test
 public void testToString() {
   System.out.println("toString");
   Rule instance = null;
   String expResult = "";
   String result = instance.toString();
   assertEquals(expResult, result);
   fail("The test case is a prototype.");
 }
 @Test
 public void testGetOriginAfterRemovingRuleFromStylesheet() {
   Rule instance = new Rule(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
   Stylesheet stylesheet = new Stylesheet();
   stylesheet.getRules().add(instance);
   stylesheet.setOrigin(StyleOrigin.INLINE);
   stylesheet.getRules().remove(instance);
   StyleOrigin result = instance.getOrigin();
   assertNull(result);
 }
 @Test
 public void testGetOriginAfterSettingOriginBeforeAddingRuleToStylesheet() {
   Rule instance = new Rule(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
   Stylesheet stylesheet = new Stylesheet();
   stylesheet.setOrigin(StyleOrigin.INLINE);
   stylesheet.getRules().add(instance);
   StyleOrigin expResult = StyleOrigin.INLINE;
   StyleOrigin result = instance.getOrigin();
   assertEquals(expResult, result);
 }
 @Ignore
 @Test
 public void testApplies() {
   System.out.println("applies");
   Node node = null;
   Rule instance = null;
   long expResult = 0l;
   long result = instance.applies(node, null);
   assertEquals(expResult, result);
   fail("The test case is a prototype.");
 }
 @Test
 public void testGetDeclarations() {
   List<Declaration> expResult = new ArrayList<Declaration>();
   expResult.add(new Declaration("one", new ParsedValueImpl<String, String>("one", null), false));
   expResult.add(new Declaration("two", new ParsedValueImpl<String, String>("two", null), false));
   expResult.add(
       new Declaration("three", new ParsedValueImpl<String, String>("three", null), false));
   Rule instance = new Rule(Collections.EMPTY_LIST, expResult);
   List result = instance.getDeclarations();
   assertEquals(expResult, result);
 }
Example #11
0
  @Test
  public void testMetadata() throws Exception {
    Rule rule = new Rule("la", new Integer(42), 2);

    rule.addMetadata("Author( A. U. Thor )");
    rule.addMetadata("Revision( 42 )");
    DRLOutput out = new DRLOutput();
    rule.renderDRL(out);

    String result = out.toString();
    assertTrue(result.contains("@Author( A. U. Thor )"));
    assertTrue(result.contains("@Revision( 42 )"));
  }
Example #12
0
  /**
   * This checks that if the rule has "nil" salience, then no salience value should be put in the
   * rule definition. This allows default salience to work as advertised.
   */
  @Test
  public void testNilSalience() {
    Rule rule = new Rule("MyRule", null, 1);

    DRLOutput out = new DRLOutput();
    rule.renderDRL(out);
    String xml = out.toString();
    int idx = xml.indexOf("salience");
    assertEquals(-1, idx);

    rule = new Rule("MyRule", new Integer(42), 1);
    out = new DRLOutput();
    rule.renderDRL(out);
    xml = out.toString();
    idx = xml.indexOf("salience");
    assertTrue(idx > -1);
  }
 @Ignore
 @Test
 public void testReadBinary() throws Exception {
   System.out.println("readBinary");
   DataInputStream is = null;
   String[] strings = null;
   Rule expResult = null;
   Rule result = Rule.readBinary(Stylesheet.BINARY_CSS_VERSION, is, strings);
   assertEquals(expResult, result);
   fail("The test case is a prototype.");
 }
Example #14
0
  @Test
  public void testAttributes() throws Exception {
    Rule rule = new Rule("la", new Integer(42), 2);

    rule.setActivationGroup("foo");
    rule.setNoLoop(true);
    rule.setRuleFlowGroup("ruleflowgroup");
    rule.setDuration(42L);
    DRLOutput out = new DRLOutput();
    rule.renderDRL(out);

    String result = out.toString();

    assertTrue(result.indexOf("ruleflow-group \"ruleflowgroup\"") > -1);
    assertTrue(result.indexOf("no-loop true") > -1);
    assertTrue(result.indexOf("activation-group \"foo\"") > -1);
    assertTrue(result.indexOf("duration 42") > -1);
  }
Example #15
0
/** E ::= E '*' E | E '+' E | 'a' */
public class Test19 {

  static Nonterminal E = Nonterminal.withName("E");
  static org.iguana.grammar.symbol.Character a = Character.from('a');
  static Character plus = Character.from('+');
  static Character star = Character.from('*');

  static Rule r1 = Rule.withHead(E).addSymbols(E, star, E).build();
  static Rule r2 = Rule.withHead(E).addSymbols(E, plus, E).build();
  static Rule r3 = Rule.withHead(E).addSymbols(a).build();

  public static Grammar grammar = Grammar.builder().addRules(r1, r2, r3).build();
  private static Nonterminal startSymbol = E;

  private static Input input1 = Input.fromString("a+a");
  private static Input input2 = Input.fromString("a+a*a");
  private static Input input3 = Input.fromString("a+a*a+a*a");

  @Test
  public void testParser1() {
    GrammarGraph graph = GrammarGraph.from(grammar, input1);
    ParseResult result = Iguana.parse(input1, graph, startSymbol);
    assertTrue(result.isParseSuccess());
    assertEquals(getParseResult1(graph), result);
    assertEquals(getTree1(), result.asParseSuccess().getTree());
  }

  @Test
  public void testParser2() {
    GrammarGraph graph = GrammarGraph.from(grammar, input2);
    ParseResult result = Iguana.parse(input2, graph, startSymbol);
    assertTrue(result.isParseSuccess());
    assertEquals(getParseResult2(graph), result);
    assertEquals(getTree2(), result.asParseSuccess().getTree());
  }

  @Test
  public void testParser3() {
    GrammarGraph graph = GrammarGraph.from(grammar, input3, Configuration.DEFAULT);
    ParseResult result = Iguana.parse(input3, graph, startSymbol);
    assertTrue(result.isParseSuccess());
    assertEquals(getParseResult3(graph), result);
    assertEquals(getTree3(), result.asParseSuccess().getTree());
  }

  private ParseSuccess getParseResult1(GrammarGraph registry) {
    ParseStatistics statistics =
        ParseStatistics.builder()
            .setDescriptorsCount(8)
            .setGSSNodesCount(2)
            .setGSSEdgesCount(5)
            .setNonterminalNodesCount(3)
            .setTerminalNodesCount(3)
            .setIntermediateNodesCount(2)
            .setPackedNodesCount(5)
            .setAmbiguousNodesCount(0)
            .build();
    return new ParseSuccess(expectedSPPF1(registry), statistics, input1);
  }

  private NonterminalNode expectedSPPF1(GrammarGraph registry) {
    TerminalNode node0 = createTerminalNode(registry.getSlot("a"), 0, 1, input1);
    NonterminalNode node1 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node0, input1);
    TerminalNode node2 = createTerminalNode(registry.getSlot("+"), 1, 2, input1);
    IntermediateNode node3 =
        createIntermediateNode(registry.getSlot("E ::= E + . E"), node1, node2);
    TerminalNode node4 = createTerminalNode(registry.getSlot("a"), 2, 3, input1);
    NonterminalNode node5 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node4, input1);
    IntermediateNode node6 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node5);
    NonterminalNode node7 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node6, input1);
    return node7;
  }

  private Tree getTree1() {
    Tree t0 = createTerminal(0, 1, input1);
    Tree t1 = createRule(r3, list(t0), input1);
    Tree t2 = createTerminal(1, 2, input1);
    Tree t3 = createTerminal(2, 3, input1);
    Tree t4 = createRule(r3, list(t3), input1);
    Tree t5 = createRule(r2, list(t1, t2, t4), input1);
    return t5;
  }

  private ParseSuccess getParseResult2(GrammarGraph registry) {
    ParseStatistics statistics =
        ParseStatistics.builder()
            .setDescriptorsCount(16)
            .setGSSNodesCount(3)
            .setGSSEdgesCount(9)
            .setNonterminalNodesCount(6)
            .setTerminalNodesCount(5)
            .setIntermediateNodesCount(7)
            .setPackedNodesCount(14)
            .setAmbiguousNodesCount(1)
            .build();
    return new ParseSuccess(expectedSPPF2(registry), statistics, input1);
  }

  private NonterminalNode expectedSPPF2(GrammarGraph registry) {
    TerminalNode node0 = createTerminalNode(registry.getSlot("a"), 0, 1, input2);
    NonterminalNode node1 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node0, input2);
    TerminalNode node2 = createTerminalNode(registry.getSlot("+"), 1, 2, input2);
    IntermediateNode node3 =
        createIntermediateNode(registry.getSlot("E ::= E + . E"), node1, node2);
    TerminalNode node4 = createTerminalNode(registry.getSlot("a"), 2, 3, input2);
    NonterminalNode node5 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node4, input2);
    TerminalNode node6 = createTerminalNode(registry.getSlot("*"), 3, 4, input2);
    IntermediateNode node7 =
        createIntermediateNode(registry.getSlot("E ::= E * . E"), node5, node6);
    TerminalNode node8 = createTerminalNode(registry.getSlot("a"), 4, 5, input2);
    NonterminalNode node9 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node8, input2);
    IntermediateNode node10 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node7, node9);
    NonterminalNode node11 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E * E ."), node10, input2);
    IntermediateNode node12 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node11);
    IntermediateNode node13 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node5);
    NonterminalNode node14 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node13, input2);
    IntermediateNode node15 =
        createIntermediateNode(registry.getSlot("E ::= E * . E"), node14, node6);
    IntermediateNode node16 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node15, node9);
    NonterminalNode node17 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node12, input2);
    node17.addPackedNode(registry.getSlot("E ::= E * E ."), node16);
    return node17;
  }

  private Tree getTree2() {
    Tree t0 = createTerminal(0, 1, input2);
    Tree t1 = createRule(r3, list(t0), input2);
    Tree t2 = createTerminal(1, 2, input2);
    Tree t3 = createTerminal(2, 3, input2);
    Tree t4 = createRule(r3, list(t3), input2);
    Tree t5 = createRule(r2, list(t1, t2, t4), input2);
    Tree t6 = createTerminal(3, 4, input2);
    Tree t7 = createTerminal(4, 5, input2);
    Tree t8 = createRule(r3, list(t7), input2);
    Tree t9 = createRule(r1, list(t4, t6, t8), input2);
    Tree t10 = createAmbiguity(set(createBranch(list(t5, t6, t8)), createBranch(list(t1, t2, t9))));
    return t10;
  }

  private ParseSuccess getParseResult3(GrammarGraph registry) {
    ParseStatistics statistics =
        ParseStatistics.builder()
            .setDescriptorsCount(41)
            .setGSSNodesCount(5)
            .setGSSEdgesCount(20)
            .setNonterminalNodesCount(15)
            .setTerminalNodesCount(9)
            .setIntermediateNodesCount(26)
            .setPackedNodesCount(51)
            .setAmbiguousNodesCount(10)
            .build();
    return new ParseSuccess(expectedSPPF3(registry), statistics, input1);
  }

  private NonterminalNode expectedSPPF3(GrammarGraph registry) {
    TerminalNode node0 = createTerminalNode(registry.getSlot("a"), 0, 1, input3);
    NonterminalNode node1 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node0, input3);
    TerminalNode node2 = createTerminalNode(registry.getSlot("+"), 1, 2, input3);
    IntermediateNode node3 =
        createIntermediateNode(registry.getSlot("E ::= E + . E"), node1, node2);
    TerminalNode node4 = createTerminalNode(registry.getSlot("a"), 2, 3, input3);
    NonterminalNode node5 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node4, input3);
    TerminalNode node6 = createTerminalNode(registry.getSlot("*"), 3, 4, input3);
    IntermediateNode node7 =
        createIntermediateNode(registry.getSlot("E ::= E * . E"), node5, node6);
    TerminalNode node8 = createTerminalNode(registry.getSlot("a"), 4, 5, input3);
    NonterminalNode node9 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node8, input3);
    IntermediateNode node10 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node7, node9);
    NonterminalNode node11 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E * E ."), node10, input3);
    TerminalNode node12 = createTerminalNode(registry.getSlot("+"), 5, 6, input3);
    IntermediateNode node13 =
        createIntermediateNode(registry.getSlot("E ::= E + . E"), node11, node12);
    TerminalNode node14 = createTerminalNode(registry.getSlot("a"), 6, 7, input3);
    NonterminalNode node15 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node14, input3);
    IntermediateNode node16 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node13, node15);
    IntermediateNode node17 =
        createIntermediateNode(registry.getSlot("E ::= E + . E"), node9, node12);
    IntermediateNode node18 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node17, node15);
    NonterminalNode node19 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node18, input3);
    IntermediateNode node20 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node7, node19);
    NonterminalNode node21 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node16, input3);
    node21.addPackedNode(registry.getSlot("E ::= E * E ."), node20);
    TerminalNode node22 = createTerminalNode(registry.getSlot("*"), 7, 8, input3);
    IntermediateNode node23 =
        createIntermediateNode(registry.getSlot("E ::= E * . E"), node21, node22);
    TerminalNode node24 = createTerminalNode(registry.getSlot("a"), 8, 9, input3);
    NonterminalNode node25 =
        createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node24, input3);
    IntermediateNode node26 =
        createIntermediateNode(registry.getSlot("E ::= E * . E"), node15, node22);
    IntermediateNode node27 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node26, node25);
    NonterminalNode node28 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E * E ."), node27, input3);
    IntermediateNode node29 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node17, node28);
    IntermediateNode node30 =
        createIntermediateNode(registry.getSlot("E ::= E * . E"), node19, node22);
    IntermediateNode node31 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node30, node25);
    NonterminalNode node32 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node29, input3);
    node32.addPackedNode(registry.getSlot("E ::= E * E ."), node31);
    IntermediateNode node33 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node23, node25);
    node33.addPackedNode(registry.getSlot("E ::= E * E ."), node7, node32);
    IntermediateNode node34 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node13, node28);
    NonterminalNode node35 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E * E ."), node33, input3);
    node35.addPackedNode(registry.getSlot("E ::= E + E ."), node34);
    IntermediateNode node36 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node11);
    IntermediateNode node37 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node5);
    NonterminalNode node38 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node37, input3);
    IntermediateNode node39 =
        createIntermediateNode(registry.getSlot("E ::= E * . E"), node38, node6);
    IntermediateNode node40 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node39, node9);
    NonterminalNode node41 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node36, input3);
    node41.addPackedNode(registry.getSlot("E ::= E * E ."), node40);
    IntermediateNode node42 =
        createIntermediateNode(registry.getSlot("E ::= E + . E"), node41, node12);
    IntermediateNode node43 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node35);
    node43.addPackedNode(registry.getSlot("E ::= E + E ."), node42, node28);
    IntermediateNode node44 =
        createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node21);
    node44.addPackedNode(registry.getSlot("E ::= E + E ."), node42, node15);
    IntermediateNode node45 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node39, node19);
    NonterminalNode node46 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node44, input3);
    node46.addPackedNode(registry.getSlot("E ::= E * E ."), node45);
    IntermediateNode node47 =
        createIntermediateNode(registry.getSlot("E ::= E * . E"), node46, node22);
    IntermediateNode node48 =
        createIntermediateNode(registry.getSlot("E ::= E * E ."), node47, node25);
    node48.addPackedNode(registry.getSlot("E ::= E * E ."), node39, node32);
    NonterminalNode node49 =
        createNonterminalNode(
            registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node43, input3);
    node49.addPackedNode(registry.getSlot("E ::= E * E ."), node48);
    return node49;
  }

  private Tree getTree3() {
    Tree t0 = createTerminal(0, 1, input3);
    Tree t1 = createRule(r3, list(t0), input3);
    Tree t2 = createTerminal(1, 2, input3);
    Tree t3 = createTerminal(2, 3, input3);
    Tree t4 = createRule(r3, list(t3), input3);
    Tree t5 = createRule(r2, list(t1, t2, t4), input3);
    Tree t6 = createTerminal(3, 4, input3);
    Tree t7 = createTerminal(4, 5, input3);
    Tree t8 = createRule(r3, list(t7), input3);
    Tree t9 = createTerminal(5, 6, input3);
    Tree t10 = createTerminal(6, 7, input3);
    Tree t11 = createRule(r3, list(t10), input3);
    Tree t12 = createRule(r2, list(t8, t9, t11), input3);
    Tree t13 = createTerminal(7, 8, input3);
    Tree t14 = createTerminal(8, 9, input3);
    Tree t15 = createRule(r3, list(t14), input3);
    Tree t16 = createRule(r1, list(t11, t13, t15), input3);
    Tree t17 =
        createAmbiguity(set(createBranch(list(t12, t13, t15)), createBranch(list(t8, t9, t16))));
    Tree t18 = createRule(r1, list(t4, t6, t8), input3);
    Tree t19 =
        createAmbiguity(set(createBranch(list(t4, t6, t12)), createBranch(list(t18, t9, t11))));
    Tree t20 =
        createAmbiguity(set(createBranch(list(t1, t2, t18)), createBranch(list(t5, t6, t8))));
    Tree t21 =
        createAmbiguity(set(createBranch(list(t20, t9, t11)), createBranch(list(t1, t2, t19))));
    Tree t22 = createAmbiguity(set(createBranch(list(t21)), createBranch(list(t5, t6, t12))));
    Tree t23 =
        createAmbiguity(set(createBranch(list(t5, t6, t17)), createBranch(list(t22, t13, t15))));
    Tree t24 =
        createAmbiguity(set(createBranch(list(t19, t13, t15)), createBranch(list(t4, t6, t17))));
    Tree t25 = createAmbiguity(set(createBranch(list(t24)), createBranch(list(t18, t9, t16))));
    Tree t26 =
        createAmbiguity(set(createBranch(list(t20, t9, t16)), createBranch(list(t1, t2, t25))));
    Tree t27 = createAmbiguity(set(createBranch(list(t26)), createBranch(list(t23))));
    return t27;
  }
}
Example #16
0
 @Test
 public void testSalienceCalculator() {
   final int rowNumber = 2;
   final int salience = Rule.calcSalience(rowNumber);
   assertEquals(65533, salience);
 }
 @Test
 public void testGetOriginWithoutAddingRuleToStylesheet() {
   Rule instance = new Rule(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
   StyleOrigin result = instance.getOrigin();
   assertNull(result);
 }