Skip to content

Commit

Permalink
Add logic operation node (#2990)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Jul 10, 2024
1 parent 253c54b commit f7e7302
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
112 changes: 112 additions & 0 deletions backend/src/packages/chaiNNer_standard/utility/math/logic_operation.py
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
7 changes: 6 additions & 1 deletion src/renderer/components/PaneNodeSearchMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,16 @@ function* getSpecialSuggestions(
schemata: readonly NodeSchema[],
searchQuery: string
): Iterable<SuggestionGroupItem> {
const equalIgnoreCase = (a: string, b: string) => {
// the length check isn't 100% correct, but it's good enough for our mostly-ASCII suggestions
return a.length === b.length && a.toLowerCase() === b.toLowerCase();
};

const parse = (
s: SpecialSuggestion,
schema: NodeSchema
): { inputs: Partial<InputData> } | undefined => {
if (searchQuery === s.query) {
if (equalIgnoreCase(searchQuery, s.query)) {
return { inputs: s.inputs };
}
if (s.parseInput != null && searchQuery.startsWith(s.query)) {
Expand Down

0 comments on commit f7e7302

Please sign in to comment.