Вы не зашли.
Страницы 1
подскажите пожалуйста , что бы разобраться вот к примеру
/* LSE_FILE_HEADER */ include "lse.locomotive.gs" class LseGraphNode { public bool Enabled = true; public int[] LinkedNodes = new int[0]; public bool Plus = false; public bool State = false; public int VisitNumber = 0; public int VisitLinkIndex = 0; public int VisitPrevNode = -1; }; class LseGraphRelay { public int Coil; public bool State = false; public int[] FrontContacts; public int[] BackContacts; public float ProcessTime; public float OffDelay; public float OnDelay; public float InvResistance; public int Flags = 0; define public int DontCountActiveFlag = 1; define public int DontCountActivatedFlag = 1 << 1; define public int DontCountDeactivatedFlag = 1 << 2; }; class LseGraphScheme { public void CreateNode(string nodeNamesStr); public void CreateAlias(string nodeName, string nodeAliasesStr); public void CreateRelay(string relayName, string frontContactsStr, string backContactsStr, string paramsStr); public void CreateRelay(string relayName, string coilNodeName, string frontContactsStr, string backContactsStr, string paramsStr); public void CreateCircuit(string circuitStr); public void CreateCircuitOneWay(string circuitStr); public LseGraphNode[] Nodes; public LseGraphRelay[] Relays; public LseGraphNode MainPlus; public int ActiveRelaysCount = 0; public float ActiveRelaysInvResistance = 0.0; public int ActivatedRelaysCount = 0; public int DeactivatedRelaysCount = 0; public float DefaultRelayResistance = 500.0; public float DefaultRelayOnDelay = 0.1; public float DefaultRelayOffDelay = 0.1; public LseGraphNode GetOrCreateNode(string nodeName); public LseGraphNode GetNode(string nodeName); public int GetOrCreateNodeId(string nodeName); public int GetNodeId(string nodeName); public void Init(); /* ============================================================================== Implementation ============================================================================== */ define bool DEBUG = true; LIntMap nodeIds; LIntMap relayIds; public void Init() { Nodes = new LseGraphNode[0]; Relays = new LseGraphRelay[0]; nodeIds = new LIntMap(); relayIds = new LIntMap(); MainPlus = GetOrCreateNode("+, PLUS"); } string[] getTokens(string str) { string[] tokens = Str.Tokens(str, ","); int i; for (i = 0; i < tokens.size(); ++i) { if (LStr.IsNullOrWhiteSpace(tokens[i])) { Interface.Exception("Invalid token '" + tokens[i] + "' in string '" + str + "'"); return new string[0]; } Str.ToLower(tokens[i]); Str.TrimLeft(tokens[i], ""); Str.TrimRight(tokens[i], ""); } return tokens; } public LseGraphNode GetNode(string nodeName) { if (nodeIds.TryGet(nodeName)) return Nodes[nodeIds.LastValue]; Interface.Exception("Node '" + nodeName + "' does not exist."); return null; } public LseGraphNode GetOrCreateNode(string nodeName) { if (!nodeIds.TryGet(nodeName)) { if (DEBUG) Interface.Log("LseGraphScheme.GetOrCreateNode> creating new node '"+nodeName+"'"); CreateNode(nodeName); } if (DEBUG) Interface.Log("LseGraphScheme.GetOrCreateNode> '"+nodeName+"' -> " + GetNodeId(nodeName)); return GetNode(nodeName); } public int GetNodeId(string nodeName) { if (nodeIds.TryGet(nodeName)) return nodeIds.LastValue; Interface.Exception("Node '" + nodeName + "' does not exist."); return 0; } public int GetOrCreateNodeId(string nodeName) { if (!nodeIds.TryGet(nodeName)) { if (DEBUG) Interface.Log("LseGraphScheme.GetOrCreateNodeId> creating new node '"+nodeName+"'"); CreateNode(nodeName); } if (DEBUG) Interface.Log("LseGraphScheme.GetOrCreateNodeId> '"+nodeName+"' -> " + GetNodeId(nodeName)); return GetNodeId(nodeName); } public void CreateNode(string nodeNamesStr) { string[] nodeNames = getTokens(nodeNamesStr); int newNodeId = Nodes.size(); int i; for (i = 0; i < nodeNames.size(); ++i) { if (nodeIds.TryGet(nodeNames[i])) { Interface.Exception("Name '" + nodeNames[i] + "' already exists."); return; } nodeIds.Set(nodeNames[i], newNodeId); } Nodes[newNodeId] = new LseGraphNode(); } public void CreateAlias(string nodeName, string nodeAliasesStr) { int nodeId = GetNodeId(nodeName); string[] nodeAliases = getTokens(nodeAliasesStr); int i; for (i = 0; i < nodeAliases.size(); ++i) { if (nodeIds.TryGet(nodeAliases[i])) { Interface.Exception("Name '" + nodeAliases[i] + "' already exists."); return; } nodeIds.Set(nodeAliases[i], nodeId); } } bool arrayFind(int[] array, int value) { int i; for (i = 0; i < array.size(); ++i) if (array[i] == value) return true; return false; } bool arrayFind(string[] array, string value) { int i; for (i = 0; i < array.size(); ++i) if (array[i] == value) return true; return false; } bool isAlreadyOwnedByRelay(int nodeId) { int i; for (i = 0; i < Relays.size(); ++i) { bool isContact = arrayFind(Relays[i].FrontContacts, nodeId) or arrayFind(Relays[i].BackContacts, nodeId); bool isCoil = Relays[i].Coil == nodeId; if (isContact or isCoil) return true; } return false; } void setContacts(LseGraphRelay relay, string[] contactNames, bool frontContacts) { int[] contacts = new int[contacts.size()]; int i; for (i = 0; i < contacts.size(); ++i) { contacts[i] = GetOrCreateNodeId(contactNames[i]); if (isAlreadyOwnedByRelay(contacts[i])) { Interface.Exception("Node '" + contactNames[i] + "' is already owned by relay."); return; } Nodes[contacts[i]].Enabled = !frontContacts; } if (frontContacts) relay.FrontContacts = contacts; else relay.BackContacts = contacts; } public void CreateRelay(string relayName, string coilNodeName, string frontContactsStr, string backContactsStr, string paramsStr) { if (relayIds.TryGet(relayName)) { Interface.Exception("Relay '" + relayName + "' already exists."); return; } int relayId = Relays.size(); relayIds.Set(relayName, relayId); LseGraphRelay relay = new LseGraphRelay(); setContacts(relay, getTokens(frontContactsStr), true); setContacts(relay, getTokens(backContactsStr), false); relay.Coil = GetOrCreateNodeId(coilNodeName); paramsStr = Str.CloneString(paramsStr); Str.ToLower(paramsStr); Soup params = LData.FromJson(paramsStr); relay.OffDelay = params.GetNamedTagAsInt("offdelay", DefaultRelayOffDelay); relay.OnDelay = params.GetNamedTagAsInt("ondelay", DefaultRelayOnDelay); float resistance = params.GetNamedTagAsFloat("r", DefaultRelayResistance); relay.InvResistance = 0.0; if (resistance > 0.0) relay.InvResistance = 1.0 / resistance; string[] flags = LData.ToStringArray(params.GetNamedSoup("flags")); relay.Flags = 0; if (arrayFind(flags, "noactive")) relay.Flags = relay.Flags | LseGraphRelay.DontCountActiveFlag; if (arrayFind(flags, "noactivated")) relay.Flags = relay.Flags | LseGraphRelay.DontCountActivatedFlag; if (arrayFind(flags, "nodeactivated")) relay.Flags = relay.Flags | LseGraphRelay.DontCountDeactivatedFlag; Relays[relayId] = relay; } public void CreateRelay(string relayName, string frontContactsStr, string backContactsStr, string paramsStr) { CreateRelay(relayName, relayName, frontContactsStr, backContactsStr, paramsStr); } void connectNodes(string srcNodeName, string dstNodeName) { if (DEBUG) Interface.Log("LseGraphScheme.connectNodes> '"+srcNodeName+"' to '"+dstNodeName+"'"); LseGraphNode srcNode = GetOrCreateNode(srcNodeName); int dstNodeId = GetOrCreateId(dstNodeName); if (!arrayFind(srcNode.LinkedNodes, dstNodeId)) srcNode.LinkedNodes[srcNode.LinkedNodes.size()] = dstNodeId; } void createCircuit(string circuitStr, bool dual) { string[] circiut = getTokens(circuitStr); int i; for (i = 1; i < circiut.size(); ++i) { connectNodes(circiut[i - 1], circiut[i]); if (dual) connectNodes(circiut[i], circiut[i - 1]); } } public void CreateCircuit(string circuitStr) { createCircuit(circuitStr, true); } public void CreateCircuitOneWay(string circuitStr) { createCircuit(circuitStr, false); } int SearchNumber = 0; void DepthSearch(int startNode) { int currentNode = startNode; if (!(Nodes[startNode].Plus and Nodes[startNode].Enabled)) return; Nodes[startNode].VisitNumber = SearchNumber; Nodes[startNode].VisitLinkIndex = 0; Nodes[startNode].VisitPrevNode = -1; while (true) { LseGraphNode currentNodeObj = Nodes[currentNode]; currentNodeObj.State = true; int nextNode = -1; while (currentNodeObj.VisitLinkIndex < currentNodeObj.LinkedNodes.size()) { int adjIndex = currentNodeObj.VisitLinkIndex++; int adjNode = currentNodeObj.LinkedNodes[adjIndex]; LseGraphNode adjNodeObj = Nodes[adjNode]; if (adjNodeObj.Enabled and adjNodeObj.VisitNumber != SearchNumber) { nextNode = adjNode; adjNodeObj.VisitNumber = SearchNumber; adjNodeObj.VisitPrevNode = currentNode; adjNodeObj.VisitLinkIndex = 0; break; } } if (nextNode < 0) { if (currentNodeObj.VisitPrevNode >= 0) nextNode = currentNodeObj.VisitPrevNode; else break; } currentNode = nextNode; } } void UpdateScheme() { ++SearchNumber; int NodesCount = Nodes.size(); int i; for (i = 0; i < NodesCount; ++i) { if (Nodes[i].VisitNumber == SearchNumber) continue; Nodes[i].State = false; if (Nodes[i].Plus) DepthSearch(i); } } void setContactsEnabled(int[] contacts, bool state) { int k, count = contacts.size(); for (k = 0; k < count; ++k) Nodes[contacts[k]].Enabled = state; } void UpdateRelays(float dt) { ActiveRelaysCount = 0; ActiveRelaysInvResistance = 0.0; ActivatedRelaysCount = 0; DeactivatedRelaysCount = 0; int RelaysCount = Relays.size(); int i; for (i = 0; i < RelaysCount; ++i) { LseGraphRelay relay = Relays[i]; bool coilState = Nodes[relay.Coil].State; if (relay.State != coilState) { relay.ProcessTime = relay.ProcessTime + dt; bool processCompleted = false; if (coilState) processCompleted = relay.ProcessTime >= relay.OnDelay; else processCompleted = relay.ProcessTime >= relay.OffDelay; if (processCompleted) { relay.State = coilState; if (relay.State and !Flags.Test(relay.Flags, LseGraphRelay.DontCountActivatedFlag)) ++ActivatedRelaysCount; if (!relay.State and !Flags.Test(relay.Flags, LseGraphRelay.DontCountDeactivatedFlag)) ++DeactivatedRelaysCount; setContactsEnabled(relay.FrontContacts, relay.State); setContactsEnabled(relay.BackContacts, !relay.State); relay.ProcessTime = 0.0; } } else relay.ProcessTime = 0.0; if (relay.State and !Flags.Test(relay.Flags, LseGraphRelay.DontCountActiveFlag)) { ActiveRelaysInvResistance = ActiveRelaysInvResistance + relay.InvResistance; ++ActiveRelaysCount; } } } public void Update(float time, float dt) { UpdateScheme(); UpdateRelays(dt); } };
Что нужно знать ? И есть люди которые разобраться могут что до как ? За интерес
Не в сети
Да, java. Похоже на какой-то парсер.
Не в сети
Думаю нет, т.к. времени совершенно нет.
Не в сети
Страницы 1