如何将Java数组转换为序言列表并使用它进行jpl查询?

时间:2019-02-14 11:06:14

标签: java prolog jpl

我正在用Java编写数独求解器,其核心是使用一个小的prolog kb。序言“ sudoku”规则需要列表的序言列表。在Java中,我具有数独值的int [] []。

我已使用列表的序言列表使查询成功运行 例如Query q1 = new Query("problem(1, Rows), sudoku(Rows).");,其中Rows是列表的序言列表, 但我还需要使其与Java int [] []一起运行 例如Query q1 = new Query("sudoku", intArrayTerm);

相关的Java代码:

    int s00 = parseTextField(t00);
    int s01 = parseTextField(t01);
    ...
    int s87 = parseTextField(t87);
    int s88 = parseTextField(t88);

    int[] row0 = {s00, s10, s20, s30, s40, s50, s60, s70, s80};
    ...
    int[] row8 = {s08, s18, s28, s38, s48, s58, s68, s78, s88};

    int[][] allRows = {row0, row1, row2, row3, row4, row5, row6, row7, row8};

    Term rowsTerm = Util.intArrayArrayToList(allRows);
    Query q0 = new Query("consult", new Term[]{new Atom("/home/mark/Documents/JavaProjects/SudokuSolver/src/com/company/sudoku.pl")});
    System.out.println("consult " + (q0.hasSolution() ? "succeeded" : "failed"));

//        Query q1 = new Query("problem(1, Rows), sudoku(Rows).");
    Query q1 = new Query("sudoku", rowsTerm);
    System.out.println("sudoku " + (q1.hasSolution() ? "succeeded" : "failed"));

    Map<String, Term> rowsTermMap = q1.oneSolution();
    Term solvedRowsTerm = (rowsTermMap.get("Rows"));

    parseSolvedRowsTerm(solvedRowsTerm);

序言代码:

sudoku(Rows) :-
    length(Rows, 9), maplist(same_length(Rows), Rows),
    append(Rows, Vs), Vs ins 1..9,
    maplist(all_distinct, Rows),
    transpose(Rows, Columns),
    maplist(all_distinct, Columns),
    Rows = [A,B,C,D,E,F,G,H,I],
    blocks(A, B, C), blocks(D, E, F), blocks(G, H, I).

blocks([], [], []).
blocks([A,B,C|Bs1], [D,E,F|Bs2], [G,H,I|Bs3]) :-
    all_distinct([A,B,C,D,E,F,G,H,I]),
    blocks(Bs1, Bs2, Bs3).


problem(1, [[_,_,_, _,_,_, _,_,_],
        [_,_,_, _,_,3, _,8,5],
        [_,_,1, _,2,_, _,_,_],

        [_,_,_, 5,_,7, _,_,_],
        [_,_,4, _,_,_, 1,_,_],
        [_,9,_, _,_,_, _,_,_],

        [5,_,_, _,_,_, _,7,3],
        [_,_,2, _,1,_, _,_,_],
        [_,_,_, _,4,_, _,_,9]]).  

函数parseTextFieldparseSolvedRowsTerm实际上是整个程序,可以与注释掉的Query q1一起正常工作,但不能与注释不出来的Query q1一起正常工作< / p>

1 个答案:

答案 0 :(得分:0)

解决了! 为q1添加了一个额外的参数 感谢github.com/zlumyo,为方便起见,我偷走了他的BuildMatrix,他的代码给了我更多参数的想法。

Query q1 = new Query("sudoku("+ buildMatrix(allRows) +", Result)");

'Buildmatrix'基本上只是StringBuilder的辅助函数:

private String buildMatrix(int[][] cells) { // build matrix as string
    StringBuilder result = new StringBuilder("[");

    ArrayList<String> strList = new ArrayList<>();
    for (int[] i : cells) {
        strList.add(buildList(i));
    }

    result.append(String.join(",", strList));

    result.append("]");

    return result.toString();
}

private String buildList(int[] line) { // build matrix as string
    StringBuilder result = new StringBuilder("[");

    ArrayList<String> intList = new ArrayList<>();
    for (int i : line) {
        String stringval;

        if(i == 0){
            stringval = "_";
        }else{
            stringval = String.valueOf(i);
        }   // if statement is a small adaptation to the version github.com/zlumyo made, because my prolog sudoku had a slightly different format for the list.

        intList.add(stringval);
    }

    result.append(String.join(",", intList));

    result.append("]");

    return result.toString();
}

序言代码没有太大变化,只是增加了一个参数和1行。

sudoku(Rows, Result) :-
    length(Rows, 9), maplist(same_length(Rows), Rows),
    append(Rows, Vs), Vs ins 1..9,
    maplist(all_distinct, Rows),
    transpose(Rows, Columns),
    maplist(all_distinct, Columns),
    Rows = [A,B,C,D,E,F,G,H,I],
    blocks(A, B, C), blocks(D, E, F), blocks(G, H, I),
    Rows = Result. %extra line