Esempio n. 1
0
 public void checkIfEmptyAndNotOutputNode(CircuitNode node)
 {
     if (node.Edges.Count == 0 && node.GetType() != typeof(OutputNode))
     {
         throw new CircuitInvalidException(" there is a dead-end in the circuit it is invalid and the node values may be incorrect");
     }
 }
Esempio n. 2
0
    /// <summary>
    /// 获取导线的连接连接结点
    /// </summary>
    private static List <CircuitNode> GetLinkOtherNode(List <CircuitNodeLine> lLineNode)
    {
        List <CircuitNode> lNode = new List <CircuitNode> ();

        foreach (CircuitNodeLine v in lLineNode)
        {
            if (v == null)
            {
                continue;
            }
            //连接到一个元器件的ID,并需把2个端口都考虑进去
            int         ObjID = v.GetLinkOtherObjID();
            CircuitNode n1    = new CircuitNode(ObjID, ElementLeapType.leadIn);
            if (lNode.Contains(n1) == false)
            {
                lNode.Add(n1);
            }

            CircuitNode n2 = new CircuitNode(ObjID, ElementLeapType.leadOut);
            if (lNode.Contains(n2) == false)
            {
                lNode.Add(n2);
            }
        }
        return(lNode);
    }
Esempio n. 3
0
    /// <summary>
    /// 获取连接的导线,
    /// </summary>
    private static List <CircuitNodeLine> NodeLinkLine(CircuitNode Node)
    {
        List <CircuitNodeLine> lNodeLine = new List <CircuitNodeLine>();

        if (Node == null)
        {
            return(lNodeLine);
        }
        //获取连接的导线
        lNodeLine = Node.GetLinkLineNode();
        //移除用过的导线
        foreach (EleLine v in g_UserLineBFS)
        {
            //删除已经拥有的。
            foreach (CircuitNodeLine vn in lNodeLine)
            {
                if (vn.Line == v)
                {
                    lNodeLine.Remove(vn);
                    break;
                }
            }
        }
        return(lNodeLine);
    }
Esempio n. 4
0
    /// <summary>
    /// 广度优先确认能否联通
    /// </summary>
    public static bool CheckloopConnectBFS(CircuitNode Start, CircuitNode End)
    {
        if (InitBFS(Start, End) == false)
        {
            return(false);
        }
        //1.获取周边导线
        List <CircuitNodeLine> llineNode = NodeLinkLine(Start);

        while (true)
        {
            //判断有没链接导线
            if (llineNode.Count == 0)
            {
                return(false);
            }
            //继续搜索
            List <CircuitNode> lNode = GetLinkOtherNode(llineNode);
            //找到目标,成功
            if (CircuitNode.CheckFindTarget(lNode, End) == true)
            {
                return(true);
            }
            //更新使用过的导线
            UpdateUserLine(ref llineNode);
            //继续搜索周边结点
            llineNode = NodeLinkLine(lNode);
        }
        return(false);
    }
Esempio n. 5
0
        public void NToNInputStrategy_Positive()
        {
            INode f1 = new FakeNode(false);
            INode f2 = new FakeNode(false);
            INode f3 = new FakeNode(false);
            INode f4 = new FakeNode(true);
            INode f5 = new FakeNode(false);
            INode f6 = new FakeNode(false);
            INode f7 = new FakeNode(false);
            INode f8 = new FakeNode(false);

            TestHelper.SetTestPaths();
            CircuitNode node = (CircuitNode) new CircuitNodeFactory().GetNode("testName", "ENCODER");

            node.Inputs.Add(f1);
            node.Inputs.Add(f2);
            node.Inputs.Add(f3);
            node.Inputs.Add(f4);
            node.Inputs.Add(f5);
            node.Inputs.Add(f6);
            node.Inputs.Add(f7);
            node.Inputs.Add(f8);

            NodeProcessContext context = new NodeProcessContext(new NToNInputStrategy());

            bool[] res = context.ProcessInput(node);

            Assert.AreEqual(true, res[0]);
            Assert.AreEqual(true, res[1]);
            Assert.AreEqual(false, res[2]);
        }
Esempio n. 6
0
 public void AddNode(LogicComponent component)
 {
     if (Nodes.ContainsKey(component))
     {
         throw new ArgumentException("Component already exists in circuit");
     }
     Nodes[component] = new CircuitNode(component);
 }
        public bool[] ProcessInput(CircuitNode node)
        {
            node.Circuit.Reset();

            (node.Circuit.InputNodes.First().Value).Value = node.Inputs.First().Process()[0];

            return(new bool[] { node.Circuit.OutputNodes.First().Value.Process()[0] });
        }
Esempio n. 8
0
    //矩阵计算
    public static void calMatrix()
    {
        if (!lu_factor(circuitMatrix, circuitMatrixSize, circuitPermute))
        {
            return;
        }

        Debug.Log("--------LU分解后的矩阵----------");
        for (int i = 0; i < CirSim.circuitMatrixSize; i++)
        {
            string s = i + ": ";
            for (int j = 0; j < CirSim.circuitMatrixSize; j++)
            {
                s += CirSim.circuitMatrix[i, j] + " ";
            }
            s += " " + CirSim.circuitRightSide[i];
            Debug.Log(s);
        }

        //矩阵求解
        lu_solve(circuitMatrix, circuitMatrixSize, circuitPermute, circuitRightSide);

        //为每个结点的电压赋值
        for (int i = 0; i < circuitMatrixFullSize; i++)
        {
            RowInfo ri  = circuitRowInfo[i];            //获取行信息
            float   res = 0;
            if (ri.type == RowInfo.ROW_CONST)
            {
                res = ri.value;
            }
            else
            {
                res = circuitRightSide[ri.mapCol];
            }

            if (double.IsNaN(res))  //若是未知数结束循环
            {
                Debug.Log("res is NaN");
                break;
            }

            if (i < nodeList.Count - 1)
            {
                CircuitNode cn = getCircuitNode(i + 1);                                 //获取结点
                foreach (KeyValuePair <CircuitElm, int> kv in cn.links)                 //遍历结点上的所有端点
                {
                    kv.Key.setNodeVoltage(kv.Value, res);                               //为结点赋予电压
                    Debug.Log(kv.Key.type + "--" + kv.Value + "         " + res);
                }
            }
            else
            {
                int j = i - (nodeList.Count - 1);
                voltageSources[j].setCurrent(j, res);
            }
        }
    }
Esempio n. 9
0
 public CircuitNodeLine(CircuitNode Node, EleLine line)
 {
     if (Node != null)
     {
         this.LabID = Node.LabID;
         this.Type  = Node.Type;
         this.Line  = line;
     }
 }
Esempio n. 10
0
    public void ConnectNodes(CircuitNode otherNode)
    {
        if (IsValidConnection(otherNode))
        {
            // draw line  --> gonna just be pink for rn
            Debug.Log("Shit's valid");

            DrawLine(otherNode);
        }
    }
Esempio n. 11
0
 public RelayCell(uint circuit_id, CellCommand command, CircuitNode node,
                  CellCommand relay_command, ushort stream_id, byte[] relay_payload)
     : base(circuit_id, command)
 {
     // : cell(circuit_id, command)
     _circuit_node     = node;
     _relay_command    = relay_command;
     _stream_id        = stream_id;
     this.RelayPayload = relay_payload;
 }
Esempio n. 12
0
        public void NotNode_1_Positive()
        {
            INode f1 = new FakeNode(true);

            TestHelper.SetTestPaths();
            CircuitNode node = (CircuitNode) new CircuitNodeFactory().GetNode("testName", "NOT");

            node.Inputs.Add(f1);

            Assert.AreEqual(false, node.Process()[0]);
        }
Esempio n. 13
0
        /// <summary>
        /// Helper function for binding nodes to the circuit
        /// </summary>
        /// <param name="ckt"></param>
        /// <param name="extra"></param>
        /// <returns></returns>
        protected CircuitNode[] BindNodes(Circuit ckt)
        {
            // Map connected nodes
            CircuitNode[] nodes = new CircuitNode[terminals.Length];
            for (int i = 0; i < terminals.Length; i++)
            {
                nodes[i] = ckt.Nodes.Map(terminals[i]);
            }

            // Return all nodes
            return(nodes);
        }
Esempio n. 14
0
        public void AllPossibleInputs()
        {
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    bool[] x = { false, false, false, false, false, false, false, false };
                    bool[] y = { false, false, false, false, false, false, false, false };
                    bool[] s = { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };

                    x[i]     = true;
                    y[j]     = true;
                    s[i + j] = true;

                    Cache.IncUserActionCounter();
                    CircuitNode node = GetCalcCircuit();

                    node.Inputs.Add(new FakeNode(x[0]));        // X0
                    node.Inputs.Add(new FakeNode(x[1]));        // X1
                    node.Inputs.Add(new FakeNode(x[2]));        // X2
                    node.Inputs.Add(new FakeNode(x[3]));        // X3
                    node.Inputs.Add(new FakeNode(x[4]));        // X4
                    node.Inputs.Add(new FakeNode(x[5]));        // X5
                    node.Inputs.Add(new FakeNode(x[6]));        // X6
                    node.Inputs.Add(new FakeNode(x[7]));        // X7

                    node.Inputs.Add(new FakeNode(y[0]));        // Y0
                    node.Inputs.Add(new FakeNode(y[1]));        // Y1
                    node.Inputs.Add(new FakeNode(y[2]));        // Y2
                    node.Inputs.Add(new FakeNode(y[3]));        // Y3
                    node.Inputs.Add(new FakeNode(y[4]));        // Y4
                    node.Inputs.Add(new FakeNode(y[5]));        // Y5
                    node.Inputs.Add(new FakeNode(y[6]));        // Y6
                    node.Inputs.Add(new FakeNode(y[7]));        // Y7

                    Assert.AreEqual(s[0], node.Process()[0]);   // S0
                    Assert.AreEqual(s[1], node.Process()[1]);   // S1
                    Assert.AreEqual(s[2], node.Process()[2]);   // S2
                    Assert.AreEqual(s[3], node.Process()[3]);   // S3
                    Assert.AreEqual(s[4], node.Process()[4]);   // S4
                    Assert.AreEqual(s[5], node.Process()[5]);   // S5
                    Assert.AreEqual(s[6], node.Process()[6]);   // S6
                    Assert.AreEqual(s[7], node.Process()[7]);   // S7
                    Assert.AreEqual(s[8], node.Process()[8]);   // S8
                    Assert.AreEqual(s[9], node.Process()[9]);   // S9
                    Assert.AreEqual(s[10], node.Process()[10]); // S10
                    Assert.AreEqual(s[11], node.Process()[11]); // S11
                    Assert.AreEqual(s[12], node.Process()[12]); // S12
                    Assert.AreEqual(s[13], node.Process()[13]); // S13
                    Assert.AreEqual(s[14], node.Process()[14]); // S14
                }
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Helper function for binding nodes to the circuit
        /// </summary>
        /// <param name="ckt"></param>
        /// <param name="extra"></param>
        /// <returns></returns>
        protected CircuitNode[] BindNodes(Circuit ckt)
        {
            // Map connected nodes
            CircuitNode[] nodes = new CircuitNode[connections.Length];
            for (int i = 0; i < connections.Length; i++)
            {
                nodes[i]   = ckt.Nodes.Map(connections[i]);
                indices[i] = nodes[i].Index;
            }

            // Return all nodes
            return(nodes);
        }
Esempio n. 16
0
    public void NodeClicked(CircuitNode node)
    {
        if (ClickNode1 == null)
        {
            ClickNode1 = node;
        }
        else
        {
            node.ConnectNodes(ClickNode1);

            // reset node
            ClickNode1 = null;
        }
    }
Esempio n. 17
0
 /// <summary>
 /// 广度优先确认能否联通
 /// </summary>
 private static bool InitBFS(CircuitNode Start, CircuitNode End)
 {
     if (Start == null || End == null)
     {
         return(false);
     }
     //初始化工作
     if (CircuitNode.Compare(Start, End) == true)
     {
         return(false);
     }
     g_UserLineBFS.Clear();
     return(true);
 }
Esempio n. 18
0
        public void visitRecursively(List <string> path, CircuitNode node)
        {
            path.Add(node.Name);
            foreach (string s in node.Edges)
            {
                var currentNode = circuit.circuitNodes.Find(x => x.Name == s);
                // We have to copy the list or we might get false positives when inputnodes are connected to a AND get from the get-go
                var copyList = new List <string>();
                copyList.AddRange(path);

                checkIfEmptyAndNotOutputNode(currentNode);
                visitRecursively(copyList, currentNode);
            }
        }
Esempio n. 19
0
        public void OneToOneInputStrategy_Positive()
        {
            INode f1 = new FakeNode(false);

            TestHelper.SetTestPaths();
            CircuitNode node = (CircuitNode) new CircuitNodeFactory().GetNode("testName", "NOT");

            node.Inputs.Add(f1);

            NodeProcessContext context = new NodeProcessContext(new OneToOneInputStrategy());

            bool[] res = context.ProcessInput(node);

            Assert.AreEqual(true, res[0]);
        }
Esempio n. 20
0
    /// <summary>
    /// 列表进行合并
    /// </summary>
    public static bool CheckFindTarget(List <CircuitNode> lHave, CircuitNode Target)
    {
        if (lHave == null || Target == null)
        {
            return(false);
        }

        foreach (CircuitNode v in lHave)
        {
            if (Compare(v, Target) == true)
            {
                return(true);
            }
        }
        return(false);
    }
Esempio n. 21
0
        void drawEdges(CircuitNode node)
        {
            foreach (string edgeNode in node.Edges)
            {
                var edge = graph.AddEdge(node.Name, edgeNode);

                if (new string[] { "A", "B", "Cin", "Cout", "S" }.Contains(node.Name))
                {
                    edge.SourceNode.LabelText = node.Name + " \n " + node.value;
                }
                else
                {
                    edge.SourceNode.LabelText = node.Type + " \n " + node.value;
                }
            }
        }
        public void visitRecursively(List <string> path, CircuitNode node)
        {
            if (path.Contains(node.Name))
            {
                throw new InfiniteLoopException("node values are incorrect, board unusable");
            }
            path.Add(node.Name);
            foreach (string s in node.Edges)
            {
                var currentNode = circuit.circuitNodes.Find(x => x.Name == s);
                // We have to copy the list or we might get false positives when inputnodes are connected to a AND get from the get-go
                var copyList = new List <string>();
                copyList.AddRange(path);

                visitRecursively(copyList, currentNode);
            }
        }
Esempio n. 23
0
        public RelayCell(CircuitNode node, Cell cell)
            : base(ref cell)
        {
            _circuit_node = node;
            MemoryStream  payload_stream = new MemoryStream(cell.Payload);
            StreamWrapper payload_buffer = new StreamWrapper(payload_stream, Endianness.big_endian);
            CellCommand   relay_command  = (CellCommand)payload_buffer.ReadByte();
            ushort        dummy          = payload_buffer.ReadUInt16();
            ushort        stream_id      = payload_buffer.ReadUInt16();
            uint          digest         = payload_buffer.ReadUInt32();
            ushort        payload_size   = payload_buffer.ReadUInt16();

            byte[] payload = new byte[payload_size];
            payload_buffer.Read(payload);
            _relay_command    = relay_command;
            _stream_id        = stream_id;
            this.RelayPayload = payload;
        }
Esempio n. 24
0
        public bool[] ProcessInput(CircuitNode node)
        {
            node.Circuit.Reset();

            List <bool> inputValuesList = new List <bool>();

            for (int i = 0; i < node.Inputs.Count; i++)
            {
                if (node.Inputs[i] is CircuitNode)
                {
                    int ret = ((CircuitNode)node.Inputs[i]).RetrievedInputsIncr(node.Name + i);

                    inputValuesList.Add(node.Inputs[i].Process()[ret]);
                }
                else
                {
                    inputValuesList.Add(node.Inputs[i].Process()[0]);
                }
            }
            bool[] inputValues = inputValuesList.ToArray();

            int j = 0;

            foreach (InputNode inputNode in node.Circuit.InputNodes.Values)
            {
                if (inputValues.Length <= j)
                {
                    break;
                }

                inputNode.Value = inputValues[j];
                j++;
            }

            List <bool> outputArray = new List <bool>();

            foreach (OutputNode outputNode in node.Circuit.OutputNodes.Values)
            {
                outputArray.Add(outputNode.Process()[0]);
            }

            return(outputArray.ToArray());
        }
Esempio n. 25
0
    /// <summary>
    /// 比较是否相等
    /// </summary>
    public static bool  Compare(CircuitNode Node1, CircuitNode Node2)
    {
        if (Node1 == null || Node2 == null)
        {
            return(false);
        }

        if (Node1.LabID != Node2.LabID)
        {
            return(false);
        }

        if (Node1.Type != Node2.Type)
        {
            return(false);
        }

        return(true);
    }
Esempio n. 26
0
 public void PresetLine(CircuitNode otherNode)
 {
     ConnectNodes(otherNode);
     if (connection1 != null)
     {
         CanChangeConnect1 = false;
     }
     if (connection2 != null)
     {
         CanChangeConnect2 = false;
     }
     if (otherNode.connection1 != null)
     {
         otherNode.CanChangeConnect1 = false;
     }
     if (otherNode.connection2 != null)
     {
         otherNode.CanChangeConnect2 = false;
     }
 }
Esempio n. 27
0
    public void DrawLine(CircuitNode otherNode)
    {
        GameObject wire = new GameObject();

        wire.transform.position = this.transform.position;
        wire.AddComponent <LineRenderer>();
        LineRenderer lr = wire.GetComponent <LineRenderer>();

        lr.startWidth = 0.1f;
        lr.endWidth   = 0.1f;
        lr.SetPosition(0, this.transform.position);
        lr.SetPosition(1, otherNode.transform.position);
        wire.tag   = "Wire";
        wire.layer = 13;

        wire.AddComponent <WireBehavior>();
        WireBehavior wb = wire.GetComponent <WireBehavior>();

        wb.wnode1 = this;
        wb.wnode2 = otherNode;
    }
Esempio n. 28
0
    public void OnTriggerEnter2D(Collider2D col)
    {
        Entity         e     = col.GetComponent <Entity> ();
        CollisionRelay relay = col.GetComponent <CollisionRelay> ();
        CircuitNode    i     = null;  //TODO bullet activated circuit node
        Destructable   d     = col.GetComponent <Destructable> ();

        if (relay != null && e == null)
        {
            e = relay.LogCollision(this);
        }

        if (e != null)
        {
            if (faction != e.GetFaction())
            {
                Entity.DamageEntity(e, source, damage, DamageType);
                OnHit(col, e);
                if (destroyOnHit)
                {
                    OnDeath();
                }
            }
        }
        else if (i != null)
        {
            OnHit(col);
//			i.OnInteract (damageType);
        }
        else if (d != null)
        {
            OnHit(col);
            d.ApplyDamage(damage);
        }
        else if (col.tag == "Indes")
        {
            OnHit(col);
            OnDeath();
        }
    }
Esempio n. 29
0
        /// <summary>
        /// Check if we are converging during iterations
        /// </summary>
        /// <param name="sim">The simulation</param>
        /// <param name="ckt">The circuit</param>
        /// <returns></returns>
        private static bool IsConvergent(SimulationConfiguration config, Circuit ckt)
        {
            var rstate = ckt.State.Real;

            // Check convergence for each node
            for (int i = 0; i < ckt.Nodes.Count; i++)
            {
                var    node = ckt.Nodes[i];
                double n    = rstate.Solution[node.Index];
                double o    = rstate.OldSolution[node.Index];

                if (double.IsNaN(n))
                {
                    throw new CircuitException($"Non-convergence, node {node} is not a number.");
                }

                if (node.Type == CircuitNode.NodeType.Voltage)
                {
                    double tol = config.RelTol * Math.Max(Math.Abs(n), Math.Abs(o)) + config.VoltTol;
                    if (Math.Abs(n - o) > tol)
                    {
                        ProblemNode = node;
                        return(false);
                    }
                }
                else
                {
                    double tol = config.RelTol * Math.Max(Math.Abs(n), Math.Abs(o)) + config.AbsTol;
                    if (Math.Abs(n - o) > tol)
                    {
                        ProblemNode = node;
                        return(false);
                    }
                }
            }

            // Convergence succeeded
            return(true);
        }
Esempio n. 30
0
        private void SendRelayCell(ushort stream_id, CellCommand relay_command,
                                   byte[] payload   = null, CellCommand cell_command = CellCommand.relay,
                                   CircuitNode node = null)
        {
            node = node ?? FinalCircuitNode;

            if ((null == GetStreamById(stream_id)) && (0 != stream_id))
            {
                Logger.Warning("circuit::send_relay_cell() attempt to send cell to non-existent stream-id: {0}",
                               stream_id);
                return;
            }

            Logger.Debug("tor_socket::send_cell() [circuit: %i%s, stream: %u, command: %i, relay_command: %i]",
                         _circuit_id & 0x7FFFFFFF,
                         ((0 != (_circuit_id & 0x80000000)) ? " (MSB set)" : ""),
                         stream_id, cell_command, relay_command);
            SendCell(
                Encrypt(
                    new RelayCell(_circuit_id, cell_command, node, relay_command,
                                  stream_id, payload)));
        }