public void NotNotNodeTest() { var inputNode = new InputNode("in", new State(true)); var notNode1 = new NotNode("not1"); var notNode2 = new NotNode("not2"); var outputNode = new OutputNode("out"); var nodeConnections = new List <NodeConnection> { new NodeConnection(inputNode, notNode1), new NodeConnection(notNode1, notNode2), new NodeConnection(notNode2, outputNode) }; var inputNodes = new List <InputNode> { inputNode }; var simulation = new NodeSimulation(nodeConnections); simulation.RunSimulation(); var output = simulation.GetOutputState(); Assert.True(output["out"].LogicState); }
public void AndNodeTest() { var inputNode1 = new InputNode("in1", new State(true)); var inputNode2 = new InputNode("in2", new State(true)); var andNode = new AndNode("and"); var outputNode = new OutputNode("out"); var nodeConnections = new List <NodeConnection> { new NodeConnection( new List <NodeBase> { inputNode1, inputNode2 }, andNode), new NodeConnection(andNode, outputNode) }; var inputNodes = new List <InputNode> { inputNode1, inputNode2 }; var simulation = new NodeSimulation(nodeConnections); simulation.RunSimulation(); var output = simulation.GetOutputState(); Assert.True(output["out"].LogicState); }
public void CreateSimulationTest() { var inputNode1 = new InputNode("in1", new State(false)); var inputNode2 = new InputNode("in2", new State(true)); var orNode = new OrNode("or"); var outputNode = new OutputNode("out"); var nodeConnections = new List <NodeConnection> { new NodeConnection( new List <NodeBase> { inputNode1, inputNode2 }, orNode), new NodeConnection(orNode, outputNode) }; var inputNodes = new List <InputNode> { inputNode1, inputNode2 }; var simulation = new NodeSimulation(nodeConnections); // TODO: Is this the same as CollectionAssert? Assert.Equal(nodeConnections, simulation.NodeConnections); }
public void SetInvalidAmountInputsTest() { var inputNode1 = new InputNode("in1"); var inputNode2 = new InputNode("in2"); var orNode = new OrNode("or"); var outputNode = new OutputNode("out"); var nodeConnections = new List <NodeConnection> { new NodeConnection( new List <NodeBase> { inputNode1, inputNode2 }, orNode), new NodeConnection(orNode, outputNode) }; var inputNodes = new List <InputNode> { inputNode1, inputNode2 }; var simulation = new NodeSimulation(nodeConnections); var inputValues = new Dictionary <string, State> { { inputNode1.NodeId, new State(true) }, { inputNode2.NodeId, new State(false) }, { orNode.NodeId, new State(false) } }; Assert.Throws <ArgumentException>(() => simulation.SetInputs(inputValues)); }
public void ResetSimulationTest() { var inputNode1 = new InputNode("in1", new State(false)); var inputNode2 = new InputNode("in2", new State(true)); var orNode = new OrNode("or"); var outputNode = new OutputNode("out"); var nodeConnections = new List <NodeConnection> { new NodeConnection( new List <NodeBase> { inputNode1, inputNode2 }, orNode), new NodeConnection(orNode, outputNode) }; var inputNodes = new List <InputNode> { inputNode1, inputNode2 }; var simulation = new NodeSimulation(nodeConnections); simulation.RunSimulation(); simulation.ResetSimulation(); var output = simulation.GetOutputState(); Assert.Null(output["out"]); }
public void ValidCheckWithLoops() { var inputNode1 = new InputNode("in1", new State(true)); var notNode = new NotNode("not"); var andNode = new AndNode("and"); var outputNode = new OutputNode("out"); var nodeConnections = new List <NodeConnection> { new NodeConnection(andNode, notNode), new NodeConnection( new List <NodeBase> { inputNode1, notNode }, andNode), new NodeConnection(andNode, outputNode) }; var inputNodes = new List <InputNode> { inputNode1 }; var simulation = new NodeSimulation(nodeConnections); Assert.Throws <PathException>(() => simulation.ValidSimulationCheck()); }
public void ValidCheckWithoutInputs() { var notNode1 = new NotNode("not1"); var notNode2 = new NotNode("not2"); var outputNode = new OutputNode("out"); var nodeConnections = new List <NodeConnection> { new NodeConnection(notNode1, notNode2), new NodeConnection(notNode2, outputNode) }; var simulation = new NodeSimulation(nodeConnections); Assert.Throws <NoInputNodesException>(() => simulation.ValidSimulationCheck()); // Assert.Equal("Simulation contains no input nodes", simulation.ValidSimulationCheck()); }
public void ValidCheckWithoutOutputs() { var inputNode = new InputNode("in", new State(true)); var notNode1 = new NotNode("not1"); var notNode2 = new NotNode("not2"); var nodeConnections = new List <NodeConnection> { new NodeConnection(inputNode, notNode1), new NodeConnection(notNode1, notNode2) }; var inputNodes = new List <InputNode> { inputNode }; var simulation = new NodeSimulation(nodeConnections); Assert.Throws <NoOutputNodesException>(() => simulation.ValidSimulationCheck()); }
public void ValidCheckWithValidSimulation() { var inputNode = new InputNode("in", new State(true)); var notNode1 = new NotNode("not1"); var notNode2 = new NotNode("not2"); var outputNode = new OutputNode("out"); var nodeConnections = new List <NodeConnection> { new NodeConnection(inputNode, notNode1), new NodeConnection(notNode1, notNode2), new NodeConnection(notNode2, outputNode) }; var inputNodes = new List <InputNode> { inputNode }; var simulation = new NodeSimulation(nodeConnections); simulation.ValidSimulationCheck(); }
public void SetInputsTest() { var inputNode1 = new InputNode("in1"); var inputNode2 = new InputNode("in2"); var orNode = new OrNode("or"); var outputNode = new OutputNode("out"); var nodeConnections = new List <NodeConnection> { new NodeConnection( new List <NodeBase> { inputNode1, inputNode2 }, orNode), new NodeConnection(orNode, outputNode) }; var inputNodes = new List <InputNode> { inputNode1, inputNode2 }; var simulation = new NodeSimulation(nodeConnections); var inputValues = new Dictionary <string, State> { { inputNode1.NodeId, new State(true) }, { inputNode2.NodeId, new State(false) } }; simulation.SetInputs(inputValues); Assert.True(simulation.GetInputNodes().Where(x => x.NodeId == inputNode1.NodeId).Single().CurrentState.LogicState); Assert.False(simulation.GetInputNodes().Where(x => x.NodeId == inputNode2.NodeId).Single().CurrentState.LogicState); }
/// <summary> /// Determines whether the player has at least one of a specified set of cards. /// </summary> /// <param name="cards">The cards in question.</param> /// <returns>True if the player holds a card, false if not, or null if it is unknown.</returns> public bool? HasAtLeastOneOf(IEnumerable<Card> cards) { Contract.Requires<ArgumentNullException>(cards != null, "cards"); Contract.Requires<InvalidOperationException>(this.Game != null); var relevantNodes = this.Game.Nodes.Where(n => n.CardHolder == this && cards.Contains(n.Card)).OfType<INode>(); // If the player is known to not have any of the cards... if (relevantNodes.All(n => n.IsSelected.HasValue && !n.IsSelected.Value)) { return false; // ...then he cannot possibly disprove the suggestion. } // If the player is known to have at least one of the cards... if (relevantNodes.Any(n => n.IsSelected.HasValue && n.IsSelected.Value)) { return true; // ...then he can disprove the suggestion. } // Maybe they must have at least one of the cards in the list, // but we just don't know which one yet. Test by setting all // indeterminate nodes to unselected, and testing if we have a valid state. using (var sim = new NodeSimulation(relevantNodes)) { foreach (INode node in relevantNodes.Where(n => !n.IsSelected.HasValue)) { node.IsSelected = false; } CompositeConstraint cc = new CompositeConstraint(this.Game.Constraints); if (cc.IsBroken) { return true; // Something's got to be selected. } } // Otherwise, we don't know for sure. return null; }
void findAllSelectionPossibilities(bool selectionStateToCheck) { INode focusedNode; while ((focusedNode = findNodeWithUnknownSolutionPossibility(selectionStateToCheck)) != null) { using (NodeSimulation sim = new NodeSimulation(indeterminateNodes)) { // Force the focusedNode into being selected or unselected (depending on argument) and search for a solution. focusedNode.IsSelected = selectionStateToCheck; var solution = cc.FindOnePossibleSolution(); if (solution != null) { learnFromSolution(solution); } else { // Since we could not find a single solution, the selection state we tried for this node must be out of the // set of possible solutions. // Record the attempt for just the focused node. nodeSolutionPossibilities[focusedNode] |= selectionStateToCheck ? NodePossibility.TriedSelectedInSolution : NodePossibility.TriedUnselectedInSolution; // Since we already know that a solution exists to the actual problem, it's safe to assume that focusedNode // MUST be able to be in the opposite selection state, since the one we tried is not possible. // This little optimization can save us time if both selected and unselected states for each node are being sought. nodeSolutionPossibilities[focusedNode] |= !selectionStateToCheck ? NodePossibility.FoundSelectedInSolution : NodePossibility.FoundUnselectedInSolution; } } } }