Esempio n. 1
0
        public void AutoItemsForNewComponent(string compName,
                                             string desc,
                                             string[] demandNames,
                                             double[] demandFailProbs,
                                             SimVariable[] demandFailVars,
                                             string[] rateNames,
                                             double[] runningFailRates,
                                             List <State> startStates,
                                             List <State> turnOffStates,
                                             string sim3DComp   = "",
                                             bool sepFailStates = false)
        {
            if (demandNames.Count() != demandFailProbs.Count())
            {
                throw new Exception("Not equal Demand names and prob list");
            }

            if (rateNames.Count() != runningFailRates.Count())
            {
                throw new Exception("Not equal Demand names and prob list");
            }

            CompDiagram addComp = new CompDiagram(compName);

            addComp.desc = desc;
            allDiagrams.Add(addComp);

            //Action addAct;
            //Event addEv;

            //all items going to have active state and failed state
            State standbyState = new State(compName + "_Standby", EnStateType.stStart, addComp);
            State activeState  = new State(compName + "_Active", EnStateType.stStandard, addComp);

            allStates.Add(standbyState);
            allStates.Add(activeState);
            List <State> failStates = new List <State>();

            State failedState = null;

            if (!sepFailStates)
            {
                failedState = new State(compName + "_Failed", EnStateType.stStandard, addComp);
                allStates.Add(failedState);
                addComp.AddEvalVal(failedState.id, false);
                failStates.Add(failedState);
            }

            addComp.AddEvalVal(activeState.id, true);

            //add event for a demand
            StateCngEvent DemandEv;

            DemandEv = new StateCngEvent(compName + "_Demand", true, false);


            //add all the start states to the related items
            if (startStates != null)
            {
                DemandEv.AddRelatedItems(startStates.Select(c => c.id).ToList());
            }
            allEvents.Add(DemandEv);

            //add action to move from off to on or failed for the demand
            TransitionAct demandAct = new TransitionAct("From_" + compName + "_Demand");

            if (demandFailProbs.Count() > 0)
            {
                for (int i = 0; i < demandFailProbs.Count(); ++i)
                {
                    if (sepFailStates)
                    {
                        failedState = new State(compName + "_Failed" + demandNames[i], EnStateType.stStandard, addComp);
                        allStates.Add(failedState);
                        addComp.AddEvalVal(failedState.id, false);
                        failStates.Add(failedState);
                    }

                    //setup the probability of failure on demand
                    //TODO : Add the demandNames[i] somehow to the path data when moving to the failed state.
                    if ((demandFailVars != null) && (demandFailVars.Count() > 0) && (demandFailVars[i] != null))
                    {
                        demandAct.AddToState(failedState, demandFailVars[i], demandNames[i]);
                    }
                    else
                    {
                        demandAct.AddToState(failedState, demandFailProbs[i], demandNames[i]);
                    }
                }

                demandAct.AddToState(activeState);
                allActions.Add(demandAct);
                //add the events and movement to the standbyState
                standbyState.AddEvent(DemandEv, true, demandAct);
            }
            else
            {
                //add the action to move to the active state automatically on start request when no fails to start.
                demandAct.AddToState(activeState);
                allActions.Add(demandAct);
                //add the events and movement to the standbyState
                standbyState.AddEvent(DemandEv, true, demandAct);
            }

            //add event for a shutoff request
            StateCngEvent offEv = new StateCngEvent(compName + "_Stop", false);

            if (turnOffStates != null)
            {
                offEv.AddRelatedItems(turnOffStates.Select(c => c.id).ToList());
            }
            allEvents.Add(offEv);

            //add action to move from off to on or failed for the demand
            TransitionAct offAct = new TransitionAct("Goto_" + compName + "_Off");

            offAct.AddToState(standbyState);
            allActions.Add(offAct);

            //add the Events and movement back to the standby state
            activeState.AddEvent(offEv, true, offAct);
            foreach (State curSt in failStates)
            {
                curSt.AddEvent(offEv, true, offAct);
            }


            if ((runningFailRates.Count() > 0) || ((sim3DComp != null) && (sim3DComp != ""))) //add the fail to run links
            {
                //add transition leading from active to failed for the event above
                //TransitionAct failAct = new TransitionAct("Goto_" + compName + "_Failed");
                //failAct.AddToState(failedState);
                //allActions.Add(failAct);

                for (int i = 0; i < runningFailRates.Count(); ++i)
                {
                    if (sepFailStates)
                    {
                        failedState = new State(compName + "_Failed" + rateNames[i], EnStateType.stStandard, addComp);
                        allStates.Add(failedState);
                        addComp.AddEvalVal(failedState.id, false);
                        failStates.Add(failedState);
                    }

                    TransitionAct failAct = new TransitionAct("Goto_" + compName + "_Had_" + rateNames[i] + "_Failure");
                    failAct.AddToState(failedState);
                    allActions.Add(failAct);

                    //add event leading from active to failed
                    FailProbEvent failEv = new FailProbEvent(compName + rateNames[i], runningFailRates[i], Globals.HourTimeSpan, Globals.DayTimeSpan);
                    allEvents.Add(failEv);

                    activeState.AddEvent(failEv, true, failAct);
                }

                if ((sim3DComp != null) && (sim3DComp != ""))
                {
                    if (sepFailStates)
                    {
                        failedState = new State(compName + "_Failed_3D", EnStateType.stStandard, addComp);
                        allStates.Add(failedState);
                        addComp.AddEvalVal(failedState.id, false);
                        failStates.Add(failedState);
                    }

                    TransitionAct failAct = new TransitionAct("Goto_" + compName + "_Had_3D" + "_Failure");
                    failAct.AddToState(failedState);
                    allActions.Add(failAct);

                    Sim3DVariable simVar  = (Sim3DVariable)this.allVariables.FindByName(sim3DComp);
                    VariableList  varList = new VariableList();
                    varList.Add(simVar);
                    EvalVarEvent sim3DEvent = new EvalVarEvent(sim3DComp, "if (" + simVar.name + " > 0) return true; else return false;", varList, simVar);
                    //sim3DEvent.AddRelatedItem(simVar.id);
                    allEvents.Add(sim3DEvent);
                    activeState.AddEvent(sim3DEvent, true, failAct);
                }
            }
        }
Esempio n. 2
0
//The following code sections are for constructing a model through code

        public void AutoItemsForNewComponent(string compName,
                                             string desc,
                                             List <EnFailType> failTypes,
                                             double runFailure,
                                             List <State> startStates,
                                             List <State> turnOffStates,
                                             double demandFailure = 0.0,
                                             string sim3DComp     = "")
        {
            CompDiagram addComp = new CompDiagram(compName);

            addComp.desc = desc;
            allDiagrams.Add(addComp);

            //Action addAct;
            //Event addEv;

            //all items going to have active state and failed state
            State standbyState = new State(compName + "_Standby", EnStateType.stStart, addComp);
            State activeState  = new State(compName + "_Active", EnStateType.stStandard, addComp);
            State failedState  = new State(compName + "_Failed", EnStateType.stStandard, addComp);

            allStates.Add(standbyState);
            allStates.Add(activeState);
            allStates.Add(failedState);
            //addComp.Add(standbyState.id, true);
            addComp.AddEvalVal(activeState.id, true);
            addComp.AddEvalVal(failedState.id, false);


            //add event for a demand
            StateCngEvent demandEv;

            demandEv = new StateCngEvent(compName + "_Demand", true, false);


            //add all the start states to the related items
            if (startStates != null)
            {
                demandEv.AddRelatedItems(startStates.Select(c => c.id).ToList());
            }
            allEvents.Add(demandEv);

            //add action to move from off to on or failed for the demand
            TransitionAct demandAct;

            if (failTypes.Contains(EnFailType.ftFailToStart))
            {
                demandAct = new TransitionAct("Goto_" + compName + "_ActOrFail");
                //setup the probability of failure on demand
                demandAct.AddToState(failedState, demandFailure);
                demandAct.AddToState(activeState);
            }
            else
            {
                demandAct = new TransitionAct("Goto_" + compName + "_Active");
                //add the action to move to the active state automatically on start request when no fails to start.
                demandAct.AddToState(activeState);
            }
            allActions.Add(demandAct);
            //add the events and movement to the standbyState
            standbyState.AddEvent(demandEv, true, demandAct);

            //add event for a shutoff request
            StateCngEvent offEv = new StateCngEvent(compName + "_Stop", false);

            if (turnOffStates != null)
            {
                offEv.AddRelatedItems(turnOffStates.Select(c => c.id).ToList());
            }
            allEvents.Add(offEv);

            //add action to move from off to on or failed for the demand
            TransitionAct offAct = new TransitionAct("Goto_" + compName + "_Off");

            offAct.AddToState(standbyState);
            allActions.Add(offAct);

            //add the Events and movement back to the standby state
            activeState.AddEvent(offEv, true, offAct);
            failedState.AddEvent(offEv, true, offAct);


            if (failTypes.Contains(EnFailType.ftFailToRun) || (sim3DComp != "")) //add the fail to run links
            {
                //add transition leading from active to failed for the event above
                TransitionAct failAct = new TransitionAct("Goto_" + compName + "_Failed");
                failAct.AddToState(failedState);
                allActions.Add(failAct);

                if (failTypes.Contains(EnFailType.ftFailToRun))
                {
                    //add event leading from active to failed
                    FailProbEvent failEv = new FailProbEvent(compName + "_FR", runFailure, Globals.HourTimeSpan, Globals.DayTimeSpan);
                    allEvents.Add(failEv);

                    activeState.AddEvent(failEv, true, failAct);
                }

                if (sim3DComp != "")
                {
                    Sim3DVariable simVar     = (Sim3DVariable)this.allVariables.FindByName(sim3DComp);
                    EvalVarEvent  sim3DEvent = new EvalVarEvent(sim3DComp, "return true;", null, simVar);
                    //sim3DEvent.AddRelatedItem(simVar.id);
                    allEvents.Add(sim3DEvent);
                    activeState.AddEvent(sim3DEvent, true, failAct);
                }
            }
        }