本文整理汇总了C#中de.unika.ipd.grGen.lgsp.SourceBuilder.AppendFront方法的典型用法代码示例。如果您正苦于以下问题:C# SourceBuilder.AppendFront方法的具体用法?C# SourceBuilder.AppendFront怎么用?C# SourceBuilder.AppendFront使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类de.unika.ipd.grGen.lgsp.SourceBuilder
的用法示例。
在下文中一共展示了SourceBuilder.AppendFront方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitMatchedCheckAndSet
public void EmitMatchedCheckAndSet(SourceBuilder builder, bool parallel)
{
if(parallel)
{
builder.AppendFrontFormat("List<ushort> flagsPerElement{0} = graph.flagsPerThreadPerElement[threadId];\n", this.Id);
builder.AppendFrontFormat("if((flagsPerElement{0}[candidate{0}.uniqueId] & (ushort)GRGEN_LGSP.LGSPElemFlagsParallel.IS_MATCHED) != 0)\n", this.Id);
builder.AppendFront("\tcontinue;\n");
builder.AppendFrontFormat("flagsPerElement{0}[candidate{0}.uniqueId] |= (ushort)GRGEN_LGSP.LGSPElemFlagsParallel.IS_MATCHED;\n", this.Id);
}
else
{
builder.AppendFrontFormat("if((candidate{0}.lgspFlags & (uint)GRGEN_LGSP.LGSPElemFlags.IS_MATCHED) != 0)\n", this.Id);
builder.AppendFront("\tcontinue;\n");
builder.AppendFrontFormat("candidate{0}.lgspFlags |= (uint)GRGEN_LGSP.LGSPElemFlags.IS_MATCHED;\n", this.Id);
}
}
示例2: GenerateKeepSameFilter
void GenerateKeepSameFilter(SourceBuilder source, LGSPRulePattern rulePattern, String filterVariable, bool sameAsFirst)
{
String rulePatternClassName = TypesHelper.GetPackagePrefixDot(rulePattern.PatternGraph.Package) + rulePattern.GetType().Name;
String matchInterfaceName = rulePatternClassName + "." + NamesOfEntities.MatchInterfaceName(rulePattern.name);
String matchesListType = "GRGEN_LIBGR.IMatchesExact<" + matchInterfaceName + ">";
String filterName = sameAsFirst ? "keepSameAsFirst_" + filterVariable : "keepSameAsLast_" + filterVariable;
source.AppendFrontFormat("public static void Filter_{0}_{1}(GRGEN_LGSP.LGSPGraphProcessingEnvironment procEnv, {2} matches)\n",
rulePattern.name, filterName, matchesListType);
source.AppendFront("{\n");
source.Indent();
source.AppendFrontFormat("List<{0}> matchesArray = matches.ToList();\n", matchInterfaceName);
if(sameAsFirst)
{
source.AppendFront("int pos = 0 + 1;\n");
source.AppendFrontFormat("while(pos < matchesArray.Count && matchesArray[pos].{0} == matchesArray[0].{0})\n",
NamesOfEntities.MatchName(filterVariable, EntityType.Variable));
source.AppendFront("{\n");
source.AppendFront("\t++pos;\n");
source.AppendFront("}\n");
source.AppendFront("for(; pos < matchesArray.Count; ++pos)\n");
source.AppendFront("{\n");
source.AppendFront("\tmatchesArray[pos] = null;\n");
source.AppendFront("}\n");
}
else
{
source.AppendFront("int pos = matchesArray.Count-1 - 1;\n");
source.AppendFrontFormat("while(pos >= 0 && matchesArray[pos].{0} == matchesArray[matchesArray.Count-1].{0})\n",
NamesOfEntities.MatchName(filterVariable, EntityType.Variable));
source.AppendFront("{\n");
source.AppendFront("\t--pos;\n");
source.AppendFront("}\n");
source.AppendFront("for(; pos >= 0; --pos)\n");
source.AppendFront("{\n");
source.AppendFront("\tmatchesArray[pos] = null;\n");
source.AppendFront("}\n");
}
source.AppendFront("matches.FromList();\n");
source.Unindent();
source.AppendFront("}\n");
}
示例3: GenerateTasksMemoryPool
/// <summary>
/// Generates memory pooling code for matching tasks of class given by it's name
/// </summary>
private void GenerateTasksMemoryPool(SourceBuilder sb, String className, bool isAlternative, bool isIterationBreaking, int branchingFactor)
{
// getNewTask method handing out new task from pool or creating task if pool is empty
if(isAlternative)
sb.AppendFront("public static " + className + " getNewTask(GRGEN_LGSP.LGSPActionExecutionEnvironment actionEnv_, "
+ "Stack<GRGEN_LGSP.LGSPSubpatternAction> openTasks_, GRGEN_LGSP.PatternGraph[] patternGraphs_) {\n");
else
sb.AppendFront("public static " + className + " getNewTask(GRGEN_LGSP.LGSPActionExecutionEnvironment actionEnv_, "
+ "Stack<GRGEN_LGSP.LGSPSubpatternAction> openTasks_) {\n");
sb.Indent();
sb.AppendFront(className + " newTask;\n");
sb.AppendFront("if(numFreeTasks>0) {\n");
sb.Indent();
sb.AppendFront("newTask = freeListHead;\n");
sb.AppendFront("newTask.actionEnv = actionEnv_; newTask.openTasks = openTasks_;\n");
if(isAlternative)
sb.AppendFront("newTask.patternGraphs = patternGraphs_;\n");
else if(isIterationBreaking)
sb.AppendFront("newTask.breakIteration = false;\n");
sb.AppendFront("freeListHead = newTask.next;\n");
sb.AppendFront("newTask.next = null;\n");
sb.AppendFront("--numFreeTasks;\n");
sb.Unindent();
sb.AppendFront("} else {\n");
sb.Indent();
if(isAlternative)
sb.AppendFront("newTask = new " + className + "(actionEnv_, openTasks_, patternGraphs_);\n");
else
sb.AppendFront("newTask = new " + className + "(actionEnv_, openTasks_);\n");
sb.Unindent();
sb.AppendFront("}\n");
sb.AppendFront("return newTask;\n");
sb.Unindent();
sb.AppendFront("}\n\n");
// releaseTask method recycling task into pool if pool is not full
sb.AppendFront("public static void releaseTask(" + className + " oldTask) {\n");
sb.Indent();
sb.AppendFront("if(numFreeTasks<MAX_NUM_FREE_TASKS) {\n");
sb.Indent();
sb.AppendFront("oldTask.next = freeListHead;\n");
sb.AppendFront("oldTask.actionEnv = null; oldTask.openTasks = null;\n");
sb.AppendFront("freeListHead = oldTask;\n");
sb.AppendFront("++numFreeTasks;\n");
sb.Unindent();
sb.AppendFront("}\n");
sb.Unindent();
sb.AppendFront("}\n\n");
// tasks pool administration data
sb.AppendFront("private static " + className + " freeListHead = null;\n");
sb.AppendFront("private static int numFreeTasks = 0;\n");
sb.AppendFront("private const int MAX_NUM_FREE_TASKS = 100;\n\n"); // todo: compute antiproportional to pattern size
sb.AppendFront("private " + className + " next = null;\n\n");
// for parallelized subpatterns/alternatives/iterateds we need a freelist per thread
if(branchingFactor > 1)
{
// getNewTask method handing out new task from pool or creating task if pool is empty
if(isAlternative)
sb.AppendFront("public static " + className + " getNewTask(GRGEN_LGSP.LGSPActionExecutionEnvironment actionEnv_, "
+ "Stack<GRGEN_LGSP.LGSPSubpatternAction> openTasks_, GRGEN_LGSP.PatternGraph[] patternGraphs_, int threadId) {\n");
else
sb.AppendFront("public static " + className + " getNewTask(GRGEN_LGSP.LGSPActionExecutionEnvironment actionEnv_, "
+ "Stack<GRGEN_LGSP.LGSPSubpatternAction> openTasks_, int threadId) {\n");
sb.Indent();
sb.AppendFront(className + " newTask;\n");
sb.AppendFront("if(numFreeTasks_perWorker[threadId]>0) {\n");
sb.Indent();
sb.AppendFront("newTask = freeListHead_perWorker[threadId];\n");
sb.AppendFront("newTask.actionEnv = actionEnv_; newTask.openTasks = openTasks_;\n");
if(isAlternative)
sb.AppendFront("newTask.patternGraphs = patternGraphs_;\n");
else if(isIterationBreaking)
sb.AppendFront("newTask.breakIteration = false;\n");
sb.AppendFront("freeListHead_perWorker[threadId] = newTask.next;\n");
sb.AppendFront("newTask.next = null;\n");
sb.AppendFront("--numFreeTasks_perWorker[threadId];\n");
sb.Unindent();
sb.AppendFront("} else {\n");
sb.Indent();
if(isAlternative)
sb.AppendFront("newTask = new " + className + "(actionEnv_, openTasks_, patternGraphs_);\n");
else
sb.AppendFront("newTask = new " + className + "(actionEnv_, openTasks_);\n");
sb.Unindent();
sb.AppendFront("}\n");
sb.AppendFront("return newTask;\n");
sb.Unindent();
sb.AppendFront("}\n\n");
// releaseTask method recycling task into pool if pool is not full
sb.AppendFront("public static void releaseTask(" + className + " oldTask, int threadId) {\n");
sb.Indent();
sb.AppendFront("if(numFreeTasks_perWorker[threadId]<MAX_NUM_FREE_TASKS/2) {\n");
sb.Indent();
//.........这里部分代码省略.........
示例4: GenerateMatcherClassHeadAlternative
/// <summary>
/// Generates matcher class head source code for the given alternative into given source builder
/// isInitialStatic tells whether the initial static version or a dynamic version after analyze is to be generated.
/// </summary>
public void GenerateMatcherClassHeadAlternative(SourceBuilder sb, LGSPMatchingPattern matchingPattern,
Alternative alternative, bool isInitialStatic)
{
PatternGraph patternGraph = (PatternGraph)matchingPattern.PatternGraph;
String namePrefix = (isInitialStatic ? "" : "Dyn") + "AlternativeAction_";
String className = namePrefix + alternative.pathPrefix+alternative.name;
if(patternGraph.Package != null)
{
sb.AppendFrontFormat("namespace {0}\n", patternGraph.Package);
sb.AppendFront("{\n");
sb.Indent();
}
sb.AppendFront("public class " + className + " : GRGEN_LGSP.LGSPSubpatternAction\n");
sb.AppendFront("{\n");
sb.Indent(); // class level
sb.AppendFront("private " + className + "(GRGEN_LGSP.LGSPActionExecutionEnvironment actionEnv_, "
+ "Stack<GRGEN_LGSP.LGSPSubpatternAction> openTasks_, GRGEN_LGSP.PatternGraph[] patternGraphs_) {\n");
sb.Indent(); // method body level
sb.AppendFront("actionEnv = actionEnv_; openTasks = openTasks_;\n");
// pfadausdruck gebraucht, da das alternative-objekt im pattern graph steckt
sb.AppendFront("patternGraphs = patternGraphs_;\n");
sb.Unindent(); // class level
sb.AppendFront("}\n\n");
GenerateTasksMemoryPool(sb, className, true, false, matchingPattern.patternGraph.branchingFactor);
Dictionary<string, bool> neededNodes = new Dictionary<string,bool>();
Dictionary<string, bool> neededEdges = new Dictionary<string,bool>();
Dictionary<string, GrGenType> neededVariables = new Dictionary<string, GrGenType>();
foreach (PatternGraph altCase in alternative.alternativeCases)
{
foreach (KeyValuePair<string, bool> neededNode in altCase.neededNodes)
neededNodes[neededNode.Key] = neededNode.Value;
foreach (KeyValuePair<string, bool> neededEdge in altCase.neededEdges)
neededEdges[neededEdge.Key] = neededEdge.Value;
foreach (KeyValuePair<string, GrGenType> neededVariable in altCase.neededVariables)
neededVariables[neededVariable.Key] = neededVariable.Value;
}
foreach (KeyValuePair<string, bool> node in neededNodes)
{
sb.AppendFront("public GRGEN_LGSP.LGSPNode " + node.Key + ";\n");
}
foreach (KeyValuePair<string, bool> edge in neededEdges)
{
sb.AppendFront("public GRGEN_LGSP.LGSPEdge " + edge.Key + ";\n");
}
foreach (KeyValuePair<string, GrGenType> variable in neededVariables)
{
sb.AppendFront("public " + TypesHelper.TypeName(variable.Value) + " " + variable.Key + ";\n");
}
foreach (PatternGraph altCase in alternative.alternativeCases)
{
GenerateIndependentsMatchObjects(sb, matchingPattern, altCase);
}
sb.AppendFront("\n");
}
示例5: GenerateParallelizationSetupAsNeeded
void GenerateParallelizationSetupAsNeeded(SourceBuilder sb, LGSPRulePattern rulePattern, SearchProgram searchProgram)
{
if(rulePattern.patternGraph.branchingFactor < 2)
return;
foreach(SearchOperation so in rulePattern.patternGraph.parallelizedSchedule[0].Operations)
{
switch(so.Type)
{
case SearchOperationType.WriteParallelPreset:
if(so.Element is SearchPlanNodeNode)
sb.AppendFrontFormat("GRGEN_LGSP.LGSPNode {0};\n", NamesOfEntities.IterationParallelizationParallelPresetCandidate(((SearchPlanNodeNode)so.Element).PatternElement.Name));
else //SearchPlanEdgeNode
sb.AppendFrontFormat("GRGEN_LGSP.LGSPEdge {0};\n", NamesOfEntities.IterationParallelizationParallelPresetCandidate(((SearchPlanEdgeNode)so.Element).PatternElement.Name));
break;
case SearchOperationType.WriteParallelPresetVar:
sb.AppendFrontFormat("{0} {1};\n",
TypesHelper.TypeName(((PatternVariable)so.Element).Type),
NamesOfEntities.IterationParallelizationParallelPresetCandidate(((PatternVariable)so.Element).Name));
break;
case SearchOperationType.SetupParallelLookup:
if(so.Element is SearchPlanNodeNode)
{
sb.AppendFrontFormat("GRGEN_LGSP.LGSPNode {0};\n", NamesOfEntities.IterationParallelizationListHead(((SearchPlanNodeNode)so.Element).PatternElement.Name));
sb.AppendFrontFormat("GRGEN_LGSP.LGSPNode {0};\n", NamesOfEntities.IterationParallelizationNextCandidate(((SearchPlanNodeNode)so.Element).PatternElement.Name));
}
else
{
sb.AppendFrontFormat("GRGEN_LGSP.LGSPEdge {0};\n", NamesOfEntities.IterationParallelizationListHead(((SearchPlanEdgeNode)so.Element).PatternElement.Name));
sb.AppendFrontFormat("GRGEN_LGSP.LGSPEdge {0};\n", NamesOfEntities.IterationParallelizationNextCandidate(((SearchPlanEdgeNode)so.Element).PatternElement.Name));
}
break;
case SearchOperationType.SetupParallelPickFromStorage:
if(TypesHelper.DotNetTypeToXgrsType(so.Storage.Variable.type).StartsWith("set") || TypesHelper.DotNetTypeToXgrsType(so.Storage.Variable.type).StartsWith("map"))
{
sb.AppendFrontFormat("IEnumerator<KeyValuePair<{0},{1}>> {2};\n",
TypesHelper.XgrsTypeToCSharpType(TypesHelper.ExtractSrc(TypesHelper.DotNetTypeToXgrsType(so.Storage.Variable.Type)), model),
TypesHelper.XgrsTypeToCSharpType(TypesHelper.ExtractDst(TypesHelper.DotNetTypeToXgrsType(so.Storage.Variable.Type)), model),
NamesOfEntities.IterationParallelizationIterator(((SearchPlanNode)so.Element).PatternElement.Name));
}
else
{
sb.AppendFrontFormat("IEnumerator<{0}> {1};\n",
TypesHelper.XgrsTypeToCSharpType(TypesHelper.ExtractSrc(TypesHelper.DotNetTypeToXgrsType(so.Storage.Variable.Type)), model),
NamesOfEntities.IterationParallelizationIterator(((SearchPlanNode)so.Element).PatternElement.Name));
}
break;
case SearchOperationType.SetupParallelPickFromStorageDependent:
if(TypesHelper.AttributeTypeToXgrsType(so.Storage.Attribute.Attribute).StartsWith("set") || TypesHelper.AttributeTypeToXgrsType(so.Storage.Attribute.Attribute).StartsWith("map"))
{
sb.AppendFrontFormat("IEnumerator<KeyValuePair<{0},{1}>> {2};\n",
TypesHelper.XgrsTypeToCSharpType(TypesHelper.ExtractSrc(TypesHelper.AttributeTypeToXgrsType(so.Storage.Attribute.Attribute)), model),
TypesHelper.XgrsTypeToCSharpType(TypesHelper.ExtractDst(TypesHelper.AttributeTypeToXgrsType(so.Storage.Attribute.Attribute)), model),
NamesOfEntities.IterationParallelizationIterator(((SearchPlanNode)so.Element).PatternElement.Name));
}
else
{
sb.AppendFrontFormat("IEnumerator<{0}> {1};\n",
TypesHelper.XgrsTypeToCSharpType(TypesHelper.ExtractSrc(TypesHelper.AttributeTypeToXgrsType(so.Storage.Attribute.Attribute)), model),
NamesOfEntities.IterationParallelizationIterator(((SearchPlanNode)so.Element).PatternElement.Name));
}
break;
case SearchOperationType.SetupParallelPickFromIndex:
sb.AppendFrontFormat("IEnumerator<{0}> {1};\n",
TypesHelper.TypeName(so.IndexAccess.Index is AttributeIndexDescription ?
((AttributeIndexDescription)so.IndexAccess.Index).GraphElementType :
((IncidenceCountIndexDescription)so.IndexAccess.Index).StartNodeType),
NamesOfEntities.IterationParallelizationIterator(((SearchPlanNode)so.Element).PatternElement.Name));
break;
case SearchOperationType.SetupParallelPickFromIndexDependent:
sb.AppendFrontFormat("IEnumerator<{0}> {1};\n",
TypesHelper.TypeName(so.IndexAccess.Index is AttributeIndexDescription ?
((AttributeIndexDescription)so.IndexAccess.Index).GraphElementType :
((IncidenceCountIndexDescription)so.IndexAccess.Index).StartNodeType),
NamesOfEntities.IterationParallelizationIterator(((SearchPlanNode)so.Element).PatternElement.Name));
break;
case SearchOperationType.SetupParallelIncoming:
case SearchOperationType.SetupParallelOutgoing:
sb.AppendFrontFormat("GRGEN_LGSP.LGSPEdge {0};\n", NamesOfEntities.IterationParallelizationListHead(((SearchPlanEdgeNode)so.Element).PatternElement.Name));
sb.AppendFrontFormat("GRGEN_LGSP.LGSPEdge {0};\n", NamesOfEntities.IterationParallelizationNextCandidate(((SearchPlanEdgeNode)so.Element).PatternElement.Name));
break;
case SearchOperationType.SetupParallelIncident:
sb.AppendFrontFormat("GRGEN_LGSP.LGSPEdge {0};\n", NamesOfEntities.IterationParallelizationListHead(((SearchPlanEdgeNode)so.Element).PatternElement.Name));
sb.AppendFrontFormat("GRGEN_LGSP.LGSPEdge {0};\n", NamesOfEntities.IterationParallelizationNextCandidate(((SearchPlanEdgeNode)so.Element).PatternElement.Name));
sb.AppendFrontFormat("int {0};\n", NamesOfEntities.IterationParallelizationDirectionRunCounterVariable(((SearchPlanEdgeNode)so.Element).PatternElement.Name));
break;
}
}
sb.AppendFront("\n");
String rulePatternClassName = rulePattern.GetType().Name;
String matchClassName = rulePatternClassName + "." + "Match_" + rulePattern.name;
String matchInterfaceName = rulePatternClassName + "." + "IMatch_" + rulePattern.name;
sb.AppendFront("private static GRGEN_LGSP.LGSPMatchesList<" + matchClassName + ", " + matchInterfaceName + ">[] parallelTaskMatches;\n");
sb.AppendFront("private static int numWorkerThreads;\n");
sb.AppendFront("private static int iterationNumber;\n");
sb.AppendFront("private static int iterationLock;\n");
sb.AppendFront("[ThreadStatic] private static int currentIterationNumber;\n");
sb.AppendFront("[ThreadStatic] private static int threadId;\n");
sb.AppendFront("private static GRGEN_LGSP.LGSPActionExecutionEnvironment actionEnvParallel;\n");
//.........这里部分代码省略.........
示例6: GenerateFileHeaderForActionsFile
/// <summary>
/// Generates file header for actions file into given source builer
/// </summary>
public void GenerateFileHeaderForActionsFile(SourceBuilder sb,
String namespaceOfModel, String namespaceOfRulePatterns)
{
sb.AppendFront("using System;\n"
+ "using System.Collections.Generic;\n"
+ "using System.Collections;\n"
+ "using System.Text;\n"
+ "using System.Threading;\n"
+ "using GRGEN_LIBGR = de.unika.ipd.grGen.libGr;\n"
+ "using GRGEN_EXPR = de.unika.ipd.grGen.expression;\n"
+ "using GRGEN_LGSP = de.unika.ipd.grGen.lgsp;\n"
+ "using GRGEN_MODEL = " + namespaceOfModel + ";\n"
+ "using GRGEN_ACTIONS = " + namespaceOfRulePatterns + ";\n"
+ "using " + namespaceOfRulePatterns + ";\n\n");
sb.AppendFront("namespace de.unika.ipd.grGen.lgspActions\n");
sb.AppendFront("{\n");
sb.Indent(); // namespace level
}
示例7: GenerateActionInterface
/// <summary>
// generate the exact action interface
/// </summary>
void GenerateActionInterface(SourceBuilder sb, LGSPRulePattern matchingPattern)
{
String actionInterfaceName = "IAction_"+matchingPattern.name;
String outParameters = "";
String refParameters = "";
String allParameters = "";
for(int i = 0; i < matchingPattern.Outputs.Length; ++i)
{
outParameters += ", out " + TypesHelper.TypeName(matchingPattern.Outputs[i]) + " output_"+i;
refParameters += ", ref " + TypesHelper.TypeName(matchingPattern.Outputs[i]) + " output_"+i;
allParameters += ", List<" + TypesHelper.TypeName(matchingPattern.Outputs[i]) + "> output_"+i;
}
String inParameters = "";
for(int i=0; i<matchingPattern.Inputs.Length; ++i) {
inParameters += ", " + TypesHelper.TypeName(matchingPattern.Inputs[i]) + " " + matchingPattern.InputNames[i];
}
String matchingPatternClassName = matchingPattern.GetType().Name;
String patternName = matchingPattern.name;
String matchType = matchingPatternClassName + "." + NamesOfEntities.MatchInterfaceName(patternName);
String matchesType = "GRGEN_LIBGR.IMatchesExact<" + matchType + ">" ;
if(matchingPattern.PatternGraph.Package != null)
{
sb.AppendFrontFormat("namespace {0}\n", matchingPattern.PatternGraph.Package);
sb.AppendFront("{\n");
sb.Indent();
}
sb.AppendFront("/// <summary>\n");
sb.AppendFront("/// An object representing an executable rule - same as IAction, but with exact types and distinct parameters.\n");
sb.AppendFront("/// </summary>\n");
sb.AppendFrontFormat("public interface {0}\n", actionInterfaceName);
sb.AppendFront("{\n");
sb.Indent();
sb.AppendFront("/// <summary> same as IAction.Match, but with exact types and distinct parameters. </summary>\n");
sb.AppendFrontFormat("{0} Match(GRGEN_LIBGR.IActionExecutionEnvironment actionEnv, int maxMatches{1});\n", matchesType, inParameters);
sb.AppendFront("/// <summary> same as IAction.Modify, but with exact types and distinct parameters. </summary>\n");
sb.AppendFrontFormat("void Modify(GRGEN_LIBGR.IActionExecutionEnvironment actionEnv, {0} match{1});\n", matchType, outParameters);
sb.AppendFront("/// <summary> same as IAction.ModifyAll, but with exact types and distinct parameters. </summary>\n");
sb.AppendFrontFormat("void ModifyAll(GRGEN_LIBGR.IActionExecutionEnvironment actionEnv, {0} matches{1});\n", matchesType, allParameters);
sb.AppendFront("/// <summary> same as IAction.Apply, but with exact types and distinct parameters; returns true if applied </summary>\n");
sb.AppendFrontFormat("bool Apply(GRGEN_LIBGR.IActionExecutionEnvironment actionEnv{0}{1});\n", inParameters, refParameters);
sb.AppendFront("/// <summary> same as IAction.ApplyAll, but with exact types and distinct parameters; returns the number of matches found/applied. </summary>\n");
sb.AppendFrontFormat("int ApplyAll(int maxMatches, GRGEN_LIBGR.IActionExecutionEnvironment actionEnv{0}{1});\n", inParameters, allParameters);
sb.AppendFront("/// <summary> same as IAction.ApplyStar, but with exact types and distinct parameters. </summary>\n");
sb.AppendFrontFormat("bool ApplyStar(GRGEN_LIBGR.IActionExecutionEnvironment actionEnv{0});\n", inParameters);
sb.AppendFront("/// <summary> same as IAction.ApplyPlus, but with exact types and distinct parameters. </summary>\n");
sb.AppendFrontFormat("bool ApplyPlus(GRGEN_LIBGR.IActionExecutionEnvironment actionEnv{0});\n", inParameters);
sb.AppendFront("/// <summary> same as IAction.ApplyMinMax, but with exact types and distinct parameters. </summary>\n");
sb.AppendFrontFormat("bool ApplyMinMax(GRGEN_LIBGR.IActionExecutionEnvironment actionEnv, int min, int max{0});\n", inParameters);
sb.Unindent();
sb.AppendFront("}\n");
if(matchingPattern.PatternGraph.Package != null)
{
sb.Unindent();
sb.AppendFront("}\n");
}
sb.AppendFront("\n");
}
示例8: Emit
public override void Emit(SourceBuilder sourceCode)
{
String ascendingVar = "ascending_" + fetchId().ToString();
String entryVar = "entry_" + fetchId().ToString();
String limitVar = "limit_" + fetchId().ToString();
sourceCode.AppendFront("int " + entryVar + " = (int)(");
Left.Emit(sourceCode);
sourceCode.AppendFront(");\n");
sourceCode.AppendFront("int " + limitVar + " = (int)(");
Right.Emit(sourceCode);
sourceCode.AppendFront(");\n");
sourceCode.AppendFront("bool " + ascendingVar + " = " + entryVar + " <= " + limitVar + ";\n");
sourceCode.AppendFront("while(" + ascendingVar + " ? " + entryVar + " <= " + limitVar + " : " + entryVar + " >= " + limitVar + ")\n");
sourceCode.AppendFront("{\n");
sourceCode.Indent();
sourceCode.AppendFront(VariableType + " " + NamesOfEntities.Variable(Variable) + " = " + entryVar + ";\n");
foreach(Yielding statement in Statements)
statement.Emit(sourceCode);
sourceCode.AppendFront("if(" + ascendingVar + ") ++" + entryVar + "; else --" + entryVar + ";\n");
sourceCode.Unindent();
sourceCode.AppendFront("}\n");
}
示例9: EmitSequence
void EmitSequence(Sequence seq, SourceBuilder source)
{
switch(seq.SequenceType)
{
case SequenceType.RuleCall:
case SequenceType.RuleAllCall:
case SequenceType.RuleCountAllCall:
EmitRuleOrRuleAllCall((SequenceRuleCall)seq, source);
break;
case SequenceType.SequenceCall:
EmitSequenceCall((SequenceSequenceCall)seq, source);
break;
case SequenceType.Not:
{
SequenceNot seqNot = (SequenceNot) seq;
EmitSequence(seqNot.Seq, source);
source.AppendFront(SetResultVar(seqNot, "!"+GetResultVar(seqNot.Seq)));
break;
}
case SequenceType.LazyOr:
case SequenceType.LazyAnd:
case SequenceType.IfThen:
{
SequenceBinary seqBin = (SequenceBinary) seq;
if(seqBin.Random)
{
Debug.Assert(seq.SequenceType != SequenceType.IfThen);
source.AppendFront("if(GRGEN_LIBGR.Sequence.randomGenerator.Next(2) == 1)\n");
source.AppendFront("{\n");
source.Indent();
EmitLazyOp(seqBin, source, true);
source.Unindent();
source.AppendFront("}\n");
source.AppendFront("else\n");
source.AppendFront("{\n");
source.Indent();
EmitLazyOp(seqBin, source, false);
source.Unindent();
source.AppendFront("}\n");
}
else
{
EmitLazyOp(seqBin, source, false);
}
break;
}
case SequenceType.ThenLeft:
case SequenceType.ThenRight:
case SequenceType.StrictAnd:
case SequenceType.StrictOr:
case SequenceType.Xor:
{
SequenceBinary seqBin = (SequenceBinary) seq;
if(seqBin.Random)
{
source.AppendFront("if(GRGEN_LIBGR.Sequence.randomGenerator.Next(2) == 1)\n");
source.AppendFront("{\n");
source.Indent();
EmitSequence(seqBin.Right, source);
EmitSequence(seqBin.Left, source);
source.Unindent();
source.AppendFront("}\n");
source.AppendFront("else\n");
source.AppendFront("{\n");
source.Indent();
EmitSequence(seqBin.Left, source);
EmitSequence(seqBin.Right, source);
source.Unindent();
source.AppendFront("}\n");
}
else
{
EmitSequence(seqBin.Left, source);
EmitSequence(seqBin.Right, source);
}
if(seq.SequenceType==SequenceType.ThenLeft) {
source.AppendFront(SetResultVar(seq, GetResultVar(seqBin.Left)));
break;
} else if(seq.SequenceType==SequenceType.ThenRight) {
source.AppendFront(SetResultVar(seq, GetResultVar(seqBin.Right)));
break;
}
String op;
switch(seq.SequenceType)
{
case SequenceType.StrictAnd: op = "&"; break;
case SequenceType.StrictOr: op = "|"; break;
case SequenceType.Xor: op = "^"; break;
default: throw new Exception("Internal error in EmitSequence: Should not have reached this!");
}
source.AppendFront(SetResultVar(seq, GetResultVar(seqBin.Left) + " "+op+" " + GetResultVar(seqBin.Right)));
break;
}
//.........这里部分代码省略.........
示例10: EmitSequenceCall
void EmitSequenceCall(SequenceSequenceCall seqSeq, SourceBuilder source)
{
SequenceInvocationParameterBindings paramBindings = seqSeq.ParamBindings;
String parameterDeclarations = null;
String parameters = null;
if(paramBindings.Subgraph != null)
parameters = BuildParametersInDeclarations(paramBindings, out parameterDeclarations);
else
parameters = BuildParameters(paramBindings);
String outParameterDeclarations;
String outArguments;
String outAssignments;
BuildOutParameters(paramBindings, out outParameterDeclarations, out outArguments, out outAssignments);
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");
}
if(outParameterDeclarations.Length != 0)
source.AppendFront(outParameterDeclarations + "\n");
source.AppendFront("if(" + TypesHelper.GetPackagePrefixDot(paramBindings.Package) + "Sequence_" + paramBindings.Name + ".ApplyXGRS_" + paramBindings.Name
+ "(procEnv" + parameters + outArguments + ")) {\n");
source.Indent();
if(outAssignments.Length != 0)
source.AppendFront(outAssignments + "\n");
source.AppendFront(SetResultVar(seqSeq, "true"));
source.Unindent();
source.AppendFront("} else {\n");
source.Indent();
source.AppendFront(SetResultVar(seqSeq, "false"));
source.Unindent();
source.AppendFront("}\n");
if(paramBindings.Subgraph != null)
{
source.AppendFront("procEnv.ReturnFromSubgraph();\n");
source.AppendFront("graph = ((GRGEN_LGSP.LGSPActionExecutionEnvironment)procEnv).graph;\n");
}
}
示例11: GenerateContainerConstructor
void GenerateContainerConstructor(SequenceExpressionContainerConstructor cc, SourceBuilder source)
{
string containerType = TypesHelper.XgrsTypeToCSharpType(GetContainerType(cc), model);
string valueType = TypesHelper.XgrsTypeToCSharpType(cc.ValueType, model);
string keyType = null;
if(cc is SequenceExpressionMapConstructor)
keyType = TypesHelper.XgrsTypeToCSharpType(((SequenceExpressionMapConstructor)cc).KeyType, model);
source.Append("\n");
source.AppendFront("public static ");
source.Append(containerType);
source.Append(" fillFromSequence_" + cc.Id);
source.Append("(");
for(int i = 0; i < cc.ContainerItems.Length; ++i)
{
if(i > 0)
source.Append(", ");
if(keyType != null)
source.AppendFormat("{0} paramkey{1}, ", keyType, i);
source.AppendFormat("{0} param{1}", valueType, i);
}
source.Append(")\n");
source.AppendFront("{\n");
source.Indent();
source.AppendFrontFormat("{0} container = new {0}();\n", containerType);
for(int i = 0; i < cc.ContainerItems.Length; ++i)
{
if(cc is SequenceExpressionSetConstructor)
source.AppendFrontFormat("container.Add(param{0}, null);\n", i);
else if(cc is SequenceExpressionMapConstructor)
source.AppendFrontFormat("container.Add(paramkey{0}, param{0});\n", i);
else if(cc is SequenceExpressionArrayConstructor)
source.AppendFrontFormat("container.Add(param{0});\n", i);
else //if(cc is SequenceExpressionDequeConstructor)
source.AppendFrontFormat("container.Enqueue(param{0});\n", i);
}
source.AppendFront("return container;\n");
source.Unindent();
source.AppendFront("}\n");
}
示例12: GenerateKeepOneForEachFilter
void GenerateKeepOneForEachFilter(SourceBuilder source, LGSPRulePattern rulePattern, String filterVariable)
{
String rulePatternClassName = TypesHelper.GetPackagePrefixDot(rulePattern.PatternGraph.Package) + rulePattern.GetType().Name;
String matchInterfaceName = rulePatternClassName + "." + NamesOfEntities.MatchInterfaceName(rulePattern.name);
String matchesListType = "GRGEN_LIBGR.IMatchesExact<" + matchInterfaceName + ">";
String filterName = "keepOneForEach_" + filterVariable;
source.AppendFrontFormat("public static void Filter_{0}_{1}(GRGEN_LGSP.LGSPGraphProcessingEnvironment procEnv, {2} matches)\n",
rulePattern.name, filterName, matchesListType);
source.AppendFront("{\n");
source.Indent();
source.AppendFrontFormat("List<{0}> matchesArray = matches.ToList();\n", matchInterfaceName);
source.AppendFront("int cmpPos = 0;\n");
source.AppendFront("int pos = 0 + 1;\n");
source.AppendFront("for(; pos < matchesArray.Count; ++pos)\n");
source.AppendFront("{\n");
source.AppendFrontFormat("\tif(matchesArray[pos].{0} == matchesArray[cmpPos].{0})\n", NamesOfEntities.MatchName(filterVariable, EntityType.Variable));
source.AppendFront("\t\tmatchesArray[pos] = null;\n");
source.AppendFront("\telse\n");
source.AppendFront("\t\tcmpPos = pos;\n");
source.AppendFront("}\n");
source.AppendFront("matches.FromList();\n");
source.Unindent();
source.AppendFront("}\n");
}
示例13: GenerateActionImplementation
//.........这里部分代码省略.........
sbOutParametersAll.Append(", List<");
sbOutParametersAll.Append(TypesHelper.TypeName(matchingPattern.Outputs[i]));
sbOutParametersAll.Append("> output_");
sbOutParametersAll.Append(i);
sbOutArgumentsAll.Append(", output_");
sbOutArgumentsAll.Append(i);
sbIntermediateLocalArgumentsAll.Append(", out output_local_");
sbIntermediateLocalArgumentsAll.Append(i);
sbOutClassArgumentsAll.Append(", output_list_");
sbOutClassArgumentsAll.Append(i);
}
String outParameters = sbOutParameters.ToString();
String refParameters = sbRefParameters.ToString();
String outLocals = sbOutLocals.ToString();
String refLocals = sbRefLocals.ToString();
String outArguments = sbOutArguments.ToString();
String refArguments = sbRefArguments.ToString();
String outIntermediateLocalsAll = sbOutIntermediateLocalsAll.ToString();
String outParametersAll = sbOutParametersAll.ToString();
String outArgumentsAll = sbOutArgumentsAll.ToString();
String intermediateLocalArgumentsAll = sbIntermediateLocalArgumentsAll.ToString();
String outClassArgumentsAll = sbOutClassArgumentsAll.ToString();
String matchingPatternClassName = matchingPattern.GetType().Name;
String patternName = matchingPattern.name;
String matchType = matchingPatternClassName + "." + NamesOfEntities.MatchInterfaceName(patternName);
String matchesType = "GRGEN_LIBGR.IMatchesExact<" + matchType + ">";
// implementation of exact action interface
sb.AppendFront("/// <summary> Type of the matcher method (with parameters processing environment containing host graph, maximum number of matches to search for (zero=unlimited), and rule parameters; returning found matches). </summary>\n");
sb.AppendFrontFormat("public delegate {0} MatchInvoker(GRGEN_LGSP.LGSPActionExecutionEnvironment actionEnv, int maxMatches{1});\n", matchesType, inParameters);
sb.AppendFront("/// <summary> A delegate pointing to the current matcher program for this rule. </summary>\n");
sb.AppendFront("public MatchInvoker DynamicMatch;\n");
sb.AppendFront("/// <summary> The RulePattern object from which this LGSPAction object has been created. </summary>\n");
sb.AppendFront("public GRGEN_LIBGR.IRulePattern RulePattern { get { return _rulePattern; } }\n");
for(int i = 0; i < matchingPattern.Outputs.Length; ++i)
{
sb.AppendFront("List<");
sb.Append(TypesHelper.TypeName(matchingPattern.Outputs[i]));
sb.Append("> output_list_");
sb.Append(i.ToString());
sb.Append(" = new List<");
sb.Append(TypesHelper.TypeName(matchingPattern.Outputs[i]));
sb.Append(">();\n");
}
sb.AppendFrontFormat("public {0} Match(GRGEN_LIBGR.IActionExecutionEnvironment actionEnv, int maxMatches{1})\n", matchesType, inParameters);
sb.AppendFront("{\n");
sb.Indent();
sb.AppendFrontFormat("return DynamicMatch((GRGEN_LGSP.LGSPActionExecutionEnvironment)actionEnv, maxMatches{0});\n", inArguments);
sb.Unindent();
sb.AppendFront("}\n");
sb.AppendFrontFormat("public void Modify(GRGEN_LIBGR.IActionExecutionEnvironment actionEnv, {0} match{1})\n", matchType, outParameters);
sb.AppendFront("{\n");
sb.Indent();
sb.AppendFrontFormat("_rulePattern.Modify((GRGEN_LGSP.LGSPActionExecutionEnvironment)actionEnv, match{0});\n", outArguments);
sb.Unindent();
sb.AppendFront("}\n");
示例14: GenerateMatcherClassHeadAction
/// <summary>
/// Generates matcher class head source code for the pattern of the rulePattern into given source builder
/// isInitialStatic tells whether the initial static version or a dynamic version after analyze is to be generated.
/// </summary>
void GenerateMatcherClassHeadAction(SourceBuilder sb, LGSPRulePattern rulePattern,
bool isInitialStatic, SearchProgram searchProgram)
{
PatternGraph patternGraph = (PatternGraph)rulePattern.PatternGraph;
String namePrefix = (isInitialStatic ? "" : "Dyn") + "Action_";
String className = namePrefix + rulePattern.name;
String rulePatternClassName = rulePattern.GetType().Name;
String matchClassName = rulePatternClassName + "." + "Match_" + rulePattern.name;
String matchInterfaceName = rulePatternClassName + "." + "IMatch_" + rulePattern.name;
String actionInterfaceName = "IAction_" + rulePattern.name;
if(patternGraph.Package != null)
{
sb.AppendFrontFormat("namespace {0}\n", patternGraph.Package);
sb.AppendFront("{\n");
sb.Indent();
}
sb.AppendFront("public class " + className + " : GRGEN_LGSP.LGSPAction, "
+ "GRGEN_LIBGR.IAction, " + actionInterfaceName + "\n");
sb.AppendFront("{\n");
sb.Indent(); // class level
sb.AppendFront("public " + className + "() {\n");
sb.Indent(); // method body level
sb.AppendFront("_rulePattern = " + rulePatternClassName + ".Instance;\n");
sb.AppendFront("patternGraph = _rulePattern.patternGraph;\n");
if(rulePattern.patternGraph.branchingFactor < 2)
{
sb.AppendFront("DynamicMatch = myMatch;\n");
if(!isInitialStatic)
sb.AppendFrontFormat("GRGEN_ACTIONS.Action_{0}.Instance.DynamicMatch = myMatch;\n", rulePattern.name);
}
else
{
sb.AppendFront("if(Environment.ProcessorCount == 1)\n");
sb.AppendFront("{\n");
sb.AppendFront("\tDynamicMatch = myMatch;\n");
if(!isInitialStatic)
sb.AppendFrontFormat("\tGRGEN_ACTIONS.Action_{0}.Instance.DynamicMatch = myMatch;\n", rulePattern.name);
sb.AppendFront("}\n");
sb.AppendFront("else\n");
sb.AppendFront("{\n");
sb.Indent();
sb.AppendFront("DynamicMatch = myMatch_parallelized;\n");
if(!isInitialStatic)
sb.AppendFrontFormat("GRGEN_ACTIONS.Action_{0}.Instance.DynamicMatch = myMatch_parallelized;\n", rulePattern.name);
sb.AppendFrontFormat("numWorkerThreads = GRGEN_LGSP.WorkerPool.EnsurePoolSize({0});\n", rulePattern.patternGraph.branchingFactor);
sb.AppendFrontFormat("parallelTaskMatches = new GRGEN_LGSP.LGSPMatchesList<{0}, {1}>[numWorkerThreads];\n", matchClassName, matchInterfaceName);
sb.AppendFront("moveHeadAfterNodes = new List<GRGEN_LGSP.LGSPNode>[numWorkerThreads];\n");
sb.AppendFront("moveHeadAfterEdges = new List<GRGEN_LGSP.LGSPEdge>[numWorkerThreads];\n");
sb.AppendFront("moveOutHeadAfter = new List<KeyValuePair<GRGEN_LGSP.LGSPNode, GRGEN_LGSP.LGSPEdge>>[numWorkerThreads];\n");
sb.AppendFront("moveInHeadAfter = new List<KeyValuePair<GRGEN_LGSP.LGSPNode, GRGEN_LGSP.LGSPEdge>>[numWorkerThreads];\n");
sb.AppendFront("for(int i=0; i<numWorkerThreads; ++i)\n");
sb.AppendFront("{\n");
sb.Indent();
sb.AppendFront("moveHeadAfterNodes[i] = new List<GRGEN_LGSP.LGSPNode>();\n");
sb.AppendFront("moveHeadAfterEdges[i] = new List<GRGEN_LGSP.LGSPEdge>();\n");
sb.AppendFront("moveOutHeadAfter[i] = new List<KeyValuePair<GRGEN_LGSP.LGSPNode, GRGEN_LGSP.LGSPEdge>>();\n");
sb.AppendFront("moveInHeadAfter[i] = new List<KeyValuePair<GRGEN_LGSP.LGSPNode, GRGEN_LGSP.LGSPEdge>>();\n");
sb.Unindent();
sb.AppendFront("}\n");
sb.AppendFront("for(int i=0; i<parallelTaskMatches.Length; ++i)\n");
sb.AppendFrontFormat("\tparallelTaskMatches[i] = new GRGEN_LGSP.LGSPMatchesList<{0}, {1}>(this);\n", matchClassName, matchInterfaceName);
sb.Unindent();
sb.AppendFront("}\n");
}
sb.AppendFrontFormat("ReturnArray = new object[{0}];\n", rulePattern.Outputs.Length);
sb.AppendFront("matches = new GRGEN_LGSP.LGSPMatchesList<" + matchClassName +", " + matchInterfaceName + ">(this);\n");
sb.Unindent(); // class level
sb.AppendFront("}\n\n");
sb.AppendFront("public " + rulePatternClassName + " _rulePattern;\n");
sb.AppendFront("public override GRGEN_LGSP.LGSPRulePattern rulePattern { get { return _rulePattern; } }\n");
sb.AppendFront("public override string Name { get { return \"" + rulePattern.name + "\"; } }\n");
sb.AppendFront("private GRGEN_LGSP.LGSPMatchesList<" + matchClassName + ", " + matchInterfaceName + "> matches;\n\n");
if (isInitialStatic)
{
sb.AppendFront("public static " + className + " Instance { get { return instance; } set { instance = value; } }\n");
sb.AppendFront("private static " + className + " instance = new " + className + "();\n");
}
GenerateIndependentsMatchObjects(sb, rulePattern, patternGraph);
sb.AppendFront("\n");
GenerateParallelizationSetupAsNeeded(sb, rulePattern, searchProgram);
}
示例15: GenerateMatcherClassHeadSubpattern
/// <summary>
/// Generates matcher class head source code for the subpattern of the rulePattern into given source builder
/// isInitialStatic tells whether the initial static version or a dynamic version after analyze is to be generated.
/// </summary>
public void GenerateMatcherClassHeadSubpattern(SourceBuilder sb, LGSPMatchingPattern matchingPattern,
bool isInitialStatic)
{
Debug.Assert(!(matchingPattern is LGSPRulePattern));
PatternGraph patternGraph = (PatternGraph)matchingPattern.PatternGraph;
String namePrefix = (isInitialStatic ? "" : "Dyn") + "PatternAction_";
String className = namePrefix + matchingPattern.name;
String matchingPatternClassName = matchingPattern.GetType().Name;
if(patternGraph.Package != null)
{
sb.AppendFrontFormat("namespace {0}\n", patternGraph.Package);
sb.AppendFront("{\n");
sb.Indent();
}
sb.AppendFront("public class " + className + " : GRGEN_LGSP.LGSPSubpatternAction\n");
sb.AppendFront("{\n");
sb.Indent(); // class level
sb.AppendFront("private " + className + "(GRGEN_LGSP.LGSPActionExecutionEnvironment actionEnv_, Stack<GRGEN_LGSP.LGSPSubpatternAction> openTasks_) {\n");
sb.Indent(); // method body level
sb.AppendFront("actionEnv = actionEnv_; openTasks = openTasks_;\n");
sb.AppendFront("patternGraph = " + matchingPatternClassName + ".Instance.patternGraph;\n");
sb.Unindent(); // class level
sb.AppendFront("}\n\n");
GenerateTasksMemoryPool(sb, className, false, false, matchingPattern.patternGraph.branchingFactor);
for (int i = 0; i < patternGraph.nodesPlusInlined.Length; ++i)
{
PatternNode node = patternGraph.nodesPlusInlined[i];
if (node.PointOfDefinition == null)
{
sb.AppendFront("public GRGEN_LGSP.LGSPNode " + node.name + ";\n");
}
}
for (int i = 0; i < patternGraph.edgesPlusInlined.Length; ++i)
{
PatternEdge edge = patternGraph.edgesPlusInlined[i];
if (edge.PointOfDefinition == null)
{
sb.AppendFront("public GRGEN_LGSP.LGSPEdge " + edge.name + ";\n");
}
}
for (int i = 0; i < patternGraph.variablesPlusInlined.Length; ++i)
{
PatternVariable variable = patternGraph.variablesPlusInlined[i];
sb.AppendFront("public " +TypesHelper.TypeName(variable.type) + " " + variable.name + ";\n");
}
GenerateIndependentsMatchObjects(sb, matchingPattern, patternGraph);
sb.AppendFront("\n");
}