Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

account for node configuration in graph editor #237

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/export-node-spec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ManualLifecycleEventEmitter,
registerCoreProfile,
validateNodeRegistry,
writeNodeSpecsToJSON
writeDefaultNodeSpecsToJSON,
} from '@behave-graph/core';
import { DummyScene, registerSceneProfile } from '@behave-graph/scene';
import { program } from 'commander';
Expand Down Expand Up @@ -59,7 +59,7 @@ export const main = async () => {
return;
}

const nodeSpecJson = writeNodeSpecsToJSON(registry);
const nodeSpecJson = writeDefaultNodeSpecsToJSON(registry);
nodeSpecJson.sort((a, b) => a.type.localeCompare(b.type));
const jsonOutput = JSON.stringify(nodeSpecJson, null, ' ');
if (programOptions.csv) {
Expand Down
2 changes: 1 addition & 1 deletion graphs/core/flow/Counter.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
"type": "flow/sequence",
"configuration": {
"numOutputs": 4
"numSockets": 4
},
"id": "2",
"flows": {
Expand Down
2 changes: 1 addition & 1 deletion graphs/core/flow/DoOnce.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
"type": "flow/sequence",
"configuration": {
"numOutputs": 4
"numSockets": 4
},
"id": "2",
"flows": {
Expand Down
2 changes: 1 addition & 1 deletion graphs/core/flow/Gate.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
"type": "flow/sequence",
"configuration": {
"numOutputs": 5
"numSockets": 5
},
"id": "2",
"flows": {
Expand Down
2 changes: 1 addition & 1 deletion graphs/core/flow/Sequence.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
"type": "flow/sequence",
"configuration": {
"numOutputs": 3
"numSockets": 3
},
"id": "2",
"flows": {
Expand Down
2 changes: 1 addition & 1 deletion graphs/core/flow/SwitchOnInt.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
"type": "flow/switch/integer",
"configuration": {
"numCases": 3
"numSockets": 3
},
"id": "2",
"parameters": {
Expand Down
2 changes: 1 addition & 1 deletion graphs/core/flow/SwitchOnString.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
"type": "flow/switch/string",
"configuration": {
"numCases": 3
"numSockets": 3
},
"id": "2",
"parameters": {
Expand Down
4 changes: 2 additions & 2 deletions graphs/core/flow/WaitAll.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
"type": "flow/sequence",
"configuration": {
"numOutputs": 4
"numSockets": 4
},
"id": "2",
"flows": {
Expand Down Expand Up @@ -113,7 +113,7 @@
{
"type": "flow/waitAll",
"configuration": {
"numInputs": 2
"numSockets": 2
},
"id": "7",
"flows": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Graphs/IO/writeGraphToJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function writeGraphToJSON(
if (Object.keys(node.metadata).length > 0) {
nodeJson.metadata = node.metadata;
}
if (Object.keys(node.description.configuration).length > 0) {
if (Object.keys(node.configuration).length > 0) {
const configurationJson: { [key: string]: ValueJSON } = {};
Object.keys(node.configuration).forEach((key) => {
configurationJson[key] = node.configuration[key];
Expand Down
104 changes: 59 additions & 45 deletions packages/core/src/Graphs/IO/writeNodeSpecsToJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NodeCategory } from '../../Nodes/NodeDefinitions.js';
import { IRegistry } from '../../Registry.js';
import { Choices } from '../../Sockets/Socket.js';
import { createNode, makeGraphApi } from '../Graph.js';
import { NodeConfigurationJSON } from './GraphJSON.js';
import {
ChoiceJSON,
InputSocketSpecJSON,
Expand All @@ -16,64 +17,77 @@ function toChoices(valueChoices: Choices | undefined): ChoiceJSON | undefined {
});
}

export function writeNodeSpecsToJSON(registry: IRegistry): NodeSpecJSON[] {
const nodeSpecsJSON: NodeSpecJSON[] = [];

// const graph = new Graph(registry);

// create JSON specs for a single node based on given configuration
export function writeNodeSpecToJSON(registry: IRegistry, nodeTypeName: string, configuration: NodeConfigurationJSON): NodeSpecJSON {
const graph = makeGraphApi({
...registry,
customEvents: {},
variables: {}
});

Object.keys(registry.nodes).forEach((nodeTypeName) => {
const node = createNode({
graph,
registry,
nodeTypeName
});
const node = createNode({
graph,
registry,
nodeTypeName,
nodeConfiguration: configuration,
});

const nodeSpecJSON: NodeSpecJSON = {
type: nodeTypeName,
category: node.description.category as NodeCategory,
label: node.description.label,
inputs: [],
outputs: [],
configuration: []
};

const nodeSpecJSON: NodeSpecJSON = {
type: nodeTypeName,
category: node.description.category as NodeCategory,
label: node.description.label,
inputs: [],
outputs: [],
configuration: []
node.inputs.forEach((inputSocket) => {
const valueType =
inputSocket.valueTypeName === 'flow'
? undefined
: registry.values[inputSocket.valueTypeName];

let defaultValue = inputSocket.value;
if (valueType !== undefined) {
defaultValue = valueType.serialize(defaultValue);
}
if (defaultValue === undefined && valueType !== undefined) {
defaultValue = valueType.serialize(valueType.creator());
}
const socketSpecJSON: InputSocketSpecJSON = {
name: inputSocket.name,
valueType: inputSocket.valueTypeName,
defaultValue,
choices: toChoices(inputSocket.valueChoices)
};
nodeSpecJSON.inputs.push(socketSpecJSON);
});

node.inputs.forEach((inputSocket) => {
const valueType =
inputSocket.valueTypeName === 'flow'
? undefined
: registry.values[inputSocket.valueTypeName];
node.outputs.forEach((outputSocket) => {
const socketSpecJSON: OutputSocketSpecJSON = {
name: outputSocket.name,
valueType: outputSocket.valueTypeName
};
nodeSpecJSON.outputs.push(socketSpecJSON);
});

let defaultValue = inputSocket.value;
if (valueType !== undefined) {
defaultValue = valueType.serialize(defaultValue);
}
if (defaultValue === undefined && valueType !== undefined) {
defaultValue = valueType.serialize(valueType.creator());
}
const socketSpecJSON: InputSocketSpecJSON = {
name: inputSocket.name,
valueType: inputSocket.valueTypeName,
defaultValue,
choices: toChoices(inputSocket.valueChoices)
};
nodeSpecJSON.inputs.push(socketSpecJSON);
Object.entries(node.description.configuration).forEach(([ configName, configSpec ]) => {
nodeSpecJSON.configuration.push({
name: configName,
valueType: configSpec.valueType,
defaultValue: configSpec.defaultValue
});
});

node.outputs.forEach((outputSocket) => {
const socketSpecJSON: OutputSocketSpecJSON = {
name: outputSocket.name,
valueType: outputSocket.valueTypeName
};
nodeSpecJSON.outputs.push(socketSpecJSON);
});
return nodeSpecJSON;
}

// create JSON specs for all nodes with empty configuration
export function writeDefaultNodeSpecsToJSON(registry: IRegistry): NodeSpecJSON[] {
const nodeSpecsJSON: NodeSpecJSON[] = [];

nodeSpecsJSON.push(nodeSpecJSON);
Object.keys(registry.nodes).forEach((nodeTypeName) => {
nodeSpecsJSON.push(writeNodeSpecToJSON(registry, nodeTypeName, {}));
});

return nodeSpecsJSON;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Profiles/Core/Flow/Sequence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type RecordedWritesType = RecordedWritesOrCommits<typeof Sequence.out>;
describe('Sequence', () => {
it('it triggeres output flows for each element in sequence when triggered', () => {
const nodeConfig = {
numOutputs: 5
numSockets: 5
};
const trigger = generateTriggerTester(Sequence, nodeConfig);

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Profiles/Core/Flow/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ export const Sequence = makeFlowNodeDefinition({
typeName: 'flow/sequence',
label: 'Sequence',
configuration: {
numOutputs: {
numSockets: {
valueType: 'number'
}
},
in: {
flow: 'flow'
},
out: (configuration) => {
const numOutputs = configuration.numOutputs;
const numSockets = configuration.numSockets;
const sockets: SocketsList = [];

for (let outputIndex = 1; outputIndex <= numOutputs; outputIndex++) {
for (let outputIndex = 1; outputIndex <= numSockets; outputIndex++) {
const key = `${outputIndex}`;

sockets.push({
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/Profiles/Core/Flow/SwitchOnInteger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const SwitchOnInteger = makeFlowNodeDefinition({
typeName: 'flow/switch/integer',
label: 'Switch on Int',
configuration: {
numCases: {
numSockets: {
valueType: 'number'
}
},
Expand All @@ -22,7 +22,7 @@ export const SwitchOnInteger = makeFlowNodeDefinition({
{ key: 'selection', valueType: 'integer' }
);

for (const index of sequence(1, configuration.numCases + 1)) {
for (const index of sequence(1, configuration.numSockets + 1)) {
sockets.push({ key: `${index}`, valueType: 'integer' });
}

Expand All @@ -32,7 +32,7 @@ export const SwitchOnInteger = makeFlowNodeDefinition({
const sockets: SocketsList = [];

sockets.push({ key: 'default', valueType: 'flow' });
for (const index of sequence(1, configuration.numCases + 1)) {
for (const index of sequence(1, configuration.numSockets + 1)) {
sockets.push({ key: `${index}`, valueType: 'flow' });
}

Expand All @@ -41,7 +41,7 @@ export const SwitchOnInteger = makeFlowNodeDefinition({
initialState: undefined,
triggered: ({ read, commit, configuration }) => {
const selection = read<bigint>('selection');
for (const index of sequence(1, configuration.numCases + 1)) {
for (const index of sequence(1, configuration.numSockets + 1)) {
if (selection === read<bigint>(`${index}`)) {
commit(`${index}`);
return;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/Profiles/Core/Flow/SwitchOnString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const SwitchOnString = makeFlowNodeDefinition({
typeName: 'flow/switch/string',
label: 'Switch on String',
configuration: {
numCases: {
numSockets: {
valueType: 'number'
}
},
Expand All @@ -22,7 +22,7 @@ export const SwitchOnString = makeFlowNodeDefinition({
{ key: 'selection', valueType: 'string' }
);

for (const index of sequence(1, configuration.numCases + 1)) {
for (const index of sequence(1, configuration.numSockets + 1)) {
sockets.push({ key: `${index}`, valueType: 'string' });
}

Expand All @@ -32,7 +32,7 @@ export const SwitchOnString = makeFlowNodeDefinition({
const sockets: SocketsList = [];

sockets.push({ key: 'default', valueType: 'flow' });
for (const index of sequence(1, configuration.numCases + 1)) {
for (const index of sequence(1, configuration.numSockets + 1)) {
sockets.push({ key: `${index}`, valueType: 'flow' });
}

Expand All @@ -41,7 +41,7 @@ export const SwitchOnString = makeFlowNodeDefinition({
initialState: undefined,
triggered: ({ read, commit, configuration }) => {
const selection = read<string>('selection');
for (const index of sequence(1, configuration.numCases + 1)) {
for (const index of sequence(1, configuration.numSockets + 1)) {
if (selection === read<string>(`${index}`)) {
commit(`${index}`);
return;
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/Profiles/Core/Flow/WaitAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ export class WaitAll extends FlowNode {
category: 'Flow',
label: 'WaitAll',
configuration: {
numInputs: {
numSockets: {
valueType: 'number',
defaultValue: 3
}
},
factory: (description, graph, configuration) =>
new WaitAll(description, graph, configuration.numInputs)
new WaitAll(description, graph, configuration.numSockets)
});

private isOn = true;

constructor(
description: NodeDescription,
graph: IGraph,
private numInputs: number
private numSockets: number
) {
const inputs: Socket[] = [];
for (let i = 1; i <= numInputs; i++) {
for (let i = 1; i <= numSockets; i++) {
inputs.push(new Socket('flow', `${i}`));
}

Expand All @@ -54,7 +54,7 @@ export class WaitAll extends FlowNode {
private outputTriggered = false;

private reset() {
for (let inputIndex = 1; inputIndex <= this.numInputs; inputIndex++) {
for (let inputIndex = 1; inputIndex <= this.numSockets; inputIndex++) {
this.triggeredMap[`${inputIndex}`] = false;
}
this.triggeredCount = 0;
Expand All @@ -75,7 +75,7 @@ export class WaitAll extends FlowNode {
this.triggeredCount++;

// if a & b are triggered, first output!
if (this.triggeredCount === this.numInputs && !this.outputTriggered) {
if (this.triggeredCount === this.numSockets && !this.outputTriggered) {
fiber.commit(this, 'flow');
this.outputTriggered = true;

Expand Down
Loading