Package com.neptuny.xgrapher.cli.mapping

Source Code of com.neptuny.xgrapher.cli.mapping.DefaultMapping

/*******************************************************************************
*   Copyright 2007 Neptuny s.r.l. - www.neptuny.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*******************************************************************************/
package com.neptuny.xgrapher.cli.mapping;

import com.neptuny.xgrapher.cli.controller.GraphAttributeType;
import com.neptuny.xgrapher.gen.model.Node;

import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.impl.DirectedSparseEdge;
import edu.uci.ics.jung.graph.impl.DirectedSparseGraph;
import edu.uci.ics.jung.graph.impl.DirectedSparseVertex;
import edu.uci.ics.jung.utils.UserData;

/**
*
* @author battlehorse
*/
public class DefaultMapping implements XGrapherJungMapping {

    public Vertex toJung(Node n, Graph g) {
        Vertex jungVertex = new DirectedSparseVertex();
        jungVertex.setUserDatum(GraphAttributeType.ID, protectNull(n.getId()), UserData.SHARED);
        jungVertex.setUserDatum(GraphAttributeType.NAME, protectNull(n.getName()), UserData.SHARED);
        jungVertex.setUserDatum(GraphAttributeType.DESCRIPTION, protectNull(n.getDescription()),
                        UserData.SHARED);
        jungVertex.setUserDatum(GraphAttributeType.DEEPNESS, n.getDeepness(), UserData.SHARED);

        if (n.getMetricAttribute() != null && n.getMetricAttribute().size() > 0)
            if (n.getMetricAttribute().get(0).getName().equals("SIZE"))
                jungVertex.addUserDatum(
                        GraphAttributeType.SIZE,
                        n.getMetricAttribute().get(0).getValue(),
                        UserData.SHARED);
       
//        System.out.println("Converting " + n.getId()) ;
        return jungVertex;
    }

    public Edge toJung(com.neptuny.xgrapher.gen.model.Edge e, Graph g) {
        Vertex v1 = null, v2 = null;
        for (Object o :  g.getVertices()) {
            Vertex v = (Vertex) o ;
            if (e.getNodeFromId().equals(v.getUserDatum(GraphAttributeType.ID)))
                v1 = v ;
            if (e.getNodeToId().equals(v.getUserDatum(GraphAttributeType.ID)))
                v2 = v;
        }
        assert v1 != null ;
        assert v2 != null ;
       
        DirectedSparseEdge jungEdge = new DirectedSparseEdge(v1, v2);
        jungEdge.setUserDatum(GraphAttributeType.ID, protectNull(e.getId()), UserData.SHARED);
        jungEdge.setUserDatum(GraphAttributeType.NAME, protectNull(e.getName()), UserData.SHARED);
        jungEdge.setUserDatum(GraphAttributeType.DESCRIPTION, protectNull(e.getDescription()),
                        UserData.SHARED);
        jungEdge.setUserDatum(GraphAttributeType.DEEPNESS, e.getDeepness(), UserData.SHARED);
       
        if (e.getMetricAttribute() != null && e.getMetricAttribute().size() > 0)
            if (e.getMetricAttribute().get(0).getName().equals("SIZE"))
                jungEdge.addUserDatum(
                        GraphAttributeType.SIZE,
                        e.getMetricAttribute().get(0).getValue(),
                        UserData.SHARED);
       
//        System.out.println("Converting " + e.getId()) ;
        return jungEdge;
    }

    public Graph toJung(com.neptuny.xgrapher.gen.model.Graph g) {
        DirectedSparseGraph jungGraph = new DirectedSparseGraph();
       
        for (Node n : g.getNode()) {
            jungGraph.addVertex(toJung(n,jungGraph));
        }
       
        for (com.neptuny.xgrapher.gen.model.Edge e : g.getEdge()) {
            jungGraph.addEdge(toJung(e, jungGraph));
        }
       
        return jungGraph ;
    }

    public com.neptuny.xgrapher.gen.model.Edge toXGrapher(Edge e) {
        com.neptuny.xgrapher.gen.model.Edge graEdge = new com.neptuny.xgrapher.gen.model.Edge();
        graEdge.setId(e.getUserDatum(GraphAttributeType.ID).toString());
        graEdge.setName(e.getUserDatum(GraphAttributeType.NAME).toString());
        graEdge.setDescription(e.getUserDatum(GraphAttributeType.DESCRIPTION).toString());
        graEdge.setDeepness((Integer) e.getUserDatum(GraphAttributeType.DEEPNESS));

        return graEdge;
    }

    public Node toXGrapher(Vertex v) {
        Node graNode = new Node();
        graNode.setId(v.getUserDatum(GraphAttributeType.ID).toString());
        graNode.setName(v.getUserDatum(GraphAttributeType.NAME).toString());
        graNode.setDescription(v.getUserDatum(GraphAttributeType.DESCRIPTION).toString());
        graNode.setDeepness((Integer) v.getUserDatum(GraphAttributeType.DEEPNESS));

        return graNode;
    }

    public com.neptuny.xgrapher.gen.model.Graph toXGrapher(Graph g) {
        throw new UnsupportedOperationException("Not yet supported.");
    }
   
    private String protectNull(String value) {
        return value == null ? "" : value ;
    }



}
TOP

Related Classes of com.neptuny.xgrapher.cli.mapping.DefaultMapping

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.