Skip to content

Commit

Permalink
refactor profile registration.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 10, 2023
1 parent 17206f1 commit e406df8
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 95 deletions.
14 changes: 4 additions & 10 deletions apps/exec-graph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import process from 'node:process';
import {
DefaultLogger,
Engine,
getCoreNodeDefinitions,
getCoreValueTypes,
Logger,
ManualLifecycleEventEmitter,
parseSafeFloat,
readGraphFromJSON,
registerCoreProfile,
validateGraph,
validateRegistry
} from '@behave-graph/core';
Expand Down Expand Up @@ -38,16 +37,14 @@ async function execGraph({
}) {
const lifecycleEventEmitter = new ManualLifecycleEventEmitter();
const logger = new DefaultLogger();
const valueTypeMap = getCoreValueTypes();
const nodeDefinitionMap = getCoreNodeDefinitions(valueTypeMap);
const registry = registerCoreProfile({ values: {}, nodes: {} });

const graphJsonPath = jsonPattern;
Logger.verbose(`reading behavior graph: ${graphJsonPath}`);
const textFile = await fs.readFile(graphJsonPath);
const graph = readGraphFromJSON({
graphJson: JSON.parse(textFile.toString('utf8')),
nodes: nodeDefinitionMap,
values: valueTypeMap,
...registry,
dependencies: {
logger,
lifecycleEventEmitter
Expand All @@ -56,10 +53,7 @@ async function execGraph({
graph.name = graphJsonPath;

const errorList: string[] = [];
errorList.push(
...validateRegistry({ nodes: nodeDefinitionMap, values: valueTypeMap }),
...validateGraph(graph)
);
errorList.push(...validateRegistry(registry), ...validateGraph(graph));

if (errorList.length > 0) {
Logger.error(`${errorList.length} errors found:`);
Expand Down
14 changes: 5 additions & 9 deletions apps/export-node-spec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { promises as fs } from 'node:fs';

import {
DefaultLogger,
getCoreNodeDefinitions,
getCoreValueTypes,
Logger,
ManualLifecycleEventEmitter,
registerCoreProfile,
validateNodeRegistry,
writeNodeSpecsToJSON
} from '@behave-graph/core';
Expand Down Expand Up @@ -37,13 +36,11 @@ export const main = async () => {

const lifecycleEventEmitter = new ManualLifecycleEventEmitter();
const logger = new DefaultLogger();
const valueTypeMap = getCoreValueTypes();
const nodeDefinitionMap = getCoreNodeDefinitions(valueTypeMap);

const registry = registerCoreProfile({ values: {}, nodes: {} });

const errorList: string[] = [];
errorList.push(
...validateNodeRegistry({ nodes: nodeDefinitionMap, values: valueTypeMap })
);
errorList.push(...validateNodeRegistry(registry));
if (errorList.length > 0) {
Logger.error(`${errorList.length} errors found:`);
errorList.forEach((errorText, errorIndex) => {
Expand All @@ -53,8 +50,7 @@ export const main = async () => {
}

const nodeSpecJson = writeNodeSpecsToJSON({
nodes: nodeDefinitionMap,
values: valueTypeMap,
...registry,
dependencies: {
logger,
lifecycleEventEmitter
Expand Down
35 changes: 15 additions & 20 deletions apps/three-viewer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@
import {
DefaultLogger,
Engine,
getCoreNodeDefinitions,
getCoreValueMap as getCoreValueMap,
Logger,
ManualLifecycleEventEmitter,
readGraphFromJSON,
registerCoreProfile,
registerLifecycleEventEmitter,
registerLogger,
registerSceneDependency,
registerSceneProfile,
Registry,
validateGraph,
validateRegistry
validateRegistry,
ValueType
} from '@behave-graph/core';
import { getSceneNodeDefinitions, getSceneValueMap } from '@behave-graph/scene';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js';
import { GLTFLoader, OrbitControls, RGBELoader } from 'three-stdlib';

import { ThreeScene } from './ThreeScene.js';

Expand Down Expand Up @@ -65,19 +61,18 @@ async function loadThreeScene() {
}

async function main() {
const registry = new Registry();
const manualLifecycleEventEmitter = new ManualLifecycleEventEmitter();
const lifecycleEventEmitter = new ManualLifecycleEventEmitter();
const logger = new DefaultLogger();

const { threeScene, gltf } = await loadThreeScene();
const valueTypeMap: Record<string, ValueType<any, any>> = {
...getCoreValueMap(),
...getSceneValueMap()
};

registerCoreProfile(registry);
registerSceneProfile(registry);
registerLogger(registry.dependencies, logger);
registerLifecycleEventEmitter(
registry.dependencies,
manualLifecycleEventEmitter
);
const nodeDefinitionMap = getCoreNodeDefinitions(valueTypeMap);
const sceneNodeDefinitionMap = getSceneNodeDefinitions(valueTypeMap);

const { threeScene, gltf } = await loadThreeScene();

const graphJsonPath = publicImageUrl(
`/graphs/scene/actions/SpinningSuzanne.json`
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Profiles/Core/readCoreGraphs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import { validateGraphAcyclic } from '../../Graphs/Validation/validateGraphAcycl
import { validateGraphLinks } from '../../Graphs/Validation/validateGraphLinks.js';
import {
getCoreNodeDefinitions,
getCoreValueTypes
getCoreValueMap
} from './registerCoreProfile.js';

const valueTypes = getCoreValueTypes();
const valueTypes = getCoreValueMap();
const nodeDefinitions = getCoreNodeDefinitions(valueTypes);

Logger.onWarn.clear();
Expand Down
68 changes: 35 additions & 33 deletions packages/core/src/Profiles/Core/registerCoreProfile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable max-len */
import {
NodeDefinition,
NodeDefinitionsMap
} from '../../Nodes/Registry/NodeDefinitionsMap.js';
import { NodeDefinition } from '../../Nodes/Registry/NodeDefinitionsMap.js';
import { getNodeDescriptions } from '../../Nodes/Registry/NodeDescription.js';
import { IRegistry } from '../../Registry.js';
import { ValueType } from '../../Values/ValueType.js';
import { ValueTypeMap } from '../../Values/ValueTypeMap.js';
import { getStringConversionsForValueType } from '../registerSerializersForValueType.js';
import { ILifecycleEventEmitter } from './Abstractions/ILifecycleEventEmitter.js';
Expand Down Expand Up @@ -47,42 +46,38 @@ import { VariableGet } from './Variables/VariableGet.js';
import { VariableSet } from './Variables/VariableSet.js';

export const makeCoreDependencies = ({
lifecyleEmitter,
lifecycleEmitter,
logger
}: {
lifecyleEmitter: ILifecycleEventEmitter;
lifecycleEmitter: ILifecycleEventEmitter;
logger: ILogger;
}) => ({
[lifecycleEventEmitterDependencyKey]: lifecyleEmitter,
[lifecycleEventEmitterDependencyKey]: lifecycleEmitter,
[loggerDependencyKey]: logger
});

export function getCoreValueTypes(): ValueTypeMap {
return toMap(
[BooleanValue, StringValue, IntegerValue, FloatValue],
(v) => v.name
);
}
export const CoreValueTypes: ValueType[] = [
BooleanValue,
StringValue,
IntegerValue,
FloatValue
];

export function toMap<T>(
elements: T[],
getName: (element: T) => string
): Record<string, T> {
return Object.fromEntries(
elements.map((element) => [getName(element), element])
);
}
export const CoreValueMap: ValueTypeMap = Object.fromEntries(
CoreValueTypes.map((valueType) => [valueType.name, valueType])
);

export const CoreValueNames = Object.keys(CoreValueMap);

function getStringConversions(values: ValueTypeMap): NodeDefinition[] {
return ['boolean', 'float', 'integer'].flatMap((valueTypeName) =>
getStringConversionsForValueType({ values, valueTypeName })
return CoreValueNames.filter((name) => name !== 'string').flatMap(
(valueTypeName) =>
getStringConversionsForValueType({ values, valueTypeName })
);
}

export function getCoreNodeDefinitions(
values: ValueTypeMap
): NodeDefinitionsMap {
const allNodeDefinitions: NodeDefinition[] = [
export function getCoreNodeDefinitions(values: ValueTypeMap): NodeDefinition[] {
return [
...getNodeDescriptions(StringNodes),
...getNodeDescriptions(BooleanNodes),
...getNodeDescriptions(IntegerNodes),
Expand Down Expand Up @@ -137,15 +132,22 @@ export function getCoreNodeDefinitions(

...getStringConversions(values)
];

// convert array to map
return toMap(allNodeDefinitions, (node) => node.typeName);
}

export const getCoreRegistry = () => {
const values = getCoreValueTypes();
export const getCoreDefinitionMap = (
values: Record<string, ValueType>
): Record<string, NodeDefinition> =>
Object.fromEntries(
getCoreNodeDefinitions(values).map((nodeDefinition) => [
nodeDefinition.typeName,
nodeDefinition
])
);

export const registerCoreProfile = (registry: IRegistry): IRegistry => {
const values = { ...registry.values, ...CoreValueMap };
return {
values,
nodes: getCoreNodeDefinitions(getCoreValueTypes())
nodes: { ...registry.nodes, ...getCoreDefinitionMap(values) }
};
};
4 changes: 2 additions & 2 deletions packages/flow/src/hooks/useCoreRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
getCoreNodeDefinitions,
getCoreValueTypes,
getCoreValueMap,
ValueTypeMap
} from '@behave-graph/core';
import { useMemo } from 'react';

import { useCoreDependencies } from './useDependencies.js';

export const useCoreValueDefinitions = () => {
return useMemo(() => getCoreValueTypes(), []);
return useMemo(() => getCoreValueMap(), []);
};

export const useCoreNodeDefinitions = ({
Expand Down
2 changes: 1 addition & 1 deletion packages/flow/src/hooks/useDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useEffect, useState } from 'react';
export const useCoreDependencies = () => {
const [dependencies] = useState(() =>
makeCoreDependencies({
lifecyleEmitter: new ManualLifecycleEventEmitter(),
lifecycleEmitter: new ManualLifecycleEventEmitter(),
logger: new DefaultLogger()
})
);
Expand Down
4 changes: 2 additions & 2 deletions packages/flow/src/hooks/useRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
DefaultLogger,
getCoreNodeDefinitions,
getCoreValueTypes,
getCoreValueMap,
IRegistry,
ManualLifecycleEventEmitter
} from '@behave-graph/core';
Expand All @@ -10,7 +10,7 @@ import { useState } from 'react';
const createRegistry = () => {
const manualLifecycleEventEmitter = new ManualLifecycleEventEmitter();
const logger = new DefaultLogger();
const valueTypeMap = getCoreValueTypes();
const valueTypeMap = getCoreValueMap();
const nodeDefinitionMap = getCoreNodeDefinitions(valueTypeMap);

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/flow/src/util/getNodeSpecJSON.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
DefaultLogger,
getCoreNodeDefinitions,
getCoreValueTypes,
getCoreValueMap,
ManualLifecycleEventEmitter,
NodeSpecJSON,
writeNodeSpecsToJSON
Expand All @@ -13,7 +13,7 @@ export const getNodeSpecJSON = (): NodeSpecJSON[] => {
if (nodeSpecJSON === undefined) {
const lifecycleEventEmitter = new ManualLifecycleEventEmitter();
const logger = new DefaultLogger();
const valueTypeMap = getCoreValueTypes();
const valueTypeMap = getCoreValueMap();
const nodeDefinitionMap = getCoreNodeDefinitions(valueTypeMap);
nodeSpecJSON = writeNodeSpecsToJSON({
nodes: nodeDefinitionMap,
Expand Down
Loading

0 comments on commit e406df8

Please sign in to comment.