123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- import { RandomGUID } from "../Misc/guid.js";
- import { FlowGraphConnectionType } from "./flowGraphConnection.js";
- import { FlowGraphDataConnection } from "./flowGraphDataConnection.js";
- import { Tools } from "../Misc/tools.js";
- import { defaultValueParseFunction, defaultValueSerializationFunction, needsPathConverter } from "./serialization.js";
- /**
- * @experimental
- * A block in a flow graph. The most basic form
- * of a block has inputs and outputs that contain
- * data.
- */
- export class FlowGraphBlock {
- /** Constructor is protected so only subclasses can be instantiated
- * @param config optional configuration for this block
- */
- constructor(
- /**
- * the configuration of the block
- */
- config) {
- this.config = config;
- /**
- * A randomly generated GUID for each block.
- */
- this.uniqueId = RandomGUID();
- this.name = this.config?.name ?? this.getClassName();
- this.dataInputs = [];
- this.dataOutputs = [];
- }
- /**
- * @internal
- */
- _updateOutputs(_context) {
- // empty by default, overriden in data blocks
- }
- /**
- * Registers a data input on the block.
- * @param name the name of the input
- * @param richType the type of the input
- * @returns the created connection
- */
- registerDataInput(name, richType) {
- const input = new FlowGraphDataConnection(name, FlowGraphConnectionType.Input, this, richType);
- this.dataInputs.push(input);
- return input;
- }
- /**
- * Registers a data output on the block.
- * @param name the name of the input
- * @param richType the type of the input
- * @returns the created connection
- */
- registerDataOutput(name, richType) {
- const output = new FlowGraphDataConnection(name, FlowGraphConnectionType.Output, this, richType);
- this.dataOutputs.push(output);
- return output;
- }
- /**
- * Given the name of a data input, returns the connection if it exists
- * @param name the name of the input
- * @returns the connection if it exists, undefined otherwise
- */
- getDataInput(name) {
- return this.dataInputs.find((i) => i.name === name);
- }
- /**
- * Given the name of a data output, returns the connection if it exists
- * @param name the name of the output
- * @returns the connection if it exists, undefined otherwise
- */
- getDataOutput(name) {
- return this.dataOutputs.find((i) => i.name === name);
- }
- /**
- * Serializes this block
- * @param serializationObject the object to serialize to
- * @param _valueSerializeFunction a function that serializes a specific value
- */
- serialize(serializationObject = {}, _valueSerializeFunction = defaultValueSerializationFunction) {
- serializationObject.uniqueId = this.uniqueId;
- serializationObject.config = {};
- if (this.config) {
- serializationObject.config["name"] = this.config.name;
- }
- serializationObject.dataInputs = [];
- serializationObject.dataOutputs = [];
- serializationObject.className = this.getClassName();
- for (const input of this.dataInputs) {
- const serializedInput = {};
- input.serialize(serializedInput);
- serializationObject.dataInputs.push(serializedInput);
- }
- for (const output of this.dataOutputs) {
- const serializedOutput = {};
- output.serialize(serializedOutput);
- serializationObject.dataOutputs.push(serializedOutput);
- }
- }
- /**
- * Gets the class name of this block
- * @returns the class name
- */
- getClassName() {
- return "FGBlock";
- }
- /**
- * Parses a block from a serialization object
- * @param serializationObject the object to parse from
- * @param parseOptions options for parsing the block
- * @returns the parsed block
- */
- static Parse(serializationObject, parseOptions) {
- const classType = Tools.Instantiate(serializationObject.className);
- const parsedConfig = {};
- const valueParseFunction = parseOptions.valueParseFunction ?? defaultValueParseFunction;
- if (serializationObject.config) {
- for (const key in serializationObject.config) {
- parsedConfig[key] = valueParseFunction(key, serializationObject.config, parseOptions.scene);
- }
- }
- if (needsPathConverter(serializationObject.className)) {
- parsedConfig.pathConverter = parseOptions.pathConverter;
- }
- const obj = new classType(parsedConfig);
- obj.uniqueId = serializationObject.uniqueId;
- for (let i = 0; i < serializationObject.dataInputs.length; i++) {
- const dataInput = obj.getDataInput(serializationObject.dataInputs[i].name);
- if (dataInput) {
- dataInput.deserialize(serializationObject.dataInputs[i]);
- }
- else {
- throw new Error("Could not find data input with name " + serializationObject.dataInputs[i].name + " in block " + serializationObject.className);
- }
- }
- for (let i = 0; i < serializationObject.dataOutputs.length; i++) {
- const dataOutput = obj.getDataOutput(serializationObject.dataOutputs[i].name);
- if (dataOutput) {
- dataOutput.deserialize(serializationObject.dataOutputs[i]);
- }
- else {
- throw new Error("Could not find data output with name " + serializationObject.dataOutputs[i].name + " in block " + serializationObject.className);
- }
- }
- obj.metadata = serializationObject.metadata;
- obj.deserialize && obj.deserialize(serializationObject);
- return obj;
- }
- }
- //# sourceMappingURL=flowGraphBlock.js.map
|