/// <summary> /// Helper for checking rule all calls. /// Type checks the input, type checks the output. /// Throws an exception when an error is found. /// </summary> /// <param name="seqRuleAllCall">The rule all call to check</param> public void CheckRuleAllCall(SequenceRuleAllCall seqRuleAllCall) { CheckInputParameters(seqRuleAllCall, seqRuleAllCall.ArgumentExpressions, null); CheckOutputParametersRuleAll(seqRuleAllCall, seqRuleAllCall.ReturnVars); CheckFilterCalls(seqRuleAllCall.Name, seqRuleAllCall.Filters); CheckSubgraph(seqRuleAllCall); // ok, this is a well-formed invocation }
public void EmitRewritingRuleAllCallRandom(SourceBuilder source) { // as long as a further rewrite has to be selected: randomly choose next match, rewrite it and remove it from available matches; fire the next match event after the first SequenceRuleAllCall seqRuleAll = (SequenceRuleAllCall)ruleCallGen.seqRule; if (returnParameterDeclarations.Length != 0) { source.AppendFront(returnParameterDeclarationsAllCall + "\n"); } String matchesToChoose = "numchooserandomvar_" + seqRuleAll.Id; // variable storing number of matches to choose randomly source.AppendFrontFormat("int {0} = (int){1};\n", matchesToChoose, seqRuleAll.MaxVarChooseRandom != null ? ruleCallGen.seqHelper.GetVar(seqRuleAll.MaxVarChooseRandom) : (seqRuleAll.MinSpecified ? "2147483647" : "1")); source.AppendFrontFormat("if({0}.Count < {1}) {1} = {0}.Count;\n", matchesName, matchesToChoose); source.AppendFrontFormat("for(int i = 0; i < {0}; ++i)\n", matchesToChoose); source.AppendFront("{\n"); source.Indent(); source.AppendFront("if(i != 0) procEnv.RewritingNextMatch();\n"); source.AppendFront(matchType + " " + matchName + " = " + matchesName + ".RemoveMatchExact(GRGEN_LIBGR.Sequence.randomGenerator.Next(" + matchesName + ".Count));\n"); if (returnParameterDeclarations.Length != 0) { source.AppendFront(returnParameterDeclarations + "\n"); } source.AppendFront(ruleCallGen.ruleName + ".Modify(procEnv, " + matchName + returnArguments + ");\n"); if (returnAssignments.Length != 0) { source.AppendFront(intermediateReturnAssignmentsAllCall + "\n"); } source.AppendFront("procEnv.PerformanceInfo.RewritesPerformed++;\n"); source.Unindent(); source.AppendFront("}\n"); if (returnAssignments.Length != 0) { source.AppendFront(returnAssignmentsAllCall + "\n"); } }
public void Emit(SourceBuilder source, SequenceGenerator seqGen, bool fireDebugEvents) { String parameterDeclarations = null; String parameters = null; if (seqRule.Subgraph != null) { parameters = seqHelper.BuildParametersInDeclarations(seqRule, ArgumentExpressions, source, out parameterDeclarations); } else { parameters = seqHelper.BuildParameters(seqRule, ArgumentExpressions, source); } if (seqRule.Subgraph != null) { source.AppendFront(parameterDeclarations + "\n"); source.AppendFront("procEnv.SwitchToSubgraph((GRGEN_LIBGR.IGraph)" + seqHelper.GetVar(seqRule.Subgraph) + ");\n"); source.AppendFront("graph = ((GRGEN_LGSP.LGSPActionExecutionEnvironment)procEnv).graph;\n"); } source.AppendFront(matchesType + " " + matchesName + " = " + ruleName + ".Match(procEnv, " + (seqRule.SequenceType == SequenceType.RuleCall ? "1" : "procEnv.MaxMatches") + parameters + ");\n"); source.AppendFront("procEnv.PerformanceInfo.MatchesFound += " + matchesName + ".Count;\n"); for (int i = 0; i < seqRule.Filters.Count; ++i) { seqExprGen.EmitFilterCall(source, (SequenceFilterCallCompiled)seqRule.Filters[i], patternName, matchesName, seqRule.PackagePrefixedName, false); } if (seqRule is SequenceRuleCountAllCall) { SequenceRuleCountAllCall seqRuleCountAll = (SequenceRuleCountAllCall)seqRule; source.AppendFront(seqHelper.SetVar(seqRuleCountAll.CountResult, matchesName + ".Count")); } String insufficientMatchesCondition; if (seqRule is SequenceRuleAllCall && ((SequenceRuleAllCall)seqRule).ChooseRandom && ((SequenceRuleAllCall)seqRule).MinSpecified) { SequenceRuleAllCall seqRuleAll = (SequenceRuleAllCall)seqRule; String minMatchesVarName = "minmatchesvar_" + seqRuleAll.Id; source.AppendFrontFormat("int {0} = (int){1};\n", minMatchesVarName, seqHelper.GetVar(seqRuleAll.MinVarChooseRandom)); insufficientMatchesCondition = matchesName + ".Count < " + minMatchesVarName; } else { insufficientMatchesCondition = matchesName + ".Count == 0"; } source.AppendFrontFormat("if({0}) {{\n", insufficientMatchesCondition); source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seqRule, "false")); source.Unindent(); source.AppendFront("} else {\n"); source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seqRule, "true")); if (fireDebugEvents) { source.AppendFront("procEnv.Matched(" + matchesName + ", null, " + specialStr + ");\n"); } if (fireDebugEvents) { source.AppendFront("procEnv.Finishing(" + matchesName + ", " + specialStr + ");\n"); } SequenceRuleOrRuleAllCallRewritingGenerator rewritingGen = new SequenceRuleOrRuleAllCallRewritingGenerator(this); if (seqRule.SequenceType == SequenceType.RuleCall) { rewritingGen.EmitRewritingRuleCall(source); } else if (seqRule.SequenceType == SequenceType.RuleCountAllCall || !((SequenceRuleAllCall)seqRule).ChooseRandom) // seq.SequenceType == SequenceType.RuleAll { rewritingGen.EmitRewritingRuleCountAllCallOrRuleAllCallNonRandom(source); } else // seq.SequenceType == SequenceType.RuleAll && ((SequenceRuleAll)seqRule).ChooseRandom { rewritingGen.EmitRewritingRuleAllCallRandom(source); } if (fireDebugEvents) { source.AppendFront("procEnv.Finished(" + matchesName + ", " + specialStr + ");\n"); } source.Unindent(); source.AppendFront("}\n"); if (seqRule.Subgraph != null) { source.AppendFront("procEnv.ReturnFromSubgraph();\n"); source.AppendFront("graph = ((GRGEN_LGSP.LGSPActionExecutionEnvironment)procEnv).graph;\n"); } }