/// <summary> /// Executes the Mutex benchmark with a problem size of n. /// This variant uses IAction.Match and IAction.Modify to apply the rules. /// This way you get access to the found matches and can select between them manually. /// </summary> /// <param name="n">The problem size, i.e. the number of Process nodes</param> static void AlternativeOne(int n) { int startTime = Environment.TickCount; MutexGraph graph = new MutexGraph(); MutexPimpedActions actions = new MutexPimpedActions(graph); LGSPGraphProcessingEnvironment procEnv = new LGSPGraphProcessingEnvironment(graph, actions); LGSPNode p1 = graph.CreateNodeProcess(); LGSPNode p2 = graph.CreateNodeProcess(); LGSPEdge n1 = graph.CreateEdgenext(p1, p2); LGSPEdge n2 = graph.CreateEdgenext(p2, p1); IMatches matches; Action_newRule newRule = Action_newRule.Instance; for (int i = 0; i < n - 2; i++) { matches = newRule.Match(procEnv, 1); newRule.Modify(procEnv, matches.First); } matches = Action_mountRule.Instance.Match(procEnv, 1); Action_mountRule.Instance.Modify(procEnv, matches.First); Action_requestRule requestRule = Action_requestRule.Instance; for (int i = 0; i < n; i++) { matches = requestRule.Match(procEnv, 1); requestRule.Modify(procEnv, matches.First); } /** * The MutexPimped.grg file has been annotated with [prio] to generate very good static * searchplans, without the need of dynamically generating searchplans. * * Otherwise you would use the following code to get better matcher programs: * * graph.AnalyzeGraph(); * LGSPAction[] newActions = actions.GenerateSearchPlans("takeRule", "releaseRule", "giveRule"); * LGSPAction takeRule = newActions[0]; * LGSPAction releaseRule = newActions[1]; * LGSPAction giveRule = newActions[2]; * * Alternatively you could also write: * * graph.AnalyzeGraph(); * actions.GenerateSearchPlans("takeRule", "releaseRule", "giveRule"); * LGSPAction takeRule = actions.GetAction("takeRule"); * LGSPAction releaseRule = actions.GetAction("releaseRule"); * LGSPAction giveRule = actions.GetAction("giveRule"); */ Action_takeRule takeRule = Action_takeRule.Instance; Action_releaseRule releaseRule = Action_releaseRule.Instance; Action_giveRule giveRule = Action_giveRule.Instance; for (int i = 0; i < n; i++) { matches = takeRule.Match(procEnv, 1); takeRule.Modify(procEnv, matches.First); matches = releaseRule.Match(procEnv, 1); releaseRule.Modify(procEnv, matches.First); matches = giveRule.Match(procEnv, 1); giveRule.Modify(procEnv, matches.First); } int endTime = Environment.TickCount; PrintResults(endTime - startTime, graph); // example for reading annotations at API level Action_annotationTestRule annotationTestRule = Action_annotationTestRule.Instance; foreach (KeyValuePair <string, string> annotation in annotationTestRule.rulePattern.Annotations) { Console.WriteLine("The rule " + annotationTestRule.Name + " is decorated with an annotation " + annotation.Key + " -> " + annotation.Value); } foreach (IPatternNode node in annotationTestRule.RulePattern.PatternGraph.Nodes) { foreach (KeyValuePair <string, string> annotation in node.Annotations) { Console.WriteLine("The pattern node " + node.Name + " of rule " + annotationTestRule.Name + " is decorated with an annotation " + annotation.Key + " -> " + annotation.Value); } } IAnnotationTestNode testNode = graph.CreateNodeAnnotationTestNode(); foreach (KeyValuePair <string, string> annotation in testNode.Type.Annotations) { Console.WriteLine("The node type " + testNode.Type.Name + " is decorated with an annotation " + annotation.Key + " -> " + annotation.Value); } }
public LGSPMatches myMatch(LGSPGraph graph, int maxMatches, IGraphElement[] parameters) { matches.matches.Clear(); // Lookup(edge__edge0:connection) foreach (EdgeType edge_type_edge__edge0 in EdgeType_connection.typeVar.SubOrSameTypes) { for (LGSPEdge edge_head_edge__edge0 = graph.edgesByTypeHeads[edge_type_edge__edge0.TypeID], edge_cur_edge__edge0 = edge_head_edge__edge0.typeNext; edge_cur_edge__edge0 != edge_head_edge__edge0; edge_cur_edge__edge0 = edge_cur_edge__edge0.typeNext) { // ImplicitTarget(edge__edge0 -> node_p2:Process) LGSPNode node_cur_node_p2 = edge_cur_edge__edge0.target; if (!NodeType_Process.isMyType[node_cur_node_p2.type.TypeID]) { goto contunmap_edge_cur_edge__edge0_2; } node_cur_node_p2.mappedTo = 1; // ImplicitSource(edge__edge0 -> node_p1:Process) LGSPNode node_cur_node_p1 = edge_cur_edge__edge0.source; if (!NodeType_Process.isMyType[node_cur_node_p1.type.TypeID]) { goto contunmap_node_cur_node_p2_4; } if (node_cur_node_p1.mappedTo != 0) { goto cont_node_cur_node_p1_7; } LGSPMatch match = matchesList.GetNewMatch(); match.nodes[0] = node_cur_node_p2; match.nodes[1] = node_cur_node_p1; match.edges[0] = edge_cur_edge__edge0; matchesList.CommitMatch(); if (maxMatches > 0 && matchesList.Count >= maxMatches) { node_cur_node_p2.mappedTo = 0; graph.MoveHeadAfter(edge_cur_edge__edge0); return(matches); } cont_node_cur_node_p1_7 :; contunmap_node_cur_node_p2_4 :; node_cur_node_p2.mappedTo = 0; contunmap_edge_cur_edge__edge0_2 :; // Tail of Lookup(edge_cur_edge__edge0) } } return(matches); }