Skip to content

Example: Lives & Knows

Verena Blaschke edited this page Jun 30, 2022 · 10 revisions

PSL-Infrastructure & PSL-RAGviewer demo: examples.livesknows

A very simple project demonstrating how to use the psl-infrastructure and psl-ragviewer packages. This demo project is based on a PSL problem for inferring whether a pair of people knows each other based on information whether they share the same address.

Classes

Predicates

We have two predicates:

  • LivesPred: Lives(P,L) -- "Person P lives at address L"
  • KnowsPred: Knows(P1,P2) -- "Person P1 knows person P2"

Both predicate classes contain templates for describing a ground atom as a noun phrase ("Alice knowing Bob") or a sentence ("Alice probably knows Bob").

The SampleConstantRenderer provides pretty-print functions for some of the atom arguments.

Rules

We have one weighted logical rule and one arithmetic constraint:

  • LivesToKnowsRule: 1.0: Lives(P1,L) & Lives(P2,L) & (P1 != P2) -> Knows(P1,P2) -- "If there is evidence that two people live at the same address, this makes it more likely that they know each other."
  • KnowsSymmetryConstraint: Knows(P1,P2) = Knows(P2,P1). -- "The knows relationship is symmetric."

Each rule overrides the method generateExplanation to provide explanation templates that match the rule logic more naturally than the default explanations might.

PslProblem & IdeaGenerator

In the SamplePslProblem, we add the two predicates to our problem set-up:

public void declarePredicates() {
    declareClosedPredicate(new LivesPred());
    declareOpenPredicate(new KnowsPred());
}

Lives is a closed predicate since we know all atom values in advance. Knows is an open predicate since we want to infer the atom values. If we had any predicates with some known atom values and some unknown ones, those predicates would also be added as open predicates.

If we wanted to add a predicate that doesn't have its own TalkingPredicate class, we could do so via declareClosedPredicate("MyPred", 2); (where 2 is the predicate's arity).

Putting it all together

TODO

Graphical user interface

To visually explore the rule-atom graph and to read the verbalized explanations for the atom values, run the EntryClass in the examples.livesknows subpackage of psl-ragviewer.

Clone this wiki locally