private void DumpOnEntereringSequence(Sequence seq)
 {
     if (seq.SequenceType == SequenceType.RuleCall || seq.SequenceType == SequenceType.RuleAllCall || seq.SequenceType == SequenceType.RuleCountAllCall)
     {
         curRule = (SequenceRuleCall)seq;
         if (curRule.Special)
         {
             impl.curShellProcEnv.ProcEnv.OnFinishing += DumpOnFinishing;
         }
     }
 }
        private void NormalEnteringSequenceHandler(Sequence seq)
        {
            if (cancelSequence)
            {
                Cancel();
            }

            if (seq.SequenceType == SequenceType.RuleCall || seq.SequenceType == SequenceType.RuleAllCall || seq.SequenceType == SequenceType.RuleCountAllCall)
            {
                curRule = (SequenceRuleCall)seq;
            }
        }
        public SequenceRuleCallMatcherGenerator(SequenceRuleCall seqRule, SequenceExpressionGenerator seqExprGen, SequenceGeneratorHelper seqHelper)
        {
            this.seqRule    = seqRule;
            this.seqExprGen = seqExprGen;
            this.seqHelper  = seqHelper;

            ArgumentExpressions = seqRule.ArgumentExpressions;
            String matchingPatternClassName = "GRGEN_ACTIONS." + TypesHelper.GetPackagePrefixDot(seqRule.Package) + "Rule_" + seqRule.Name;

            patternName = seqRule.Name;
            ruleName    = "rule_" + TypesHelper.PackagePrefixedNameUnderscore(seqRule.Package, seqRule.Name);
            matchType   = matchingPatternClassName + "." + NamesOfEntities.MatchInterfaceName(patternName);
            matchesType = "GRGEN_LIBGR.IMatchesExact<" + matchType + ">";
            matchesName = "matches_" + seqRule.Id;
        }
        private void DumpOnExitingSequence(Sequence seq)
        {
            if (seq.SequenceType == SequenceType.RuleCall || seq.SequenceType == SequenceType.RuleAllCall || seq.SequenceType == SequenceType.RuleCountAllCall)
            {
                SequenceRuleCall ruleSeq = (SequenceRuleCall)seq;
                if (ruleSeq != null && ruleSeq.Special)
                {
                    impl.curShellProcEnv.ProcEnv.OnFinishing -= DumpOnFinishing;
                }
            }

            if (cancelSequence)
            {
                Cancel();
            }
        }
Beispiel #5
0
        public SequenceSomeRuleCallRewritingGenerator(SequenceSomeFromSet seqSome, SequenceRuleCall seqRule, SequenceExpressionGenerator seqExprGen, SequenceGeneratorHelper seqHelper)
        {
            this.seqSome    = seqSome; // parent
            this.seqExprGen = seqExprGen;
            this.seqHelper  = seqHelper;

            this.seqRule = seqRule;

            specialStr = seqRule.Special ? "true" : "false";
            matchingPatternClassName = "GRGEN_ACTIONS." + TypesHelper.GetPackagePrefixDot(seqRule.Package) + "Rule_" + seqRule.Name;
            patternName = seqRule.Name;
            ruleName    = "rule_" + TypesHelper.PackagePrefixedNameUnderscore(seqRule.Package, seqRule.Name);
            matchType   = matchingPatternClassName + "." + NamesOfEntities.MatchInterfaceName(patternName);
            matchName   = "match_" + seqRule.Id;
            matchesType = "GRGEN_LIBGR.IMatchesExact<" + matchType + ">";
            matchesName = "matches_" + seqRule.Id;
        }
        public SequenceBacktrackGenerator(SequenceBacktrack seq, SequenceExpressionGenerator seqExprGen, SequenceGeneratorHelper seqHelper)
        {
            this.seq        = seq;
            this.seqExprGen = seqExprGen;
            this.seqHelper  = seqHelper;

            seqRule                  = seq.Rule;
            ArgumentExpressions      = seqRule.ArgumentExpressions;
            ReturnVars               = seqRule.ReturnVars;
            specialStr               = seqRule.Special ? "true" : "false";
            matchingPatternClassName = "GRGEN_ACTIONS." + TypesHelper.GetPackagePrefixDot(seqRule.Package) + "Rule_" + seqRule.Name;
            patternName              = seqRule.Name;
            ruleName                 = "rule_" + TypesHelper.PackagePrefixedNameUnderscore(seqRule.Package, seqRule.Name);
            matchType                = matchingPatternClassName + "." + NamesOfEntities.MatchInterfaceName(patternName);
            matchName                = "match_" + seq.Id;
            matchesType              = "GRGEN_LIBGR.IMatchesExact<" + matchType + ">";
            matchesName              = "matches_" + seq.Id;
        }
Beispiel #7
0
        /// <summary>
        /// Helper for checking rule calls.
        /// Type checks the input, type checks the output.
        /// Throws an exception when an error is found.
        /// </summary>
        /// <param name="seqRuleCall">The rule call to check</param>
        public void CheckRuleCall(SequenceRuleCall seqRuleCall)
        {
            CheckInputParameters(seqRuleCall, seqRuleCall.ArgumentExpressions, null);
            if (seqRuleCall.IsRuleForMultiRuleAllCallReturningArrays)
            {
                CheckOutputParametersRuleAll(seqRuleCall, seqRuleCall.ReturnVars);
            }
            else
            {
                CheckOutputParameters(seqRuleCall, seqRuleCall.ReturnVars, null);
            }

            CheckFilterCalls(seqRuleCall.Name, seqRuleCall.Filters);

            CheckSubgraph(seqRuleCall);

            // ok, this is a well-formed invocation
        }
        public SequenceMultiBacktrackRuleRewritingGenerator(SequenceMultiBacktrack seqMulti, SequenceRuleCall seqRule, SequenceExpressionGenerator seqExprGen, SequenceGeneratorHelper seqHelper)
        {
            this.seqMulti   = seqMulti;
            this.seqExprGen = seqExprGen;
            this.seqHelper  = seqHelper;

            this.seqRule = seqRule;

            ReturnVars = seqRule.ReturnVars;
            specialStr = seqRule.Special ? "true" : "false";
            matchingPatternClassName = "GRGEN_ACTIONS." + TypesHelper.GetPackagePrefixDot(seqRule.Package) + "Rule_" + seqRule.Name;
            patternName   = seqRule.Name;
            plainRuleName = TypesHelper.PackagePrefixedNameDoubleColon(seqRule.Package, seqRule.Name);
            ruleName      = "rule_" + TypesHelper.PackagePrefixedNameUnderscore(seqRule.Package, seqRule.Name);
            matchType     = matchingPatternClassName + "." + NamesOfEntities.MatchInterfaceName(patternName);
            matchName     = "match_" + seqRule.Id;
            matchesType   = "GRGEN_LIBGR.IMatchesExact<" + matchType + ">";
            matchesName   = "matches_" + seqRule.Id;
        }
Beispiel #9
0
        public SequenceMultiRuleAllCallGenerator(SequenceMultiRuleAllCall seqMulti, SequenceRuleCall seqRule, SequenceExpressionGenerator seqExprGen, SequenceGeneratorHelper seqHelper)
        {
            this.seqMulti   = seqMulti; // parent
            this.seqRule    = seqRule;
            this.seqExprGen = seqExprGen;
            this.seqHelper  = seqHelper;

            ArgumentExpressions      = seqRule.ArgumentExpressions;
            specialStr               = seqRule.Special ? "true" : "false";
            matchingPatternClassName = "GRGEN_ACTIONS." + TypesHelper.GetPackagePrefixDot(seqRule.Package) + "Rule_" + seqRule.Name;
            patternName              = seqRule.Name;
            plainRuleName            = TypesHelper.PackagePrefixedNameDoubleColon(seqRule.Package, seqRule.Name);
            ruleName    = "rule_" + TypesHelper.PackagePrefixedNameUnderscore(seqRule.Package, seqRule.Name);
            matchType   = matchingPatternClassName + "." + NamesOfEntities.MatchInterfaceName(patternName);
            matchName   = "match_" + seqRule.Id;
            matchesType = "GRGEN_LIBGR.IMatchesExact<" + matchType + ">";
            matchesName = "matches_" + seqRule.Id;

            seqHelper.BuildReturnParameters(seqRule, seqRule.ReturnVars,
                                            out returnParameterDeclarations, out returnArguments, out returnAssignments,
                                            out returnParameterDeclarationsAllCall, out intermediateReturnAssignmentsAllCall, out returnAssignmentsAllCall);
        }
Beispiel #10
0
        /// <summary>
        /// pre-run for emitting the needed entities before emitting the real code
        /// - emits result variable declarations
        /// - emits sequence variable declarations (only once for every variable, declaration only possible at assignment targets)
        /// - collects used rules into knownRules, emit local rule declaration (only once for every rule)
        /// </summary>
        public void EmitNeededVarAndRuleEntities(Sequence seq, SourceBuilder source)
        {
            source.AppendFront(COMP_HELPER.DeclareResultVar(seq));

            switch (seq.SequenceType)
            {
            case SequenceType.AssignUserInputToVar:
            case SequenceType.AssignRandomIntToVar:
            case SequenceType.AssignRandomDoubleToVar:
            case SequenceType.DeclareVariable:
            case SequenceType.AssignConstToVar:
            case SequenceType.AssignContainerConstructorToVar:
            case SequenceType.AssignVarToVar:
            {
                SequenceAssignToVar toVar = (SequenceAssignToVar)seq;
                EmitVarIfNew(toVar.DestVar, source);
                break;
            }

            case SequenceType.AssignSequenceResultToVar:
            case SequenceType.OrAssignSequenceResultToVar:
            case SequenceType.AndAssignSequenceResultToVar:
            {
                SequenceAssignSequenceResultToVar seqToVar = (SequenceAssignSequenceResultToVar)seq;
                EmitVarIfNew(seqToVar.DestVar, source);
                EmitNeededVarAndRuleEntities(seqToVar.Seq, source);
                break;
            }

            case SequenceType.RuleCall:
            case SequenceType.RuleAllCall:
            case SequenceType.RuleCountAllCall:
            {
                SequenceRuleCall seqRule  = (SequenceRuleCall)seq;
                String           ruleName = seqRule.PackagePrefixedName;
                if (!knownRules.ContainsKey(ruleName))
                {
                    knownRules.Add(ruleName, null);
                    source.AppendFront("GRGEN_ACTIONS." + TypesHelper.GetPackagePrefixDot(seqRule.Package) + "Action_" + seqRule.Name + " " + "rule_" + TypesHelper.PackagePrefixedNameUnderscore(seqRule.Package, seqRule.Name));
                    source.Append(" = " + "GRGEN_ACTIONS." + TypesHelper.GetPackagePrefixDot(seqRule.Package) + "Action_" + seqRule.Name + ".Instance;\n");
                }
                // no handling for the input arguments seqRule.ArgumentExpressions needed
                // because there can only be variable uses
                for (int i = 0; i < seqRule.ReturnVars.Length; ++i)
                {
                    EmitVarIfNew(seqRule.ReturnVars[i], source);
                }
                if (seq.SequenceType == SequenceType.RuleCountAllCall)
                {
                    SequenceRuleCountAllCall seqCountRuleAll = (SequenceRuleCountAllCall)seqRule;
                    EmitVarIfNew(seqCountRuleAll.CountResult, source);
                }
                break;
            }

            case SequenceType.SequenceCall:
            {
                SequenceSequenceCall seqSeq = (SequenceSequenceCall)seq;
                // no handling for the input arguments seqSeq.ArgumentExpressions or the optional Subgraph needed
                // because there can only be variable uses
                for (int i = 0; i < seqSeq.ReturnVars.Length; ++i)
                {
                    EmitVarIfNew(seqSeq.ReturnVars[i], source);
                }
                break;
            }

            case SequenceType.ForContainer:
            {
                SequenceForContainer seqFor = (SequenceForContainer)seq;
                EmitVarIfNew(seqFor.Var, source);
                if (seqFor.VarDst != null)
                {
                    EmitVarIfNew(seqFor.VarDst, source);
                }
                EmitNeededVarAndRuleEntities(seqFor.Seq, source);
                break;
            }

            case SequenceType.ForIntegerRange:
            {
                SequenceForIntegerRange seqFor = (SequenceForIntegerRange)seq;
                EmitVarIfNew(seqFor.Var, source);
                EmitNeededVarAndRuleEntities(seqFor.Seq, source);
                break;
            }

            case SequenceType.ForIndexAccessEquality:
            {
                SequenceForIndexAccessEquality seqFor = (SequenceForIndexAccessEquality)seq;
                EmitVarIfNew(seqFor.Var, source);
                EmitNeededVarAndRuleEntities(seqFor.Seq, source);
                break;
            }

            case SequenceType.ForIndexAccessOrdering:
            {
                SequenceForIndexAccessOrdering seqFor = (SequenceForIndexAccessOrdering)seq;
                EmitVarIfNew(seqFor.Var, source);
                EmitNeededVarAndRuleEntities(seqFor.Seq, source);
                break;
            }

            case SequenceType.ForAdjacentNodes:
            case SequenceType.ForAdjacentNodesViaIncoming:
            case SequenceType.ForAdjacentNodesViaOutgoing:
            case SequenceType.ForIncidentEdges:
            case SequenceType.ForIncomingEdges:
            case SequenceType.ForOutgoingEdges:
            case SequenceType.ForReachableNodes:
            case SequenceType.ForReachableNodesViaIncoming:
            case SequenceType.ForReachableNodesViaOutgoing:
            case SequenceType.ForReachableEdges:
            case SequenceType.ForReachableEdgesViaIncoming:
            case SequenceType.ForReachableEdgesViaOutgoing:
            case SequenceType.ForBoundedReachableNodes:
            case SequenceType.ForBoundedReachableNodesViaIncoming:
            case SequenceType.ForBoundedReachableNodesViaOutgoing:
            case SequenceType.ForBoundedReachableEdges:
            case SequenceType.ForBoundedReachableEdgesViaIncoming:
            case SequenceType.ForBoundedReachableEdgesViaOutgoing:
            case SequenceType.ForNodes:
            case SequenceType.ForEdges:
            {
                SequenceForFunction seqFor = (SequenceForFunction)seq;
                EmitVarIfNew(seqFor.Var, source);
                EmitNeededVarAndRuleEntities(seqFor.Seq, source);
                break;
            }

            case SequenceType.ForMatch:
            {
                SequenceForMatch seqFor = (SequenceForMatch)seq;
                EmitVarIfNew(seqFor.Var, source);
                EmitNeededVarAndRuleEntities(seqFor.Seq, source);
                EmitNeededVarAndRuleEntities(seqFor.Rule, source);
                break;
            }

            case SequenceType.BooleanComputation:
            {
                SequenceBooleanComputation seqBoolComp = (SequenceBooleanComputation)seq;
                EmitNeededVarEntities(seqBoolComp.Computation, source);
                break;
            }

            default:
                foreach (Sequence childSeq in seq.Children)
                {
                    EmitNeededVarAndRuleEntities(childSeq, source);
                }
                break;
            }
        }
        public void ApplyRewriteSequence(Sequence seq, bool debug)
        {
            bool installedDumpHandlers = false;

            if (!impl.ActionsExists())
            {
                return;
            }

            if (debug || CheckDebuggerAlive())
            {
                debugger.NotifyOnConnectionLost = true;
                debugger.InitNewRewriteSequence(seq, debug);
            }

            if (!InDebugMode && ContainsSpecial(seq))
            {
                impl.curShellProcEnv.ProcEnv.OnEntereringSequence += DumpOnEntereringSequence;
                impl.curShellProcEnv.ProcEnv.OnExitingSequence    += DumpOnExitingSequence;
                installedDumpHandlers = true;
            }
            else
            {
                impl.curShellProcEnv.ProcEnv.OnEntereringSequence += NormalEnteringSequenceHandler;
            }

            curGRS  = seq;
            curRule = null;

            impl.debugOut.WriteLine("Executing Graph Rewrite Sequence (CTRL+C for abort) ...");
            cancelSequence = false;
            WorkaroundManager.Workaround.PreventComputerGoingIntoSleepMode(true);
            impl.curShellProcEnv.ProcEnv.PerformanceInfo.Reset();
            StatisticsSource statisticsSource = new StatisticsSource(impl.curShellProcEnv.ProcEnv.NamedGraph, impl.curShellProcEnv.ProcEnv);
            Timer            timer            = null;

            if (!debug && !silenceExec)
            {
                timer = new Timer(new TimerCallback(PrintStatistics), statisticsSource, 1000, 1000);
            }

            try
            {
                bool result = impl.curShellProcEnv.ProcEnv.ApplyGraphRewriteSequence(seq);
                if (timer != null)
                {
                    timer.Dispose();
                }

                seq.ResetExecutionState();
                impl.debugOut.WriteLine("Executing Graph Rewrite Sequence done after {0} ms with result: {1}",
                                        (impl.curShellProcEnv.ProcEnv.PerformanceInfo.TimeNeeded * 1000).ToString("F1", System.Globalization.CultureInfo.InvariantCulture),
                                        result);
                if (impl.newGraphOptions.Profile)
                {
                    impl.debugOut.WriteLine(" - {0} search steps executed", impl.curShellProcEnv.ProcEnv.PerformanceInfo.SearchSteps);
                }
#if DEBUGACTIONS || MATCHREWRITEDETAIL // spread over multiple files now, search for the corresponding defines to reactivate
                impl.debugOut.WriteLine(" - {0} matches found in {1} ms", perfInfo.MatchesFound, perfInfo.TotalMatchTimeMS);
                impl.debugOut.WriteLine(" - {0} rewrites performed in {1} ms", perfInfo.RewritesPerformed, perfInfo.TotalRewriteTimeMS);
#if DEBUGACTIONS
                impl.debugOut.WriteLine("\nDetails:");
                ShowSequenceDetails(seq, perfInfo);
#endif
#else
                impl.debugOut.WriteLine(" - {0} matches found", impl.curShellProcEnv.ProcEnv.PerformanceInfo.MatchesFound);
                impl.debugOut.WriteLine(" - {0} rewrites performed", impl.curShellProcEnv.ProcEnv.PerformanceInfo.RewritesPerformed);
#endif
            }
            catch (OperationCanceledException)
            {
                cancelSequence = true;      // make sure cancelSequence is set to true
                if (timer != null)
                {
                    timer.Dispose();
                }
                if (curRule == null)
                {
                    impl.errOut.WriteLine("Rewrite sequence aborted!");
                }
                else
                {
                    impl.errOut.WriteLine("Rewrite sequence aborted after position:");
                    SequencePrinter.PrintSequence(curGRS, curRule);
                    impl.errOut.WriteLine();
                }
            }
            WorkaroundManager.Workaround.PreventComputerGoingIntoSleepMode(false);
            curRule = null;
            curGRS  = null;

            if (InDebugMode)
            {
                debugger.NotifyOnConnectionLost = false;
                debugger.FinishRewriteSequence();
            }

            StreamWriter emitWriter = impl.curShellProcEnv.ProcEnv.EmitWriter as StreamWriter;
            if (emitWriter != null)
            {
                emitWriter.Flush();
            }

            if (installedDumpHandlers)
            {
                impl.curShellProcEnv.ProcEnv.OnEntereringSequence -= DumpOnEntereringSequence;
                impl.curShellProcEnv.ProcEnv.OnExitingSequence    -= DumpOnExitingSequence;
            }
            else
            {
                impl.curShellProcEnv.ProcEnv.OnEntereringSequence -= NormalEnteringSequenceHandler;
            }
        }
        public void ApplyRewriteSequenceExpression(SequenceExpression seqExpr)
        {
            SequenceDummy seq = new SequenceDummy();

            if (!impl.ActionsExists())
            {
                return;
            }

            if (CheckDebuggerAlive())
            {
                debugger.NotifyOnConnectionLost = true;
                debugger.InitNewRewriteSequence(seq, false);
            }

            curGRS  = seq;
            curRule = null;

            impl.debugOut.WriteLine("Evaluating Sequence Expression (CTRL+C for abort) ...");
            cancelSequence = false;
            WorkaroundManager.Workaround.PreventComputerGoingIntoSleepMode(true);
            impl.curShellProcEnv.ProcEnv.PerformanceInfo.Reset();
            StatisticsSource statisticsSource = new StatisticsSource(impl.curShellProcEnv.ProcEnv.NamedGraph, impl.curShellProcEnv.ProcEnv);
            Timer            timer            = null;

            if (!silenceExec)
            {
                timer = new Timer(new TimerCallback(PrintStatistics), statisticsSource, 1000, 1000);
            }

            try
            {
                object result = impl.curShellProcEnv.ProcEnv.EvaluateGraphRewriteSequenceExpression(seqExpr);
                if (timer != null)
                {
                    timer.Dispose();
                }

                seq.ResetExecutionState();
                impl.debugOut.WriteLine("Evaluating Sequence Expression done after {0} ms with result: {1}",
                                        (impl.curShellProcEnv.ProcEnv.PerformanceInfo.TimeNeeded * 1000).ToString("F1", System.Globalization.CultureInfo.InvariantCulture),
                                        EmitHelper.ToStringAutomatic(result, impl.curShellProcEnv.ProcEnv.Graph));
                if (impl.newGraphOptions.Profile)
                {
                    impl.debugOut.WriteLine(" - {0} search steps executed", impl.curShellProcEnv.ProcEnv.PerformanceInfo.SearchSteps);
                }
#if DEBUGACTIONS || MATCHREWRITEDETAIL // spread over multiple files now, search for the corresponding defines to reactivate
                impl.debugOut.WriteLine(" - {0} matches found in {1} ms", perfInfo.MatchesFound, perfInfo.TotalMatchTimeMS);
                impl.debugOut.WriteLine(" - {0} rewrites performed in {1} ms", perfInfo.RewritesPerformed, perfInfo.TotalRewriteTimeMS);
#if DEBUGACTIONS
                impl.debugOut.WriteLine("\nDetails:");
                ShowSequenceDetails(seq, perfInfo);
#endif
#else
                impl.debugOut.WriteLine(" - {0} matches found", impl.curShellProcEnv.ProcEnv.PerformanceInfo.MatchesFound);
#endif
            }
            catch (OperationCanceledException)
            {
                cancelSequence = true;      // make sure cancelSequence is set to true
                if (timer != null)
                {
                    timer.Dispose();
                }
                impl.errOut.WriteLine("Sequence expression aborted!");
            }
            WorkaroundManager.Workaround.PreventComputerGoingIntoSleepMode(false);
            curRule = null;
            curGRS  = null;

            if (InDebugMode)
            {
                debugger.NotifyOnConnectionLost = false;
                debugger.FinishRewriteSequence();
            }

            StreamWriter emitWriter = impl.curShellProcEnv.ProcEnv.EmitWriter as StreamWriter;
            if (emitWriter != null)
            {
                emitWriter.Flush();
            }
        }