Thursday, June 14, 2007

Using Prolog to Find a Friend

I was just experimenting with the foaf (Friend of a Friend). So, if we have an RDF database Jena, and can store the aggregated foaf:knows harvested from Google. A Prolog engine can be then embedded inside the Query Engine. A knows() predicate can be build-in, that retrieves the foaf:knows values for a given person.

The following sample program can be used to determine if two persons directly or indirectly know each other.

/* predicate will be build-in that retrieves foaf:knows from the RDF repository */
knows(a,b).
knows(b,c).
knows(c,d).
knows(x,e).

/* Actual theory */
exists(X) :- knows(X,_).
exists(X) :- knows(_,X).

foaf(X,Y) :- knows(X,Y).
foaf(X,Y) :- knows(Y,X).
foaf(X,Y) :- knows(X,Z), foaf(Z,Y).

friend(X,Y) :- exists(X), exists(Y), foaf(X,Y).

The above program can be tested within any Prolog system, with the goal :
?- friend(a,d).

The above program is the bare bone proof-of-concept. It can be further improved by adding degrees of separation and common linkages of friends separation the two persons.