% Prova 1.9:
%   BOTH Object[] AND Java List returned from Java methods are returned to Prova as a Java List
%      UNLESS
%         The type of a variable on the LHS is RList
%            OR
%         A Prolog-type bracketed list (e.g., [X|Xs]) is specified in the LHS.
%         The usual type-aware unification rules apply in the last case. 
% The example uses built-in methods getObjectArray and getJavaList in ws.prova.examples.IBMPortfolio
% These method return respectively an Object[] or a List with {"A",3.14,4}.  
% Returning a Prova List from a Java method does not add anything so it is normally not needed.

:- eval(demoObjectArrayReturn()).
:- eval(demoJavaListReturn()).

demoJavaListReturn() :-
	println(['========= Java List return demos (the List contains {"A",3.14,4}) ==========']),
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	RList.ProvaListAsVariable = IBMPortfolio.getJavaList(),
	println(["Prova list passed as typed RList variable RList.ProvaListAsVariable: ",RList.ProvaListAsVariable]). 
demoJavaListReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	JavaList = IBMPortfolio.getJavaList(),
	println(["Java list returned by default JavaList: ",JavaList]).
demoJavaListReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	[X,Double.D,I] = IBMPortfolio.getJavaList(),
	println(["Prova list using specified types that we will only accept on return [X,Double.D,I]: ",[X,Double.D,I]]). 
demoJavaListReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	[X,Double.D,4] = IBMPortfolio.getJavaList(),
	println(["Prova list with some constants as well [X,Double.D,4]: ",[X,Double.D,4]]). 
demoJavaListReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	println(["The following line will not unify: [X] = IBMPortfolio.getJavaList()"), 
	[X] = IBMPortfolio.getJavaList(),
	% WIll never make it here
	println(["Prova list [X]: ",[X]]). 
demoJavaListReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	[X|Xs] = IBMPortfolio.getJavaList(),
	println(["Prova list with specified pattern [X|Xs]: ",[X|Xs]]). 

demoObjectArrayReturn() :-
	println(['========= Object Array return demos  (Object[] contains {"A",3.14,4})==========']),
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	RList.ProvaListAsVariable = IBMPortfolio.getObjectArray(),
	println(["Prova list passed as typed RList variable RList.ProvaListAsVariable: ",RList.ProvaListAsVariable]). 
demoObjectArrayReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	JavaList = IBMPortfolio.getObjectArray(),
	println(["Java list returned by default JavaList: ",JavaList]).
demoObjectArrayReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	[X,Double.D,I] = IBMPortfolio.getObjectArray(),
	println(["Prova list using specified types that we will only accept on return [X,Double.D,I]: ",[X,Double.D,I]]). 
demoObjectArrayReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	[X,Double.D,4] = IBMPortfolio.getObjectArray(),
	println(["Prova list with some constants as well [X,Double.D,4]: ",[X,Double.D,4]]). 
demoObjectArrayReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	println(["The following line will not unify: [X] = IBMPortfolio.getObjectArray()"), 
	[X] = IBMPortfolio.getObjectArray(),
	% WIll never make it here
	println(["Prova list [X]: ",[X]]). 
demoObjectArrayReturn() :-
	IBMPortfolio = ws.prova.examples.IBMPortfolio(),
	[X|Xs] = IBMPortfolio.getObjectArray(),
	println(["Prova list with specified pattern [X|Xs]: ",[X|Xs]]). 
