来自文件的Java Graph。让HashMap记住从文件中读取时收集的对象属性

时间:2016-11-15 23:33:20

标签: java graph hashmap

我的buildMap函数出现问题,并在main方法中创建图形结构。我们非常感谢您提供的任何想法或帮助。我是编程和创建自己的Java类的新手,用于创建基于文件的图形数据结构,然后在其上执行一些旅行销售人员算法。

我的图形结构看起来像:

public class TSP_Graph {
// Hashmap where key is the geographic point and the pairs are of type   Office Node
// this connects each office node to a geographic point
private HashMap<GeographicPoint, OfficeNode> officeMap;

我从我的计算机上读取了一个文件,其中包含办公室的地理位置以及办公室中的一些数据,就像其中的顾问一样......

public static void buildMap(String filename, TSP_Graph map){
    // makes sure the reader is null
    BufferedReader reader = null;
    // neighbors is the hashset for keeping track of each officeNode created from the file.
    HashSet<OfficeNode> neighbors = new HashSet<OfficeNode>();
    Queue<OfficeNode> queue = new LinkedList<OfficeNode>();
    try{
        // nextline is a line to be parsed
        String nextLine;            

        reader = new BufferedReader(new FileReader(filename));
        while ((nextLine = reader.readLine()) != null){
            // this is pulling in the Office Node from the the line.  It should have the advisors added to the node but they are not coming into the map
            OfficeNode office = splitInputString(nextLine);

            map.addOffice(office.getLocation());
            queue.add(office);
            neighbors.add(office);
        }
        reader.close();         

    } catch (IOException e){
        System.err.println("Problem loading file");
    }
    OfficeNode next = null;

    while(!queue.isEmpty()){
        next = queue.remove();
        for (OfficeNode neighbor : neighbors){
            if(next.getLocation() != neighbor.getLocation()){
                map.addOfficeEdge(next.getLocation(), neighbor.getLocation());
                next.addEdge(new OfficeEdge(next, neighbor));
            }
        }
    }
}

我现在很困惑,似乎无法破解我的代码所发生的事情。当我用main方法调用加载函数时:

System.out.println("Number of Offices: " + firstMap.getNumOffices());
    System.out.println("Number of Edges: " + firstMap.getNumEdges());

    for (OfficeNode node : firstMap.officeMap.values()){
        System.out.println("Data collected from main method");
        System.out.println("Number of Advisors " + node.getNumAdvisors());
        System.out.println("Number of Edges " + node.getNumEdges());
        System.out.println("Number of Advisor AUM total " + node.getOfficeAUM());
        System.out.println("Location:  " + node.getLocation());
    }

我能够检索每个对象的地理位置,但使用getter方法获取OfficeNode对象中包含的所有其他数据的null。

在代码的文件加载部分中,我检查了数据并且所有内容都已完成。在我从buildMap函数和主要方法传输的某个地方,数据没有进行旅程。

感谢您的帮助。

这是我的splitInputString方法的代码:

private static OfficeNode splitInputString(String input){
    ArrayList<String> tokens = new ArrayList<String>();
    Pattern tokSplitter = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"");
    Matcher m = tokSplitter.matcher(input);

    while (m.find()) {
        if (m.group(1) != null) {
            tokens.add(m.group(1)); 
        }
        else {
            tokens.add(m.group());
        }
    }

    double lat1 = Double.parseDouble(tokens.get(0));
    double lon1 = Double.parseDouble(tokens.get(1));
    // seems to be working correctly here
    OfficeNode node = new OfficeNode(new GeographicPoint(lat1, lon1));

    int count = 3;
    while(count < tokens.size()){

        node.addAdvisor(new FinancialAdvisor(tokens.get(count)));
        count++;
    }

    return node;
}

对于它在OfficeNode类中的价值:

public class OfficeNode {
// member variables
private GeographicPoint location;
private HashSet<OfficeEdge> officeEdges;
// set of advisors
private HashSet<FinancialAdvisor> advisors;
private long officeAUM;

OfficeNode(GeographicPoint loc){
    location = loc;
    officeEdges = new HashSet<OfficeEdge>();
    advisors = new HashSet<FinancialAdvisor>();
    officeAUM = 0;
}

// functions
// add a new edge to the node
void addEdge(OfficeEdge edge){
    officeEdges.add(edge);
}
// add a new advisor to the officeNode
void addAdvisor(FinancialAdvisor advisor){
    advisors.add(advisor);
}


// check for two nodes being equal or null being compared
public boolean equals(Object o){
    if(!(o instanceof OfficeNode) || (o == null)){
        return false;
    }
    OfficeNode node = (OfficeNode) o;
    return node.location.equals(this.location);
}

// getters
GeographicPoint getLocation(){
    return location;
}

HashSet<OfficeEdge> getEdges(){
    return this.officeEdges;
}

Set<FinancialAdvisor> getAdvisors(){
    return this.advisors;
}

int getNumAdvisors(){
    return this.advisors.size();
}

int getNumEdges(){
    return this.officeEdges.size();
}

long getOfficeAUM(){
    for (FinancialAdvisor advisor : advisors){
        officeAUM += advisor.getBookSize();
    }
    return this.officeAUM;
}

}

0 个答案:

没有答案