Esempio n. 1
0
        public void AutoItemsForPipeFail(string compName,
                                         string desc,
                                         string[] demandNames,
                                         SimVariable[] demandFailVars,
                                         string[] setVarsIf,
                                         string[] setToVals,
                                         string[] startStates,
                                         string[] turnOffStates)
        {
            if ((demandNames.Count() != demandFailVars.Count()) || (demandNames.Count() != setVarsIf.Count()) || (demandNames.Count() != setToVals.Count()))
            {
                throw new Exception("Not equal Demand names and lists");
            }


            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 = 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
            foreach (string sName in startStates)
            {
                State stState = this.allStates.FindByName(sName);
                if (stState != null)
                {
                    demandEv.AddRelatedItem(stState.id);
                }
            }
            allEvents.Add(demandEv);

            TransitionAct toFailedAct = new TransitionAct("_Goto_" + compName + "_Failed");
            NowTimerEvent nowEv       = new NowTimerEvent(compName + "_Failed");

            toFailedAct.AddToState(failedState);
            allActions.Add(toFailedAct);

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

            for (int i = 0; i < demandFailVars.Count(); ++i)
            {
                VarValueAct setVarVal = new VarValueAct("_Set_" + compName + "_Demand_val" + i.ToString(), allVariables.FindByName(setVarsIf[i]),
                                                        setToVals[i], typeof(double), null);
                allActions.Add(setVarVal);

                State tempState = new State(compName + "_Temp" + demandNames[i], EnStateType.stStandard, addComp); //"Joint_3_Temp_SIL1"
                allStates.Add(tempState);
                tempState.AddImmediateAction(setVarVal);
                //tempState.AddImmediateAction(toFailedAct);
                tempState.AddEvent(nowEv, true, toFailedAct);

                addComp.AddEvalVal(tempState.id, false);
                failStates.Add(tempState);

                demandAct.AddToState(tempState, demandFailVars[i], demandNames[i]);
            }

            demandAct.AddToState(activeState, -1, "No joint Failure");
            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);

            foreach (string sName in turnOffStates)
            {
                State endState = this.allStates.FindByName(sName);
                if (endState != null)
                {
                    offEv.AddRelatedItem(endState.id);
                }
            }
            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);
            }
        }
Esempio n. 2
0
        public void AutoItemsForNewComponent(string compName,
                                             string desc,
                                             string[] demandNames,
                                             double[] demandFailProbs,
                                             SimVariable[] demandFailVars,
                                             string[] rateNames,
                                             double[] runningFailRates,
                                             string[] startStates,
                                             string[] turnOffStates,
                                             string sim3DComp      = "",
                                             bool sepFailStates    = false,
                                             EmraldModel refLookup = null) //only have this not null if trying to get the JSON for this.
        {
            if (refLookup == null)
            {
                refLookup = this;
            }

            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.stStandard, 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
            TransitionAct gotoDemandEv = new TransitionAct("Start_" + compName + "_Demand");

            gotoDemandEv.AddToState(standbyState);
            allActions.Add(gotoDemandEv);
            //add all the start states to the related items
            foreach (string sName in startStates)
            {
                State stState = refLookup.allStates.FindByName(sName);
                if (stState != null)
                {
                    stState.AddImmediateAction(gotoDemandEv);
                }
                else
                {
                    throw new Exception("Failed to find start system State named " + stState);
                }
            }


            //add action to move from off to on or failed for the demand
            TransitionAct demandAct = new TransitionAct("From_" + compName + "_Demand");
            NowTimerEvent demandEv  = new NowTimerEvent(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);
                //standbyState.AddImmediateAction(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);
                //standbyState.AddImmediateAction(demandAct);
            }

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

            foreach (string sName in turnOffStates)
            {
                State endState = refLookup.allStates.FindByName(sName);
                if (endState != null)
                {
                    offEv.AddRelatedItem(endState.id);
                }
            }
            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)refLookup.allVariables.FindByName(sim3DComp);
                    VariableList  varList = new VariableList();
                    varList.Add(simVar);

                    EvalVarEvent sim3DEvent = (EvalVarEvent)allEvents.FindByName(sim3DComp, false);
                    if (sim3DEvent == null)
                    {
                        sim3DEvent = new EvalVarEvent(sim3DComp, "if (" + simVar.name + " > 0) return true; else return false;", varList, simVar);
                        allEvents.Add(sim3DEvent);
                    }
                    activeState.AddEvent(sim3DEvent, true, failAct);
                }
            }
        }