conorheffron / conorheffron/rules-engine
Rules Engine Logic: Implement `AND` / `OR` logic per `Rule Group` type (`Gate`), starting with `ALL` / `ANY`
- Dominant language
- Java
- Stars
- 4
- Forks
- 0
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 1
Description
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`:
- https://www.geeksforgeeks.org/electronics-engineering/truth-table/
- https://mechtrician.com/logic-gates-or-and-not-nor-nand-xor-xnor-truth-table/
---
### Java to represent truth table
#### - Note: This is based on two inputs or rules only (A : B)
```java
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
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.