:-mode fill_matrix(+,+).
fill_matrix(L,Matrix):-
(foreach(entry with [i:I,j:J,value:V],L),
param(Matrix) do
subscript(Matrix,[I,J],V)
).
It is not required that the input list contains all index combinations, we can use the iteration on array concept to fill any un-set elements with a default value.
:-mode fill_with_hops(+,+,+).
fill_with_hops([],_,_).
fill_with_hops([hop(Source,Dest,List)|R],Nodes,PMatrix):-
find_node_index(Source,Nodes,S),
find_node_index(Dest,Nodes,D),
find_node_indices(List,Nodes,L),
length(L,N),
subscript(PMatrix,[S,D],pi_entry with [list:L,
length:N]),
fill_with_hops(R,Nodes,PMatrix).