SequentialFirstRuleCheckFixpointRuleController extends AbstractRuleController { @Override public boolean rewriteWithRuleCollection(Mutable<ILogicalOperator> root, Collection<IAlgebraicRewriteRule> ruleCollection) throws AlgebricksException { List<IAlgebraicRewriteRule> rules; if (ruleCollection instanceof List) { rules = (List<IAlgebraicRewriteRule>) ruleCollection; } else { throw AlgebricksException.create(ErrorCode.RULECOLLECTION_NOT_INSTANCE_OF_LIST, this.getClass().getName()); } if (rules.isEmpty()) { return false; } boolean anyRuleFired = false; boolean anyChange; boolean firstRuleChecked = false; do { anyChange = false; for (int i = 0; i < rules.size(); i++) { boolean ruleFired = rewriteOperatorRef(root, rules.get(i), true, fullDfs); if (!firstRuleChecked && i == 0 && !ruleFired) { return ruleFired; } if (ruleFired) { anyChange = true; anyRuleFired = true; } } firstRuleChecked = true; } while (anyChange); return anyRuleFired; } SequentialFirstRuleCheckFixpointRuleController(boolean fullDfs); @Override boolean rewriteWithRuleCollection(Mutable<ILogicalOperator> root,
            Collection<IAlgebraicRewriteRule> ruleCollection);  }
@Test public void testRewriteWithRuleCollection() throws Exception { SequentialFirstRuleCheckFixpointRuleController ruleController = new SequentialFirstRuleCheckFixpointRuleController(true); IAlgebraicRewriteRule firstRule = mock(IAlgebraicRewriteRule.class); IAlgebraicRewriteRule secondRule = mock(IAlgebraicRewriteRule.class); IAlgebraicRewriteRule thirdRule = mock(IAlgebraicRewriteRule.class); List<IAlgebraicRewriteRule> ruleList = new LinkedList<>(); ruleList.add(firstRule); ruleList.add(secondRule); ruleList.add(thirdRule); @SuppressWarnings("unchecked") Mutable<ILogicalOperator> root = PowerMockito.mock(Mutable.class); AbstractLogicalOperator rootOp = PowerMockito.mock(AbstractLogicalOperator.class); List<Mutable<ILogicalOperator>> emptyList = new ArrayList<>(); PowerMockito.when(root.getValue()).thenReturn(rootOp); PowerMockito.when(rootOp.getInputs()).thenReturn(emptyList); PowerMockito.when(firstRule.rewritePre(any(), any())).thenReturn(true).thenReturn(false); PowerMockito.when(secondRule.rewritePre(any(), any())).thenReturn(false); PowerMockito.when(thirdRule.rewritePre(any(), any())).thenReturn(false); ruleController.rewriteWithRuleCollection(root, ruleList); verify(firstRule, times(2)).rewritePre(any(), any()); verify(secondRule, times(2)).rewritePre(any(), any()); verify(thirdRule, times(2)).rewritePre(any(), any()); reset(firstRule); reset(secondRule); reset(thirdRule); PowerMockito.when(firstRule.rewritePre(any(), any())).thenReturn(false); PowerMockito.when(secondRule.rewritePre(any(), any())).thenReturn(true); PowerMockito.when(thirdRule.rewritePre(any(), any())).thenReturn(true); ruleController.rewriteWithRuleCollection(root, ruleList); verify(firstRule, times(1)).rewritePre(any(), any()); verify(secondRule, times(0)).rewritePre(any(), any()); verify(thirdRule, times(0)).rewritePre(any(), any()); reset(firstRule); reset(secondRule); reset(thirdRule); PowerMockito.when(firstRule.rewritePre(any(), any())).thenReturn(true).thenReturn(false); PowerMockito.when(secondRule.rewritePre(any(), any())).thenReturn(true).thenReturn(true).thenReturn(false); PowerMockito.when(thirdRule.rewritePre(any(), any())).thenReturn(false); ruleController.rewriteWithRuleCollection(root, ruleList); verify(firstRule, times(3)).rewritePre(any(), any()); verify(secondRule, times(3)).rewritePre(any(), any()); verify(thirdRule, times(3)).rewritePre(any(), any()); }