Example usage for org.apache.commons.collections15 Factory create

List of usage examples for org.apache.commons.collections15 Factory create

Introduction

In this page you can find the example usage for org.apache.commons.collections15 Factory create.

Prototype

public T create();

Source Link

Document

Create a new object.

Usage

From source file:edu.uci.ics.jung.io.CustomPajekNetReader.java

/**
 * Returns the graph created by parsing the specified file, as created by
 * the specified factory./*  w w w .j  a  va2s. c  o m*/
 *
 * @throws IOException
 */
public G load(String filename, Factory<? extends G> graph_factory) throws IOException {
    return load(new FileReader(filename), graph_factory.create());
}

From source file:edu.uci.ics.jung.algorithms.flows.TestEdmondsKarpMaxFlow.java

public void testAnotherSimpleFlow() {
    DirectedGraph<Number, Number> graph = new DirectedSparseMultigraph<Number, Number>();
    Factory<Number> edgeFactory = new Factory<Number>() {
        int count = 0;

        public Number create() {
            return count++;
        }/*from   w  w w.  j a v  a 2  s. c o  m*/
    };

    Map<Number, Number> edgeCapacityMap = new HashMap<Number, Number>();
    for (int i = 0; i < 6; i++) {
        graph.addVertex(i);
    }

    Map<Number, Number> edgeFlowMap = new HashMap<Number, Number>();

    graph.addEdge(edgeFactory.create(), 0, 1, EdgeType.DIRECTED);
    edgeCapacityMap.put(0, 5);

    graph.addEdge(edgeFactory.create(), 0, 2, EdgeType.DIRECTED);
    edgeCapacityMap.put(1, 3);

    graph.addEdge(edgeFactory.create(), 1, 5, EdgeType.DIRECTED);
    edgeCapacityMap.put(2, 2);

    graph.addEdge(edgeFactory.create(), 1, 2, EdgeType.DIRECTED);
    edgeCapacityMap.put(3, 8);

    graph.addEdge(edgeFactory.create(), 2, 3, EdgeType.DIRECTED);
    edgeCapacityMap.put(4, 4);

    graph.addEdge(edgeFactory.create(), 2, 4, EdgeType.DIRECTED);
    edgeCapacityMap.put(5, 2);

    graph.addEdge(edgeFactory.create(), 3, 4, EdgeType.DIRECTED);
    edgeCapacityMap.put(6, 3);

    graph.addEdge(edgeFactory.create(), 3, 5, EdgeType.DIRECTED);
    edgeCapacityMap.put(7, 6);

    graph.addEdge(edgeFactory.create(), 4, 5, EdgeType.DIRECTED);
    edgeCapacityMap.put(8, 1);

    EdmondsKarpMaxFlow<Number, Number> ek = new EdmondsKarpMaxFlow<Number, Number>(graph, 0, 5,
            MapTransformer.<Number, Number>getInstance(edgeCapacityMap), edgeFlowMap, edgeFactory);
    ek.evaluate();

    assertTrue(ek.getMaxFlow() == 7);
}

From source file:edu.uci.ics.jung.algorithms.flows.TestEdmondsKarpMaxFlow.java

public void testSimpleFlow() {
    DirectedGraph<Number, Number> graph = new DirectedSparseMultigraph<Number, Number>();
    Factory<Number> edgeFactory = new Factory<Number>() {
        int count = 0;

        public Number create() {
            return count++;
        }/*from   www  .j  a  v a  2s.  c  o  m*/
    };

    Map<Number, Number> edgeCapacityMap = new HashMap<Number, Number>();
    for (int i = 0; i < 6; i++) {
        graph.addVertex(i);
    }

    Map<Number, Number> edgeFlowMap = new HashMap<Number, Number>();

    graph.addEdge(edgeFactory.create(), 0, 1, EdgeType.DIRECTED);
    edgeCapacityMap.put(0, 16);

    graph.addEdge(edgeFactory.create(), 0, 2, EdgeType.DIRECTED);
    edgeCapacityMap.put(1, 13);

    graph.addEdge(edgeFactory.create(), 1, 2, EdgeType.DIRECTED);
    edgeCapacityMap.put(2, 6);

    graph.addEdge(edgeFactory.create(), 1, 3, EdgeType.DIRECTED);
    edgeCapacityMap.put(3, 12);

    graph.addEdge(edgeFactory.create(), 2, 4, EdgeType.DIRECTED);
    edgeCapacityMap.put(4, 14);

    graph.addEdge(edgeFactory.create(), 3, 2, EdgeType.DIRECTED);
    edgeCapacityMap.put(5, 9);

    graph.addEdge(edgeFactory.create(), 3, 5, EdgeType.DIRECTED);
    edgeCapacityMap.put(6, 20);

    graph.addEdge(edgeFactory.create(), 4, 3, EdgeType.DIRECTED);
    edgeCapacityMap.put(7, 7);

    graph.addEdge(edgeFactory.create(), 4, 5, EdgeType.DIRECTED);
    edgeCapacityMap.put(8, 4);

    EdmondsKarpMaxFlow<Number, Number> ek = new EdmondsKarpMaxFlow<Number, Number>(graph, 0, 5,
            MapTransformer.<Number, Number>getInstance(edgeCapacityMap), edgeFlowMap, edgeFactory);
    ek.evaluate();

    assertTrue(ek.getMaxFlow() == 23);
    Set<Number> nodesInS = ek.getNodesInSourcePartition();
    assertEquals(4, nodesInS.size());

    for (Number v : nodesInS) {
        Assert.assertTrue(v.intValue() != 3 && v.intValue() != 5);
    }

    Set<Number> nodesInT = ek.getNodesInSinkPartition();
    assertEquals(2, nodesInT.size());

    for (Number v : nodesInT) {
        Assert.assertTrue(v.intValue() == 3 || v.intValue() == 5);
    }

    Set<Number> minCutEdges = ek.getMinCutEdges();
    int maxFlow = 0;
    for (Number e : minCutEdges) {
        Number flow = edgeFlowMap.get(e);
        maxFlow += flow.intValue();
    }
    Assert.assertEquals(23, maxFlow);
    Assert.assertEquals(3, minCutEdges.size());
}

From source file:edu.uci.ics.jung.io.PajekNetReader.java

@SuppressWarnings("unchecked")
protected E createAddEdge(StringTokenizer st, V v1, EdgeType directed, Graph<V, E> g, List<V> id,
        Factory<E> edge_factory) {
    int vid2 = Integer.parseInt(st.nextToken()) - 1;
    V v2;/*from  ww  w.j a  v  a  2s .c o m*/
    if (id != null)
        v2 = id.get(vid2);
    else
        v2 = (V) new Integer(vid2);
    E e = edge_factory.create();

    // don't error-check this: let the graph implementation do whatever it's going to do 
    // (add the edge, replace the existing edge, throw an exception--depends on the graph implementation)
    g.addEdge(e, v1, v2, directed);
    return e;
}

From source file:edu.uci.ics.jung.io.CustomPajekNetReader.java

@SuppressWarnings("unchecked")
protected E createAddEdge(StringTokenizer st, V v1, EdgeType directed, Graph<V, E> g, List<V> id,
        Factory<E> edge_factory) {
    int vid2 = Integer.parseInt(st.nextToken()) - 1;
    V v2;/*from  w ww  . j  ava2  s  .  c  o m*/
    if (id != null) {
        v2 = id.get(vid2);
    } else {
        v2 = (V) new Integer(vid2);
    }
    E e = edge_factory.create();

    // don't error-check this: let the graph implementation do whatever it's going to do 
    // (add the edge, replace the existing edge, throw an exception--depends on the graph implementation)
    g.addEdge(e, v1, v2, directed);
    return e;
}

From source file:net.itransformers.topologyviewer.gui.GraphViewerPanelManager.java

public GraphViewerPanelManager(TopologyManagerFrame frame, String projectType, File projectPath,
        TopologyViewerConfigManager viewerConfigPath, File graphmlFile, Factory<G> factory,
        JTabbedPane tabbedPane, GraphType graphType, GraphViewerPanelFactory graphViewerPanelFactory)
        throws Exception {
    this.frame = frame;
    this.projectPath = projectPath;
    this.graphType = graphType;
    this.viewerConfigManager = viewerConfigPath;
    versionDir = new File(new File(graphmlFile.getParent()).getParent());
    // TODO remove this Hardcode
    this.deviceXmlPath = versionDir;
    this.graphmlFileName = graphmlFile;
    this.factory = factory;
    this.tabbedPane = tabbedPane;
    entireGraph = factory.create();
    viewerConfig = viewerConfigManager.getTopologyViewerConfType();
    this.layout = "FRLayout";
    this.graphViewerPanelFactory = graphViewerPanelFactory;
}

From source file:edu.uci.ics.jung.algorithms.generators.Lattice2DGenerator.java

/**
 * Creates a generator of {@code row_count} x {@code col_count} lattices 
 * with the specified parameters.//from  www .  j  av  a  2 s  .c o m
 * 
 * @param graph_factory used to create the {@code Graph} for the lattice
 * @param vertex_factory used to create the lattice vertices
 * @param edge_factory used to create the lattice edges
 * @param row_count the number of rows in the lattice
 * @param col_count the number of columns in the lattice
 * @param isToroidal if true, the created lattice wraps from top to bottom and left to right
 */
public Lattice2DGenerator(Factory<? extends Graph<V, E>> graph_factory, Factory<V> vertex_factory,
        Factory<E> edge_factory, int row_count, int col_count, boolean isToroidal) {
    if (row_count < 2 || col_count < 2) {
        throw new IllegalArgumentException("Row and column counts must each be at least 2.");
    }

    this.row_count = row_count;
    this.col_count = col_count;
    this.is_toroidal = isToroidal;
    this.graph_factory = graph_factory;
    this.vertex_factory = vertex_factory;
    this.edge_factory = edge_factory;
    this.is_directed = (graph_factory.create().getDefaultEdgeType() == EdgeType.DIRECTED);
}

From source file:nubisave.component.graph.splitteradaption.NubisaveEditor.java

protected void interconnectNubisaveComponents(Graph<AbstractNubisaveComponent, Object> nubisaveComponentGraph,
        Factory<? extends NubiSaveEdge> edgeFactory) {
    boolean connected = false;
    WeightedNubisaveVertexEdge edge;/* w w  w  . ja  v a 2  s. c om*/
    File file = new File(storage_directory + "/" + "connections.txt");
    HashMap hh = new HashMap();
    ArrayList<AbstractNubisaveComponent> myNodeList = new ArrayList<AbstractNubisaveComponent>(
            nubisaveComponentGraph.getVertices());
    ArrayList str = new ArrayList();
    for (int i = 0; i < nubisaveComponentGraph.getVertices().size(); i++) {
        str.add(myNodeList.get(i).getUniqueName());
    }
    if (file.exists()) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String strLine;
            try {
                //Read the text file Line By Line
                while ((strLine = reader.readLine()) != null) {
                    String startVertex = strLine.split(" ")[0];
                    String endVertex = strLine.split(" ")[1];
                    hh.put(startVertex, endVertex);

                    for (AbstractNubisaveComponent component : nubisaveComponentGraph.getVertices()) {
                        if (component.getUniqueName().equals(startVertex)) {
                            String endpoint = (String) hh.get(startVertex);
                            int pos = str.indexOf(endpoint);
                            AbstractNubisaveComponent endcomponent = myNodeList.get(pos);
                            edge = (WeightedNubisaveVertexEdge) edgeFactory.create();
                            edge.setWeight(component.getNrOfFilePartsToStore());
                            graph.addEdge(edge, component.getRequiredPorts().iterator().next(),
                                    endcomponent.getProvidedPorts().iterator().next(), EdgeType.DIRECTED);
                        }
                    }
                }
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(NubisaveEditor.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NubisaveEditor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}