Skip to content

Commit

Permalink
support subTypeGraphFetchTree (#2259)
Browse files Browse the repository at this point in the history
  • Loading branch information
siaka-Akash authored May 28, 2023
1 parent 9070a13 commit 004bfc0
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .changeset/fifty-bats-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
'@finos/legend-manual-tests': patch
---

5 changes: 5 additions & 0 deletions .changeset/soft-pens-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@finos/legend-graph': minor
---

Added support for `subTypeGraphFetchTree`
4 changes: 4 additions & 0 deletions .changeset/warm-oranges-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
'@finos/legend-query-builder': patch
---

Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@

import type { V1_PropertyGraphFetchTree } from './V1_PropertyGraphFetchTree.js';
import type { V1_RootGraphFetchTree } from './V1_RootGraphFetchTree.js';
import type { V1_SubTypeGraphFetchTree } from './V1_SubTypeGraphFetchTree.js';

export interface V1_GraphFetchTreeVisitor<T> {
visit_RootGraphFetchTree(valueSpecification: V1_RootGraphFetchTree): T;
visit_PropertyGraphFetchTree(
valueSpecification: V1_PropertyGraphFetchTree,
): T;
visit_SubTypeGraphFetchTree(valueSpecification: V1_SubTypeGraphFetchTree): T;
}

export abstract class V1_GraphFetchTree {
subTrees: V1_GraphFetchTree[] = [];
subTypeTrees: V1_SubTypeGraphFetchTree[] = [];

abstract accept_GraphFetchTreeVisitor<T>(
visitor: V1_GraphFetchTreeVisitor<T>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
V1_GraphFetchTree,
type V1_GraphFetchTreeVisitor,
} from './V1_GraphFetchTree.js';

export class V1_SubTypeGraphFetchTree extends V1_GraphFetchTree {
subTypeClass!: string;

accept_GraphFetchTreeVisitor<T>(visitor: V1_GraphFetchTreeVisitor<T>): T {
return visitor.visit_SubTypeGraphFetchTree(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
type GraphFetchTreeInstanceValue,
PropertyGraphFetchTree,
RootGraphFetchTree,
SubTypeGraphFetchTree,
} from '../../../../../../../graph/metamodel/pure/valueSpecification/GraphFetchTree.js';
import type {
PrimitiveInstanceValue,
Expand Down Expand Up @@ -69,6 +70,7 @@ import { V1_EnumValue } from '../../../model/valueSpecification/raw/V1_EnumValue
import { V1_PropertyGraphFetchTree } from '../../../model/valueSpecification/raw/classInstance/graph/V1_PropertyGraphFetchTree.js';
import { V1_RootGraphFetchTree } from '../../../model/valueSpecification/raw/classInstance/graph/V1_RootGraphFetchTree.js';
import type { V1_GraphFetchTree } from '../../../model/valueSpecification/raw/classInstance/graph/V1_GraphFetchTree.js';
import { V1_SubTypeGraphFetchTree } from '../../../model/valueSpecification/raw/classInstance/graph/V1_SubTypeGraphFetchTree.js';
import { V1_Collection } from '../../../model/valueSpecification/raw/V1_Collection.js';
import { V1_PackageableElementPtr } from '../../../model/valueSpecification/raw/V1_PackageableElementPtr.js';
import { V1_KeyExpression } from '../../../model/valueSpecification/raw/V1_KeyExpression.js';
Expand Down Expand Up @@ -386,6 +388,18 @@ export function V1_transformGraphFetchTree(
useAppliedFunction,
),
);
_root.subTypeTrees = value.subTypeTrees.map((e) =>
guaranteeType(
V1_transformGraphFetchTree(
e,
inScope,
open,
isParameter,
useAppliedFunction,
),
V1_SubTypeGraphFetchTree,
),
);
return _root;
} else if (value instanceof PropertyGraphFetchTree) {
const _propertyGraphTree = new V1_PropertyGraphFetchTree();
Expand All @@ -412,6 +426,19 @@ export function V1_transformGraphFetchTree(
),
);
return _propertyGraphTree;
} else if (value instanceof SubTypeGraphFetchTree) {
const _subTypeGraphFetchTree = new V1_SubTypeGraphFetchTree();
_subTypeGraphFetchTree.subTypeClass = value.subTypeClass.value.path;
_subTypeGraphFetchTree.subTrees = value.subTrees.map((subTree) =>
V1_transformGraphFetchTree(
subTree,
inScope,
open,
isParameter,
useAppliedFunction,
),
);
return _subTypeGraphFetchTree;
}
throw new UnsupportedOperationError(
`Can't build graph fetch tree node of type '${value.toString()}'`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
type GraphFetchTree,
PropertyGraphFetchTree,
RootGraphFetchTree,
SubTypeGraphFetchTree,
GraphFetchTreeInstanceValue,
} from '../../../../../../../../graph/metamodel/pure/valueSpecification/GraphFetchTree.js';
import { ValueSpecification } from '../../../../../../../../graph/metamodel/pure/valueSpecification/ValueSpecification.js';
Expand Down Expand Up @@ -99,6 +100,7 @@ import {
KeyExpression,
KeyExpressionInstanceValue,
} from '../../../../../../../../graph/metamodel/pure/valueSpecification/KeyExpressionInstanceValue.js';
import { V1_SubTypeGraphFetchTree } from '../../../../model/valueSpecification/raw/classInstance/graph/V1_SubTypeGraphFetchTree.js';

const buildPrimtiveInstanceValue = (
type: PRIMITIVE_TYPE,
Expand Down Expand Up @@ -691,6 +693,13 @@ export function V1_buildGraphFetchTree(
openVariables,
processingContext,
);
} else if (graphFetchTree instanceof V1_SubTypeGraphFetchTree) {
return buildSubTypeGraphFetchTree(
graphFetchTree,
context,
openVariables,
processingContext,
);
}
throw new UnsupportedOperationError(
`Can't build graph fetch tree`,
Expand Down Expand Up @@ -791,7 +800,7 @@ function buildRootGraphFetchTree(
processingContext: V1_ProcessingContext,
): RootGraphFetchTree {
const _class = context.resolveClass(rootGraphFetchTree.class);
const children = rootGraphFetchTree.subTrees.map((subTree) =>
const subTreeChildren = rootGraphFetchTree.subTrees.map((subTree) =>
V1_buildGraphFetchTree(
subTree,
context,
Expand All @@ -800,9 +809,42 @@ function buildRootGraphFetchTree(
processingContext,
),
);
const subTypeTreeChildren = rootGraphFetchTree.subTypeTrees.map(
(subTypeTree) =>
buildSubTypeGraphFetchTree(
subTypeTree,
context,
openVariables,
processingContext,
),
);
const _rootGraphFetchTree = new RootGraphFetchTree(
PackageableElementExplicitReference.create(_class.value),
);
_rootGraphFetchTree.subTrees = children;
_rootGraphFetchTree.subTrees = subTreeChildren;
_rootGraphFetchTree.subTypeTrees = subTypeTreeChildren;
return _rootGraphFetchTree;
}

function buildSubTypeGraphFetchTree(
subTypeGraphFetchTree: V1_SubTypeGraphFetchTree,
context: V1_GraphBuilderContext,
openVariables: string[],
processingContext: V1_ProcessingContext,
): SubTypeGraphFetchTree {
const subTypeClass = context.resolveClass(subTypeGraphFetchTree.subTypeClass);
const children = subTypeGraphFetchTree.subTrees.map((subTree) =>
V1_buildGraphFetchTree(
subTree,
context,
subTypeClass.value,
openVariables,
processingContext,
),
);
const _subTypeGraphFetchTree = new SubTypeGraphFetchTree(
PackageableElementExplicitReference.create(subTypeClass.value),
);
_subTypeGraphFetchTree.subTrees = children;
return _subTypeGraphFetchTree;
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const cases: TestCase[] = [
subTrees: [],
},
],
subTypeTrees: [],
},
{
_type: 'classInstance',
Expand All @@ -199,6 +200,7 @@ const cases: TestCase[] = [
subTrees: [],
},
],
subTypeTrees: [],
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ const rootRelationalClassMappingModelSchema = createModelSchema(
{
_type: usingConstantValueSchema(V1_ClassMappingType.ROOT_RELATIONAL),
class: primitive(),
extendsClassMappingId: optional(primitive()),
distinct: primitive(),
extendsClassMappingId: optional(primitive()),
filter: usingModelSchema(V1_filterMappingModelSchema),
groupBy: customList(
V1_serializeRelationalOperationElement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { V1_Pair } from '../../../model/valueSpecification/raw/classInstance/V1_
import { V1_RuntimeInstance } from '../../../model/valueSpecification/raw/classInstance/V1_RuntimeInstance.js';
import { V1_ExecutionContextInstance } from '../../../model/valueSpecification/raw/classInstance/V1_ExecutionContextInstance.js';
import { V1_PropertyGraphFetchTree } from '../../../model/valueSpecification/raw/classInstance/graph/V1_PropertyGraphFetchTree.js';
import { V1_SubTypeGraphFetchTree } from '../../../model/valueSpecification/raw/classInstance/graph/V1_SubTypeGraphFetchTree.js';
import { V1_SerializationConfig } from '../../../model/valueSpecification/raw/classInstance/V1_SerializationConfig.js';
import { V1_KeyExpression } from '../../../model/valueSpecification/raw/V1_KeyExpression.js';
import { V1_PureList } from '../../../model/valueSpecification/raw/classInstance/V1_PureList.js';
Expand Down Expand Up @@ -100,7 +101,7 @@ export enum V1_GraphFetchTreeType {

export enum V1_ClassInstanceType {
ROOT_GRAPH_FETCH_TREE = 'rootGraphFetchTree',

SUBTYPE_GRAPH_FETCH_TREE = 'subTypeGraphFetchTree',
PATH = 'path',

AGGREGATE_VALUE = 'aggregateValue',
Expand Down Expand Up @@ -445,6 +446,12 @@ const rootGraphFetchTreeModelSchema = (
(val) => V1_deserializeGraphFetchTree(val, plugins),
),
),
subTypeTrees: list(
custom(
(val) => V1_serializeGraphFetchTree(val, plugins),
(val) => V1_deserializeGraphFetchTree(val, plugins),
),
),
});

const propertyGraphFetchTreeModelSchema = (
Expand All @@ -471,6 +478,22 @@ const propertyGraphFetchTreeModelSchema = (
subType: optional(primitive()),
});

const subTypeGraphFetchTreeModelSchema = (
plugins: PureProtocolProcessorPlugin[],
): ModelSchema<V1_SubTypeGraphFetchTree> =>
createModelSchema(V1_SubTypeGraphFetchTree, {
_type: usingConstantValueSchema(
V1_ClassInstanceType.SUBTYPE_GRAPH_FETCH_TREE,
),
subTrees: list(
custom(
(val) => V1_serializeGraphFetchTree(val, plugins),
(val) => V1_deserializeGraphFetchTree(val, plugins),
),
),
subTypeClass: primitive(),
});

function V1_serializeGraphFetchTree(
protocol: V1_GraphFetchTree,
plugins: PureProtocolProcessorPlugin[],
Expand All @@ -479,6 +502,8 @@ function V1_serializeGraphFetchTree(
return serialize(propertyGraphFetchTreeModelSchema(plugins), protocol);
} else if (protocol instanceof V1_RootGraphFetchTree) {
return serialize(rootGraphFetchTreeModelSchema(plugins), protocol);
} else if (protocol instanceof V1_SubTypeGraphFetchTree) {
return serialize(subTypeGraphFetchTreeModelSchema(plugins), protocol);
}
throw new UnsupportedOperationError(
`Can't serialize graph fetch tree`,
Expand All @@ -495,6 +520,8 @@ function V1_deserializeGraphFetchTree(
return deserialize(propertyGraphFetchTreeModelSchema(plugins), json);
case V1_ClassInstanceType.ROOT_GRAPH_FETCH_TREE:
return deserialize(rootGraphFetchTreeModelSchema(plugins), json);
case V1_ClassInstanceType.SUBTYPE_GRAPH_FETCH_TREE:
return deserialize(subTypeGraphFetchTreeModelSchema(plugins), json);
default:
throw new UnsupportedOperationError(
`Can't deserialize graph fetch tree node of type '${json._type}'`,
Expand Down
2 changes: 2 additions & 0 deletions packages/legend-graph/src/graph/Core_HashUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export enum CORE_HASH_STRUCTURE {
ALLOY_SERIALIZATION_CONFIG = 'ALLOY_SERIALIZATION_CONFIG',
ROOT_GRAPH_FETCH_TREE = 'ROOT_GRAPH_FETCH_TREE',
PROPERTY_GRAPH_FETCH_TREE = 'PROPERTY_GRAPH_FETCH_TREE',
SUBTYPE_GRAPH_FETCH_TREE = 'SUBTYPE_GRAPH_FETCH_TREE',
SUBTYPE_GRAPH_FETCH_TREE_INSTANCE_VALUE = 'SUBTYPE_GRAPH_FETCH_TREE_INSTANCE_VALUE',
PROPERTY_GRAPH_FETCH_TREE_INSTANCE_VALUE = 'PROEPRTY_GRAPH_FETCH_TREE_INSTANCE_VALUE',
ROOT_GRAPH_FETCH_TREE_INSTANCE_VALUE = 'ROOT_GRAPH_FETCH_TREE_INSTANCE_VALUE',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Multiplicity } from '../packageableElements/domain/Multiplicity.js';

export abstract class GraphFetchTree implements Hashable {
subTrees: GraphFetchTree[] = [];

subTypeTrees: SubTypeGraphFetchTree[] = [];
abstract get hashCode(): string;

get isEmpty(): boolean {
Expand All @@ -43,6 +43,7 @@ export class RootGraphFetchTree extends GraphFetchTree implements Hashable {
return hashArray([
CORE_HASH_STRUCTURE.ROOT_GRAPH_FETCH_TREE,
hashArray(this.subTrees),
hashArray(this.subTypeTrees),
this.class.valueForSerialization ?? '',
]);
}
Expand Down Expand Up @@ -80,6 +81,23 @@ export class PropertyGraphFetchTree extends GraphFetchTree implements Hashable {
}
}

export class SubTypeGraphFetchTree extends GraphFetchTree implements Hashable {
subTypeClass: PackageableElementReference<Class>;

constructor(subTypeClass: PackageableElementReference<Class>) {
super();
this.subTypeClass = subTypeClass;
}

get hashCode(): string {
return hashArray([
CORE_HASH_STRUCTURE.SUBTYPE_GRAPH_FETCH_TREE,
hashArray(this.subTrees),
this.subTypeClass.valueForSerialization ?? '',
]);
}
}

export class GraphFetchTreeInstanceValue
extends InstanceValue
implements Hashable
Expand Down
Loading

0 comments on commit 004bfc0

Please sign in to comment.