package com.poco.PoCoRuntime;

import java.util.ArrayList;

/**
 * Created by caoyan on 12/3/14.
 */
public class MapExecution extends SequentialExecution implements Queryable,
        Matchable {
    private String operator;
    private SRE matchSre = null;
    private Boolean resultBool = false;
    private SRE resultSRE = null;

    public MapExecution(String modifier, String operator, SRE matchSre)
            throws PoCoException {
        super(modifier);
        this.operator = operator;
        this.matchSre = matchSre;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public void setMatchSre(SRE matchSre) {
        this.matchSre = matchSre;
    }

    public MapExecution(String modifier) throws PoCoException {
        super(modifier);
    }

    // use to set the current modifier for the first child before start query,
    // and later update modifier while advance cursor
    public boolean getCurrentChildModifier(String str) {
        if (currentCursor < children.size()) {
            if (str.equals("isZeroPlus"))
                return ((AbstractExecution) this.children.get(currentCursor))
                        .isZeroPlus();
            else
                return ((AbstractExecution) this.children.get(currentCursor))
                        .isOnePlus();
        } else
            return false;
    }

    private void advanceCursor() {
        if (isZeroPlus || isOnePlus)
            currentCursor = (currentCursor + 1) % children.size();
        else
            currentCursor++;
        if (currentCursor >= children.size())
            exhausted = true;
    }

    @Override
    public SRE query(Event event) {
        if (children.size() == 0 || exhausted){
            return null;
        }
        if (!resultBool) {
            EventResponder currentChild = children.get(currentCursor);
            if (currentChild.accepts(event)) {
                resultBool = true;
                if (!getCurrentChildModifier("isZeroPlus")
                        && !getCurrentChildModifier("isOnePlus")) {
                    advanceCursor();
                }
                SRE result = currentChild.query(event);
                resultSRE = result;
                resultSRE = SRELib.PerformBOPs("Union",matchSre, result);
                return resultSRE;
            } else { // not accepting
                if (getCurrentChildModifier("isZeroPlus")) {
                    // We can skip a zero-plus (*) modifier
                    advanceCursor();
                    this.query(event);
                } else {
                    // CurrentChild doesn't accept and can't be skipped
                    resultBool = false;
                    return null;
                }
            }
        }
        else {
            SRE temp = resultSRE;
            resultBool = false;
            return temp;
        }
        return null;
    }

    @Override
    public boolean accepts(Event event) {
        if (children.size() == 0) {
            return false;
        }
        // The first child of a sequential execution must accept for its parent
        // to accept
        this.query(event);
        return resultBool;
    }

    @Override
    public void addChild(EventResponder child) {
        super.addChild(child);
    }

    @Override
    public ArrayList<EventResponder> getChildren() {
        return super.getChildren();
    }

    @Override
    public String toString() {
        return "MapExecution [operator=" + operator + ", matchSre=" + matchSre
                + ", isZeroPlus=" + isZeroPlus + ", isOnePlus=" + isOnePlus
                + ", children=" + children + "]\n";
    }
}
@@ -247,24 +247,64 @@
 hSre = null;
+%0A%0A    private Boolean isQueried = false;
 %0A    private
@@ -348,22 +348,24 @@
 ate SRE 
-result
+operated
 SRE = nu
@@ -561,32 +561,69 @@
 Sre = matchSre;%0A
+        this.operatedSRE = matchSre;%0A
     %7D%0A%0A    publi
@@ -771,32 +771,69 @@
 Sre = matchSre;%0A
+        this.operatedSRE = matchSre;%0A
     %7D%0A%0A    publi
@@ -1856,16 +1856,17 @@
 hausted)
+ 
 %7B%0A      
@@ -1907,26 +1907,25 @@
    if (!
-resultBool
+isQueried
 ) %7B%0A    
@@ -2034,24 +2034,58 @@
 s(event)) %7B%0A
+                isQueried = true;%0A
             
@@ -2309,155 +2309,89 @@
     
-SRE result = currentChild.query(event);%0A                resultSRE = result;%0A                resultSRE = SRELib.PerformBOPs(%22Union%22,matchSre, result
+operatedSRE = SRELib.PerformBOPs(operator, operatedSRE, currentChild.query(event)
 );%0A 
@@ -2412,22 +2412,24 @@
  return 
-result
+operated
 SRE;%0A   
@@ -2871,24 +2871,16 @@
        %7D
-%0A       
  else %7B%0A
@@ -2895,53 +2895,17 @@
     
-SRE temp = resultSRE;%0A            resultBool 
+isQueried
 = fa
@@ -2932,12 +2932,19 @@
 urn 
-temp
+operatedSRE
 ;%0A  
@@ -2972,23 +2972,24 @@
 n null;%0A
-
     %7D%0A%0A
+%0A
     @Ove

fix union logic