|
| 1 | +/* |
| 2 | + * Copyright 2008 - 2019, Arnaud Casteigts and the JBotSim contributors <contact@jbotsim.io> |
| 3 | + * |
| 4 | + * |
| 5 | + * This file is part of JBotSim. |
| 6 | + * |
| 7 | + * JBotSim is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * JBotSim is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with JBotSim. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package examples.features.multigraph; |
| 23 | + |
| 24 | +import io.jbotsim.core.Link; |
| 25 | +import io.jbotsim.core.Node; |
| 26 | +import io.jbotsim.core.Topology; |
| 27 | +import io.jbotsim.core.event.CommandListener; |
| 28 | +import io.jbotsim.ui.JViewer; |
| 29 | + |
| 30 | +/** |
| 31 | + * This class shows how JBotSim handles multigraphs |
| 32 | + */ |
| 33 | +public class MultigraphTest { |
| 34 | + private final Topology topology; |
| 35 | + |
| 36 | + public MultigraphTest(Link.Orientation orientation) { |
| 37 | + |
| 38 | + topology = new Topology(); |
| 39 | + topology.setOrientation(orientation); |
| 40 | + |
| 41 | + Node n1 = addNode(topology, 50, 50); |
| 42 | + Node n2 = addNode(topology, 80, 50); |
| 43 | + |
| 44 | + // This link duplicates the automatically generated wireless one |
| 45 | + Link wirelessLink = new Link(n1, n2, orientation, Link.Mode.WIRELESS); |
| 46 | + topology.addLink(wirelessLink); |
| 47 | + |
| 48 | + // This wired link is added twice |
| 49 | + Link wiredLink = new Link(n1, n2, orientation, Link.Mode.WIRED); |
| 50 | + topology.addLink(wiredLink); |
| 51 | + topology.addLink(wiredLink); |
| 52 | + |
| 53 | + // Another wired link which duplicates the previous one |
| 54 | + Link wiredLink1 = new Link(n1, n2, orientation, Link.Mode.WIRED); |
| 55 | + topology.addLink(wiredLink1); |
| 56 | + |
| 57 | + // Loops |
| 58 | + topology.addLink(new Link(n1, n1, orientation, Link.Mode.WIRED)); |
| 59 | + topology.addLink(new Link(n1, n1, orientation, Link.Mode.WIRELESS)); |
| 60 | + |
| 61 | + new JViewer(topology); |
| 62 | + |
| 63 | + addCommandListener(topology); |
| 64 | + listLinks(topology); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + public void run() { |
| 69 | + topology.start(); |
| 70 | + } |
| 71 | + |
| 72 | + private Node addNode(Topology topology, double x, double y) { |
| 73 | + Node n2 = new Node(); |
| 74 | + n2.setLocation(x, y); |
| 75 | + topology.addNode(n2); |
| 76 | + return n2; |
| 77 | + } |
| 78 | + |
| 79 | + private void addCommandListener(Topology topology) { |
| 80 | + String listLinksCommand = "List links"; |
| 81 | + topology.addCommand(listLinksCommand); |
| 82 | + topology.addCommandListener(new ListLinksCommandListener(listLinksCommand, topology)); |
| 83 | + } |
| 84 | + |
| 85 | + private class ListLinksCommandListener implements CommandListener { |
| 86 | + private final String listLinksCommand; |
| 87 | + private final Topology topology; |
| 88 | + |
| 89 | + public ListLinksCommandListener(String listLinksCommand, Topology topology) { |
| 90 | + this.listLinksCommand = listLinksCommand; |
| 91 | + this.topology = topology; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onCommand(String command) { |
| 96 | + if(listLinksCommand.equalsIgnoreCase(command)) |
| 97 | + listLinks(topology); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void listLinks(Topology topology) { |
| 102 | + for (Link link : topology.getLinks()) |
| 103 | + printLink(link); |
| 104 | + System.out.println(); |
| 105 | + } |
| 106 | + |
| 107 | + private static void printLink(Link link) { |
| 108 | + Node source = link.source; |
| 109 | + Node destination = link.destination; |
| 110 | + Link.Orientation orientation = link.orientation; |
| 111 | + String edgeString = (orientation == Link.Orientation.UNDIRECTED)? " <--> " : " --> "; |
| 112 | + Link.Mode mode = link.mode; |
| 113 | + |
| 114 | + System.out.println("[" + link.hashCode() + "] " + source + edgeString + destination + " " + mode); |
| 115 | + } |
| 116 | +} |
0 commit comments