Class FormulaFactory
- Namespace
- LogicalOptimizer
- Assembly
- LogicalOptimizer.Core.dll
LogicNG-style formula construction: n-ary And/Or with automatic flattening, duplicate-operand removal, constant folding, complement folding (x and !x collapse the connective), canonical operand ordering and structural interning — equal formulas built through the factory are the SAME instance, so reference equality works as structural equality and repeated subformulas share memory. Because operands are sorted with a stable canonical key at construction time, structural (order-sensitive) equality is effectively commutative for factory-built trees. The intern table is a ConcurrentDictionary<TKey, TValue>, so a factory instance may be shared across threads.
public sealed class FormulaFactory
- Inheritance
-
FormulaFactory
- Inherited Members
Properties
False
Constant false (interned).
public AstNode False { get; }
Property Value
True
Constant true (interned).
public AstNode True { get; }
Property Value
Methods
And(params AstNode[])
N-ary conjunction: nested ANDs are flattened, duplicates removed, constants folded (0 annihilates, 1 disappears), complementary operands collapse to 0, and the surviving operands are sorted into canonical order.
public AstNode And(params AstNode[] operands)
Parameters
operandsAstNode[]
Returns
And(IEnumerable<AstNode>)
N-ary conjunction: nested ANDs are flattened, duplicates removed, constants folded (0 annihilates, 1 disappears), complementary operands collapse to 0, and the surviving operands are sorted into canonical order.
public AstNode And(IEnumerable<AstNode> operands)
Parameters
operandsIEnumerable<AstNode>
Returns
Equivalence(AstNode, AstNode)
Equivalence a ↔ b constructed as (a & b) | (!a & !b).
public AstNode Equivalence(AstNode left, AstNode right)
Parameters
Returns
Implication(AstNode, AstNode)
Implication a → b constructed as !a | b.
public AstNode Implication(AstNode left, AstNode right)
Parameters
Returns
Import(AstNode)
Rebuild an existing tree through the factory: the result carries every construction-time canonicalization and is interned. Derived operators (Xor/Eqv/Imp/Nand/Nor) are decomposed into And/Or/Not.
public AstNode Import(AstNode node)
Parameters
nodeAstNode
Returns
Not(AstNode)
Negation with double-negation and constant folding.
public AstNode Not(AstNode operand)
Parameters
operandAstNode
Returns
Or(params AstNode[])
N-ary disjunction: nested ORs are flattened, duplicates removed, constants folded (1 annihilates, 0 disappears), complementary operands collapse to 1, and the surviving operands are sorted into canonical order.
public AstNode Or(params AstNode[] operands)
Parameters
operandsAstNode[]
Returns
Or(IEnumerable<AstNode>)
N-ary disjunction: nested ORs are flattened, duplicates removed, constants folded (1 annihilates, 0 disappears), complementary operands collapse to 1, and the surviving operands are sorted into canonical order.
public AstNode Or(IEnumerable<AstNode> operands)
Parameters
operandsIEnumerable<AstNode>
Returns
Parse(string)
Parse text through the standard grammar, building canonical nodes via this factory. Throws FormulaParseException (a subclass of ArgumentException) on invalid input; use TryParse(string, out AstNode?, out ParseDiagnostic?) to avoid the exception.
public AstNode Parse(string expression)
Parameters
expressionstring
Returns
TryParse(string, out AstNode?, out ParseDiagnostic?)
Attempts to parse expression. On success returns true with the
canonical tree in formula; on invalid input returns false with a
structured diagnostic (position, length, expected tokens, machine-readable
code and a caret snippet) instead of throwing. A null expression is still a contract
violation and throws ArgumentNullException.
public bool TryParse(string expression, out AstNode? formula, out ParseDiagnostic? diagnostic)
Parameters
expressionstringformulaAstNodediagnosticParseDiagnostic
Returns
Variable(string)
Interned variable.
public AstNode Variable(string name)
Parameters
namestring
Returns
Xor(AstNode, AstNode)
Exclusive or constructed as (a & !b) | (!a & b).
public AstNode Xor(AstNode left, AstNode right)