示例#1
0
文件: Sim.cs 项目: thehoglet/RVis
 public static TSimConfig ToToml(SimConfig config)
 {
     return(new TSimConfig
     {
         Title = config.Title,
         Description = config.Description,
         ImportedOn = config.ImportedOn,
         Code = new TSimCode
         {
             File = config.SimCode.File,
             Exec = config.SimCode.Exec,
             Formal = config.SimCode.Formal
         },
         Input = new TSimInput
         {
             Parameters = config.SimInput.SimParameters.IsEmpty
     ? default
     : config.SimInput.SimParameters.Map(p => new TSimParameter
             {
                 Name = p.Name,
                 Value = p.Value,
                 Unit = p.Unit,
                 Description = p.Description
             }).ToArray(),
             IsDefault = config.SimInput.IsDefault
         },
示例#2
0
        public MCSimExecutor(string pathToSimulation, SimConfig config)
        {
            RequireDirectory(pathToSimulation);
            RequireNotNullEmptyWhiteSpace(config.SimCode.File);

            var pathToExecutable = Combine(pathToSimulation, config.SimCode.File);

            RequireFile(pathToExecutable);

            var templateInFileName = GetFileNameWithoutExtension(config.SimCode.File) + ".in";
            var pathToTemplateIn   = Combine(pathToSimulation, templateInFileName);

            RequireFile(pathToTemplateIn);

            var templateIn = ReadAllText(pathToTemplateIn);

            _templateIn = Template.ParseLiquid(templateIn);

            var scriptVariables = _templateIn.Page.Body.Statements
                                  .OfType <ScriptExpressionStatement>()
                                  .Select(ses => ses.Expression)
                                  .OfType <ScriptVariable>()
                                  .Select(e => e.Name)
                                  .OrderBy(s => s)
                                  .ToArr();
            var configParameters = config.SimInput.SimParameters.Map(p => p.Name);

            if (scriptVariables != configParameters)
            {
                var missingScriptVariables = configParameters.Filter(p => !scriptVariables.Contains(p));
                RequireTrue(
                    missingScriptVariables.IsEmpty,
                    "Missing parameters in .in file: " + Join(", ", missingScriptVariables)
                    );

                var missingConfigParameters = scriptVariables.Filter(p => !configParameters.Contains(p));
                RequireTrue(
                    missingConfigParameters.IsEmpty,
                    "Unknown parameters in .in file: " + Join(", ", missingConfigParameters)
                    );
            }

            do
            {
                _pathToInOutDirectory = Combine(GetTempPath(), GetRandomFileName());
            }while (Directory.Exists(_pathToInOutDirectory));

            RequireNotNull(CreateDirectory(_pathToInOutDirectory));

            _pathToInFile = Combine(
                _pathToInOutDirectory,
                GetFileNameWithoutExtension(pathToExecutable) + ".in");

            _pathToOutFile = Combine(
                _pathToInOutDirectory,
                GetFileNameWithoutExtension(pathToExecutable) + ".out");

            _processStartInfo = new ProcessStartInfo
            {
                UseShellExecute        = false,
                CreateNoWindow         = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                FileName  = pathToExecutable,
                Arguments = _pathToInFile + " " + _pathToOutFile
            };

            _independentVariableName = config.SimOutput.IndependentVariable.Name;
            _dependentVariableNames  = config.SimOutput.DependentVariables.Map(v => v.Name);
            _tableName     = config.Title;
            _defaultSource = config.SimInput.SimParameters;
        }
示例#3
0
 public Simulation(string pathToSimulation, SimConfig simConfig)
 {
     _pathToSimulation = pathToSimulation;
     _simConfig        = simConfig;
 }