% input company graph, described as ownership edges
% the company A owns 0.2% of shares of the company B
own("A", "B", 0.2).
% the company B owns 0.8% of shares of the company A
own("B", "A", 0.8).
own("B", "C", 0.2).
own("C", "D", 0.6).
own("D", "A", 0.9).
own("A", "C", 0.2).
% if the company X owns a percentage W of shares of Y and we add in a set
% the visited node X and the visited node Y, then there is a closeLinkPaths of X
% over Y with shares W and we bring the list of visited nodes P.
closeLinkPaths(X,Y,W,P) <- own(X,Y,W), P={}|X|Y, X<>Y.
% if there is a closeLinkPath from X to Y with shares W1 with a set of visited
% nodes P1, and the node Z is not in the list of the visited nodes, the node Z is
% added in the set of visited nodes, then there is a closeLinkPaths from X,Z
% with visited nodes P
closeLinkPaths(X,Z,J,P) <- closeLinkPaths(X,Y,W1,P1),
own(Y,Z,W2),
J = W1*W2,
P = P1|Z,
Z !in P1.
% if there is a closeLinkPaths from the company X to the company Y with shares W
% and the set of visited nodes P, then there is a close_link_sum by grouping by
% X and Y with total share J, by aggregating the sum of W
close_link_sum(X,Y,J) <- closeLinkPaths(X,Y,W,P), J = msum(W).
% if there is a close_link_sum of X over Y with shares W, and the shares is
% greater than 0.2, then the companies X and Y are in close link with shares W
close_link(X,Y,W) <- close_link_sum(X,Y,W), W >= 0.2.
@output("close_link").