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.algorithms.matrix.GraphMatrixOperations.java

/**
 * Creates a graph from a square (weighted) adjacency matrix.  If 
 * <code>nev</code> is non-null then it will be used to store the edge weights.
 * /*ww  w .j  a  va 2s.co m*/
 * <p>Notes on implementation: 
 * <ul>
 * <li>The matrix indices will be mapped onto vertices in the order in which the
 * vertex factory generates the vertices.  This means the user is responsible
 * <li>The type of edges created (directed or undirected) depends
 * entirely on the graph factory supplied, regardless of whether the 
 * matrix is symmetric or not.  The Colt {@code Property.isSymmetric} 
 * method may be used to find out whether the matrix
 * is symmetric prior to making this call.
 * <li>The matrix supplied need not be square.  If it is not square, then 
 * the 
 * 
 * @return a representation of <code>matrix</code> as a JUNG
 *         <code>Graph</code>
 */
public static <V, E> Graph<V, E> matrixToGraph(DoubleMatrix2D matrix,
        Factory<? extends Graph<V, E>> graphFactory, Factory<V> vertexFactory, Factory<E> edgeFactory,
        Map<E, Number> nev) {
    if (matrix.rows() != matrix.columns()) {
        throw new IllegalArgumentException("Matrix must be square.");
    }
    int size = matrix.rows();

    Graph<V, E> graph = graphFactory.create();

    for (int i = 0; i < size; i++) {
        V vertex = vertexFactory.create();
        graph.addVertex(vertex);
    }

    List<V> vertices = new ArrayList<V>(graph.getVertices());
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            double value = matrix.getQuick(i, j);
            if (value != 0) {
                E e = edgeFactory.create();
                if (graph.addEdge(e, vertices.get(i), vertices.get(j))) {
                    if (e != null && nev != null)
                        nev.put(e, value);
                }
            }
        }
    }

    return graph;
}

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

/**
 * Returns a random mixed-mode graph.  Starts with a randomly generated 
 * Barabasi-Albert (preferential attachment) generator 
 * (4 initial vertices, 3 edges added at each step, and num_vertices - 4 evolution steps).
 * Then takes the resultant graph, replaces random undirected edges with directed
 * edges, and assigns random weights to each edge.
 *///w w w .jav  a  2 s . co m
public static <V, E> Graph<V, E> generateMixedRandomGraph(Factory<Graph<V, E>> graphFactory,
        Factory<V> vertexFactory, Factory<E> edgeFactory, Map<E, Number> edge_weights, int num_vertices,
        boolean parallel, Set<V> seedVertices) {
    int seed = (int) (Math.random() * 10000);
    BarabasiAlbertGenerator<V, E> bag = new BarabasiAlbertGenerator<V, E>(graphFactory, vertexFactory,
            edgeFactory, 4, 3, //false, parallel, 
            seed, seedVertices);
    bag.evolveGraph(num_vertices - 4);
    Graph<V, E> ug = bag.create();

    // create a SparseMultigraph version of g
    Graph<V, E> g = graphFactory.create();
    //new SparseMultigraph<V, E>();
    for (V v : ug.getVertices()) {
        g.addVertex(v);
    }

    // randomly replace some of the edges by directed edges to 
    // get a mixed-mode graph, add random weights

    for (E e : ug.getEdges()) {
        V v1 = ug.getEndpoints(e).getFirst();
        V v2 = ug.getEndpoints(e).getSecond();

        E me = edgeFactory.create();
        g.addEdge(me, v1, v2, Math.random() < .5 ? EdgeType.DIRECTED : EdgeType.UNDIRECTED);
        edge_weights.put(me, Math.random());
    }

    return g;
}

From source file:com.diversityarrays.dalclient.httpandroid.AndroidDalHttpFactory.java

@Override
public DalRequest createForUpload(String url, List<Pair<String, String>> pairs, String rand_num,
        String namesInOrder, String signature, Factory<InputStream> factory) {

    MultipartEntityBuilder builder = MultipartEntityBuilder.create()
            .addPart("uploadfile", new InputStreamBody(factory.create(), "uploadfile"))
            .addTextBody("rand_num", rand_num).addTextBody("url", url);

    for (Pair<String, String> pair : pairs) {
        builder.addTextBody(pair.a, pair.b);
    }//from w  w  w  . jav  a2s.  c o m

    HttpEntity entity = builder.addTextBody("param_order", namesInOrder).addTextBody("signature", signature)
            .build();

    HttpPost post = new HttpPost(url);
    post.setEntity(entity);
    return new AndroidDalRequest(post);
}

From source file:com.diversityarrays.dalclient.httpimpl.DalHttpFactoryImpl.java

@Override
public DalRequest createForUpload(String url, List<Pair<String, String>> pairs, String rand_num,
        String namesInOrder, String signature, Factory<InputStream> factory) {

    MultipartEntityBuilder builder = MultipartEntityBuilder.create()
            .addPart("uploadfile", new InputStreamBody(factory.create(), "uploadfile"))
            .addTextBody("rand_num", rand_num).addTextBody("url", url);

    for (Pair<String, String> pair : pairs) {
        builder.addTextBody(pair.a, pair.b);
    }/*from   w  w  w .j a va2  s . com*/

    HttpEntity entity = builder.addTextBody("param_order", namesInOrder).addTextBody("signature", signature)
            .build();

    HttpPost post = new HttpPost(url);
    post.setEntity(entity);
    return new DalRequestImpl(post);
}

From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformer.java

/**
 * Creates a <code>Graph</code> which is a vertex-folded version of <code>h</code>, whose
 * vertices are the input's hyperedges and whose edges are induced by adjacent hyperedges
 * in the input.// w  ww  .ja v a2  s  .  c  om
 * 
 * <p>The vertices of the new graph are the same objects as the hyperedges of 
 * <code>h</code>, and <code>a</code> 
 * is connected to <code>b</code> in the new graph if the corresponding edges
 * in <code>h</code> have a vertex in common.  Thus, each vertex incident to  
 * <i>k</i> edges in <code>h</code> induces a <i>k</i>-clique in the new graph.</p>
 * 
 * <p>The edges of the new graph consist of collections of each vertex incident to 
 * the corresponding hyperedge pair in the original graph.</p>
 * 
 * @param h hypergraph to be folded
 * @param graph_factory factory used to generate the output graph
 * @return a transformation of the input graph whose vertices correspond to the input's hyperedges 
 * and edges are induced by hyperedges sharing vertices in the input
 */
public Graph<E, Collection<V>> foldHypergraphVertices(Hypergraph<V, E> h,
        Factory<Graph<E, Collection<V>>> graph_factory) {
    Graph<E, Collection<V>> target = graph_factory.create();

    for (E e : h.getEdges())
        target.addVertex(e);

    for (V v : h.getVertices()) {
        ArrayList<E> incident = new ArrayList<E>(h.getIncidentEdges(v));
        populateTarget(target, v, incident);
    }
    return target;
}

From source file:com.diversityarrays.dalclient.HttpPostBuilder.java

/**
 * Create an HttpPost instance for uploading an InputStream using the other supplied
 * parameters, ResponseType etc./*from w w w . j  a  va 2 s  .  co m*/
 * @param writeKey
 * @param factory Factory&lt;InputStream&gt;
 * @return an DalRequest instance
 */
public DalRequest buildForUpload(String writeKey, Factory<InputStream> factory) {
    String rand_num = DalUtil.createRandomNumberString();
    String md5 = DalUtil.computeMD5checksum(factory.create());

    StringBuilder dataForSignature = new StringBuilder(dalCommandUrl);
    dataForSignature.append(rand_num);

    StringBuilder namesInOrderBuilder = new StringBuilder();
    for (Pair<String, String> pair : collectedPairs) {
        dataForSignature.append(pair.b);
        namesInOrderBuilder.append(pair.a).append(',');
    }
    dataForSignature.append(md5);
    String namesInOrder = namesInOrderBuilder.toString();

    String signature = DalUtil.computeHmacSHA1(writeKey, dataForSignature.toString());

    if (log != null && log.isDebugEnabled()) {
        log.debug(this.getClass().getName() + ".buildForUpload(" + writeKey + " , InputStream )"); //$NON-NLS-1$ //$NON-NLS-2$
        log.debug("  dataForSignature=" + dataForSignature); //$NON-NLS-1$
        log.debug("  param_order=" + namesInOrder); //$NON-NLS-1$
        log.debug("  signature=" + signature); //$NON-NLS-1$
    }

    return dalHttpFactory.createForUpload(dalCommandUrl, collectedPairs, rand_num, namesInOrder, signature,
            factory);
}

From source file:net.itransformers.ws.rs.TopologyManagementResource.java

@Context
private <G extends Hypergraph<String, String>> void init(Factory<G> factory, File graphmlFile)
        throws IOException, SAXException, ParserConfigurationException {
    gmlr = new GraphMLReader(null, null);
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(graphmlFile)));
    graph = factory.create();
    gmlr.load(in, graph);// w  w w  .jav  a  2 s  . c om

}

From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformerFixed.java

/**
 * Creates a <code>Graph</code> which is a vertex-folded version of <code>h</code>, whose
 * vertices are the input's hyperedges and whose edges are induced by adjacent hyperedges
 * in the input.//from ww w.j a v a  2 s  .c o  m
 * 
 * <p>The vertices of the new graph are the same objects as the hyperedges of 
 * <code>h</code>, and <code>a</code> 
 * is connected to <code>b</code> in the new graph if the corresponding edges
 * in <code>h</code> have a vertex in common.  Thus, each vertex incident to  
 * <i>k</i> edges in <code>h</code> induces a <i>k</i>-clique in the new graph.</p>
 * 
 * <p>The edges of the new graph consist of collections of each vertex incident to 
 * the corresponding hyperedge pair in the original graph.</p>
 * 
 * @param h hypergraph to be folded
 * @param graph_factory factory used to generate the output graph
 * @return a transformation of the input graph whose vertices correspond to the input's hyperedges 
 * and edges are induced by hyperedges sharing vertices in the input
 */
public UndirectedGraph<E, FoldedEdge<E, V>> foldHypergraphVertices(Hypergraph<V, E> h,
        Factory<UndirectedGraph<E, FoldedEdge<E, V>>> graph_factory) {
    UndirectedGraph<E, FoldedEdge<E, V>> target = graph_factory.create();

    for (E e : h.getEdges())
        target.addVertex(e);

    for (V v : h.getVertices()) {
        ArrayList<E> incident = new ArrayList<E>(h.getIncidentEdges(v));
        populateTarget(target, v, incident);
    }
    return target;
}

From source file:edu.uci.ics.jung.algorithms.matrix.GraphMatrixOperations.java

/**
 * Returns the graph that corresponds to the square of the (weighted)
 * adjacency matrix that the specified graph <code>g</code> encodes. The
 * implementation of MatrixElementOperations that is furnished to the
 * constructor specifies the implementation of the dot product, which is an
 * integral part of matrix multiplication.
 * /* ww  w. j  a va2 s  .c o  m*/
 * @param g
 *            the graph to be squared
 * @return the result of squaring g
 */
@SuppressWarnings("unchecked")
public static <V, E> Graph<V, E> square(Graph<V, E> g, Factory<E> edgeFactory, MatrixElementOperations<E> meo) {
    // create new graph of same type
    Graph<V, E> squaredGraph = null;
    try {
        squaredGraph = g.getClass().newInstance();
    } catch (InstantiationException e3) {
        e3.printStackTrace();
    } catch (IllegalAccessException e3) {
        e3.printStackTrace();
    }

    Collection<V> vertices = g.getVertices();
    for (V v : vertices) {
        squaredGraph.addVertex(v);
    }
    for (V v : vertices) {
        for (V src : g.getPredecessors(v)) {
            // get the edge connecting src to v in G
            E e1 = g.findEdge(src, v);
            for (V dest : g.getSuccessors(v)) {
                // get edge connecting v to dest in G
                E e2 = g.findEdge(v, dest);
                // collect data on path composed of e1 and e2
                Number pathData = meo.computePathData(e1, e2);
                E e = squaredGraph.findEdge(src, dest);
                // if no edge from src to dest exists in G2, create one
                if (e == null) {
                    e = edgeFactory.create();
                    squaredGraph.addEdge(e, src, dest);
                }
                meo.mergePaths(e, pathData);
            }
        }
    }
    return squaredGraph;
}

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

/**
 * Returns the graph created by parsing the specified reader, as created by
 * the specified factory.//  w  w w  . ja  va2 s.  com
 *
 * @throws IOException
 */
public G load(Reader reader, Factory<? extends G> graph_factory) throws IOException {
    return load(reader, graph_factory.create());
}