diff --git a/README.md b/README.md index 780c941..6260403 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,10 @@ Simple demand assignment network calculator. dl c1000 1010 dt c1000 1010 1020 +## Notes + +* Centroids have their own namepsace, separate from nodes + # Installation * git clone the repo diff --git a/src/main/scala/gncc/network/Network.scala b/src/main/scala/gncc/network/Network.scala index 8c375a9..284832c 100644 --- a/src/main/scala/gncc/network/Network.scala +++ b/src/main/scala/gncc/network/Network.scala @@ -31,6 +31,10 @@ trait Network { def al(i: Int, j: Int): Reason def addLink(i: Int, j: Int) = al(i, j) def +(i: Int, j: Int) = al(i, j) + + def at(i: Int, j: Int, k: Int): Reason + def addTurn(i: Int, j: Int, k: Int) = at(i,j,k) + def +(i: Int, j: Int, k: Int) = at(i,j,k) } class DefaultNetwork extends Network { @@ -39,11 +43,19 @@ class DefaultNetwork extends Network { val nodes = new HashSet[Node] val links = new HashSet[Link] + val turns = new HashSet[Turn] def getNode(id: Int) = { nodes.findEntry(Node(id)) } + def getLink(i: Int, j: Int): Option[Link] = { + links.findEntry(Link(i, j)) + } + def getLink(i: Node, j: Node): Option[Link] = { + getLink(i.id, j.id) + } + def an(id: Int): Reason = { if (nodes.add(Node(id))) Success @@ -63,6 +75,17 @@ class DefaultNetwork extends Network { } } + def at(i: Int, j: Int, k: Int): Reason = { + val turnLinks = (getLink(i, j), getLink(j, k)) + + turnLinks match { + case (Some(i), Some(j)) => if(turns.add(Turn(i, j))) Success else TurnExists + case (None, None) => NoNodesAreLinked + case (None, _) => IJAreNotLinked + case (_, None) => JKAreNotLinked + } + } + // Currently using this for debugging override def toString = { "Nodes: " + nodes.toString + "Links: " + links.toString @@ -72,4 +95,4 @@ class DefaultNetwork extends Network { abstract class NetworkElement case class Node(id: Int) extends NetworkElement case class Link(iNode: Int, jNode: Int) extends NetworkElement -case class Turn(iLink: Int, jLink: Int) extends NetworkElement \ No newline at end of file +case class Turn(iLink: Link, jLink: Link) extends NetworkElement \ No newline at end of file diff --git a/src/main/scala/gncc/network/package.scala b/src/main/scala/gncc/network/package.scala index b10bda2..4fc1e11 100644 --- a/src/main/scala/gncc/network/package.scala +++ b/src/main/scala/gncc/network/package.scala @@ -18,7 +18,7 @@ package gncc package object network { object NetworkOperationStatus extends Enumeration { type Reason = Value - val Success, NodeExists, LinkExists, TurnExists, InvalidNodes, INodeInvalid, JNodeInvalid, KNodeInvalid, ILinkInvalid, JLinkInvalid = Value + val Success, NodeExists, LinkExists, TurnExists, InvalidNodes, INodeInvalid, JNodeInvalid, KNodeInvalid, IJAreNotLinked, JKAreNotLinked, NoNodesAreLinked = Value } } diff --git a/src/test/scala/network/NetworkSuite.scala b/src/test/scala/network/NetworkSuite.scala index 8765501..c06c8e7 100644 --- a/src/test/scala/network/NetworkSuite.scala +++ b/src/test/scala/network/NetworkSuite.scala @@ -29,6 +29,7 @@ class NetworkSuiteSpec extends WordSpec with ShouldMatchers { network.addNode(1) should be (Success) network + 2 should be (Success) network an 3 should be (Success) + network + 4 should be (Success) } "while they produce an error if you add an existing one" in { @@ -40,9 +41,10 @@ class NetworkSuiteSpec extends WordSpec with ShouldMatchers { "links are easy to add" in { network.addLink(1, 2) should be (Success) network al (2, 3) should be (Success) + network + (4, 3) should be (Success) } - "but error when we add one that already exists" in { + "error when we add one that already exists" in { network.addLink(1, 2) should be (LinkExists) } @@ -58,5 +60,27 @@ class NetworkSuiteSpec extends WordSpec with ShouldMatchers { network.addLink(11,11) should be (InvalidNodes) } } + + "finally, atop links we can add turns" that { + "turns require three nodes" in { + network at(1,2,3) should be (Success) + } + + "error when the first two nodes are not linked" in { + network at(1,4,3) should be (IJAreNotLinked) + } + + "error when the second two nodes are not linked" in { + network at(1,2,4) should be (JKAreNotLinked) + } + + "error when all nodes are invalid" in { + network at(1,3,4) should be (NoNodesAreLinked) + } + + "error when we try to add the same turn over again" in { + network + (1,2,3) should be (TurnExists) + } + } } } \ No newline at end of file