-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
253c54b
commit f7e7302
Showing
2 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
backend/src/packages/chaiNNer_standard/utility/math/logic_operation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
|
||
from api import Lazy, SpecialSuggestion | ||
from nodes.groups import if_enum_group | ||
from nodes.properties.inputs import BoolInput, EnumInput | ||
from nodes.properties.outputs import BoolOutput | ||
|
||
from .. import math_group | ||
|
||
|
||
class LogicOperation(Enum): | ||
AND = "and" | ||
OR = "or" | ||
XOR = "xor" | ||
NOT = "not" | ||
|
||
|
||
OP_LABEL: dict[LogicOperation, str] = { | ||
LogicOperation.AND: "AND: a & b", | ||
LogicOperation.OR: "OR: a | b", | ||
LogicOperation.XOR: "XOR: a ^ b", | ||
LogicOperation.NOT: "NOT: !a", | ||
} | ||
|
||
|
||
@math_group.register( | ||
schema_id="chainner:utility:logic_operation", | ||
name="Logic Operation", | ||
description="Perform logic operations on conditions.", | ||
see_also=[ | ||
"chainner:utility:conditional", | ||
], | ||
icon="MdCalculate", | ||
inputs=[ | ||
EnumInput( | ||
LogicOperation, | ||
"Logic Operation", | ||
option_labels=OP_LABEL, | ||
label_style="hidden", | ||
).with_id(0), | ||
BoolInput("A", has_handle=True).with_id(1), | ||
if_enum_group(0, (LogicOperation.AND, LogicOperation.OR, LogicOperation.XOR))( | ||
BoolInput("B", has_handle=True).with_id(2).make_lazy(), | ||
), | ||
], | ||
outputs=[ | ||
BoolOutput( | ||
label="Result", | ||
output_type=""" | ||
let a = Input1; | ||
let b = Input2; | ||
match Input0 { | ||
LogicOperation::And => a and b, | ||
LogicOperation::Or => a or b, | ||
LogicOperation::Xor => a != b, | ||
LogicOperation::Not => not a, | ||
} | ||
""", | ||
) | ||
.suggest() | ||
.as_passthrough_of(1), | ||
], | ||
suggestions=[ | ||
SpecialSuggestion( | ||
"AND", | ||
name="Logic Operation: AND", | ||
inputs={0: LogicOperation.AND}, | ||
), | ||
SpecialSuggestion( | ||
"&", | ||
name="Logic Operation: AND", | ||
inputs={0: LogicOperation.AND}, | ||
), | ||
SpecialSuggestion( | ||
"OR", | ||
name="Logic Operation: OR", | ||
inputs={0: LogicOperation.OR}, | ||
), | ||
SpecialSuggestion( | ||
"|", | ||
name="Logic Operation: OR", | ||
inputs={0: LogicOperation.OR}, | ||
), | ||
SpecialSuggestion( | ||
"XOR", | ||
name="Logic Operation: XOR", | ||
inputs={0: LogicOperation.XOR}, | ||
), | ||
SpecialSuggestion( | ||
"NOT", | ||
name="Logic Operation: NOT", | ||
inputs={0: LogicOperation.NOT}, | ||
), | ||
SpecialSuggestion( | ||
"!", | ||
name="Logic Operation: NOT", | ||
inputs={0: LogicOperation.NOT}, | ||
), | ||
], | ||
) | ||
def logic_operation_node(op: LogicOperation, a: bool, b: Lazy[bool]) -> bool: | ||
if op == LogicOperation.AND: | ||
return a and b.value | ||
if op == LogicOperation.OR: | ||
return a or b.value | ||
if op == LogicOperation.XOR: | ||
return a != b.value | ||
if op == LogicOperation.NOT: | ||
return not a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters