Example #1
0
        public override bool Connect(Port port)
        {
            if (!port.IsStatePort)
            {
                return(false);
            }
            if (object.ReferenceEquals(port, this))
            {
                return(false);
            }
            if (port.IsStateRootPort)
            {
                return(port.Connect(this));
            }

            StatePort source = this;
            StatePort target = (StatePort)port;

            Connection existingTransition = connections.Find(t => object.ReferenceEquals(t.SourcePort, source) && object.ReferenceEquals(t.TargetPort, target));

            if (existingTransition != null)
            {
                existingTransition.Disconnect();
                return(true);
            }

            StateMachineChip targetRoot;
            StateMachineChip sourceRoot;

            {
                if (source.IsStateRootPort)
                {
                    sourceRoot = (StateMachineChip)source.Node;
                }
                else
                {
                    sourceRoot = ((StateChip)source.Node).StateMachine;
                }
                targetRoot = ((StateChip)target.Node).StateMachine;
            }

            if (sourceRoot != null && targetRoot != null && !object.ReferenceEquals(sourceRoot, targetRoot))
            {
                return(false);
            }

            if (source.IsStateRootPort)
            {
                source.DisconnectAll();
            }

            StateMachineTransition transition = new StateMachineTransition(source, target);

            connections.Add(transition);
            port.connections.Add(transition);
            EmitConnected(transition);
            return(true);
        }
Example #2
0
        protected CircuitNode(CircuitManager manager, int inputCount, int outputCount, bool hasReset, Port.PortType statePortType = StatePort.PortType.None)
        {
            this.inputPortCount  = inputCount;
            this.outputPortCount = outputCount;
            this.hasReset        = hasReset;
            this.settings        = CreateSettings();
            int totalInputCount = inputCount + (hasReset ? 1 : 0);

            inputPorts = new InputPort[totalInputCount];
            for (int i = 0; i < inputCount; i++)
            {
                inputPorts[i] = new InputPort(this, false);
                inputPorts[i].UnconnectedValue = DefaultInputValue(i);
                inputPorts[i].ValueChanged    += CircuitNode_ValueChanged;
                inputPorts[i].Connected       += CircuitNode_Connected;
                inputPorts[i].Disconnected    += CircuitNode_Disconnected;
            }
            if (hasReset)
            {
                inputPorts[inputCount] = new InputPort(this, true);
                inputPorts[inputCount].UnconnectedValue = false;
                inputPorts[inputCount].ValueChanged    += CircuitNode_ValueChanged;
                inputPorts[inputCount].Connected       += CircuitNode_Connected;
                inputPorts[inputCount].Disconnected    += CircuitNode_Disconnected;
            }

            int totalOutputCount = outputCount + (hasReset ? 1 : 0);

            outputPorts = new OutputPort[totalOutputCount];
            for (int i = 0; i < outputCount; i++)
            {
                outputPorts[i] = new OutputPort(this, false);
                outputPorts[i].expectedType  = ExpectedOutputType(i);
                outputPorts[i].Connected    += CircuitNode_Connected;
                outputPorts[i].Disconnected += CircuitNode_Disconnected;
            }
            if (hasReset)
            {
                outputPorts[outputCount]               = new OutputPort(this, true);
                outputPorts[outputCount].Connected    += CircuitNode_Connected;
                outputPorts[outputCount].Disconnected += CircuitNode_Disconnected;
            }

            if ((statePortType & Port.PortType.StatePort) != 0)
            {
                statePort = new StatePort(this, (statePortType & Port.PortType.StateRoot) == Port.PortType.StateRoot);
            }

            Manager = manager;
        }
Example #3
0
        public StateChip NextStateAfterValidTransition()
        {
            StatePort port = activeState.statePort;

            foreach (StateMachineTransition transition in port.connections.Where(t => object.ReferenceEquals(t.SourcePort, port)).OrderBy(t => t.TargetPort.Node))
            {
                DebugUtils.Assert(transition.TargetStatePort != null);
                if (ValueToBool(transition.TransitionEnabledPort.GetValue()))
                {
                    return((StateChip)transition.TargetStatePort.Node);
                }
            }
            return(null);
        }
Example #4
0
        private IEnumerable <StateChip> FindConnectedStates(StatePort port, HashSet <StatePort> visited)
        {
            if (visited.Contains(port))
            {
                yield break;
            }
            DebugUtils.Assert(!port.IsStateRootPort);
            visited.Add(port);

            yield return((StateChip)port.Node);

            foreach (StateMachineTransition connection in port.connections)
            {
                StatePort otherPort = connection.GetOtherPort(port);
                DebugUtils.Assert(otherPort != null);
                foreach (StateChip state in FindConnectedStates(otherPort, visited))
                {
                    yield return(state);
                }
            }
        }
Example #5
0
        private StateMachineChip FindConnectedRoot(StatePort port, HashSet <StatePort> visited)
        {
            if (port.IsStateRootPort)
            {
                return((StateMachineChip)port.Node);
            }
            visited.Add(port);

            foreach (StateMachineTransition connection in port.connections)
            {
                StatePort otherPort = connection.GetOtherPort(port);
                if (otherPort != null && otherPort.IsStatePort && !visited.Contains(otherPort))
                {
                    StateMachineChip root = FindConnectedRoot(otherPort, visited);
                    if (root != null)
                    {
                        return(root);
                    }
                }
            }
            return(null);
        }
Example #6
0
 public virtual void Destroy()
 {
     if (RingEvaluationPriority < 0)
     {
         return;
     }
     foreach (OutputPort port in outputPorts)
     {
         port.Destroy();
     }
     foreach (InputPort port in inputPorts)
     {
         port.Destroy();
     }
     if (statePort != null)
     {
         statePort.Destroy();
         statePort = null;
     }
     if (manager != null)
     {
         manager.RemoveNode(this);
     }
 }