Skip to content

Commit

Permalink
Merge pull request #536 from jvalue/dependency-cycles
Browse files Browse the repository at this point in the history
Refactor Wrapper to Injected Service (WrapperFactory)
  • Loading branch information
georg-schwarz committed Apr 5, 2024
2 parents 33dd57c + 7f93694 commit b851b83
Show file tree
Hide file tree
Showing 90 changed files with 1,802 additions and 1,450 deletions.
2 changes: 2 additions & 0 deletions apps/docs/generator/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function generateBlockTypeDocs(
): void {
const blockTypes = getAllBuiltinBlocktypes(
services.shared.workspace.LangiumDocuments,
services.WrapperFactories,
);

const docsPath = join(
Expand Down Expand Up @@ -72,6 +73,7 @@ function generateConstraintTypeDocs(
);
const constraintTypes = getAllBuiltinConstraintTypes(
services.shared.workspace.LangiumDocuments,
services.WrapperFactories,
);

for (const constraintType of constraintTypes) {
Expand Down
4 changes: 2 additions & 2 deletions libs/execution/src/lib/blocks/block-execution-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
BlockDefinition,
CompositeBlocktypeDefinition,
PipelineDefinition,
PipelineWrapper,
} from '@jvalue/jayvee-language-server';

import { type ExecutionContext } from '../execution-context';
Expand Down Expand Up @@ -34,7 +33,8 @@ export async function executeBlocks(
pipesContainer: CompositeBlocktypeDefinition | PipelineDefinition,
initialInputValue: IOTypeImplementation | undefined = undefined,
): Promise<R.Result<ExecutionOrderItem[]>> {
const pipelineWrapper = new PipelineWrapper(pipesContainer);
const pipelineWrapper =
executionContext.wrapperFactories.Pipeline.wrap(pipesContainer);
const executionOrder: {
block: BlockDefinition;
value: IOTypeImplementation | null;
Expand Down
5 changes: 5 additions & 0 deletions libs/execution/src/lib/blocks/composite-block-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
IOType,
InternalValueRepresentation,
Valuetype,
WrapperFactoryProvider,
createValuetype,
evaluateExpression,
evaluatePropertyValue,
Expand Down Expand Up @@ -133,6 +134,7 @@ export function createCompositeBlockExecutor(
block,
properties,
context.evaluationContext,
context.wrapperFactories,
);

assert(
Expand All @@ -153,6 +155,7 @@ export function createCompositeBlockExecutor(
block: BlockDefinition,
properties: BlocktypeProperty[],
evaluationContext: EvaluationContext,
wrapperFactories: WrapperFactoryProvider,
): InternalValueRepresentation | undefined {
const propertyFromBlock = block.body.properties.find(
(property) => property.name === name,
Expand All @@ -162,6 +165,7 @@ export function createCompositeBlockExecutor(
const value = evaluatePropertyValue(
propertyFromBlock,
evaluationContext,
wrapperFactories,
valueType,
);

Expand All @@ -181,6 +185,7 @@ export function createCompositeBlockExecutor(
return evaluateExpression(
propertyFromBlockType.defaultValue,
evaluationContext,
wrapperFactories,
);
}
};
Expand Down
10 changes: 5 additions & 5 deletions libs/execution/src/lib/blocks/execution-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ export type Err = E.Left<ExecutionErrorDetails>;
export type Ok<T> = E.Right<T>;

/**
* Creates an @Ok object from a data object typed T.
* Creates an @see Ok object from a data object typed T.
* @param data the data object
* @returns the created @Ok object
* @returns the created @see Ok object
*/
export function ok<T>(data: T): Result<T> {
return E.right(data);
}
/**
* Creates an @Err object from a @ExecutionErrorDetails object.
* @param details the @ExecutionErrorDetails object
* @returns the created @Err object
* Creates an @see Err object from a @see ExecutionErrorDetails object.
* @param details the @see ExecutionErrorDetails object
* @returns the created @see Err object
*/
export function err<T>(details: ExecutionErrorDetails): Result<T> {
return E.left(details);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('default constraint extension', () => {

getAllBuiltinConstraintTypes(
services.shared.workspace.LangiumDocuments,
services.WrapperFactories,
).forEach((constraintType) => {
const matchingConstraintExecutorClass = defaultConstraintExtension
.getConstraintExecutors()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export class ExpressionConstraintExecutor

context.evaluationContext.setValueForValueKeyword(value);

const result = evaluateExpression(expression, context.evaluationContext);
const result = evaluateExpression(
expression,
context.evaluationContext,
context.wrapperFactories,
);
assert(PrimitiveValuetypes.Boolean.isInternalValueRepresentation(result));

context.evaluationContext.deleteValueForValueKeyword();
Expand Down
21 changes: 5 additions & 16 deletions libs/execution/src/lib/execution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import { strict as assert } from 'assert';

import {
BlockDefinition,
BlockTypeWrapper,
ConstraintDefinition,
ConstraintTypeWrapper,
EvaluationContext,
InternalValueRepresentation,
PipelineDefinition,
PropertyAssignment,
TransformDefinition,
Valuetype,
type WrapperFactoryProvider,
evaluatePropertyValue,
isBlockDefinition,
isExpressionConstraintDefinition,
Expand Down Expand Up @@ -46,6 +45,7 @@ export class ExecutionContext {
public readonly executionExtension: JayveeExecExtension,
public readonly constraintExtension: JayveeConstraintExtension,
public readonly logger: Logger,
public readonly wrapperFactories: WrapperFactoryProvider,
public readonly runOptions: {
isDebugMode: boolean;
debugGranularity: DebugGranularity;
Expand Down Expand Up @@ -99,6 +99,7 @@ export class ExecutionContext {
const propertyValue = evaluatePropertyValue(
property,
this.evaluationContext,
this.wrapperFactories,
valuetype,
);
assert(propertyValue !== undefined);
Expand Down Expand Up @@ -152,21 +153,9 @@ export class ExecutionContext {

assert(isReference(currentNode.type));
if (isTypedConstraintDefinition(currentNode)) {
assert(
ConstraintTypeWrapper.canBeWrapped(currentNode.type),
`ConstraintType ${
currentNode.type.ref?.name ?? '<unresolved reference>'
} cannot be wrapped`,
);
return new ConstraintTypeWrapper(currentNode.type);
return this.wrapperFactories.ConstraintType.wrap(currentNode.type);
} else if (isBlockDefinition(currentNode)) {
assert(
BlockTypeWrapper.canBeWrapped(currentNode.type),
`Blocktype ${
currentNode.type.ref?.name ?? '<unresolved reference>'
} cannot be wrapped`,
);
return new BlockTypeWrapper(currentNode.type);
return this.wrapperFactories.BlockType.wrap(currentNode.type);
}
assertUnreachable(currentNode);
}
Expand Down
1 change: 1 addition & 0 deletions libs/execution/src/lib/transforms/transform-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class TransformExecutor {
newValue = evaluateExpression(
this.getOutputAssignment().expression,
context.evaluationContext,
context.wrapperFactories,
);
} catch (e) {
if (e instanceof Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ValueRepresentationValidityVisitor extends ValuetypeVisitor<boolean> {

const constraints = valuetype.getConstraints(
this.context.evaluationContext,
this.context.wrapperFactories,
);
for (const constraint of constraints) {
const constraintExecutor =
Expand Down
10 changes: 9 additions & 1 deletion libs/execution/test/utils/test-infrastructure-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// SPDX-License-Identifier: AGPL-3.0-only

import {
DefaultOperatorEvaluatorRegistry,
EvaluationContext,
PipelineDefinition,
RuntimeParameterProvider,
WrapperFactoryProvider,
} from '@jvalue/jayvee-language-server';
import { AstNode, AstNodeLocator, LangiumDocument } from 'langium';

Expand Down Expand Up @@ -55,13 +57,19 @@ export function getTestExecutionContext(
'pipelines@0',
) as PipelineDefinition;

const operatorEvaluatorRegistry = new DefaultOperatorEvaluatorRegistry();

const executionContext = new ExecutionContext(
pipeline,
new TestExecExtension(),
new DefaultConstraintExtension(),
new CachedLogger(runOptions.isDebugMode, undefined, loggerPrintLogs),
new WrapperFactoryProvider(operatorEvaluatorRegistry),
runOptions,
new EvaluationContext(new RuntimeParameterProvider()),
new EvaluationContext(
new RuntimeParameterProvider(),
operatorEvaluatorRegistry,
),
);

initialStack.forEach((node) => executionContext.enterNode(node));
Expand Down
22 changes: 14 additions & 8 deletions libs/interpreter-lib/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
JayveeModel,
JayveeServices,
PipelineDefinition,
PipelineWrapper,
RuntimeParameterProvider,
WrapperFactoryProvider,
createJayveeServices,
initializeWorkspace,
internalValueToString,
Expand Down Expand Up @@ -136,7 +136,7 @@ export async function interpretModel(
model,
new StdExecExtension(),
new DefaultConstraintExtension(),
services.RuntimeParameterProvider,
services,
loggerFactory,
{
debug: options.debug,
Expand Down Expand Up @@ -177,7 +177,7 @@ async function interpretJayveeModel(
model: JayveeModel,
executionExtension: JayveeExecExtension,
constraintExtension: JayveeConstraintExtension,
runtimeParameterProvider: RuntimeParameterProvider,
jayveeServices: JayveeServices,
loggerFactory: LoggerFactory,
runOptions: InterpreterOptions,
): Promise<ExitCode> {
Expand All @@ -186,7 +186,7 @@ async function interpretJayveeModel(
pipeline,
executionExtension,
constraintExtension,
runtimeParameterProvider,
jayveeServices,
loggerFactory,
runOptions,
);
Expand All @@ -203,7 +203,7 @@ async function runPipeline(
pipeline: PipelineDefinition,
executionExtension: JayveeExecExtension,
constraintExtension: JayveeConstraintExtension,
runtimeParameterProvider: RuntimeParameterProvider,
jayveeServices: JayveeServices,
loggerFactory: LoggerFactory,
runOptions: InterpreterOptions,
): Promise<ExitCode> {
Expand All @@ -212,18 +212,23 @@ async function runPipeline(
executionExtension,
constraintExtension,
loggerFactory.createLogger(),
jayveeServices.WrapperFactories,
{
isDebugMode: runOptions.debug,
debugGranularity: runOptions.debugGranularity,
debugTargets: runOptions.debugTargets,
},
new EvaluationContext(runtimeParameterProvider),
new EvaluationContext(
jayveeServices.RuntimeParameterProvider,
jayveeServices.operators.EvaluatorRegistry,
),
);

logPipelineOverview(
pipeline,
runtimeParameterProvider,
jayveeServices.RuntimeParameterProvider,
executionContext.logger,
jayveeServices.WrapperFactories,
);

const startTime = new Date();
Expand All @@ -248,8 +253,9 @@ export function logPipelineOverview(
pipeline: PipelineDefinition,
runtimeParameterProvider: RuntimeParameterProvider,
logger: Logger,
wrapperFactories: WrapperFactoryProvider,
) {
const pipelineWrapper = new PipelineWrapper(pipeline);
const pipelineWrapper = wrapperFactories.Pipeline.wrap(pipeline);

const toString = (block: BlockDefinition, depth = 0): string => {
const blockTypeName = block.type.ref?.name;
Expand Down
5 changes: 4 additions & 1 deletion libs/interpreter-lib/src/std-extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { NodeFileSystem } from 'langium/node';
async function loadAllBuiltinBlocktypes(): Promise<BlockTypeWrapper[]> {
const services = createJayveeServices(NodeFileSystem).Jayvee;
await initializeWorkspace(services);
return getAllBuiltinBlocktypes(services.shared.workspace.LangiumDocuments);
return getAllBuiltinBlocktypes(
services.shared.workspace.LangiumDocuments,
services.WrapperFactories,
);
}

describe('std extension', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import * as path from 'path';

import { parseValueToInternalRepresentation } from '@jvalue/jayvee-execution';
import {
DefaultOperatorEvaluatorRegistry,
DefaultOperatorTypeComputerRegistry,
EvaluationContext,
RuntimeParameterLiteral,
RuntimeParameterProvider,
ValidationContext,
WrapperFactoryProvider,
createJayveeServices,
} from '@jvalue/jayvee-language-server';
import {
Expand Down Expand Up @@ -60,11 +63,24 @@ describe('Validation of validateRuntimeParameterLiteral', () => {
}
}

validateRuntimeParameterLiteral(
runtimeParameter,
new ValidationContext(validationAcceptorMock),
new EvaluationContext(runtimeProvider),
const operatorEvaluatorRegistry = new DefaultOperatorEvaluatorRegistry();
const operatorTypeComputerRegistry =
new DefaultOperatorTypeComputerRegistry();
const wrapperFactories = new WrapperFactoryProvider(
operatorEvaluatorRegistry,
);

validateRuntimeParameterLiteral(runtimeParameter, {
validationContext: new ValidationContext(
validationAcceptorMock,
operatorTypeComputerRegistry,
),
evaluationContext: new EvaluationContext(
runtimeProvider,
operatorEvaluatorRegistry,
),
wrapperFactories: wrapperFactories,
});
}

beforeAll(async () => {
Expand Down
Loading

0 comments on commit b851b83

Please sign in to comment.