Skip to content

Implement AND / OR logic per Rule Group type (Gate), starting with ALL / ANY #13

@conorheffron

Description

@conorheffron

Implement AND / OR logic per Rule Group type, starting with all/any rule group Gates.

Initial gates to implement (key in yml -> logic table)

  • all -> AND logic
  • any -> OR logic
  • nall -> Not AND / NAND logic
  • nany -> Not OR / NOR logic

Other derived gates to consider such as XOR & XNOR.

  • Note: Analogy/Description to work against:
    • OR (any) = "At least one is true"
    • XOR (xany) = "One or the other, but not both" / one condition out of several is exclusive match.
    • XNOR (xnany) = "They agree" / can deduce from all conditions not matching that the current case must be true.

Further reading / refresher on Truth Tables:


Java to represent truth table

- Note: This is based on two inputs or rules only (A : B)

import java.util.Arrays;

public class TruthTables {

    // Logical operations
    private static boolean AND(boolean a, boolean b) {
        return a && b;
    }

    private static boolean OR(boolean a, boolean b) {
        return a || b;
    }

    private static boolean XOR(boolean a, boolean b) {
        return a ^ b;
    }

    private static boolean XAND(boolean a, boolean b) {
        // XAND is essentially AND, but labeled differently
        return a && b;
    }

    private static boolean NAND(boolean a, boolean b) {
        return !(a && b);
    }

    private static boolean XNAND(boolean a, boolean b) {
        // Same as NAND, but labeled differently
        return !(a && b);
    }

    private static boolean NOR(boolean a, boolean b) {
        return !(a || b);
    }

    private static boolean XNOR(boolean a, boolean b) {
        return !(a ^ b); // true if both are equal
    }

    // Print truth table for selected gates
    private static void printTruthTable() {
        boolean[] values = {false, true};

        // Header
        System.out.printf("%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s%n",
                "A", "B", "AND", "XAND", "OR", "XOR", "XNOR", "NAND", "XNAND", "NOR");

        // Rows
        for (boolean a : values) {
            for (boolean b : values) {
                System.out.printf("%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s%n",
                        a, b,
                        AND(a, b),
                        XAND(a, b),
                        OR(a, b),
                        XOR(a, b),
                        XNOR(a, b),
                        NAND(a, b),
                        XNAND(a, b),
                        NOR(a, b)
                );
            }
        }
    }

    public static void main(String[] args) {
        printTruthTable();
    }
}
1. Boolean Functions:
  • Each logic gate is implemented as a separate method for clarity.
2. XAND & XNAND:
  • XAND is just AND but labeled differently.
  • XNAND is just NAND but labeled differently.This is useful if you want them explicitly in the table.
3. XNOR
  • Implemented as !(a ^ b) — true when both inputs are the same.
4. Output Formatting:
  • Uses printf with fixed-width columns for a clean table.

Sample Output:

A     B     AND   XAND  OR    XOR   XNOR  NAND  XNAND NOR  
false false false false false false true  true  true  true 
false true  false false true  true  false true  true  false
true  false false false true  true  false true  true  false
true  true  true  true  true  false true  false false false

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestgood first issueGood for newcomersquestionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions