Пример #1
0
 private void processTimer_Tick(object sender, ElapsedEventArgs e)
 {
     if (!SearchIO.GetTerminateRequest(Thread.CurrentThread.Name))
     {
         return;
     }
     choice = new[] { -2 };
     Close();
 }
Пример #2
0
        public static void Run(designGraph seed, grammarRule rule, Relaxation RelaxationTemplate = null)
        {
            try
            {
                if (RelaxationTemplate == null)
                {
                    RelaxationTemplate = new Relaxation(0);
                }
                var rnd             = new Random();
                int k               = 0;
                var continueTesting = true;

                SearchIO.output("begin recognizing rule: " + rule.name + "on graph :" + seed.name, 2);
                var dummyRS = new ruleSet();
                dummyRS.Add(rule);
                if (SearchIO.GetTerminateRequest(Thread.CurrentThread.ManagedThreadId))
                {
                    return;
                }
                var options = dummyRS.recognize(seed, false, RelaxationTemplate.copy());
                if (SearchIO.GetTerminateRequest(Thread.CurrentThread.ManagedThreadId))
                {
                    return;
                }
                var numOptions = options.Count;
                if (numOptions == 0)
                {
                    if (MessageBox.Show("There were no recognized options. Should the rule be relaxed?", "Test Rule Status",
                                        MessageBoxButton.YesNo, MessageBoxImage.Asterisk, MessageBoxResult.No) ==
                        MessageBoxResult.Yes)
                    {
                        Run(seed, rule, new Relaxation(RelaxationTemplate.NumberAllowable + 1));
                    }
                    return;
                }
                do
                {
                    var status = "There ";
                    int choice = -1;
                    switch (numOptions)
                    {
                    case 0: throw new Exception("Should not be able to reach here. (Test Rule Chooser, zero options.)");

                    case 1:
                        status += "was only one recognized option and it applied as shown.\n";
                        choice  = 0;
                        status += options[choice].Relaxations.RelaxationSummary;
                        break;

                    default:
                        status += "were " + numOptions + " recognized locations.\n";
                        choice  = rnd.Next(options.Count);
                        status += options[choice].Relaxations.RelaxationSummary;
                        option.AssignOptionConfluence(options, new candidate(seed, 0), ConfluenceAnalysis.Full);
                        var numberWithConfluence = options.Count(o => (o.confluence.Count > 0));
                        var maxConfluence        = options.Max(o => o.confluence.Count);
                        var withMaxConfluence    = options.Count(o => (o.confluence.Count == maxConfluence));
                        if (numberWithConfluence > 0)
                        {
                            status += "Confluence existed between " + numberWithConfluence + " of them";
                            if (maxConfluence > 1)
                            {
                                status += "; \nwith " + withMaxConfluence + " options having a confluence with "
                                          + maxConfluence + " other options.\n";
                            }
                            else
                            {
                                status += ".\n";
                            }
                        }
                        status += "Option #" + choice + " was randomly chosen, and invoked.\n";
                        break;
                    }
                    if (!continueTesting)
                    {
                        continue;
                    }
                    if (SearchIO.GetTerminateRequest(Thread.CurrentThread.ManagedThreadId))
                    {
                        return;
                    }
                    var chosenOption = options[choice];
                    {
                        var seedCopy = seed.copy();
                        SearchProcess.transferLmappingToChild(seedCopy, seed, chosenOption);
                        seed = seedCopy;
                    }
                    chosenOption.apply(seed, null);
                    SearchIO.output("Rule sucessfully applied", 4);
                    SearchIO.addAndShowGraphWindow(seed, "After calling " + ++k + " rules");
                    if (SearchIO.GetTerminateRequest(Thread.CurrentThread.ManagedThreadId))
                    {
                        return;
                    }
                    options = dummyRS.recognize(seed, true, RelaxationTemplate.copy());
                    if (SearchIO.GetTerminateRequest(Thread.CurrentThread.ManagedThreadId))
                    {
                        return;
                    }
                    numOptions = options.Count;
                    switch (numOptions)
                    {
                    case 0:
                        status         += "There are no recognized locations on the new graph";
                        continueTesting = false;
                        MessageBox.Show(status, "Test Rule Status", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                        break;

                    case 1:
                        status         += "There is one recognized location on the new graph. Would you like to invoke it?";
                        continueTesting = (MessageBox.Show(status, "Continue Applying?", MessageBoxButton.YesNo,
                                                           MessageBoxImage.Asterisk, MessageBoxResult.No) ==
                                           MessageBoxResult.Yes);
                        break;

                    default:
                        status += "There are " + options.Count + " recognized locations on the new graph. Would you "
                                  + "like to randomly invoke one?";
                        continueTesting = (MessageBox.Show(status, "Continue Applying?", MessageBoxButton.YesNo,
                                                           MessageBoxImage.Asterisk, MessageBoxResult.No) ==
                                           MessageBoxResult.Yes);
                        break;
                    }
                } while (continueTesting);
            }
            catch (Exception exc)
            {
                ErrorLogger.Catch(exc);
            }
        }
Пример #3
0
        /// <summary>
        ///   Recognizes the choose apply cycle. Here is the main Recognize, Choose, and
        ///   Apply Generation Cycle. It accepts the host candidate (not graph), the index
        ///   of what ruleSet to invoke, and an array of size equal to the number of ruleSets.
        ///   At the end of the process, it returns the updated candidate. The three step
        ///   process may, however exit at any of five places in the loop, these are described below.
        ///   1. the ruleSet invoked may not have any calls left. This will cause the GenerationStatus
        ///   to be CycleLimit, and the process will execute what is stored in the 3rd position of
        ///   generationSteps, ruleSet->nextGenerationStep[2], either Stop, Loop, GoToPrevious(ruleSet),
        ///   GoToNext(ruleSet), or GoToRuleSet#
        ///   2. the choice operation has sent a STOP message, or more precisely a negative # or
        ///   a number greater than the list of option. This results in a GenerationStatus of Choice
        ///   and the execution of ruleSet->nextGenerationStep[1] (any of the options stated above).
        ///   3. there are no rules recognized for the graph. This results in a GenerationStatus of
        ///   NoRules and the execution of ruleSet->nextGenerationStep[3] (any of the options above).
        ///   4. A trigger rule has been applied. This results in a GenerationStatus of TriggerRule
        ///   and the execution of ruleSet->nextGenerationStep[4] (any of the options stated above).
        ///   5. the recognize, choose, and apply cycle performed as intended - no abnormal activites.
        ///   This results in a GenerationStatus of Normal and the execution of
        ///   ruleSet->nextGenerationStep[0] (any of the options stated above).*/
        /// </summary>
        /// <param name = "host">The host.</param>
        /// <param name = "ruleSetIndex">Index of the rule set.</param>
        /// <param name = "numOfCallsLeft">The num of calls left.</param>
        protected void RecognizeChooseApplyCycle(candidate host, int ruleSetIndex, int[] numOfCallsLeft)
        {
            while ((ruleSetIndex >= 0) && (ruleSetIndex < NumOfRuleSets))
            {
                host.activeRuleSetIndex = ruleSetIndex;
                SearchIO.output("Active Rule Set = " + ruleSetIndex, 4);

                #region terminate immediately if there are no cycles left

                if (numOfCallsLeft[ruleSetIndex] == 0)
                {
                    /* it is possible that another ruleset intends to invoke this one, but your
                     * number of calls for this set has hit its limit. */
                    host.GenerationStatus[ruleSetIndex] = GenerationStatuses.CycleLimit;
                    ruleSetIndex = nextRuleSet(ruleSetIndex, GenerationStatuses.CycleLimit);
                    SearchIO.output("cycle limit reached", 4);
                    continue;
                }

                #endregion

                #region ***** RECOGNIZE *****


                SearchIO.output("begin RCA loop for RuleSet #" + ruleSetIndex, 4);
                var options = Rulesets[ruleSetIndex].recognize(host.graph, InParallel);

                SearchIO.output("There are " + options.Count + " rule choices.", 4);
                if (options.Count == 0)
                {
                    /* There are no rules to recognize, exit here. */
                    host.GenerationStatus[ruleSetIndex] = GenerationStatuses.NoRules;
                    var newRSIndex = nextRuleSet(ruleSetIndex, GenerationStatuses.NoRules);
                    if (newRSIndex == ruleSetIndex)
                    {
                        throw new Exception("Same ruleset chosen when no rules are recognized.");
                    }
                    ruleSetIndex = newRSIndex;
                    continue;
                }
                if (SearchIO.GetTerminateRequest(Thread.CurrentThread.ManagedThreadId))
                {
                    return;
                }

                #endregion

                #region ***** CHOOSE *****

                if (Rulesets[ruleSetIndex].choiceMethod == choiceMethods.Automatic)
                {
                    choice = new[] { 0 }
                }
                ;
                else
                {
                    choice = choose(options, host);
                }
                if (choice[0] == -1)
                {
                    host.undoLastRule();
                    if (Display)
                    {
                        SearchIO.addAndShowGraphWindow(host.graph.copy(),
                                                       "Revert to after calling " + host.numRulesCalled + " rules");
                    }
                    continue;
                }
                if ((choice == null) || (choice[0] < 0) || (choice[0] >= options.Count))
                {
                    SearchIO.output("Choice = #" + IntCollectionConverter.Convert(choice), 4);

                    /* the overloaded choice function may want to communicate to the loop that it
                     * should finish the process. */
                    SearchIO.output("Choice received a STOP request", 4);
                    host.GenerationStatus[ruleSetIndex] = GenerationStatuses.Choice;
                    ruleSetIndex = nextRuleSet(ruleSetIndex, GenerationStatuses.Choice);
                    continue;
                }
                if (SearchIO.GetTerminateRequest(Thread.CurrentThread.ManagedThreadId))
                {
                    return;
                }

                #endregion

                #region ***** APPLY *****
                host.saveCurrent();
                foreach (var c in choice)
                {
                    options[c].apply(host.graph, choose(options[c], host));
                    host.addToRecipe(options[c]);
                    SearchIO.output("Rule sucessfully applied", 4);
                }
                if (Display && Rulesets[ruleSetIndex].choiceMethod == choiceMethods.Design)
                {
                    SearchIO.addAndShowGraphWindow(host.graph.copy(),
                                                   "After calling " + host.numRulesCalled + " rules");
                }
                if (SearchIO.GetTerminateRequest(Thread.CurrentThread.ManagedThreadId))
                {
                    return;
                }

                #endregion

                #region Check to see if loop is done

                /* First thing we do is reduce the number of calls left. Note that if you start with
                 * a negative number, the process will continue to make it more negative - mimicking
                 * no cycle limit. It is safer to use the globalvar, maxRulesToApply though.*/
                if (this is LindenmayerChooseRCA)
                {
                    numOfCallsLeft[ruleSetIndex]--;
                }
                else
                {
                    numOfCallsLeft[ruleSetIndex] = numOfCallsLeft[ruleSetIndex] - choice.GetLength(0);
                }

                if (choice.Any(c => (options[c].ruleNumber == Rulesets[ruleSetIndex].TriggerRuleNum)))
                {
                    /* your ruleset loops until a trigger rule and the trigger rule was just called. */
                    SearchIO.output("The trigger rule has been chosen.", 4);
                    host.GenerationStatus[ruleSetIndex] = GenerationStatuses.TriggerRule;
                    ruleSetIndex = nextRuleSet(ruleSetIndex, GenerationStatuses.TriggerRule);
                }
                else
                {
                    /* Normal operation */
                    SearchIO.output("RCA loop executed normally.", 4);
                    host.GenerationStatus[ruleSetIndex] = GenerationStatuses.Normal;
                    ruleSetIndex = nextRuleSet(ruleSetIndex, GenerationStatuses.Normal);
                }

                #endregion
            }
        }