/// <summary>
 /// Tells the Commander that all the subsequent Commands will have to be carried out in parallel, until the
 /// EndParallel method is called
 /// </summary>
 public void BeginParallel()
 {
     if (_currentParallel == null)
     {
         _currentParallel = new ParallelCommand();
     }
 }
 /// <summary>
 /// Stops the addition of parallel Commands and marks them as ready for execution
 /// </summary>
 public void EndParallel()
 {
     if (_currentParallel != null)
     {
         _operations.Add(_currentParallel);
         _currentParallel = null;
     }
 }
        public void OnEvent(IUnit hit)
        {
            var rootCmd = new ParallelCommand();

            if (_gameScene.SelectedUnit != null)
            {
                rootCmd.Commands.Add(new SetUnitColor(_gameScene.SelectedUnit, Color.white));
            }

            _gameScene.SelectedUnit = hit;
            rootCmd.Commands.Add(new SetUnitColor(_gameScene.SelectedUnit, Color.yellow));

            _commandsManager.ExecuteCommand(rootCmd);
        }
Example #4
0
        public override CommandResult Execute(IContext ctx)
        {
            this.Frozen = true;

            ManualResetEvent[] waiters = new ManualResetEvent[this.Commands.Count];
            ParallelCommand[] cmds = new ParallelCommand[this.Commands.Count];
            Stack<IFilter> filters = new Stack<IFilter>();

            int i = -1;
            foreach (ICommand cmd in this.Commands)
            {
                i++;

                waiters[i] = new ManualResetEvent(false);
                cmds[i] = new ParallelCommand(cmd, waiters[i]);
                ThreadPool.QueueUserWorkItem(cmds[i].Execute, ctx);

                if (cmd is IFilter)
                    filters.Push((IFilter)cmd);
            }

            logger.Debug("Waiting");

            foreach (ManualResetEvent m in waiters)
                m.WaitOne();

            logger.Debug("Got all results");

            Exception ex = null;

            foreach (ParallelCommand c in cmds)
            {
                try
                {
                    c.GetResult();
                }
                catch (Exception e)
                {
                    logger.Debug("Collected the first exception to pass to filters");
                    ex = e;
                    break;
                }
            }

            if (!this.InvokeFilters(filters, ctx, ex))
                throw ex;

            return CommandResult.Continue;
        }
Example #5
0
        public void OnEvent(List <Tuple <string, Vector3> > hit)
        {
            var updateHPCommand = new ParallelCommand();

            foreach (var data in hit)
            {
                IUnit unit = _unitsFactory.CreateUnit <IUnit>(data.Item1);
                unit.HP       = 100;
                unit.Attack   = 23;
                unit.Position = data.Item2;
                _gameScene.Units.Add(unit);
                updateHPCommand.Commands.Add(new UpdateUnitHP(unit));
            }

            _commandsManager.ExecuteCommand(updateHPCommand);
        }
Example #6
0
        private List<ParallelParameter> GeneratePredictUVCommands(string feaDir)
        {
            List<ParallelParameter> parallelCommands = new List<ParallelParameter>();
            foreach (string sid in FileMap.Map.Keys)
            {
                // check feature file
                string feaFile = FileMap.BuildPath(feaDir, sid, FileExtensions.Text);
                if (!File.Exists(feaFile))
                {
                    Logger.LogLine("Fail to get scaled svm features for sentence {0}", sid);
                    continue;
                }

                // predict one sentence's uv
                string uvFile = FileMap.BuildPath(UVDir, sid, FileExtensions.Text);
                Helper.EnsureFolderExistForFile(uvFile);
                ParallelCommand command = new ParallelCommand
                {
                    ExecutableFile = SvmPredictTool,
                    Argument = Helper.NeutralFormat(" \"{0}\" \"{1}\" \"{2}\"", feaFile, ModelFilePath, uvFile),
                    Description = Helper.NeutralFormat("Predict uv for sentence \"{0}\"", sid),
                    Logger = Logger
                };

                parallelCommands.Add(command);
            }

            return parallelCommands;
        }
Example #7
0
        private List<ParallelParameter> GenerateScaleFeatureCommands(string rangeFile)
        {
            List<ParallelParameter> parallelCommands = new List<ParallelParameter>();
            foreach (string sid in FileMap.Map.Keys)
            {
                // scale features by feature range file
                string feaFile = FileMap.BuildPath(SvmFeaDir, sid, FileExtensions.Text);
                if (!File.Exists(feaFile))
                {
                    Logger.LogLine("Fail to get svm features for sentence {0}", sid);
                    continue;
                }

                string scaledFeaFile = FileMap.BuildPath(ScaledSvmFeaDir, sid, FileExtensions.Text);
                Helper.EnsureFolderExistForFile(scaledFeaFile);
                Helper.SafeDelete(scaledFeaFile);

                ParallelCommand command = new ParallelCommand
                {
                    ExecutableFile = SvmScaleTool,
                    Argument = Helper.NeutralFormat(" -r \"{0}\" \"{1}\"", rangeFile, feaFile),
                    Description = Helper.NeutralFormat("Scale fea for sentence \"{0}\"", sid),
                    Logger = Logger,
                    ResultFileLogger = new TextLogger(scaledFeaFile)
                };

                parallelCommands.Add(command);
            }

            return parallelCommands;
        }
Example #8
0
        private List<ParallelParameter> GenerateExtractLpcCommands()
        {
            List<ParallelParameter> parallelCommands = new List<ParallelParameter>();
            string of0Dir = Path.Combine(IntermediateDir, "lpc_of0");
            Helper.EnsureFolderExist(of0Dir);

            // extract direct features from wave.
            foreach (string sid in FileMap.Map.Keys)
            {
                // extract lpc features from one wave file.
                string waveFile = FileMap.BuildPath(WaveDir, sid, FileExtensions.Waveform);
                string f0File = FileMap.BuildPath(F0Dir, sid, FileExtensions.F0File);
                if (!File.Exists(f0File))
                {
                    Logger.LogLine("Fail to get f0 for sentence {0}", sid);
                    continue;
                }

                // output file.
                string lpcFile = FileMap.BuildPath(LPCDir, sid, FileExtensions.LpcFile);
                Helper.EnsureFolderExistForFile(lpcFile);
                string of0File = FileMap.BuildPath(of0Dir, sid, FileExtensions.F0File);
                Helper.EnsureFolderExistForFile(of0File);

                // parallel computing.
                ParallelCommand command = new ParallelCommand
                {
                    // mode = 16: dump plain text of lpc.
                    ExecutableFile = StraightTool,
                    Argument = GenerateExtractLpcArgument(waveFile, f0File, lpcFile, of0File, FFTDim, LpcOrder, SecondsPerFrame),
                    Description = Helper.NeutralFormat("Get lpc from sentence \"{0}\"", sid),
                    Logger = Logger,
                };

                parallelCommands.Add(command);
            }

            return parallelCommands;
        }
Example #9
0
        private List<ParallelParameter> GenerateExtractF0NccfCommands()
        {
            List<ParallelParameter> parallelCommands = new List<ParallelParameter>();

            // extract f0_nccf from wave
            foreach (string sid in FileMap.Map.Keys)
            {
                // process one wave file
                string waveFile = FileMap.BuildPath(WaveDir, sid, FileExtensions.Waveform);

                // 1) extract <f0 nccf> from one wave file
                string f0NccfFile = FileMap.BuildPath(F0NccfDir, sid, FileExtensions.F0File);
                Helper.EnsureFolderExistForFile(f0NccfFile);
                ParallelCommand command = new ParallelCommand
                {
                    ExecutableFile = GetF0Tool,

                    // TODO: need to evaluate "-b 1" furthermore.
                    Argument = GenerateExtractF0NccfArgument(GetF0Config, waveFile, f0NccfFile, FrameBias, SecondsPerFrame, MinF0Value, MaxF0Value),
                    Description = Helper.NeutralFormat("Get f0_nccf for sentence \"{0}\"", sid),
                    Logger = Logger,
                };

                parallelCommands.Add(command);
            }

            return parallelCommands;
        }