Beispiel #1
0
        public void Announce(PrtEventValue ev, PrtValue payload, PrtMachine parent)
        {
            if (ev.Equals(PrtValue.@null))
            {
                throw new PrtIllegalEnqueueException("Enqueued event must not be null");
            }

            PrtType prtType = ev.evt.payloadType;

            //assertion to check if argument passed inhabits the payload type.
            if (prtType is PrtNullType)
            {
                if (!payload.Equals(PrtValue.@null))
                {
                    throw new PrtIllegalEnqueueException("Did not expect a payload value");
                }
            }
            else if (!PrtValue.PrtInhabitsType(payload, prtType))
            {
                throw new PrtInhabitsTypeException(String.Format("Payload <{0}> does not match the expected type <{1}> with event <{2}>", payload.ToString(), prtType.ToString(), ev.evt.name));
            }

            var allSpecMachines = GetSpecMachines(parent.renamedName);

            foreach (var mon in allSpecMachines)
            {
                if (mon.observes.Contains(ev))
                {
                    TraceLine("<AnnounceLog> Enqueued Event <{0}, {1}> to Spec Machine {2}", ev, payload, mon.Name);
                    mon.PrtEnqueueEvent(ev, payload, parent);
                }
            }
        }
Beispiel #2
0
 public PrtEvent(string name, PrtType payload, int mInstances, bool doAssume)
 {
     this.name         = name;
     this.payloadType  = payload;
     this.maxInstances = mInstances;
     this.doAssume     = doAssume;
 }
Beispiel #3
0
 public static PrtValue PrtConvertValue(PrtValue value, PrtType type)
 {
     //cast for interface types is implemented as reduce.
     if (type is PrtInterfaceType)
     {
         return((type as PrtInterfaceType).PrtReduceValue(value));
     }
     else if (type is PrtIntType)
     {
         if (value is PrtIntValue)
         {
             return(new PrtIntValue((value as PrtIntValue).nt));
         }
         else
         {
             return(new PrtIntValue((Int64)(value as PrtFloatValue).ft));
         }
     }
     else if (type is PrtFloatType)
     {
         if (value is PrtIntValue)
         {
             return(new PrtFloatValue((value as PrtIntValue).nt));
         }
         else
         {
             return(new PrtFloatValue((Int64)(value as PrtFloatValue).ft));
         }
     }
     else
     {
         throw new PrtInternalException("unexpected type in convert operation");
     }
 }
Beispiel #4
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());
 }
Beispiel #5
0
        public PrtNamedTupleValue(PrtType type, params PrtValue[] elems) : base(elems)
        {
            var tupType = type as PrtNamedTupleType;

            fieldNames = new List <string>(tupType.fieldTypes.Count);
            foreach (var fn in tupType.fieldNames)
            {
                fieldNames.Add(fn);
            }
        }
Beispiel #6
0
        public PrtTupleValue(PrtType type)
        {
            var tupType = type as PrtTupleType;

            fieldValues = new List <PrtValue>(tupType.fieldTypes.Count);
            foreach (var ft in tupType.fieldTypes)
            {
                fieldValues.Add(PrtMkDefaultValue(ft));
            }
        }
Beispiel #7
0
 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());
     }
 }
Beispiel #8
0
 public static PrtValue PrtMkDefaultValue(PrtType type)
 {
     if (type is PrtAnyType || type is PrtNullType || type is PrtEventType || type is PrtMachineType || type is PrtInterfaceType)
     {
         return(@null);
     }
     else if (type is PrtIntType)
     {
         return(new PrtIntValue());
     }
     else if (type is PrtFloatType)
     {
         return(new PrtFloatValue());
     }
     else if (type is PrtEnumType)
     {
         PrtEnumType enumType = type as PrtEnumType;
         return(new PrtEnumValue(enumType.DefaultConstant, 0));
     }
     else if (type is PrtBoolType)
     {
         return(new PrtBoolValue());
     }
     else if (type is PrtMapType)
     {
         return(new PrtMapValue());
     }
     else if (type is PrtSeqType)
     {
         return(new PrtSeqValue());
     }
     else if (type is PrtNamedTupleType)
     {
         return(new PrtNamedTupleValue(type as PrtNamedTupleType));
     }
     else if (type is PrtTupleType)
     {
         return(new PrtTupleValue(type as PrtTupleType));
     }
     else
     {
         throw new PrtInternalException("Invalid type in PrtMkDefaultType");
     }
 }
Beispiel #9
0
        public static PrtValue PrtMkDefaultValue(PrtType type)
        {
            if (type is PrtAnyType || type is PrtNullType || type is PrtEventType || type is PrtMachineType)
            {
                return(new PrtNullValue());
            }

            else if (type is PrtIntType)
            {
                return(new PrtIntValue());
            }
            else if (type is PrtBoolType)
            {
                return(new PrtBoolValue());
            }
            else if (type is PrtMapType)
            {
                return(new PrtMapValue());
            }
            else if (type is PrtSeqType)
            {
                return(new PrtSeqValue());
            }
            else if (type is PrtNamedTupleType)
            {
                return(new PrtNamedTupleValue(type as PrtNamedTupleType));
            }
            else if (type is PrtTupleType)
            {
                return(new PrtTupleValue(type as PrtTupleType));
            }
            else
            {
                Debug.Assert(false, "Invalid type !!");
                return(null);
            }
        }
Beispiel #10
0
        public override void PrtEnqueueEvent(PrtValue e, PrtValue arg, PrtMachine source, PrtMachineValue target = null)
        {
            PrtEventValue ev = e as PrtEventValue;

            if (ev.evt.name == "null")
            {
                throw new PrtIllegalEnqueueException("Enqueued event must not be null");
            }

            //check if the sent event is in source send set
            if (!source.sends.Contains(e as PrtEventValue))
            {
                throw new PrtIllegalEnqueueException(String.Format("Machine <0> cannot send event {1}", source.Name, e.ToString()));
            }

            //check if the sent event is in target permissions
            if (target is PrtInterfaceValue)
            {
                if (!(target as PrtInterfaceValue).permissions.Contains(e as PrtEventValue))
                {
                    throw new PrtIllegalEnqueueException(String.Format("Event {1} is not in the permission set of the target", e.ToString()));
                }
            }

            PrtType prtType = ev.evt.payloadType;

            //assertion to check if argument passed inhabits the payload type.
            if (prtType is PrtNullType)
            {
                if (!arg.Equals(PrtValue.@null))
                {
                    throw new PrtIllegalEnqueueException("Did not expect a payload value");
                }
            }
            else if (!PrtValue.PrtInhabitsType(arg, prtType))
            {
                throw new PrtInhabitsTypeException(String.Format("Payload <{0}> does not match the expected type <{1}> with event <{2}>", arg.ToString(), prtType.ToString(), ev.evt.name));
            }

            //check if the event sent is in the permissions

            if (currentStatus == PrtMachineStatus.Halted)
            {
                stateImpl.Trace(
                    @"<EnqueueLog> {0}-{1} Machine has been halted and Event {2} is dropped",
                    this.Name, this.instanceNumber, ev.evt.name);
            }
            else
            {
                stateImpl.Trace(
                    @"<EnqueueLog> Enqueued Event <{0}, {1}> in {2}-{3} by {4}-{5}",
                    ev.evt.name, arg.ToString(), this.Name, this.instanceNumber, source.Name, source.instanceNumber);
                this.eventQueue.EnqueueEvent(e, arg);
                if (this.maxBufferSize != DefaultMaxBufferSize && this.eventQueue.Size() > this.maxBufferSize)
                {
                    if (this.doAssume)
                    {
                        throw new PrtAssumeFailureException();
                    }
                    else
                    {
                        throw new PrtMaxBufferSizeExceededException(
                                  String.Format(@"<EXCEPTION> Event Buffer Size Exceeded {0} in Machine {1}-{2}",
                                                this.maxBufferSize, this.Name, this.instanceNumber));
                    }
                }
                if (currentStatus == PrtMachineStatus.Blocked && this.eventQueue.IsEnabled(this))
                {
                    currentStatus = PrtMachineStatus.Enabled;
                }
            }

            //Announce it to all the spec machines
            stateImpl.Announce(e as PrtEventValue, arg, source);
        }
Beispiel #11
0
        //public abstract bool Equals(PrtValue val);

        public static bool PrtInhabitsType(PrtValue value, PrtType type)
        {
            if (type is PrtAnyType)
            {
                return(true);
            }
            else if (value.Equals(@null))
            {
                return(type is PrtNullType || type is PrtEventType || type is PrtMachineType);
            }
            else if (type is PrtEnumType)
            {
                PrtEnumType enumType = type as PrtEnumType;
                PrtIntValue intValue = value as PrtIntValue;
                if (intValue == null)
                {
                    return(false);
                }
                return(enumType.enumConstants.ContainsKey(intValue.nt));
            }
            else if (type is PrtIntType)
            {
                return(value is PrtIntValue);
            }
            else if (type is PrtFloatType)
            {
                return(value is PrtFloatValue);
            }
            else if (type is PrtBoolType)
            {
                return(value is PrtBoolValue);
            }
            else if (type is PrtEventType)
            {
                return(value is PrtEventValue);
            }
            else if (type is PrtMachineType)
            {
                return(value is PrtMachineValue || value is PrtInterfaceValue);
            }
            else if (type is PrtInterfaceType)
            {
                var interValue = value as PrtInterfaceValue;
                if (interValue == null)
                {
                    return(false);
                }
                else
                {
                    if (interValue.permissions == null)
                    {
                        return((type as PrtInterfaceType).permissions == null);
                    }
                    else
                    {
                        if ((type as PrtInterfaceType).permissions == null)
                        {
                            return(false);
                        }
                    }

                    if (interValue.permissions.Count() != (type as PrtInterfaceType).permissions.Count())
                    {
                        return(false);
                    }
                    else
                    {
                        foreach (var ev in interValue.permissions)
                        {
                            if (!(type as PrtInterfaceType).permissions.Contains(ev))
                            {
                                return(false);
                            }
                        }
                        return(true);
                    }
                }
            }
            else if (type is PrtNamedTupleType) // must come before PrtTupleType since PrtNamedTupleType derives from PrtTupleType
            {
                var nmtupType = type as PrtNamedTupleType;
                var nmtupVal  = value as PrtNamedTupleValue;
                if (nmtupVal == null)
                {
                    return(false);
                }
                if (nmtupVal.fieldValues.Count != nmtupType.fieldTypes.Count)
                {
                    return(false);
                }
                for (int i = 0; i < nmtupVal.fieldValues.Count; i++)
                {
                    if (nmtupVal.fieldNames[i] != nmtupType.fieldNames[i])
                    {
                        return(false);
                    }
                }
                for (int i = 0; i < nmtupVal.fieldValues.Count; i++)
                {
                    if (!PrtInhabitsType(nmtupVal.fieldValues[i], nmtupType.fieldTypes[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else if (type is PrtTupleType)
            {
                var tupType = type as PrtTupleType;
                var tupVal  = value as PrtTupleValue;
                if (tupVal == null)
                {
                    return(false);
                }
                if (tupVal.fieldValues.Count != tupType.fieldTypes.Count)
                {
                    return(false);
                }
                for (int i = 0; i < tupVal.fieldValues.Count; i++)
                {
                    if (!PrtInhabitsType(tupVal.fieldValues[i], tupType.fieldTypes[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else if (type is PrtMapType)
            {
                var mapType = type as PrtMapType;
                var mapVal  = value as PrtMapValue;
                if (mapVal == null)
                {
                    return(false);
                }
                foreach (var p in mapVal.keyToValueMap)
                {
                    if (!PrtInhabitsType(p.Key.key, mapType.keyType))
                    {
                        return(false);
                    }
                    if (!PrtInhabitsType(p.Value, mapType.valType))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else if (type is PrtSeqType)
            {
                var seqType = type as PrtSeqType;
                var seqVal  = value as PrtSeqValue;
                if (seqVal == null)
                {
                    return(false);
                }
                foreach (var elem in seqVal.elements)
                {
                    if (!PrtInhabitsType(elem, seqType.elemType))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else
            {
                throw new PrtInternalException("Unknown type in PrtInhabitsType");
            }
        }
Beispiel #12
0
Datei: PrtType.cs Projekt: up1/P
 public PrtSeqType(PrtType s)
 {
     this.elemType = s;
 }
Beispiel #13
0
Datei: PrtType.cs Projekt: up1/P
 public PrtMapType(PrtType k, PrtType v)
 {
     this.keyType = k;
     this.valType = v;
 }
Beispiel #14
0
        public static bool PrtInhabitsType(PrtValue value, PrtType type)
        {
            if (type is PrtAnyType)
            {
                return(true);
            }

            if (value is PrtNullValue)
            {
                if (type is PrtEventType || type is PrtMachineType)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            if (type is PrtIntType)
            {
                return(value is PrtIntValue);
            }
            else if (type is PrtBoolType)
            {
                return(value is PrtBoolValue);
            }
            else if (type is PrtEventType)
            {
                return(value is PrtEventValue);
            }
            else if (type is PrtMachineType)
            {
                return(value is PrtMachineValue);
            }
            else if (type is PrtTupleType)
            {
                if ((!(value is PrtTupleValue)) || (value as PrtTupleValue).fieldValues.Count != (type as PrtTupleType).fieldTypes.Count)
                {
                    return(false);
                }
                else
                {
                    int index   = 0;
                    var tupVal  = (value as PrtTupleValue);
                    var tupType = (type as PrtTupleType);
                    while (index < tupVal.fieldValues.Count)
                    {
                        if (!PrtInhabitsType(tupVal.fieldValues[index], tupType.fieldTypes[index]))
                        {
                            return(false);
                        }
                        index++;
                    }

                    return(true);
                }
            }
            else if (type is PrtNamedTupleType)
            {
                if ((!(value is PrtNamedTupleValue)) || (value as PrtNamedTupleValue).fieldValues.Count != (type as PrtNamedTupleType).fieldTypes.Count)
                {
                    return(false);
                }
                else
                {
                    int index     = 0;
                    var nmtupVal  = (value as PrtNamedTupleValue);
                    var nmtupType = (type as PrtNamedTupleType);
                    while (index < nmtupVal.fieldValues.Count)
                    {
                        if (nmtupVal.fieldNames[index] != nmtupType.fieldNames[index])
                        {
                            return(false);
                        }
                        else if (!PrtInhabitsType(nmtupVal.fieldValues[index], nmtupType.fieldTypes[index]))
                        {
                            return(false);
                        }
                        index++;
                    }

                    return(true);
                }
            }
            else if (type is PrtMapType)
            {
                if (!(value is PrtMapValue))
                {
                    return(false);
                }
                else
                {
                    var mapVal = (value as PrtMapValue);
                    foreach (var key in mapVal.keys)
                    {
                        if (!PrtInhabitsType(key, (type as PrtMapType).keyType))
                        {
                            return(false);
                        }
                    }
                    foreach (var val in mapVal.values)
                    {
                        if (!PrtInhabitsType(val, (type as PrtMapType).valType))
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }
            else if (type is PrtSeqType)
            {
                if (!(value is PrtSeqValue))
                {
                    return(false);
                }
                else
                {
                    var seqVal = (value as PrtSeqValue);
                    foreach (var elem in seqVal.elements)
                    {
                        if (!PrtInhabitsType(elem, (type as PrtSeqType).elemType))
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }
            else
            {
                Debug.Assert(false, "Unknown type");
                return(false);
            }
        }