1

我是一个 prolog-noob,并且在 java 中的 prolog-list 有问题。

序言:

event(  'Number1', 
  54.320021,
  13.043840,
  [sport,hotel,schwimmen,sauna]).
event(  'Number2',
  54.320678,
  13.046984,
  [einkaufen]).


findAllCategories(Categories):-
  findall(X, event(_,_,_,X), L),
  appendCategories(C1,L),
  Categories = C1.

appendCategories(C1,[R|[]]):-
  C1 = R.

appendCategories(C,[R|L]):-
  appendCategories(C1,L),
  append(C1,R,X),
  C = X.

append([],L,L).
append([H|T],L2,[H|L3]):-  
   append(T,L2,L3). 

prologconsole 中的解决方案:[einkaufen,sport,hotel,schwimmen,sauna]

爪哇:

import java.util.ArrayList;
import java.util.Map;
import org.jpl7.*; 

public ArrayList<String> getCategoriesByProlog(){
    ArrayList<String> categories = new ArrayList<String>();

    Variable X = new Variable("X");
        Query q4 =
            new Query(
                "findAllCategories",
                new Term[] {X}
            );

    Map<String, Term>[] solutions = q4.allSolutions();

    for ( int i=0 ; i<solutions.length ; i++ ) {
        System.out.println( "X = " + solutions[i].get("X"));
        categories.add(solutions[i].get("X").toString());
    }

    return categories;

Java 输出: X = '[|]'(einkaufen, '[|]'(sport, '[|]'(hotel, '[|]'(schwimmen, '[|]'(sauna, '[]' )))))

我如何将 prolog-term 读取为 Java 的 ArrayList?

4

1 回答 1

0

您需要将其更改为这样的术语数组:

Map<String, Term>[] solutions = q4.allSolutions();

for (int i=0 ; i<solutions.length ; i++ )
{                 
    System.out.println("X = " + solutions[i].get("X"));

    //ADD THIS PART
    Term term = solutions[i].get("X");

    int u=0;       
    for (Term oneTerm : term.toTermArray()) 
    {
        categories.add(oneTerm);
        System.out.println("term["+i+"]["+u+"] = " + oneTerm);
        u++;
    }
}
于 2017-05-03T07:24:41.947 回答