コード例 #1
0
ファイル: PrtBaseClasses.cs プロジェクト: linpelvis/P
 public PrtEventNode(PrtValue e, PrtValue payload, string senderMachineName, string senderMachineStateName)
 {
     ev  = e;
     arg = payload.Clone();
     this.senderMachineName      = senderMachineName;
     this.senderMachineStateName = senderMachineStateName;
 }
コード例 #2
0
 public static PrtValue PrtCastValue(PrtValue value, PrtType type)
 {
     if (!PrtInhabitsType(value, type))
     {
         throw new PrtInhabitsTypeException(String.Format("value {0} is not a member of type {1}", value.ToString(), type.ToString()));
     }
     return(value.Clone());
 }
コード例 #3
0
        public void Update(PrtValue key, PrtValue val)
        {
            if (!Contains(key))
            {
                //TODO: raise an exception for invalid update
            }
            var index = keys.FindIndex((k => k.Equals(key)));

            values[index] = val.Clone();
        }
コード例 #4
0
 public void Add(PrtValue key, PrtValue val)
 {
     if (Contains(key))
     {
         //TODO: raise an exception for invalid add
     }
     else
     {
         keys.Add(key.Clone());
         values.Add(val.Clone());
     }
 }
コード例 #5
0
ファイル: PrtValue.cs プロジェクト: linpelvis/P
 public static PrtValue PrtCastValue(PrtValue value, PrtType type)
 {
     //cast for interface types is implemented as reduce.
     if (type is PrtInterfaceType)
     {
         return((type as PrtInterfaceType).PrtReduceValue(value));
     }
     else
     {
         if (!PrtInhabitsType(value, type))
         {
             throw new PrtInhabitsTypeException(String.Format("value {0} is not a member of type {1}", value.ToString(), type.ToString()));
         }
         return(value.Clone());
     }
 }
コード例 #6
0
ファイル: PrtType.cs プロジェクト: tvrprasad/P
        public PrtValue PrtReduceValue(PrtValue value)
        {
            if (value == PrtValue.@null)
            {
                return(value.Clone());
            }
            else if (value is PrtMachineValue)
            {
                return(new PrtInterfaceValue(((PrtMachineValue)value).mach, permissions));
            }
            else if (value is PrtInterfaceValue)
            {
                if (permissions == null)
                {
                    throw new PrtInternalException("Unexpected value of permissions in Interface Type");
                }


                var iVal = value as PrtInterfaceValue;

                //permissions in iVal is all events
                if (iVal.permissions == null)
                {
                    return(new PrtInterfaceValue(iVal.mach, permissions));
                }


                //type_permissions is subset of value_permissions
                if (permissions.Where(ev => !iVal.permissions.Contains(ev)).Count() > 0)
                {
                    throw new PrtInhabitsTypeException(String.Format("value {0} cannot be reduced to value of type {1}", value.ToString(), this.ToString()));
                }
                else
                {
                    return(new PrtInterfaceValue(iVal.mach, permissions));
                }
            }
            else
            {
                throw new PrtInhabitsTypeException(String.Format("value {0} cannot be reduced to value of type {1}", value.ToString(), this.ToString()));
            }
        }
コード例 #7
0
ファイル: PrtBaseClasses.cs プロジェクト: vincentjzimmer/P
 public PrtEventNode(PrtValue e, PrtValue payload)
 {
     ev  = e;
     arg = payload.Clone();
 }
コード例 #8
0
ファイル: PrtSpecMachine.cs プロジェクト: tvrprasad/P
        public override void PrtEnqueueEvent(PrtValue e, PrtValue arg, PrtMachine source, PrtMachineValue target = null)
        {
            int numOfStepsTaken = 0;

            // set the currentTrigger and currentPayload fields
            // so that we can reuse the common functions
            currentPayload = arg.Clone();
            currentTrigger = e;

            PrtValue currEventValue;
            PrtFun   currAction;
            bool     hasMoreWork = false;

            try
            {
Start:
                switch (nextSMOperation)
                {
                case PrtNextStatemachineOperation.ExecuteFunctionOperation:
                    goto DoExecuteFunction;

                case PrtNextStatemachineOperation.HandleEventOperation:
                    goto DoHandleEvent;
                }

DoExecuteFunction:
                if (invertedFunStack.TopOfStack == null)
                {
                    //Trace: entered state
                    PrtFun entryFun = CurrentState.entryFun;
                    if (entryFun.IsAnonFun)
                    {
                        PrtPushFunStackFrame(entryFun, entryFun.CreateLocals(currentPayload));
                    }
                    else
                    {
                        PrtPushFunStackFrame(entryFun, entryFun.CreateLocals());
                    }
                }
                //invoke the function
                invertedFunStack.TopOfStack.fun.Execute(stateImpl, this);
                goto CheckFunLastOperation;

DoAction:
                currAction = PrtFindActionHandler(eventValue);
                if (currAction == PrtFun.IgnoreFun)
                {
                    //Trace: Performed ignore action for the event
                    currentTrigger = PrtValue.@null;
                    currentPayload = PrtValue.@null;
                }
                else
                {
                    if (invertedFunStack.TopOfStack == null)
                    {
                        //Trace: executed the action handler for event
                        if (currAction.IsAnonFun)
                        {
                            PrtPushFunStackFrame(currAction, currAction.CreateLocals(currentPayload));
                        }
                        else
                        {
                            PrtPushFunStackFrame(currAction, currAction.CreateLocals());
                        }
                    }
                    //invoke the action handler
                    invertedFunStack.TopOfStack.fun.Execute(stateImpl, this);
                }
                goto CheckFunLastOperation;

CheckFunLastOperation:
                switch (continuation.reason)
                {
                case PrtContinuationReason.Goto:
                {
                    stateExitReason = PrtStateExitReason.OnGotoStatement;
                    nextSMOperation = PrtNextStatemachineOperation.ExecuteFunctionOperation;
                    PrtPushExitFunction();
                    goto DoExecuteFunction;
                }

                case PrtContinuationReason.Raise:
                {
                    nextSMOperation = PrtNextStatemachineOperation.HandleEventOperation;
                    hasMoreWork     = true;
                    goto Finish;
                }

                case PrtContinuationReason.Return:
                {
                    switch (stateExitReason)
                    {
                    case PrtStateExitReason.NotExit:
                    {
                        nextSMOperation = PrtNextStatemachineOperation.HandleEventOperation;
                        hasMoreWork     = false;
                        goto Finish;
                    }

                    case PrtStateExitReason.OnGotoStatement:
                    {
                        PrtChangeState(destOfGoto);
                        currentTemperature = destOfGoto.temperature;
                        nextSMOperation    = PrtNextStatemachineOperation.ExecuteFunctionOperation;
                        stateExitReason    = PrtStateExitReason.NotExit;
                        hasMoreWork        = true;
                        goto Finish;
                    }

                    case PrtStateExitReason.OnUnhandledEvent:
                    {
                        hasMoreWork     = !PrtPopState(false);
                        nextSMOperation = PrtNextStatemachineOperation.HandleEventOperation;
                        stateExitReason = PrtStateExitReason.NotExit;
                        goto Finish;
                    }

                    case PrtStateExitReason.OnTransition:
                    {
                        stateExitReason = PrtStateExitReason.OnTransitionAfterExit;
                        nextSMOperation = PrtNextStatemachineOperation.ExecuteFunctionOperation;
                        PrtPushTransitionFun(eventValue);
                        goto DoExecuteFunction;
                    }

                    case PrtStateExitReason.OnTransitionAfterExit:
                    {
                        // The parameter to an anonymous transition function is always passed as swap.
                        // Update currentPayload to the latest value of the parameter so that the correct
                        // value gets passed to the entry function of the target state.
                        PrtTransition transition    = CurrentState.transitions[eventValue];
                        PrtFun        transitionFun = transition.transitionFun;
                        if (transitionFun.IsAnonFun)
                        {
                            currentPayload = continuation.retLocals[0];
                        }
                        PrtChangeState(transition.gotoState);
                        currentTemperature = transition.gotoState.temperature;
                        hasMoreWork        = true;
                        nextSMOperation    = PrtNextStatemachineOperation.ExecuteFunctionOperation;
                        stateExitReason    = PrtStateExitReason.NotExit;
                        goto Finish;
                    }

                    default:
                    {
                        Debug.Assert(false, "Unexpected value for exit reason");
                        goto Finish;
                    }
                    }
                }

                default:
                {
                    Debug.Assert(false, "Unexpected value for continuation reason");
                    goto Finish;
                }
                }

DoHandleEvent:
                if (!currentTrigger.Equals(PrtValue.@null))
                {
                    currEventValue = currentTrigger;
                    currentTrigger = PrtValue.@null;
                }
                else
                {
                    currEventValue = eventValue;
                }

                if (PrtIsTransitionPresent(currEventValue))
                {
                    stateExitReason = PrtStateExitReason.OnTransition;
                    nextSMOperation = PrtNextStatemachineOperation.ExecuteFunctionOperation;
                    eventValue      = currEventValue;
                    PrtPushExitFunction();
                    goto DoExecuteFunction;
                }
                else if (PrtIsActionInstalled(currEventValue))
                {
                    eventValue = currEventValue;
                    goto DoAction;
                }
                else
                {
                    stateExitReason = PrtStateExitReason.OnUnhandledEvent;
                    nextSMOperation = PrtNextStatemachineOperation.ExecuteFunctionOperation;
                    eventValue      = currEventValue;
                    PrtPushExitFunction();
                    goto DoExecuteFunction;
                }

Finish:
                if (hasMoreWork)
                {
                    if (numOfStepsTaken > 100000)
                    {
                        throw new PrtInfiniteRaiseLoop("Infinite loop in spec machine");
                    }
                    else
                    {
                        numOfStepsTaken++;
                        goto Start;
                    }
                }
                else
                {
                    return;
                }
            }
            catch (PrtException ex)
            {
                stateImpl.Exception = ex;
            }
        }
コード例 #9
0
        public override void PrtEnqueueEvent(PrtValue e, PrtValue arg, PrtMachine source)
        {
            int numOfStepsTaken = 0;

            // set the currentTrigger and currentPayload fields
            // so that we can reuse the common functions
            currentPayload = arg.Clone();
            currentTrigger = e;

            PrtValue currEventValue;
            PrtFun   currAction;
            bool     hasMoreWork = false;

            try
            {
Start:
                switch (nextSMOperation)
                {
                case PrtNextStatemachineOperation.EntryOperation:
                    goto DoEntry;

                case PrtNextStatemachineOperation.HandleEventOperation:
                    goto DoHandleEvent;
                }

DoEntry:
                if (invertedFunStack.TopOfStack == null)
                {
                    //Trace: entered state
                    if (CurrentState.entryFun.IsAnonFun)
                    {
                        PrtPushFunStackFrame(CurrentState.entryFun, CurrentState.entryFun.CreateLocals(currentPayload));
                    }
                    else
                    {
                        PrtPushFunStackFrame(CurrentState.entryFun, CurrentState.entryFun.CreateLocals());
                    }
                }
                //invoke the function
                invertedFunStack.TopOfStack.fun.Execute(stateImpl, this);
                goto CheckFunLastOperation;

DoAction:
                currAction = PrtFindActionHandler(eventValue);
                if (currAction == PrtCommonFunctions.IgnoreFun)
                {
                    //Trace: Performed ignore action for the event
                    currentTrigger = PrtValue.NullValue;
                    currentPayload = PrtValue.NullValue;
                }
                else
                {
                    if (invertedFunStack.TopOfStack == null)
                    {
                        //Trace: executed the action handler for event
                        PrtPushFunStackFrame(currAction, currAction.CreateLocals(currentPayload));
                    }
                    //invoke the action handler
                    invertedFunStack.TopOfStack.fun.Execute(stateImpl, this);
                }
                goto CheckFunLastOperation;

CheckFunLastOperation:
                switch (continuation.reason)
                {
                case PrtContinuationReason.Goto:
                {
                    stateExitReason = PrtStateExitReason.OnGotoStatement;
                    PrtExecuteExitFunction();
                    goto CheckFunLastOperation;
                }

                case PrtContinuationReason.Raise:
                {
                    nextSMOperation = PrtNextStatemachineOperation.HandleEventOperation;
                    hasMoreWork     = true;
                    goto Finish;
                }

                case PrtContinuationReason.Return:
                {
                    switch (stateExitReason)
                    {
                    case PrtStateExitReason.NotExit:
                    {
                        nextSMOperation = PrtNextStatemachineOperation.HandleEventOperation;
                        hasMoreWork     = false;
                        goto Finish;
                    }

                    case PrtStateExitReason.OnGotoStatement:
                    {
                        PrtChangeState(destOfGoto);
                        nextSMOperation = PrtNextStatemachineOperation.EntryOperation;
                        stateExitReason = PrtStateExitReason.NotExit;
                        hasMoreWork     = true;
                        goto Finish;
                    }

                    case PrtStateExitReason.OnUnhandledEvent:
                    {
                        hasMoreWork     = !PrtPopState(false);
                        nextSMOperation = PrtNextStatemachineOperation.HandleEventOperation;
                        stateExitReason = PrtStateExitReason.NotExit;
                        goto Finish;
                    }

                    case PrtStateExitReason.OnTransition:
                    {
                        stateExitReason = PrtStateExitReason.OnTransitionAfterExit;
                        PrtExecuteTransitionFun(eventValue);
                        goto CheckFunLastOperation;
                    }

                    case PrtStateExitReason.OnTransitionAfterExit:
                    {
                        PrtChangeState(CurrentState.transitions[eventValue].gotoState);
                        hasMoreWork     = true;
                        nextSMOperation = PrtNextStatemachineOperation.EntryOperation;
                        stateExitReason = PrtStateExitReason.NotExit;
                        goto Finish;
                    }

                    default:
                    {
                        Debug.Assert(false, "Unexpected value for exit reason");
                        goto Finish;
                    }
                    }
                }

                default:
                {
                    Debug.Assert(false, "Unexpected value for continuation reason");
                    goto Finish;
                }
                }

DoHandleEvent:
                if (!currentTrigger.IsEqual(PrtValue.NullValue))
                {
                    currEventValue = currentTrigger;
                    currentTrigger = PrtValue.NullValue;
                }
                else
                {
                    currEventValue = eventValue;
                }

                if (PrtIsTransitionPresent(currEventValue))
                {
                    stateExitReason = PrtStateExitReason.OnTransition;
                    eventValue      = currEventValue;
                    PrtExecuteExitFunction();
                    goto CheckFunLastOperation;
                }
                else if (PrtIsActionInstalled(currEventValue))
                {
                    goto DoAction;
                }
                else
                {
                    stateExitReason = PrtStateExitReason.OnUnhandledEvent;
                    eventValue      = currEventValue;
                    PrtExecuteExitFunction();
                    goto CheckFunLastOperation;
                }

Finish:
                if (hasMoreWork)
                {
                    if (numOfStepsTaken > 100000)
                    {
                        throw new PrtInfiniteRaiseLoop("Infinite loop in monitor");
                    }
                    else
                    {
                        numOfStepsTaken++;
                        goto Start;
                    }
                }
                else
                {
                    return;
                }
            }
            catch (PrtException ex)
            {
                stateImpl.Exception = ex;
            }
        }
コード例 #10
0
 public void Insert(int index, PrtValue val)
 {
     //TODO: raise an exception for invalid index
     elements.Insert(index, val.Clone());
 }