繁体   English   中英

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

[英]How to turn a java array into a prolog list and make a jpl Query with it?

我正在用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

解决了! 在github.com/zlumyo的q1积分中添加了一个额外的参数,为了方便起见,我偷了他的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM