Java: propositional entailment by model checking

A knowledge base is a set of sentences. In propositional logic, a model assigns true or false to every symbol. A sentence is entailed by the knowledge base if it is true in every model where the whole knowledge base is true.

Java
class Main {
  public static void main(String[] args) {
    boolean entailsR = true;
    boolean entailsNotR = true;
    int models = 0;
    for (int bits = 0; bits < 8; bits++) {
      boolean p = (bits & 1) != 0;
      boolean q = (bits & 2) != 0;
      boolean r = (bits & 4) != 0;
      boolean kb = (!p || q) && (!q || r) && p;
      if (kb) {
        models++;
        if (!r) {
          entailsR = false;
        }
        if (r) {
          entailsNotR = false;
        }
      }
    }
    System.out.println("models of KB: " + models);
    System.out.println("KB entails R: " + entailsR);
    System.out.println("KB entails not R: " + entailsNotR);
  }
}
Read the full lesson