1
votes

i am a prolog-noob and have a problem with a prolog-list in java.

Prolog:

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

Solution in prologconsole: [einkaufen,sport,hotel,schwimmen,sauna]

Java:

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;

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

How can i read the prolog-term as a ArrayList for Java?

1

1 Answers

0
votes

you need to change it into a term array like this:

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++;
    }
}