public override bool Rewrite(IGraphProcessingEnvironment procEnv, IMatches matches, IMatch chosenMatch) { CountResult.SetVariableValue(matches.Count, procEnv); if(matches.Count == 0) return false; if(Test) return false; procEnv.Finishing(matches, Special); #if DEBUGACTIONS || MATCHREWRITEDETAIL procEnv.PerformanceInfo.StartLocal(); #endif object[] retElems = null; IEnumerator<IMatch> matchesEnum = matches.GetEnumerator(); while(matchesEnum.MoveNext()) { IMatch match = matchesEnum.Current; if(match != matches.First) procEnv.RewritingNextMatch(); retElems = matches.Producer.Modify(procEnv, match); procEnv.PerformanceInfo.RewritesPerformed++; } if(retElems == null) retElems = NoElems; for(int i = 0; i < ParamBindings.ReturnVars.Length; i++) ParamBindings.ReturnVars[i].SetVariableValue(retElems[i], procEnv); #if DEBUGACTIONS || MATCHREWRITEDETAIL procEnv.PerformanceInfo.StopRewrite(); // total rewrite time does NOT include listeners anymore #endif procEnv.Finished(matches, Special); #if LOG_SEQUENCE_EXECUTION procEnv.Recorder.WriteLine("Matched/Applied " + Symbol); procEnv.Recorder.Flush(); #endif return true; }
public override bool Rewrite(IGraphProcessingEnvironment procEnv, IMatches matches, IMatch chosenMatch) { if (matches.Count == 0) return false; if (Test) return false; if (MinSpecified) { if(!(MinVarChooseRandom.GetVariableValue(procEnv) is int)) throw new InvalidOperationException("The variable '" + MinVarChooseRandom + "' is not of type int!"); if(matches.Count < (int)MinVarChooseRandom.GetVariableValue(procEnv)) return false; } procEnv.Finishing(matches, Special); #if DEBUGACTIONS || MATCHREWRITEDETAIL procEnv.PerformanceInfo.StartLocal(); #endif object[] retElems = null; if (!ChooseRandom) { if (chosenMatch!=null) throw new InvalidOperationException("Chosen match given although all matches should get rewritten"); IEnumerator<IMatch> matchesEnum = matches.GetEnumerator(); while (matchesEnum.MoveNext()) { IMatch match = matchesEnum.Current; if (match != matches.First) procEnv.RewritingNextMatch(); retElems = matches.Producer.Modify(procEnv, match); procEnv.PerformanceInfo.RewritesPerformed++; } if (retElems == null) retElems = NoElems; } else { object val = MaxVarChooseRandom != null ? MaxVarChooseRandom.GetVariableValue(procEnv) : (MinSpecified ? 2147483647 : 1); if (!(val is int)) throw new InvalidOperationException("The variable '" + MaxVarChooseRandom.Name + "' is not of type int!"); int numChooseRandom = (int)val; if (matches.Count < numChooseRandom) numChooseRandom = matches.Count; for (int i = 0; i < numChooseRandom; i++) { if (i != 0) procEnv.RewritingNextMatch(); int matchToApply = randomGenerator.Next(matches.Count); if (Choice) matchToApply = procEnv.UserProxy.ChooseMatch(matchToApply, matches, numChooseRandom - 1 - i, this); IMatch match = matches.RemoveMatch(matchToApply); if (chosenMatch != null) match = chosenMatch; retElems = matches.Producer.Modify(procEnv, match); procEnv.PerformanceInfo.RewritesPerformed++; } if (retElems == null) retElems = NoElems; } for(int i = 0; i < ParamBindings.ReturnVars.Length; i++) ParamBindings.ReturnVars[i].SetVariableValue(retElems[i], procEnv); #if DEBUGACTIONS || MATCHREWRITEDETAIL procEnv.PerformanceInfo.StopRewrite(); // total rewrite time does NOT include listeners anymore #endif procEnv.Finished(matches, Special); #if LOG_SEQUENCE_EXECUTION procEnv.Recorder.WriteLine("Matched/Applied " + Symbol); procEnv.Recorder.Flush(); #endif return true; }