如何从预定义的类集创建我的类型的Java对象?

时间:2018-01-18 12:19:38

标签: java

我需要提示如何解决我遇到的这个问题,所以我可以就此开始。

我有这三个类(无法更改或修改):

  • GraphDescriptor
  • NodeDescriptor
  • LinkDescriptor

GraphDescriptor:

public class GraphDescriptor {
    public Set<NodeDescriptor> nodes;

    public GraphDescriptor() {
        nodes = new HashSet<NodeDescriptor>();
    }

    public Set<NodeDescriptor> getNodes() {
        return nodes;
    }

    public void setNodes(Set<NodeDescriptor> nodes) {
        this.nodes = nodes;
    }
}

NodeDescriptor:

public class NodeDescriptor {
    private Set<LinkDescriptor> links;

    public NodeDescriptor() {
        links = new HashSet<LinkDescriptor>();
    }

    public Set<LinkDescriptor> getLinks() {
        return links;
    }

    public void setLinks(Set<LinkDescriptor> links) {
        this.links = links;
    }
}

LinkDescriptor:

public class LinkDescriptor {
    private NodeDescriptor sourceNode; //a Descriptor for the source node of the link
    private NodeDescriptor destinationNode; // a Descriptor for the destination node of the link

    public LinkDescriptor() {
    }

    public NodeDescriptor getSourceNode() {
        return sourceNode;
    }

    public void setSourceNode(NodeDescriptor sourceNode) {
        this.sourceNode = sourceNode;
    }

    public NodeDescriptor getDestinationNode() {
        return destinationNode;
    }

    public void setDestinationNode(NodeDescriptor destinationNode) {
        this.destinationNode = destinationNode;
    }

}

我有我的课程myGraph,Nodes,Links,Node和Link:

myGraph:

        public class MyGraph {

            protected Nodes nodes;
            protected Links links;
            protected String name;

            public Nodes getNodes() {
                return nodes;
            }

            public void setNodes(Nodes value) {
                this.nodes = value;
            }

            public Links getLinks() {
                return links;
            }

           public void setLinks(Links value) {
                this.links = value;
            }

            public String getName() {
                return name;
            }

            public void setName(String value) {
                this.name = value;
           }
    }

节点

    public class Nodes {

        protected List<Nodes.Node> node;

        public List<Nodes.Node> getNode() {
            if (node == null) {
                node = new ArrayList<Nodes.Node>();
            }
            return this.node;
        }

节点

        public static class Node {

            protected String name;

            public String getName() {
                return name;
            }

            public void setName(String value) {
                this.name = value;
            }
    }

链接:

        public class Links {

            protected List<Links.Link> link;

            public List<Links.Link> getLink() {
                if (link == null) {
                    link = new ArrayList<Links.Link>();
                }
                return this.link;
            }
    }

链接

public static class Link {

        protected String name;
        protected String source;
        protected String destination;


        public String getName() {
            return name;
        }

        public void setName(String value) {
            this.name = value;
        }
        public String getSource() {
            return source;
        }

        public void setSource(String value) {
            this.source = value;
        }

        public String getDestination() {
            return destination;
        }

        public void setDestination(String value) {
            this.destination = value;
        }
}

我需要创建一个我的类型的新图形对象(myGraph),给出一个 GraphDescriptor 的实例。

我遇到的问题是由于描述符(节点或链接)没有名称,我找不到互连它们的方法。

例如:首先,我可以从创建MyGraph图开始,然后我可以为属于GraphDescriptor集的每个NodeDescriptor创建一个新节点。对于每个NodeDescripor,我可以循环遍历其所有的LinkDescriptors,将此Node设置为源并创建一个新的Node作为目标。依此类推,直到最后一个节点,但最后我将有单独的“子系统”(每次迭代):一个节点与每个迭代的节点的目的地连接,但不是一个完整的互连系统,因为我不知道如何链接他们。

我还考虑过创建一个新类,例如Node和NodeDescriptor。

编辑:我建议的解决方案:

public class TEST {

    public static void main(String[] args) {

        GraphDescriptor graphD = new GraphDescriptor();
        Set<NodeDescriptor> nodesD = new HashSet<NodeDescriptor>();

        //NODE1
        NodeDescriptor nodeD1 = new NodeDescriptor();
        NodeDescriptor nodeD2 = new NodeDescriptor();
        NodeDescriptor nodeD3 = new NodeDescriptor();

        Set<LinkDescriptor> linksD1 = new HashSet<LinkDescriptor>();
        Set<LinkDescriptor> linksD2 = new HashSet<LinkDescriptor>();
        Set<LinkDescriptor> linksD3 = new HashSet<LinkDescriptor>();

        LinkDescriptor linkD12 = new LinkDescriptor();
        LinkDescriptor linkD13 = new LinkDescriptor();

        //LINKD12
        linkD12.setSourceNode(nodeD1);
        linkD12.setDestinationNode(nodeD2);

        //LINK13
        linkD13.setSourceNode(nodeD1);
        linkD13.setDestinationNode(nodeD3);     

        linksD1.add(linkD12);
        linksD1.add(linkD13);

        nodeD1.setLinks(linksD1);

        //NODE2
        LinkDescriptor linkD23 = new LinkDescriptor();
        linkD23.setSourceNode(nodeD2);
        linkD23.setDestinationNode(nodeD3);
        linksD2.add(linkD23);

        nodeD2.setLinks(linksD2);

        //NODE3
        LinkDescriptor linkD31 = new LinkDescriptor();
        linkD31.setSourceNode(nodeD3);
        linkD31.setDestinationNode(nodeD1);

        linksD3.add(linkD31);

        nodeD3.setLinks(linksD3);

        nodesD.add(nodeD1);
        nodesD.add(nodeD2);
        nodesD.add(nodeD3);

        graphD.setNodes(nodesD);


        MyGraph mygraph = convertGraph(graphD, "myGraph");

        System.out.println("MY GRAPH");

        //print NODES
        System.out.println("NODES");
        for(Node node: mygraph.getNodes().getNode()){
            System.out.println(node.getName());
        }

        //print LINKS
        System.out.println();
        for(Link link: mygraph.getLinks().getLink()) {
            System.out.println(link.getName()+" Source: "+link.getSource()+" Destination: "+ link.getDestination() );
        }

    }

    public static MyGraph convertGraph(GraphDescriptor graphD, String name) {

        MyGraph myGraph = new MyGraph();
        Nodes nodes = new Nodes();
        Links links = new Links();  

        Map<NodeDescriptor,String> nodeDName = new HashMap<NodeDescriptor,String>();

        myGraph.setName(name);

        int nodeN = 1;
        for(NodeDescriptor nodeD : graphD.getNodes()) {

            Node node = new Node();
            node.setName("Node"+nodeN);
            nodes.getNode().add(node);

            nodeDName.put(nodeD, node.getName());
            nodeN++;
        }

        int linkN = 1;
        for(NodeDescriptor nodeD : graphD.getNodes()) {

            for (LinkDescriptor linkD : nodeD.getLinks()) {

                Link link = new Link();

                link.setName("Link"+linkN);
                link.setDestination(nodeDName.get(linkD.getDestinationNode()));
                link.setSource(nodeDName.get(linkD.getSourceNode()));

                linkN++;
                links.getLink().add(link);
            }
        }

        myGraph.setLinks(links);
        myGraph.setNodes(nodes);
        return myGraph;
    }

}

0 个答案:

没有答案