@Test
public void testTwoNodePattern() throws PlanException{
//create pattern (foreach -> filter)
Class<?>[] nodes = {POForEach.class, POFilter.class};
PatternPlan ptPlan = PatternPlan.create(nodes);
PatternNode ptFilNode = (PatternNode) ptPlan.getSinks().get(0);
PatternNode ptFENode = (PatternNode) ptPlan.getSources().get(0);
//create plan with single Filter node
PhysicalPlan pplan = new PhysicalPlan();
POFilter fil = new POFilter(getNewOpKey());
pplan.add(fil);
// verify that match is false
assertFalse("plan not matched", ptPlan.match(pplan));
//verify that there is no match in the pattern nodes
assertEquals("null match", ptFilNode.getMatch(), null);
assertEquals("null match", ptFENode.getMatch(), null);
//add a foreach to the plan (fe -> fil)
POForEach fe = new POForEach(getNewOpKey());
pplan.add(fe);
pplan.connect(fe, fil);
// verify that match is true
assertTrue("plan matched", ptPlan.match(pplan));
// set leaf and source properties and try again
ptFilNode.setLeafNode(true);
ptFENode.setSourceNode(true);
assertTrue("plan matched", ptPlan.match(pplan));
//add a store to the plan to make it (fe -> fil -> store)
POStore store = new POStore(getNewOpKey());
pplan.add(store);
pplan.connect(fil, store);
// match should fail because filter pt node leaf property is set
assertFalse("plan matched", ptPlan.match(pplan));
//reset patter filter node leaf property
ptFilNode.setLeafNode(false);
// verify that match is true
assertTrue("plan matched", ptPlan.match(pplan));
assertEquals("filter pt node match", ptFilNode.getMatch(), fil);
assertEquals("foreach pt node match", ptFENode.getMatch(), fe);
//add a store to the plan to make it (fe -> fe -> fil -> store)
POForEach fe2 = new POForEach(getNewOpKey());
pplan.add(fe2);
pplan.connect(fe2, fe);
// match fails because fe pattern node is set to be source
assertFalse("plan matched", ptPlan.match(pplan));
ptFENode.setSourceNode(false);
// verify that match is true
assertTrue("plan matched", ptPlan.match(pplan));
assertEquals("filter pt node match", ptFilNode.getMatch(), fil);
assertEquals("foreach pt node match", ptFENode.getMatch(), fe);
//create new plan (fil -> fe)
PhysicalPlan pplan2 = new PhysicalPlan();
POFilter fil2 = new POFilter(getNewOpKey());
pplan.add(fil2);
POForEach fe21 = new POForEach(getNewOpKey());
pplan.add(fe21);
pplan.connect(fil2, fe21);
//verify that plan does not match
assertFalse("plan not matched", ptPlan.match(pplan2));
assertEquals("null match", ptFilNode.getMatch(), null);
assertEquals("null match", ptFENode.getMatch(), null);
}