public SimQueryVariableAttribute(SimVariable variable, UnitOfMeasurement unitOfMeasurement)
 {
     Variable          = variable;
     UnitOfMeasurement = unitOfMeasurement;
 }
Exemplo n.º 2
0
        public void RunBatch()
        {
            //if there were any xmpp connections specified in the perams not set by the user, connect here
            if (!AutoConnectExtSim())
            {
                this._error = "Failed to connect to external simulation.";
                logger.Error(this.error);
            }


            batchSuccess = false;
            _stop        = false;
            bool      retVal    = true;
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            TimeSpan resTime = stopWatch.Elapsed;
            int      actRuns = 0;
            int      curI    = 0;

            StreamWriter pathOutputFile = null;

            if (!string.IsNullOrEmpty(_keyPathsOutput))
            {
                pathOutputFile = new StreamWriter(_keyPathsOutput, append: false);
            }

            //if user defined the seed then reset random so that seed is used.
            if (ConfigData.seed != null)
            {
                SingleRandom.Reset();
            }

            //make a list of result items for each key state.
            //ResultState[] keyStates = _lists.allStates.Where(i => i.Value.stateType == EnStateType.stKeyState).Select(i => new ResultState(i.Value.name)).ToArray();
            //Dictionary<string, ResultState> keyStateResMap = null;

            //progressCallback(stopWatch.Elapsed, 0);
            try
            {
                _lists.allVariables.ReInitAll();

                for (int i = 1; i <= _numRuns; ++i)
                {
                    SetLog(i);

                    logger.Info("StartRun: " + i);

                    curI = i;
                    if (_stop)
                    {
                        break;
                    }

                    SimulationTracking.StateTracker trackSim;

                    if (_msgServer == null)
                    {
                        trackSim = new SimulationTracking.StateTracker(_lists, _endTime, 0, null);
                    }
                    else
                    {
                        trackSim = new SimulationTracking.StateTracker(_lists, _endTime, _frameRate, _msgServer);
                    }

                    //trackSim.logFunc = logFunc;

                    //logFunc("Run " + i.ToString() + Environment.NewLine);
                    List <int> finalStates = trackSim.StartTracker();
                    //logFunc("Run " + i.ToString() + " done" + Environment.NewLine);

                    if (trackSim.keyStateCnt > 0)
                    {
                        StatePath[] failedComps = null;

                        if (_logFailedComps)
                        {
                            failedComps = trackSim.GetFailedComponents();
                        }

                        //if (failedComps.Length == 0)
                        //  failedComps = trackSim.GetCurrentStates();
                        if (pathOutputFile != null)
                        {
                            pathOutputFile.WriteLine("Run - " + i.ToString());
                        }

                        trackSim.GetKeyPaths(keyPaths, otherPaths);

                        foreach (SimulationEngine.ResultStateBase path in keyPaths.Values)
                        {
                            string keyStateName = path.name;

                            if (logVarVals.Count > 0)
                            {
                                Dictionary <string, List <string> > varDict;
                                if (!_variableVals.TryGetValue(keyStateName, out varDict))
                                {
                                    varDict = new Dictionary <string, List <string> >();
                                    _variableVals.Add(keyStateName, varDict);
                                }

                                List <string> varVals;

                                foreach (string varName in logVarVals)
                                {
                                    SimVariable curVar = _lists.allVariables.FindByName(varName);
                                    if (curVar == null)
                                    {
                                        this._error = "No variable found named - " + varName;
                                        logger.Error(this.error);
                                    }

                                    if (!varDict.TryGetValue(varName, out varVals))
                                    {
                                        varVals = new List <string>();
                                        varDict.Add(varName, varVals);
                                    }

                                    varVals.Add(curVar.strValue);
                                }
                            }

                            if (_logFailedComps && (failedComps.Length > 0))
                            {
                                int[] idArray = failedComps.Select(j => j.state.id).ToArray();
                                if (!keyFailedItems.ContainsKey(keyStateName))
                                {
                                    keyFailedItems.Add(keyStateName, new FailedItems());
                                }

                                keyFailedItems[keyStateName].AddCompFailSet(idArray);
                            }
                        }
                    }

                    if (((stopWatch.Elapsed - resTime) > TimeSpan.FromSeconds(1)) && (i > 0))
                    {
                        stopWatch.Stop();
                        progressCallback(stopWatch.Elapsed, i, _logFailedComps);
                        stopWatch.Start();
                        resTime = stopWatch.Elapsed;
                    }

                    actRuns = i;

                    logger.Info("EndOfRun: " + i);
                }
            }
            catch (Exception e)
            {
                _error = "Exception - " + e.Message;
                if (e.InnerException != null)
                {
                    _error = _error + Environment.NewLine + e.InnerException;
                }

//#if DEBUG
//        throw e;
//#else

                logger.Info(_error);
//#endif
                retVal = false;
            }

            stopWatch.Stop();
            if (pathOutputFile != null)
            {
                pathOutputFile.Close();
            }
            progressCallback(stopWatch.Elapsed, actRuns, _logFailedComps);

            if (_jsonResultPaths != "")
            {
                OverallResults resultObj = new OverallResults();
                resultObj.name            = this._lists.name;
                resultObj.keyStates       = keyPaths.Values.ToList();
                resultObj.otherStatePaths = otherPaths.Values.ToList();
                resultObj.numRuns         = curI;
                resultObj.CalcStats();
                Dictionary <string, int> inStateCnts = new Dictionary <string, int>();
                //foreach (var keyS in resultObj.keyStates)
                //{
                //  Dictionary<string, int> depth = new Dictionary<string, int>();
                //  StateCounts(keyS, inStateCnts, depth);
                //}
                foreach (var keyS in resultObj.keyStates)
                {
                    // Dictionary<string, int> depth = new Dictionary<string, int>();
                    //SetResultStatsRec(keyS, inStateCnts, curI);//, depth);

                    if (_variableVals.Count > 0) //if there are any being tracked, they should have a value for each key state.
                    {
                        keyS.watchVariables = _variableVals[keyS.name];
                    }
                }

                string output = JsonConvert.SerializeObject(resultObj, Formatting.Indented);
                File.WriteAllText(_jsonResultPaths, output);
            }
            batchSuccess = retVal;
        }