%% Return Rules in User-Defined Aggregates % Schema %------- database({sppp(S:string, Item:string, Price:integer), stock_closing(Company:string, Price:real)}). %Derivated predicates and rules %------------------------------ % General format % return(newaggr, NewY, OldV, VR) <- ... % % Find suppliers who supply more than 7 items select(Sup) <- allcounts(Sup, CC), CC > 7 . allcounts(Sup, cntol) <- sppp(Sup, Itm, Price). single(cntol, _, 1). multi(cntol, S, Old, New) <- New = Old+1. ereturn(cntol, S, Old, Value) <- Old ~= nil, Value = Old+1. export select(X). % Moving time window aggregation : % Average the prices of IBM stocks over the last five days. result(mw5avg) <- stock_closing('IBM', Stock). single(mw5avg, X, [X]). multi(mw5avg, X, OL, NL) <- if(OL= [X1, X2, X3, X4, X5] then L = [X1, X2, X3, X4] else L = OL), NL = [X | L]. ereturn(mw5avg, _, [X5,X4,X3,X2,X1], Avg) <- Avg = (X1+X2+X3+X4+X5)/5. export result(X).