예제 #1
0
        /// <summary>
        /// Creates a new <see cref="Machine"/> of the specified <see cref="Type"/>.
        /// </summary>
        /// <param name="mid">Unbound machine id</param>
        /// <param name="type">Type of the machine</param>
        /// <param name="friendlyName">Friendly machine name used for logging</param>
        /// <returns>Machine</returns>
        private Machine CreateMachine(MachineId mid, Type type, string friendlyName)
        {
            this.Assert(type.IsSubclassOf(typeof(Machine)), "Type '{0}' is not a machine.", type.Name);

            if (mid == null)
            {
                mid = new MachineId(type, friendlyName, this);
            }
            else
            {
                this.Assert(mid.Runtime == null || mid.Runtime == this, "Unbound machine id '{0}' was created by another runtime.", mid.Value);
                this.Assert(mid.Type == type.FullName, "Cannot bind machine id '{0}' of type '{1}' to a machine of type '{2}'.",
                            mid.Value, mid.Type, type.FullName);
                mid.Bind(this);
            }

            Machine machine = MachineFactory.Create(type);

            machine.Initialize(this, mid, new MachineInfo(mid));
            machine.InitializeStateInformation();

            bool result = this.MachineMap.TryAdd(mid, machine);

            this.Assert(result, "Machine with id '{0}' was already created in generation '{1}'. This typically occurs " +
                        "either if the machine id was created by another runtime instance, or if a machine id from a previous " +
                        "runtime generation was deserialized, but the current runtime has not increased its generation value.",
                        mid.Value, mid.Generation);

            return(machine);
        }
예제 #2
0
        /// <summary>
        /// Creates a new <see cref="Machine"/> of the specified <see cref="Type"/>.
        /// </summary>
        private Machine CreateMachine(MachineId mid, Type type, string machineName, Machine creator, Guid opGroupId)
        {
            if (!type.IsSubclassOf(typeof(Machine)))
            {
                this.Assert(false, "Type '{0}' is not a machine.", type.FullName);
            }

            if (mid is null)
            {
                mid = new MachineId(type, machineName, this);
            }
            else if (mid.Runtime != null && mid.Runtime != this)
            {
                this.Assert(false, "Unbound machine id '{0}' was created by another runtime.", mid.Value);
            }
            else if (mid.Type != type.FullName)
            {
                this.Assert(false, "Cannot bind machine id '{0}' of type '{1}' to a machine of type '{2}'.",
                            mid.Value, mid.Type, type.FullName);
            }
            else
            {
                mid.Bind(this);
            }

            // The operation group id of the machine is set using the following precedence:
            // (1) To the specified machine creation operation group id, if it is non-empty.
            // (2) To the operation group id of the creator machine, if it exists.
            // (3) To the empty operation group id.
            if (opGroupId == Guid.Empty && creator != null)
            {
                opGroupId = creator.OperationGroupId;
            }

            Machine machine = MachineFactory.Create(type);
            IMachineStateManager stateManager = new MachineStateManager(this, machine, opGroupId);
            IEventQueue          eventQueue   = new EventQueue(stateManager);

            machine.Initialize(this, mid, stateManager, eventQueue);
            machine.InitializeStateInformation();

            if (!this.MachineMap.TryAdd(mid, machine))
            {
                string info = "This typically occurs if either the machine id was created by another runtime instance, " +
                              "or if a machine id from a previous runtime generation was deserialized, but the current runtime " +
                              "has not increased its generation value.";
                this.Assert(false, "Machine with id '{0}' was already created in generation '{1}'. {2}", mid.Value, mid.Generation, info);
            }

            return(machine);
        }