Example #1
0
        /// <summary>
        ///   Initializes a new instance.
        /// </summary>
        /// <param name="serializedData">The serialized data describing the model.</param>
        /// <param name="stateHeaderBytes">
        ///   The number of bytes that should be reserved at the beginning of each state vector for the model checker tool.
        /// </param>
        internal RuntimeModel(SerializedRuntimeModel serializedData, int stateHeaderBytes = 0)
        {
            Requires.That(serializedData.Model != null, "Expected a valid model instance.");

            var buffer         = serializedData.Buffer;
            var rootComponents = serializedData.Model.Roots;
            var objectTable    = serializedData.ObjectTable;
            var formulas       = serializedData.Formulas;

            Requires.NotNull(buffer, nameof(buffer));
            Requires.NotNull(rootComponents, nameof(rootComponents));
            Requires.NotNull(objectTable, nameof(objectTable));
            Requires.NotNull(formulas, nameof(formulas));
            Requires.That(stateHeaderBytes % 4 == 0, nameof(stateHeaderBytes), "Expected a multiple of 4.");

            Model                     = serializedData.Model;
            SerializedModel           = buffer;
            RootComponents            = rootComponents.Cast <Component>().ToArray();
            Faults                    = objectTable.OfType <Fault>().Where(fault => fault.Activation == Activation.Nondeterministic && fault.IsUsed).ToArray();
            ActivationSensitiveFaults = Faults.Where(fault => fault.RequiresActivationNotification).ToArray();
            StateFormulas             = objectTable.OfType <StateFormula>().ToArray();
            Formulas                  = formulas;

            // Create a local object table just for the objects referenced by the model; only these objects
            // have to be serialized and deserialized. The local object table does not contain, for instance,
            // the closure types of the state formulas
            var objects             = Model.ReferencedObjects;
            var deterministicFaults = objectTable.OfType <Fault>().Where(fault => fault.Activation != Activation.Nondeterministic);

            _serializedObjects = new ObjectTable(objects.Except(deterministicFaults, ReferenceEqualityComparer <object> .Default));
            Objects            = objectTable;

            StateVectorLayout = SerializationRegistry.Default.GetStateVectorLayout(Model, _serializedObjects, SerializationMode.Optimized);

            _deserialize      = StateVectorLayout.CreateDeserializer(_serializedObjects);
            _serialize        = StateVectorLayout.CreateSerializer(_serializedObjects);
            _restrictRanges   = StateVectorLayout.CreateRangeRestrictor(_serializedObjects);
            _stateHeaderBytes = stateHeaderBytes;

            PortBinding.BindAll(objectTable);
            _choiceResolver = new ChoiceResolver(objectTable);

            ConstructionState = new byte[StateVectorSize];
            fixed(byte *state = ConstructionState)
            {
                Serialize(state);
                _restrictRanges();
            }

            FaultSet.CheckFaultCount(Faults.Length);
            StateFormulaSet.CheckFormulaCount(StateFormulas.Length);
        }
Example #2
0
        protected void GenerateCode(SerializationMode mode, params object[] objects)
        {
            objects = SerializationRegistry.Default.GetReferencedObjects(objects, mode).ToArray();
            var model = TestModel.InitializeModel(new DummyComponent(objects));

            _objectTable      = new ObjectTable(objects);
            StateVectorLayout = SerializationRegistry.Default.GetStateVectorLayout(model, _objectTable, mode);
            _serializer       = StateVectorLayout.CreateSerializer(_objectTable);
            _deserializer     = StateVectorLayout.CreateDeserializer(_objectTable);
            _rangeRestrictor  = StateVectorLayout.CreateRangeRestrictor(_objectTable);

            StateSlotCount  = StateVectorLayout.SizeInBytes / 4;
            StateVectorSize = StateVectorLayout.SizeInBytes;
            _buffer         = new MemoryBuffer();
            _buffer.Resize(StateVectorSize, zeroMemory: true);
            SerializedState = _buffer.Pointer;

            Output.Log("{0}", StateVectorLayout);
        }
		protected void GenerateCode(SerializationMode mode, params object[] objects)
		{
			objects = SerializationRegistry.Default.GetReferencedObjects(objects, mode).ToArray();
			var model = TestModel.InitializeModel(new DummyComponent(objects));

			_objectTable = new ObjectTable(objects);
			StateVectorLayout = SerializationRegistry.Default.GetStateVectorLayout(model, _objectTable, mode);
			_serializer = StateVectorLayout.CreateSerializer(_objectTable);
			_deserializer = StateVectorLayout.CreateDeserializer(_objectTable);
			_rangeRestrictor = StateVectorLayout.CreateRangeRestrictor(_objectTable);

			StateSlotCount = StateVectorLayout.SizeInBytes / 4;
			StateVectorSize = StateVectorLayout.SizeInBytes;
			_buffer = new MemoryBuffer();
			_buffer.Resize(StateVectorSize, zeroMemory: true);
			SerializedState = _buffer.Pointer;

			Output.Log("{0}", StateVectorLayout);
		}
Example #4
0
        /// <summary>
        ///   Initializes a new instance.
        /// </summary>
        /// <param name="serializedData">The serialized data describing the model.</param>
        /// <param name="stateHeaderBytes">
        ///   The number of bytes that should be reserved at the beginning of each state vector for the model checker tool.
        /// </param>
        internal SafetySharpRuntimeModel(SerializedRuntimeModel serializedData, int stateHeaderBytes = 0) : base(stateHeaderBytes)
        {
            Requires.That(serializedData.Model != null, "Expected a valid model instance.");

            var buffer         = serializedData.Buffer;
            var rootComponents = serializedData.Model.Roots;
            var objectTable    = serializedData.ObjectTable;
            var formulas       = serializedData.Formulas;

            Requires.NotNull(buffer, nameof(buffer));
            Requires.NotNull(rootComponents, nameof(rootComponents));
            Requires.NotNull(objectTable, nameof(objectTable));
            Requires.NotNull(formulas, nameof(formulas));
            Requires.That(stateHeaderBytes % 4 == 0, nameof(stateHeaderBytes), "Expected a multiple of 4.");

            Model                   = serializedData.Model;
            SerializedModel         = buffer;
            RootComponents          = rootComponents.Cast <Component>().ToArray();
            ExecutableStateFormulas = objectTable.OfType <ExecutableStateFormula>().ToArray();
            Formulas                = formulas;
            StateConstraints        = Model.Components.Cast <Component>().SelectMany(component => component.StateConstraints).ToArray();

            // Create a local object table just for the objects referenced by the model; only these objects
            // have to be serialized and deserialized. The local object table does not contain, for instance,
            // the closure types of the state formulas.
            Faults             = objectTable.OfType <Fault>().Where(fault => fault.IsUsed).ToArray();
            _serializedObjects = new ObjectTable(Model.ReferencedObjects);

            Objects           = objectTable;
            StateVectorLayout = SerializationRegistry.Default.GetStateVectorLayout(Model, _serializedObjects, SerializationMode.Optimized);
            UpdateFaultSets();

            _deserialize    = StateVectorLayout.CreateDeserializer(_serializedObjects);
            _serialize      = StateVectorLayout.CreateSerializer(_serializedObjects);
            _restrictRanges = StateVectorLayout.CreateRangeRestrictor(_serializedObjects);

            PortBinding.BindAll(objectTable);

            InitializeConstructionState();
            CheckConsistencyAfterInitialization();
        }
		/// <summary>
		///   Serializes the <paramref name="model" />.
		/// </summary>
		private unsafe void SerializeModel(BinaryWriter writer, ModelBase model, Formula[] formulas)
		{
			// Collect all objects contained in the model
			var objectTable = CreateObjectTable(model, formulas);

			// Prepare the serialization of the model's initial state
			lock (_syncObject)
			{
				_stateVector = SerializationRegistry.Default.GetStateVectorLayout(model, objectTable, SerializationMode.Full);
				_deserializer = null;
			}

			var stateVectorSize = _stateVector.SizeInBytes;
			var serializer = _stateVector.CreateSerializer(objectTable);

			// Serialize the object table
			SerializeObjectTable(objectTable, writer);

			// Serialize the object identifier of the model itself and the formulas
			writer.Write(objectTable.GetObjectIdentifier(model));
			writer.Write(formulas.Length);
			foreach (var formula in formulas)
				writer.Write(objectTable.GetObjectIdentifier(formula));

			// Serialize the initial state
			var serializedState = stackalloc byte[stateVectorSize];
			serializer(serializedState);

			// Copy the serialized state to the stream
			writer.Write(stateVectorSize);
			for (var i = 0; i < stateVectorSize; ++i)
				writer.Write(serializedState[i]);
		}