Skip to content

Commit

Permalink
Review legend compile pmcd (#3212)
Browse files Browse the repository at this point in the history
* compilePMCD and basic Test

* compilePMCD and basic Test
  • Loading branch information
hbalasu2 authored Oct 29, 2024
1 parent d6dd8d5 commit dea7680
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ function meta::legend::executePlanAsJSON(plan:String[1], vars: Pair<String, Any>
let func = $fStr->pathToElement()->cast(@Function<{String[1], Pair<String, Any>[*]->String[1]}>);
$func->eval($plan, $vars);
}

function meta::legend::compileLegendPMCD(pmcdJSON : String[1]):PackageableElement[*]
{
let fStr = 'meta::legend::compilePMCD_String_1__PackageableElement_MANY_';
let func = $fStr->pathToElement()->cast(@Function<{String[1]->PackageableElement[*]}>);
$func->eval($pmcdJSON);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ import meta::pure::mapping::*;
native function meta::legend::compile(s:String[1]):PackageableElement[*];

native function meta::legend::compileVS(s:String[1]):Any[1];

native function meta::legend::compilePMCD(pmcdJSON:String[1]):PackageableElement[*];
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.collections.api.factory.Lists;
import org.finos.legend.engine.pure.runtime.compiler.compiled.natives.LegendCompile;
import org.finos.legend.engine.pure.runtime.compiler.compiled.natives.LegendCompilePMCD;
import org.finos.legend.engine.pure.runtime.compiler.compiled.natives.LegendCompileVS;
import org.finos.legend.pure.runtime.java.compiled.extension.BaseCompiledExtension;
import org.finos.legend.pure.runtime.java.compiled.extension.CompiledExtension;
Expand All @@ -26,7 +27,7 @@ public CompileExtensionCompiled()
{
super(
"core_external_compiler",
() -> Lists.fixedSize.with(new LegendCompile(), new LegendCompileVS()),
() -> Lists.fixedSize.with(new LegendCompile(), new LegendCompileVS(), new LegendCompilePMCD()),
Lists.fixedSize.with(),
Lists.fixedSize.empty(),
Lists.fixedSize.empty());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2022 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.

package org.finos.legend.engine.pure.runtime.compiler.compiled.natives;

import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.list.MutableList;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement;
import org.finos.legend.pure.m3.execution.ExecutionSupport;
import org.finos.legend.pure.m3.navigation.Instance;
import org.finos.legend.pure.m3.navigation.M3Properties;
import org.finos.legend.pure.m3.navigation.ProcessorSupport;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;
import org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport;
import org.finos.legend.pure.runtime.java.compiled.generation.ProcessorContext;
import org.finos.legend.pure.runtime.java.compiled.generation.processors.natives.AbstractNative;
import org.finos.legend.pure.runtime.java.compiled.generation.processors.valuespecification.ValueSpecificationProcessor;

public class LegendCompilePMCD extends AbstractNative
{
public LegendCompilePMCD()
{
super("compilePMCD_String_1__PackageableElement_MANY_");
}

@Override
public String build(CoreInstance topLevelElement, CoreInstance functionExpression, ListIterable<String> listIterable, ProcessorContext processorContext)
{
final ProcessorSupport processorSupport = processorContext.getSupport();
final ListIterable<? extends CoreInstance> parametersValues = Instance.getValueForMetaPropertyToManyResolved(functionExpression, M3Properties.parametersValues, processorSupport);

String code = ValueSpecificationProcessor.processValueSpecification(topLevelElement, parametersValues.get(0), processorContext);

return "org.finos.legend.engine.pure.runtime.compiler.compiled.natives.LegendCompilePMCD.compileExecPMCD(" + code + ", es)";
}

@Override
public String buildBody()
{
return "new SharedPureFunction<Object>()\n" +
" {\n" +
" @Override\n" +
" public Object execute(ListIterable<?> vars, final ExecutionSupport es)\n" +
" {\n" +
" return org.finos.legend.engine.pure.runtime.compiler.compiled.natives.LegendCompilePMCD.compileExecPMCD((String) vars.get(0), es);\n" +
" }\n" +
" }";
}

public static MutableList<PackageableElement> compileExecPMCD(String code, final ExecutionSupport es)
{
return org.finos.legend.engine.pure.runtime.compiler.shared.LegendCompile.doCompilePMCD(code, ((CompiledExecutionSupport) es).getProcessorSupport().getMetadata());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<artifactId>legend-pure-m2-dsl-diagram-grammar</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.tuple.Tuples;
import org.finos.legend.engine.pure.runtime.compiler.interpreted.natives.LegendCompile;
import org.finos.legend.engine.pure.runtime.compiler.interpreted.natives.LegendCompilePMCD;
import org.finos.legend.engine.pure.runtime.compiler.interpreted.natives.LegendCompileVS;
import org.finos.legend.pure.runtime.java.interpreted.extension.BaseInterpretedExtension;
import org.finos.legend.pure.runtime.java.interpreted.extension.InterpretedExtension;
Expand All @@ -27,7 +28,8 @@ public CompileExtensionInterpreted()
{
super(Lists.mutable.with(
Tuples.pair("compile_String_1__PackageableElement_MANY_", LegendCompile::new),
Tuples.pair("compileVS_String_1__Any_1_", LegendCompileVS::new)
Tuples.pair("compileVS_String_1__Any_1_", LegendCompileVS::new),
Tuples.pair("compilePMCD_String_1__PackageableElement_MANY_", LegendCompilePMCD::new)
));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2022 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.

package org.finos.legend.engine.pure.runtime.compiler.interpreted.natives;

import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.finos.legend.pure.m3.compiler.Context;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement;
import org.finos.legend.pure.m3.exception.PureExecutionException;
import org.finos.legend.pure.m3.navigation.Instance;
import org.finos.legend.pure.m3.navigation.M3Properties;
import org.finos.legend.pure.m3.navigation.ProcessorSupport;
import org.finos.legend.pure.m3.navigation.ValueSpecificationBootstrap;
import org.finos.legend.pure.m4.ModelRepository;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;
import org.finos.legend.pure.runtime.java.interpreted.ExecutionSupport;
import org.finos.legend.pure.runtime.java.interpreted.FunctionExecutionInterpreted;
import org.finos.legend.pure.runtime.java.interpreted.VariableContext;
import org.finos.legend.pure.runtime.java.interpreted.natives.InstantiationContext;
import org.finos.legend.pure.runtime.java.interpreted.natives.NativeFunction;
import org.finos.legend.pure.runtime.java.interpreted.profiler.Profiler;

import java.util.Stack;

public class LegendCompilePMCD extends NativeFunction
{
private final FunctionExecutionInterpreted functionExecution;

public LegendCompilePMCD(FunctionExecutionInterpreted functionExecution, ModelRepository modelRepository)
{
this.functionExecution = functionExecution;
}

@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, Stack<MutableMap<String, CoreInstance>> stack, Stack<MutableMap<String, CoreInstance>> stack1, VariableContext variableContext, CoreInstance coreInstance, Profiler profiler, InstantiationContext instantiationContext, ExecutionSupport executionSupport, Context context, ProcessorSupport processorSupport) throws PureExecutionException
{
String code = Instance.getValueForMetaPropertyToOneResolved(params.get(0), M3Properties.values, processorSupport).getName();

MutableList<PackageableElement> createdElements = null;
createdElements = org.finos.legend.engine.pure.runtime.compiler.shared.LegendCompile.doCompilePMCD(code, new InterpretedMetadata(processorSupport));
return ValueSpecificationBootstrap.wrapValueSpecification(createdElements, true, functionExecution.getProcessorSupport());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,14 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- JACKSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package org.finos.legend.engine.pure.runtime.compiler.shared;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.utility.ListIterate;
import org.finos.legend.engine.language.pure.compiler.toPureGraph.HelperModelBuilder;
Expand All @@ -22,9 +24,9 @@
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Function;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex;
import org.finos.legend.engine.shared.core.ObjectMapperFactory;
import org.finos.legend.engine.shared.core.deployment.DeploymentMode;
import org.finos.legend.engine.shared.core.identity.Identity;
import org.finos.legend.engine.shared.core.identity.factory.*;
import org.finos.legend.engine.shared.core.operational.Assert;
import org.finos.legend.pure.m3.coreinstance.Package;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.ModelElementAccessor;
Expand Down Expand Up @@ -55,6 +57,27 @@ public static ValueSpecification doCompileVS(String code, Metadata metadata)
return ((ConcreteFunctionDefinition<?>) extractCreatedElementFromCompiledGraph(data, pm).getFirst())._expressionSequence().getFirst();
}

public static MutableList<PackageableElement> doCompilePMCD(String code, Metadata metadata)
{
// Parse
ObjectMapper objectMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports();
PureModelContextData data = null;
try
{
data = objectMapper.readValue(code, PureModelContextData.class);
}
catch (JsonProcessingException e)
{
throw new RuntimeException(e);
}

// Compile
PureModel pm = org.finos.legend.engine.language.pure.compiler.Compiler.compile(data, DeploymentMode.PROD, Identity.getAnonymousIdentity().getName(), "", metadata);

// Extract Compiled created elements
return extractCreatedElementFromCompiledGraph(data, pm);
}

private static MutableList<PackageableElement> extractCreatedElementFromCompiledGraph(PureModelContextData pureModelContextData, PureModel pureModel)
{
return ListIterate.collectIf(pureModelContextData.getElements(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

package org.finos.legend.engine.pure.runtime.compiler.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.collections.impl.tuple.Tuples;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class;
import org.finos.legend.engine.shared.core.ObjectMapperFactory;
import org.finos.legend.pure.m3.execution.FunctionExecution;
import org.finos.legend.pure.m3.serialization.runtime.PureRuntime;
import org.junit.Test;
Expand Down Expand Up @@ -127,6 +132,23 @@ public void testValueSpecificationEmbeddedPure()
"assertEquals('X X Test', $x);");
}

@Test
public void testPMCD() throws JsonProcessingException
{
Class test = new Class();
test.name = "a";
test._package = "test::class";

PureModelContextData pmcd = PureModelContextData.newBuilder().withElement(test).build();
ObjectMapper objectMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports();
String pmcdJson = objectMapper.writeValueAsString(pmcd);

test("let x = meta::legend::compilePMCD('" + pmcdJson + "');\n" +
"let p = $x->at(0)->cast(@Class<Any>).name;\n " +
"assertEquals('a', $p);"
);
}



private void test(String code)
Expand Down

0 comments on commit dea7680

Please sign in to comment.