Refactored into smaller expressions.
team_home_game_points(Team,Points) :-
jogo(_,Team,_,A,_,B,_,C,_,D,_),
Points is (A+B+C+D).
team_away_game_points(Team,Points) :-
jogo(_, _, Team,_,A,_,B,_,C,_,D),
Points is (A+B+C+D).
team_game_points(Team,Points) :-
team_home_game_points(Team,Points).
team_game_points(Team,Points) :-
team_away_game_points(Team,Points).
total_points(Team,Total_points) :-
team(_, Team), % guard against invalid team name
aggregate_all(
sum(Points),
team_game_points(Team,Points),
Total_points
).
team_points_descending(Names) :-
aggregate_all(
bag(Name),
order_by(
[desc(Total_points)],
total_points(Name,Total_points)
),
Names).
test(005,nondet) :-
Team = nets,
team_home_game_points(Team,Points),
assertion( Points == 107 ).
test(006,all(Points == [107,114,120,111,122,107,119,102,125,91,125,97,112,106,125,144,115,106,111,134,126,116,109,123,114,109,122,94,135,106,99,101,116,112,127,113,103,110,121,105,113])) :-
Team = nets,
team_home_game_points(Team,Points).
test(007,nondet) :-
Team = nets,
team_away_game_points(Team,Points),
assertion( Points == 100 ).
test(008,all(Points == [100,112,102,115,96,104,112,100,113,115,104,113,88,112,127,96,87,115,109,117,95,105,145,117,104,114,89,125,148,117,88,114,96,98,116,123,111,144,110,133,108])) :-
Team = nets,
team_away_game_points(Team,Points).
test(009, all(Points == [107,114,120,111,122,107,119,102,125,91,125,97,112,106,125,144,115,106,111,134,126,116,109,123,114,109,122,94,135,106,99,101,116,112,127,113,103,110,121,105,113,100,112,102,115,96,104,112,100,113,115,104,113,88,112,127,96,87,115,109,117,95,105,145,117,104,114,89,125,148,117,88,114,96,98,116,123,111,144,110,133,108]) ) :-
Team = nets,
team_game_points(Team,Points).
test(010) :-
Name = nets,
total_points(Name,Total_points),
assertion( Total_points == 9204 ).
test(011) :-
team_points_descending(Names),
assertion( Names == [bucks,warriors,pelicans,philadelphia,clippers,blazers,thunder,raptors,sacramento,wizards,rockets,hawks,timberwolves,celtics,nets,lakers,utah,spurs,hornets,nuggets,mavericks,pacers,suns,magic,pistons,heat,bulls,knicks,cavaliers,grizzlies] ).
?- run_tests(stats:[5,6,7,8,9,10,11]).
% PL-Unit: stats:[5,6,7,8,9,10,11] ....... done
% All 7 tests passed
true.