/** * This function returns the cost of the rule for the specified stack. Lower * the cost, the better the rule is matched * * @param stack * Node stack encountered so far * @return cost of the function * @throws SemanticException */ @Override public int cost(Stack<Node> stack) throws SemanticException { if (rulePatternIsValidWithoutWildCardChar()) { return costPatternWithoutWildCardChar(stack); } if (rulePatternIsValidWithWildCardChar()) { return costPatternWithWildCardChar(stack); } if (rulePatternIsValidWithORWildCardChar()) { return costPatternWithORWildCardChar(stack); } // If we reached here, either : // 1. patternWithWildCardChar and patternWithoutWildCardChar are both nulls. // 2. patternWithWildCardChar and patternWithoutWildCardChar are both not nulls. // This is an internal error and we should not let this happen, so throw an exception. throw new SemanticException ( "Rule pattern is invalid for " + getName() + " : patternWithWildCardChar = " + patternWithWildCardChar + " patternWithoutWildCardChar = " + patternWithoutWildCardChar); }
/** * This function returns the cost of the rule for the specified stack. Lower * the cost, the better the rule is matched * * @param stack * Node stack encountered so far * @return cost of the function * @throws SemanticException */ @Override public int cost(Stack<Node> stack) throws SemanticException { if (rulePatternIsValidWithoutWildCardChar()) { return costPatternWithoutWildCardChar(stack); } if (rulePatternIsValidWithWildCardChar()) { return costPatternWithWildCardChar(stack); } if (rulePatternIsValidWithORWildCardChar()) { return costPatternWithORWildCardChar(stack); } // If we reached here, either : // 1. patternWithWildCardChar and patternWithoutWildCardChar are both nulls. // 2. patternWithWildCardChar and patternWithoutWildCardChar are both not nulls. // This is an internal error and we should not let this happen, so throw an exception. throw new SemanticException ( "Rule pattern is invalid for " + getName() + " : patternWithWildCardChar = " + patternWithWildCardChar + " patternWithoutWildCardChar = " + patternWithoutWildCardChar); }
@Test public void testPatternWithoutWildCardChar() { String patternStr = ReduceSinkOperator.getOperatorName() + "%" + SelectOperator.getOperatorName() + "%" + FileSinkOperator.getOperatorName() + "%"; RuleRegExp rule1 = new RuleRegExp("R1", patternStr); assertEquals(rule1.rulePatternIsValidWithoutWildCardChar(), true); assertEquals(rule1.rulePatternIsValidWithWildCardChar(), false); // positive test Stack<Node> ns1 = new Stack<Node>(); ns1.push(new TestNode(ReduceSinkOperator.getOperatorName())); ns1.push(new TestNode(SelectOperator.getOperatorName())); ns1.push(new TestNode(FileSinkOperator.getOperatorName())); try { assertEquals(rule1.cost(ns1), patternStr.length()); } catch (SemanticException e) { fail(e.getMessage()); } // negative test Stack<Node> ns2 = new Stack<Node>(); ns2.push(new TestNode(ReduceSinkOperator.getOperatorName())); ns1.push(new TestNode(TableScanOperator.getOperatorName())); ns1.push(new TestNode(FileSinkOperator.getOperatorName())); try { assertEquals(rule1.cost(ns2), -1); } catch (SemanticException e) { fail(e.getMessage()); } }
+ FileSinkOperator.getOperatorName() + "%)"); assertEquals(rule1.rulePatternIsValidWithoutWildCardChar(), false); assertEquals(rule1.rulePatternIsValidWithWildCardChar(), true);