简体   繁体   中英

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

I'm making a sudoku solver in java, using a small prolog kb at it's core. The prolog "sudoku" rule requires a prolog list of lists. In java I have an int[][] with the sudoku values.

I've made the Query run succesfully with a prolog list of lists eg Query q1 = new Query("problem(1, Rows), sudoku(Rows)."); where Rows is a prolog list of lists, but I need to also make it run with a Java int[][] eg Query q1 = new Query("sudoku", intArrayTerm);

The relevant java code:

    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);

the prolog code:

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]]).  

the functions parseTextField and parseSolvedRowsTerm , actually the whole program, works fine with the commented-out Query q1 , but not with the not-commented-out Query q1

solved it! added an extra argument to q1 Credits to github.com/zlumyo, I stole his BuildMatrix for convenience and his code gave me the idea for the extra argument.

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

'Buildmatrix' is basically just a StringBuilder helper function:

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();
}

The prolog code hasn't changed a lot, just an extra argument and 1 extra line.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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