public void CreateSlotLoop() { Console.Write("Slot's name: "); string name = Console.ReadLine(); PetriSlot s = new PetriSlot(slotsList.ToArray().Length, name); slotsList.Add(s); Console.WriteLine("Created Slot: " + s.name + " with ID " + s.id); int id = slotsList.ToArray().Length - 1; Console.Write("How many tokens?"); int tokens = -1; Int32.TryParse(Console.ReadLine(), out tokens); if (tokens >= 0) { AddTokensToSlot(id, tokens); } string l = "Created a slot. Id: " + id + "; Name: " + name + "; Tokens: " + tokens + ";"; UpdateLogs(l); }
public void CreateConnectionTSLoop() { int slotID = 0; int transID = 0; int weight = 1; ConnectionType type = ConnectionType.Normal; Console.WriteLine("====="); ListTransitions(); Console.WriteLine("====="); Console.Write("Transition's ID: "); Int32.TryParse(Console.ReadLine(), out transID); if (transID < 0) { Console.WriteLine("Failed"); return; } Console.WriteLine("====="); ListSlots(); Console.WriteLine("====="); Console.Write("Slot's ID: "); Int32.TryParse(Console.ReadLine(), out slotID); if (slotID < 0) { Console.WriteLine("Failed"); return; } Console.Write("Connection's Weight: "); Int32.TryParse(Console.ReadLine(), out weight); if (weight < 0) { Console.WriteLine("Forcing value of 1 to the connection"); weight = 1; return; } PetriConnection c = new PetriConnection(connectionsList.ToArray().Length, slotID, transID, true, weight, type); transitionsList[transID].outputs.Add(c); slotsList[slotID].inputs.Add(c); connectionsList.Add(c); PetriTransition trans = transitionsList[transID]; PetriSlot slot = slotsList[slotID]; string l = "Created a connection from the transition [" + trans.name + "(" + trans.id + ")" + "] to the slot [" + slot.name + "(" + slot.id + ")]" + "Id: " + c.id; UpdateLogs(l); listStr = ""; }
public bool Run() { bool somethingHappened = false; foreach (PetriTransition transition in transitionsList) // For each transition that there is { bool enabled = true; foreach (PetriConnection inputConnection in transition.inputs) // Check for input connections { PetriSlot slot = slotsList[inputConnection.s]; if ((slot.tokens < inputConnection.weight && inputConnection.type != ConnectionType.Inhibitor) || (slot.tokens > 0 && inputConnection.type == ConnectionType.Inhibitor)) { enabled = false; // If conditions are satisfied, enable the transition break; } } if (enabled) // If the transition's enabled { foreach (PetriConnection inputConnection in transition.inputs) // Get each input { PetriSlot slot = slotsList[inputConnection.s]; // Reach for their slot if (inputConnection.type != ConnectionType.Inhibitor) { RemoveTokensFromSlot(inputConnection.s, inputConnection.weight); // In case it's a normal or reset connection, remove some tokens from the slot } else if (inputConnection.type == ConnectionType.Reset) { RemoveTokensFromSlot(inputConnection.s, slotsList[inputConnection.s].tokens); // If it's a reset connection, remove all tokens } UpdateLogs("Taking " + inputConnection.weight + " tokens from slot [" + slot.name + "(" + slot.id + ")]"); // Log what happened somethingHappened = true; } foreach (PetriConnection outputConnection in transition.outputs) // Get each output { PetriSlot slot = slotsList[outputConnection.s]; // Reach for their slot AddTokensToSlot(outputConnection.s, outputConnection.weight); // Give tokens to it UpdateLogs("Gave " + outputConnection.weight + " tokens to slot [" + slot.name + "(" + slot.id + ")]"); // Log what happened somethingHappened = true; } } } if (!somethingHappened) // If nothing happened at all, stop the loop since there are no available transitions { UpdateLogs("No available transitions"); } return(somethingHappened); }
public void ListConnections() { foreach (PetriConnection c in connectionsList) { PetriSlot slot = slotsList[c.s]; PetriTransition trans = transitionsList[c.t]; if (c.output) { listStr += "Connection " + c.id + ": " + "From the transition [" + trans.name + "(" + trans.id + ")" + "] to the slot [" + slot.name + "(" + slot.id + ")]; " + "Weight: " + c.weight + "\n"; Console.WriteLine("Connection " + c.id + ": " + "From the transition [" + trans.name + "(" + trans.id + ")" + "] to the slot [" + slot.name + "(" + slot.id + ")]; " + "Weight: " + c.weight + "\n"); } else { listStr += "Connection " + c.id + ": " + "From the slot [" + slot.name + "(" + slot.id + ")" + "] to the transition [" + trans.name + "(" + trans.id + ")]; " + "Weight: " + c.weight + "\n"; Console.WriteLine("Connection " + c.id + ": " + "From the slot [" + slot.name + "(" + slot.id + ")" + "] to the transition [" + trans.name + "(" + trans.id + ")]; " + "Weight: " + c.weight + "\n"); } } }
public void CreateConnectionTS(int slot, int transition, int w = 1) { int slotID = slot; int transID = transition; int weight = w; ConnectionType type = ConnectionType.Normal; PetriConnection c = new PetriConnection(connectionsList.ToArray().Length, slotID, transID, true, weight, type); transitionsList[transID].outputs.Add(c); slotsList[slotID].inputs.Add(c); connectionsList.Add(c); PetriTransition trans = transitionsList[transID]; PetriSlot pslot = slotsList[slotID]; string l = "Created a connection from the transition [" + trans.name + "(" + trans.id + ")" + "] to the slot [" + pslot.name + "(" + pslot.id + ")]" + "Id: " + c.id; UpdateLogs(l); listStr = ""; }
public void CreateSlot(string slotName, int slotTokens) { string name = slotName; PetriSlot s = new PetriSlot(slotsList.ToArray().Length, name); slotsList.Add(s); int id = slotsList.ToArray().Length - 1; int tokens = slotTokens; if (tokens >= 0) { AddTokensToSlot(id, tokens); } string l = "Created a slot. Id: " + id + "; Name: " + name + "; Tokens: " + tokens + ";"; UpdateLogs(l); }
public void CreateConnectionSTLoop() { int slotID = 0; int transID = 0; int weight = 1; string inputType; ConnectionType type = ConnectionType.Normal; bool testing = false; while (testing == false) { Console.WriteLine("====="); ListSlots(); Console.WriteLine("====="); Console.Write("Slot's ID: "); Int32.TryParse(Console.ReadLine(), out slotID); if (slotID < 0 || slotID >= slotsList.ToArray().Length) { Console.WriteLine("Failed"); } else { testing = true; } } testing = false; while (testing == false) { Console.WriteLine("====="); ListTransitions(); Console.WriteLine("====="); Console.Write("Transition's ID: "); Int32.TryParse(Console.ReadLine(), out transID); if (transID < 0 || transID >= transitionsList.ToArray().Length) { Console.WriteLine("Failed"); } else { testing = true; } } testing = false; if (weight < 0) { Console.WriteLine("Forcing value of 1 to the connection"); weight = 1; return; } Console.WriteLine("====="); Console.WriteLine("Possible Types: "); Console.WriteLine("- Normal"); Console.WriteLine("- Inhibitor"); Console.WriteLine("- Reset"); Console.WriteLine("====="); while (!testing) { Console.Write("Connection Type: "); inputType = Console.ReadLine().ToLower(); if (inputType == "normal" || inputType == "n") { type = ConnectionType.Normal; testing = true; } else if (inputType == "inhibitor" || inputType == "i") { type = ConnectionType.Inhibitor; testing = true; } else if (inputType == "reset" || inputType == "r") { type = ConnectionType.Reset; testing = true; } else { Console.WriteLine("Incorrect type"); } } if (type == ConnectionType.Normal) { Console.Write("Connection's Weight: "); Int32.TryParse(Console.ReadLine(), out weight); } else { weight = 0; } PetriConnection c = new PetriConnection(connectionsList.ToArray().Length, slotID, transID, false, weight, type); transitionsList[transID].inputs.Add(c); slotsList[slotID].outputs.Add(c); connectionsList.Add(c); PetriTransition trans = transitionsList[transID]; PetriSlot slot = slotsList[slotID]; string l = "Created a connection from the slot [" + slot.name + "(" + slot.id + ")" + "] to the transition [" + trans.name + "(" + trans.id + ")]" + "Id: " + c.id;; UpdateLogs(l); listStr = ""; }