java关键路径算法数据库实现,不匹配

8fsztsew  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(281)

所以我有一个关键路径算法,它的功能是使用向后传递。现在的问题是我有两个变体。一个是我必须提供的数据,另一个是使用sqlite数据库的实现。我使用构造函数定义散列集变量以向关键路径算法提供数据,并收到以下正确的输出。

Critical Path: [Fourth Task: 17, Second Task: 9, Fifth Task: 8, Third Task: 8, First Task: 5, End: 0]

此代码:

public static void main(String[] args) {
    //The example dependency graph from
    HashSet<Task> allTasks = new HashSet<Task>();
    Task end = new Task("End", 0);
    Task F = new Task("F", 2, end);
    Task A = new Task("A", 3, end);
    Task X = new Task("X", 4, F, A);
    Task Q = new Task("Q", 2, A, X);
    Task start = new Task("Start", 0, Q);

    allTasks.add(end);
    allTasks.add(F);
    allTasks.add(A);
    allTasks.add(X);
    allTasks.add(Q);
    allTasks.add(start);
    System.out.println("Critical Path: "+Arrays.toString(criticalPath(allTasks)));
}

现在,使用我的数据库的关键路径算法的结果如下所示,但与第一个结果不匹配:

[fourth task: 12, second task: 9, fifth task: 8, third task: 8, first task: 5, first task: 5, first task: 5, third task: 3, none: 0, none: 0]

此结果的以下代码是:

public static HashSet<Task> testingData () {
        //Connecting to database
        Connection con = DbConnection.connect();
        //Preparing statement
        PreparedStatement ps = null;
        //result set declaration
        ResultSet rs = null;
        HashSet <Task> allTasks = new HashSet<>();

        try {
            //Database initialization
            String sql = "SELECT * FROM ProjectInfo";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                //for each iteration store the data into variable a from column projectName
                String a = rs.getString("projectName");
                int b = rs.getInt("cost");
                String c = rs.getString("dependency");
                int d = rs.getInt("dependencyCost");

                if (c.equals("none")) {

                    Task end = new Task ("End",d);
                    Task A = new Task(a,b,end);
                    allTasks.add(end);
                    allTasks.add(A);

                } else {

                    Task dep = new Task (c,d);
                    Task A = new Task (a,b,dep);
                    allTasks.add(dep);
                    allTasks.add(A);

                }

            }
            //other catch exceptions
        } catch (SQLException e) {
            System.out.println(e.toString());
        } finally {
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e){
                System.out.println(e.toString());
            }
        }

        return allTasks;
    }

尝试使用不同的条件,但最终失败了,不知道这里到底发生了什么,所以如果有人能帮助我就太好了。我还将提供一个用于生成算法的算法和构造函数的gist链接:
https://gist.github.com/jabz259/bda49835eaa2e3d5f8834c07e870cc50

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题