Skip to content

Commit

Permalink
refactor dataspace executable (finos#3002)
Browse files Browse the repository at this point in the history
  • Loading branch information
YannanGao-gs authored Aug 20, 2024
1 parent 33f95dd commit 9e2a29b
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.Handlers;
import org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.IncludedMappingHandler;
import org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.StoreProviderCompilerHelper;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType;
import org.finos.legend.engine.protocol.pure.v1.model.context.PackageableElementType;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
Expand All @@ -51,8 +52,6 @@
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.dataSpace.MappingIncludeDataSpace;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.dataSpace.DataSpacePackageableElementExecutable;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.dataSpace.DataSpaceTemplateExecutable;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.dataSpace.DataSpaceTemplateExecutablePointer;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.dataSpace.DataSpaceInlineTemplateExecutable;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.dataSpace.DataSpaceExecutionContext;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.diagram.Diagram;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.PackageableRuntime;
Expand Down Expand Up @@ -194,48 +193,62 @@ public Iterable<? extends Processor<?>> getExtraProcessors()
HashSet<String> executableIds = new HashSet<>();
metamodel._executables(dataSpace.executables != null ? ListIterate.collect(dataSpace.executables, executable ->
{
if (executable instanceof DataSpacePackageableElementExecutable)
if (executable.executionContextKey != null && !dataSpace.executionContexts.stream().map(c -> c.name).collect(Collectors.toList()).contains(executable.executionContextKey))
{
return new Root_meta_pure_metamodel_dataSpace_DataSpacePackageableElementExecutable_Impl("", null, context.pureModel.getClass("meta::pure::metamodel::dataSpace::DataSpacePackageableElementExecutable"))
._title(executable.title)
._description(executable.description)
._executable(context.pureModel.getPackageableElement(((DataSpacePackageableElementExecutable) executable).executable.path, ((DataSpacePackageableElementExecutable) executable).executable.sourceInformation));
throw new EngineException("Data space template executable's executionContextKey, " + executable.executionContextKey + ", is not valid. Please specify one from " + dataSpace.executionContexts.stream().map(c -> c.name).collect(Collectors.toList()).toString(), dataSpace.sourceInformation, EngineErrorType.COMPILATION);
}
else if (executable instanceof DataSpaceTemplateExecutable)
if (executable instanceof DataSpacePackageableElementExecutable)
{
if (executableIds.add(((DataSpaceTemplateExecutable) executable).id))
PackageableElement element;
String executablePath = ((DataSpacePackageableElementExecutable) executable).executable.path;
SourceInformation sourceInformation = ((DataSpacePackageableElementExecutable) executable).executable.sourceInformation;
String executableId;
try
{
if (((DataSpaceTemplateExecutable) executable).executionContextKey != null && !dataSpace.executionContexts.stream().map(c -> c.name).collect(Collectors.toList()).contains(((DataSpaceTemplateExecutable) executable).executionContextKey))
{
throw new EngineException("Data space template executable's executionContextKey, " + ((DataSpaceTemplateExecutable) executable).executionContextKey + ", is not valid. Please specify one from " + dataSpace.executionContexts.stream().map(c -> c.name).collect(Collectors.toList()).toString(), dataSpace.sourceInformation, EngineErrorType.COMPILATION);
}

FunctionDefinition<?> templateExecutableQuery = null;
if (executable instanceof DataSpaceInlineTemplateExecutable)
element = context.pureModel.getPackageableElement(executablePath, sourceInformation);
executableId = executable.id == null ? ((DataSpacePackageableElementExecutable) executable).executable.path : executable.id;
}
catch (Exception exception)
{
try
{
templateExecutableQuery = HelperValueSpecificationBuilder.buildLambda(((DataSpaceInlineTemplateExecutable) executable).query, context);
element = context.pureModel.getPackageableElement(FunctionDescriptor.functionDescriptorToId(executablePath), sourceInformation);
executableId = executable.id;
}
else if (executable instanceof DataSpaceTemplateExecutablePointer)
catch (InvalidFunctionDescriptorException e)
{
try
{
templateExecutableQuery = (FunctionDefinition<?>) context.resolvePackageableElement(FunctionDescriptor.functionDescriptorToId((((DataSpaceTemplateExecutablePointer) executable).query).path), (((DataSpaceTemplateExecutablePointer) executable).query).sourceInformation);
}
catch (InvalidFunctionDescriptorException e)
{
throw new RuntimeException(e);
}
throw new EngineException(exception.getMessage(), EngineErrorType.COMPILATION);
}
}
if (executableIds.add(executableId))
{
return new Root_meta_pure_metamodel_dataSpace_DataSpacePackageableElementExecutable_Impl("", null, context.pureModel.getClass("meta::pure::metamodel::dataSpace::DataSpacePackageableElementExecutable"))
._id(executable.id)
._title(executable.title)
._description(executable.description)
._executionContextKey(executable.executionContextKey)
._executable(element);
}
else
{
throw new EngineException("Data space executable id, " + executableId + ", is not unique", dataSpace.sourceInformation, EngineErrorType.COMPILATION);
}
}
else if (executable instanceof DataSpaceTemplateExecutable)
{
if (executableIds.add(executable.id))
{
FunctionDefinition<?> templateExecutableQuery = HelperValueSpecificationBuilder.buildLambda(((DataSpaceTemplateExecutable) executable).query, context);
return new Root_meta_pure_metamodel_dataSpace_DataSpaceTemplateExecutable_Impl("", null, context.pureModel.getClass("meta::pure::metamodel::dataSpace::DataSpaceTemplateExecutable"))
._id(((DataSpaceTemplateExecutable) executable).id)
._id(executable.id)
._title(executable.title)
._description(executable.description)
._query(templateExecutableQuery)
._executionContextKey(((DataSpaceTemplateExecutable) executable).executionContextKey);
._executionContextKey(executable.executionContextKey);
}
else
{
throw new EngineException("Data space executable id, " + ((DataSpaceTemplateExecutable) executable).id + ", is not unique", dataSpace.sourceInformation, EngineErrorType.COMPILATION);
throw new EngineException("Data space executable id, " + executable.id + ", is not unique", dataSpace.sourceInformation, EngineErrorType.COMPILATION);
}
}
else
Expand Down Expand Up @@ -301,14 +314,23 @@ else if (dataSpace.supportInfo instanceof DataSpaceSupportCombinedInfo)
{
dataSpace.executables.forEach(executable ->
{
if (executable instanceof DataSpaceTemplateExecutablePointer)
if (executable instanceof DataSpacePackageableElementExecutable)
{
FunctionDefinition<?> executableFunction = null;
try
{
FunctionDefinition<?> templateExecutableQuery = (FunctionDefinition<?>) context.resolvePackageableElement(FunctionDescriptor.functionDescriptorToId((((DataSpaceTemplateExecutablePointer) executable).query).path), (((DataSpaceTemplateExecutablePointer) executable).query).sourceInformation);
if (templateExecutableQuery instanceof Root_meta_pure_metamodel_function_ConcreteFunctionDefinition_Impl)
// function
executableFunction = (FunctionDefinition<?>) context.resolvePackageableElement(FunctionDescriptor.functionDescriptorToId((((DataSpacePackageableElementExecutable) executable).executable).path), ((DataSpacePackageableElementExecutable) executable).executable.sourceInformation);
}
catch (Exception e)
{
// service
}
if (executableFunction != null)
{
if (executableFunction instanceof Root_meta_pure_metamodel_function_ConcreteFunctionDefinition_Impl)
{
Optional<? extends ValueSpecification> fromFunc = templateExecutableQuery._expressionSequence().toList().stream()
Optional<? extends ValueSpecification> fromFunc = executableFunction._expressionSequence().toList().stream()
.filter(func -> func instanceof Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl && ((Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl) func)._functionName.equals("from")).findAny();
if (fromFunc.isPresent())
{
Expand All @@ -318,7 +340,7 @@ else if (dataSpace.supportInfo instanceof DataSpaceSupportCombinedInfo)
ValueSpecification mappingInstance = fromFuncExpression._parametersValues().toList().get(1);
if (mappingInstance instanceof Root_meta_pure_metamodel_valuespecification_InstanceValue_Impl)
{
String executionContextKey = ((DataSpaceTemplateExecutable) executable).executionContextKey != null ? ((DataSpaceTemplateExecutable) executable).executionContextKey : dataSpace.defaultExecutionContext;
String executionContextKey = executable.executionContextKey != null ? executable.executionContextKey : dataSpace.defaultExecutionContext;
DataSpaceExecutionContext executionContext = dataSpace.executionContexts.stream().filter(ec -> ec.name.equals(executionContextKey)).collect(Collectors.toList()).get(0);
// check if mapping matches to what is used in execution key
Object mappingImpl = ((Root_meta_pure_metamodel_valuespecification_InstanceValue_Impl) mappingInstance)._values().toList().get(0);
Expand All @@ -331,7 +353,7 @@ else if (dataSpace.supportInfo instanceof DataSpaceSupportCombinedInfo)
}
}
// check if runtime matches to what is used in execution key
RichIterable<? extends org.finos.legend.pure.generated.Root_meta_core_runtime_Runtime> runtimes = org.finos.legend.pure.generated.core_pure_corefunctions_metaExtension.Root_meta_pure_functions_meta_extractRuntimesFromFunctionDefinition_FunctionDefinition_1__Runtime_MANY_(templateExecutableQuery, context.pureModel.getExecutionSupport());
RichIterable<? extends Root_meta_core_runtime_Runtime> runtimes = core_pure_corefunctions_metaExtension.Root_meta_pure_functions_meta_extractRuntimesFromFunctionDefinition_FunctionDefinition_1__Runtime_MANY_(executableFunction, context.pureModel.getExecutionSupport());
Root_meta_core_runtime_Runtime runtimeImpl;
if (runtimes.isEmpty())
{
Expand All @@ -357,10 +379,6 @@ else if (runtimes.size() == 1)
}
}
}
catch (InvalidFunctionDescriptorException e)
{
throw new RuntimeException(e);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ public void testDataSpaceWithTemplateExecutableWithInlineQuery()
}

@Test
public void testDataSpaceWithTemplateExecutableWithFunctionPointer()
public void testDataSpaceWithExecutableWithFunctionPointer()
{
String model = "Class model::element {}\n" +
"Class model::sub::element\n" +
Expand Down Expand Up @@ -1055,19 +1055,19 @@ public void testDataSpaceWithTemplateExecutableWithFunctionPointer()
" {\n" +
" id: 1;\n" +
" title: 'Template 1';\n" +
" query: model::templateFunc():TabularDataSet[1];\n" +
" executable: model::templateFunc():TabularDataSet[1];\n" +
" executionContextKey: 'Context 1';\n" +
" },\n" +
" {\n" +
" id: 2;\n" +
" title: 'Template 2';\n" +
" query: model::templateFunc():TabularDataSet[1];\n" +
" executable: model::templateFunc():TabularDataSet[1];\n" +
" },\n" +
" {\n" +
" id: 3;\n" +
" title: 'Template 3';\n" +
" description: 'an example of a template query';\n" +
" query: model::templateFunc():TabularDataSet[1];\n" +
" executable: model::templateFunc():TabularDataSet[1];\n" +
" }\n" +
" ];\n" +
"}\n");
Expand All @@ -1092,7 +1092,38 @@ public void testDataSpaceWithTemplateExecutableWithFunctionPointer()
" {\n" +
" id: 1;\n" +
" title: 'Template 1';\n" +
" query: model::templateFunc1():TabularDataSet[1];\n" +
" executable: model::templateFunc():TabularDataSet[1];\n" +
" executionContextKey: 'Context 1';\n" +
" },\n" +
" {\n" +
" id: 1;\n" +
" title: 'Template 2';\n" +
" executable: model::templateFunc():TabularDataSet[1];\n" +
" }\n" +
" ];\n" +
"}\n","COMPILATION error at [44:1-70:1]: Data space executable id, 1, is not unique");

test(model +
"###DataSpace\n" +
"DataSpace model::dataSpace" +
"{\n" +
" executionContexts:\n" +
" [\n" +
" {\n" +
" name: 'Context 1';\n" +
" description: 'some information about the context';\n" +
" mapping: model::dummyMapping;\n" +
" defaultRuntime: model::dummyRuntime;\n" +
" }\n" +
" ];\n" +
" defaultExecutionContext: 'Context 1';\n" +
" elements: [model::element, model, model::sub];\n" +
" executables:\n" +
" [\n" +
" {\n" +
" id: 1;\n" +
" title: 'Template 1';\n" +
" executable: model::templateFunc1():TabularDataSet[1];\n" +
" executionContextKey: 'Context 1';\n" +
" }\n" +
" ];\n" +
Expand All @@ -1118,7 +1149,7 @@ public void testDataSpaceWithTemplateExecutableWithFunctionPointer()
" {\n" +
" id: 1;\n" +
" title: 'Template 1';\n" +
" query: model::templateFunc2():TabularDataSet[1];\n" +
" executable: model::templateFunc2():TabularDataSet[1];\n" +
" executionContextKey: 'Context 1';\n" +
" }\n" +
" ];\n" +
Expand Down Expand Up @@ -1377,6 +1408,6 @@ public void testDataSpaceExecutablesCompilation()
" ];\n" +
" defaultExecutionContext: 'Context 1';\n" +
" executables: [{ title: 'MyExec'; executable: model::MyService; }, { title: 'MyExec1'; executable: model::Mine; }];\n" +
"}\n", "COMPILATION error at [53:89-112]: Can't find the packageable element 'model::Mine'");
"}\n", "COMPILATION error at [42:1-54:1]: Error in 'model::dataSpace': Can't find the packageable element 'model::Mine'");
}
}
Loading

0 comments on commit 9e2a29b

Please sign in to comment.