public SequenceBacktrack(Sequence seqRule, Sequence seq) : base(SequenceType.Backtrack) { Rule = (SequenceRuleCall)seqRule; Seq = seq; }
public SequenceForMatch(SequenceVariable var, Sequence rule, Sequence seq, List<SequenceVariable> variablesFallingOutOfScopeOnLeavingFor) : base(seq, SequenceType.ForMatch) { Var = var; Rule = (SequenceRuleCall)rule; VariablesFallingOutOfScopeOnLeavingFor = variablesFallingOutOfScopeOnLeavingFor; }
protected override string FilterFunctionParameterType(int i, FilterCall filterCall, SequenceRuleCall seq) { if(filterCall.Name == "keepFirst" || filterCall.Name == "removeFirst") return "int"; if(filterCall.Name == "keepFirstFraction" || filterCall.Name == "removeFirstFraction") return "double"; if(filterCall.Name == "keepLast" || filterCall.Name == "removeLast") return "int"; if(filterCall.Name == "keepLastFraction" || filterCall.Name == "removeLastFraction") return "double"; foreach(IFilter filter in seq.ParamBindings.Action.RulePattern.Filters) { if(filter is IFilterFunction) { IFilterFunction filterFunction = (IFilterFunction)filter; if(filterCall.PackagePrefixedName == filterFunction.PackagePrefixedName) return TypesHelper.DotNetTypeToXgrsType(filterFunction.Inputs[i]); } } throw new Exception("Internal error"); }
protected bool ApplyRule(SequenceRuleCall rule, IGraphProcessingEnvironment procEnv, IMatches matches, IMatch match) { bool result; procEnv.EnteringSequence(rule); rule.executionState = SequenceExecutionState.Underway; #if LOG_SEQUENCE_EXECUTION procEnv.Recorder.WriteLine("Before executing sequence " + rule.Id + ": " + rule.Symbol); #endif procEnv.Matched(matches, null, rule.Special); result = rule.Rewrite(procEnv, matches, match); #if LOG_SEQUENCE_EXECUTION procEnv.Recorder.WriteLine("After executing sequence " + rule.Id + ": " + rule.Symbol + " result " + result); #endif rule.executionState = result ? SequenceExecutionState.Success : SequenceExecutionState.Fail; procEnv.ExitingSequence(rule); return result; }
protected override bool IsFilterExisting(FilterCall filterCall, SequenceRuleCall seq) { if(filterCall.Name == "keepFirst" || filterCall.Name == "removeFirst" || filterCall.Name == "keepFirstFraction" || filterCall.Name == "removeFirstFraction" || filterCall.Name == "keepLast" || filterCall.Name == "removeLast" || filterCall.Name == "keepLastFraction" || filterCall.Name == "removeLastFraction") { filterCall.Package = null; filterCall.PackagePrefixedName = filterCall.Name; return true; } filterCall.Package = filterCall.PrePackage; filterCall.PackagePrefixedName = filterCall.Package != null ? filterCall.Package + "::" + filterCall.Name : filterCall.Name; if(filterCall.IsContainedIn(seq.ParamBindings.Action.RulePattern.Filters)) return true; if(filterCall.IsAutoGenerated && seq.ParamBindings.Package != null) { filterCall.Package = seq.ParamBindings.Package; filterCall.PackagePrefixedName = seq.ParamBindings.Package + "::" + filterCall.Name; return filterCall.IsContainedIn(seq.ParamBindings.Action.RulePattern.Filters); } return false; }
protected override int NumFilterFunctionParameters(FilterCall filterCall, SequenceRuleCall seq) { if(filterCall.Name == "keepFirst" || filterCall.Name == "removeFirst" || filterCall.Name == "keepFirstFraction" || filterCall.Name == "removeFirstFraction" || filterCall.Name == "keepLast" || filterCall.Name == "removeLast" || filterCall.Name == "keepLastFraction" || filterCall.Name == "removeLastFraction") { return 1; } foreach(IFilter filter in seq.ParamBindings.Action.RulePattern.Filters) { if(filter is IFilterFunction) { IFilterFunction filterFunction = (IFilterFunction)filter; if(filterCall.PackagePrefixedName == filterFunction.PackagePrefixedName) return filterFunction.Inputs.Length; } } return 0; // auto-generated }
protected abstract int NumFilterFunctionParameters(FilterCall filterCall, SequenceRuleCall seq);
protected abstract string FilterFunctionParameterType(int i, FilterCall filterCall, SequenceRuleCall seq);
protected abstract bool IsFilterExisting(FilterCall filterCall, SequenceRuleCall seq);
protected override string FilterFunctionParameterType(int i, FilterCall filterCall, SequenceRuleCall seq) { if(filterCall.Name == "keepFirst" || filterCall.Name == "removeFirst") return "int"; if(filterCall.Name == "keepFirstFraction" || filterCall.Name == "removeFirstFraction") return "double"; if(filterCall.Name == "keepLast" || filterCall.Name == "removeLast") return "int"; if(filterCall.Name == "keepLastFraction" || filterCall.Name == "removeLastFraction") return "double"; return filterFunctionsToInputTypes[filterCall.PackagePrefixedName][i]; }
protected override int NumFilterFunctionParameters(FilterCall filterCall, SequenceRuleCall seq) { if(filterCall.Name == "keepFirst" || filterCall.Name == "removeFirst" || filterCall.Name == "keepFirstFraction" || filterCall.Name == "removeFirstFraction" || filterCall.Name == "keepLast" || filterCall.Name == "removeLast" || filterCall.Name == "keepLastFraction" || filterCall.Name == "removeLastFraction") { return 1; } if(filterFunctionsToInputTypes.ContainsKey(filterCall.PackagePrefixedName)) return filterFunctionsToInputTypes[filterCall.PackagePrefixedName].Count; else return 0; // auto-supplied }
protected override bool IsFilterExisting(FilterCall filterCall, SequenceRuleCall seq) { if(filterCall.Name == "keepFirst" || filterCall.Name == "removeFirst" || filterCall.Name == "keepFirstFraction" || filterCall.Name == "removeFirstFraction" || filterCall.Name == "keepLast" || filterCall.Name == "removeLast" || filterCall.Name == "keepLastFraction" || filterCall.Name == "removeLastFraction") { filterCall.Package = null; filterCall.PackagePrefixedName = filterCall.Name; return true; } if(filterCall.PrePackage != null) { filterCall.Package = filterCall.PrePackage; filterCall.PackagePrefixedName = filterCall.PrePackage + "::" + filterCall.Name; return filterCall.IsContainedIn(rulesToFilters[seq.ParamBindings.PackagePrefixedName]); } else { filterCall.Package = null; filterCall.PackagePrefixedName = filterCall.Name; if(filterCall.IsContainedIn(rulesToFilters[seq.ParamBindings.PackagePrefixedName])) return true; if(filterCall.PrePackageContext != null) { filterCall.Package = filterCall.PrePackageContext; filterCall.PackagePrefixedName = filterCall.PrePackageContext + "::" + filterCall.Name; if(filterCall.IsContainedIn(rulesToFilters[seq.ParamBindings.PackagePrefixedName])) return true; } if(filterCall.IsAutoGenerated && seq.ParamBindings.Package != null) { filterCall.Package = seq.ParamBindings.Package; filterCall.PackagePrefixedName = seq.ParamBindings.Package + "::" + filterCall.Name; return filterCall.IsContainedIn(rulesToFilters[seq.ParamBindings.PackagePrefixedName]); } return false; } }
void EmitRuleOrRuleAllCall(SequenceRuleCall seqRule, SourceBuilder source) { RuleInvocationParameterBindings paramBindings = seqRule.ParamBindings; String specialStr = seqRule.Special ? "true" : "false"; String parameterDeclarations = null; String parameters = null; if(paramBindings.Subgraph != null) parameters = BuildParametersInDeclarations(paramBindings, out parameterDeclarations); else parameters = BuildParameters(paramBindings); String matchingPatternClassName = TypesHelper.GetPackagePrefixDot(paramBindings.Package) + "Rule_" + paramBindings.Name; String patternName = paramBindings.Name; String matchType = matchingPatternClassName + "." + NamesOfEntities.MatchInterfaceName(patternName); String matchName = "match_" + seqRule.Id; String matchesType = "GRGEN_LIBGR.IMatchesExact<" + matchType + ">"; String matchesName = "matches_" + seqRule.Id; if(paramBindings.Subgraph != null) { source.AppendFront(parameterDeclarations + "\n"); source.AppendFront("procEnv.SwitchToSubgraph((GRGEN_LIBGR.IGraph)" + GetVar(paramBindings.Subgraph) + ");\n"); source.AppendFront("graph = ((GRGEN_LGSP.LGSPActionExecutionEnvironment)procEnv).graph;\n"); } source.AppendFront(matchesType + " " + matchesName + " = rule_" + TypesHelper.PackagePrefixedNameUnderscore(paramBindings.Package, paramBindings.Name) + ".Match(procEnv, " + (seqRule.SequenceType == SequenceType.RuleCall ? "1" : "procEnv.MaxMatches") + parameters + ");\n"); for(int i = 0; i < seqRule.Filters.Count; ++i) { EmitFilterCall(source, seqRule.Filters[i], patternName, matchesName); } if(gen.FireDebugEvents) source.AppendFront("procEnv.Matched(" + matchesName + ", null, " + specialStr + ");\n"); if(seqRule is SequenceRuleCountAllCall) { SequenceRuleCountAllCall seqRuleCountAll = (SequenceRuleCountAllCall)seqRule; source.AppendFront(SetVar(seqRuleCountAll.CountResult, matchesName + ".Count")); } if(seqRule is SequenceRuleAllCall && ((SequenceRuleAllCall)seqRule).ChooseRandom && ((SequenceRuleAllCall)seqRule).MinSpecified) { SequenceRuleAllCall seqRuleAll = (SequenceRuleAllCall)seqRule; source.AppendFrontFormat("int minmatchesvar_{0} = (int){1};\n", seqRuleAll.Id, GetVar(seqRuleAll.MinVarChooseRandom)); source.AppendFrontFormat("if({0}.Count < minmatchesvar_{1}) {{\n", matchesName, seqRuleAll.Id); } else { source.AppendFront("if(" + matchesName + ".Count==0) {\n"); } source.Indent(); source.AppendFront(SetResultVar(seqRule, "false")); source.Unindent(); source.AppendFront("} else {\n"); source.Indent(); source.AppendFront(SetResultVar(seqRule, "true")); source.AppendFront("procEnv.PerformanceInfo.MatchesFound += " + matchesName + ".Count;\n"); if(gen.FireDebugEvents) source.AppendFront("procEnv.Finishing(" + matchesName + ", " + specialStr + ");\n"); String returnParameterDeclarations; String returnArguments; String returnAssignments; String returnParameterDeclarationsAllCall; String intermediateReturnAssignmentsAllCall; String returnAssignmentsAllCall; BuildReturnParameters(paramBindings, out returnParameterDeclarations, out returnArguments, out returnAssignments, out returnParameterDeclarationsAllCall, out intermediateReturnAssignmentsAllCall, out returnAssignmentsAllCall); if(seqRule.SequenceType == SequenceType.RuleCall) { source.AppendFront(matchType + " " + matchName + " = " + matchesName + ".FirstExact;\n"); if(returnParameterDeclarations.Length!=0) source.AppendFront(returnParameterDeclarations + "\n"); source.AppendFront("rule_" + TypesHelper.PackagePrefixedNameUnderscore(paramBindings.Package, paramBindings.Name) + ".Modify(procEnv, " + matchName + returnArguments + ");\n"); if(returnAssignments.Length != 0) source.AppendFront(returnAssignments + "\n"); source.AppendFront("procEnv.PerformanceInfo.RewritesPerformed++;\n"); } else if(seqRule.SequenceType == SequenceType.RuleCountAllCall || !((SequenceRuleAllCall)seqRule).ChooseRandom) // seq.SequenceType == SequenceType.RuleAll { // iterate through matches, use Modify on each, fire the next match event after the first if(returnParameterDeclarations.Length != 0) source.AppendFront(returnParameterDeclarationsAllCall + "\n"); String enumeratorName = "enum_" + seqRule.Id; source.AppendFront("IEnumerator<" + matchType + "> " + enumeratorName + " = " + matchesName + ".GetEnumeratorExact();\n"); source.AppendFront("while(" + enumeratorName + ".MoveNext())\n"); source.AppendFront("{\n"); source.Indent(); source.AppendFront(matchType + " " + matchName + " = " + enumeratorName + ".Current;\n"); source.AppendFront("if(" + matchName + "!=" + matchesName + ".FirstExact) procEnv.RewritingNextMatch();\n"); if(returnParameterDeclarations.Length != 0) source.AppendFront(returnParameterDeclarations + "\n"); source.AppendFront("rule_" + TypesHelper.PackagePrefixedNameUnderscore(paramBindings.Package, paramBindings.Name) + ".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"); } else // seq.SequenceType == SequenceType.RuleAll && ((SequenceRuleAll)seqRule).ChooseRandom { // 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)seqRule; if(returnParameterDeclarations.Length != 0) source.AppendFront(returnParameterDeclarationsAllCall + "\n"); source.AppendFrontFormat("int numchooserandomvar_{0} = (int){1};\n", seqRuleAll.Id, seqRuleAll.MaxVarChooseRandom != null ? GetVar(seqRuleAll.MaxVarChooseRandom) : (seqRuleAll.MinSpecified ? "2147483647" : "1")); source.AppendFrontFormat("if({0}.Count < numchooserandomvar_{1}) numchooserandomvar_{1} = {0}.Count;\n", matchesName, seqRule.Id); source.AppendFrontFormat("for(int i = 0; i < numchooserandomvar_{0}; ++i)\n", seqRule.Id); 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("rule_" + TypesHelper.PackagePrefixedNameUnderscore(paramBindings.Package, paramBindings.Name) + ".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"); } if(gen.FireDebugEvents) source.AppendFront("procEnv.Finished(" + matchesName + ", " + specialStr + ");\n"); source.Unindent(); source.AppendFront("}\n"); if(paramBindings.Subgraph != null) { source.AppendFront("procEnv.ReturnFromSubgraph();\n"); source.AppendFront("graph = ((GRGEN_LGSP.LGSPActionExecutionEnvironment)procEnv).graph;\n"); } }