Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uplift and fix canReactivateDynamically native function #646

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,114 @@ public void testEval()
this.execute("test():Boolean[1]");
}

@Test
public void testNonReactivatableFunction()
{
compileTestSource("fromString.pure",
"function test():Boolean[1]\n" +
"{\n" +
" assert(false == canReactivateDynamically({s:String[1]|$s}->evaluateAndDeactivate().expressionSequence->toOne()), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");
}

@Test
public void testNonReactivatableNestedFunction()
{
compileTestSource("fromString.pure",
"function test():Boolean[1]\n" +
"{\n" +
" assert(false == canReactivateDynamically({s:String[1]|$s + 'hi'}->evaluateAndDeactivate().expressionSequence->toOne()), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");

runtime.delete("fromString.pure");
compileTestSource("fromString.pure",
"function test():Boolean[1]\n" +
"{\n" +
" assert(false == canReactivateDynamically({s:Any[1]|instanceOf($s, String) && true}->evaluateAndDeactivate().expressionSequence->toOne()), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");

runtime.delete("fromString.pure");
compileTestSource("fromString.pure",
"Class A\n" +
"{\n" +
" prop: B[1];\n" +
"}\n" +
"Class B\n" +
"{\n" +
" prop: String[1];\n" +
"}\n" +
"function test():Boolean[1]\n" +
"{\n" +
" assert(false == canReactivateDynamically({s:String[1]|^A(prop = ^B(prop = $s))}->evaluateAndDeactivate().expressionSequence->toOne()), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");
}

@Test
public void testNonReactivatableNestedFunctionWithSomeVariablesMissing()
{
compileTestSource("fromString.pure",
"function test():Boolean[1]\n" +
"{\n" +
" assert(false == canReactivateDynamically({s:String[1], y:String[1]|$s + $y}->evaluateAndDeactivate().expressionSequence->toOne(), newMap(^Pair<String, List<Any>>(first = 's', second = ^List<Any>(values = 'dummy')))), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");
}

@Test
public void testSimpleFuncExpressionReactivationInScopeOfParams()
{
compileTestSource("fromString.pure",
"function test():Boolean[1]\n" +
"{\n" +
" assert(canReactivateDynamically({s:String[1]|$s}->evaluateAndDeactivate().expressionSequence->toOne(), newMap(^Pair<String, List<Any>>(first = 's', second = ^List<Any>(values = 'dummy')))), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");
}

@Test
public void testNestedFuncExpressionReactivationInScopeOfParams()
{
compileTestSource("fromString.pure",
"function test():Boolean[1]\n" +
"{\n" +
" assert(canReactivateDynamically({s:String[1]|$s + 'hi'}->evaluateAndDeactivate().expressionSequence->toOne(), newMap(^Pair<String, List<Any>>(first = 's', second = ^List<Any>(values = 'dummy')))), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");

runtime.delete("fromString.pure");
compileTestSource("fromString.pure",
"function test():Boolean[1]\n" +
"{\n" +
" assert(canReactivateDynamically({s:String[1], y:String[1]|$s + $y}->evaluateAndDeactivate().expressionSequence->toOne(), newMap([^Pair<String, List<Any>>(first = 's', second = ^List<Any>(values = 'dummy')), ^Pair<String, List<Any>>(first = 'y', second = ^List<Any>(values = 'dummy'))])), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");

runtime.delete("fromString.pure");
compileTestSource("fromString.pure",
"function test():Boolean[1]\n" +
"{\n" +
" assert(canReactivateDynamically({s:Any[1]|instanceOf($s, String) && true}->evaluateAndDeactivate().expressionSequence->toOne(), newMap(^Pair<String, List<Any>>(first = 's', second = ^List<Any>(values = 'dummy')))), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");

runtime.delete("fromString.pure");
compileTestSource("fromString.pure",
"Class A\n" +
"{\n" +
" prop: B[1];" +
"}\n" +
"Class B\n" +
"{\n" +
" prop: String[1];" +
"}\n" +
"function test():Boolean[1]\n" +
"{\n" +
" assert(canReactivateDynamically({s:String[1]|^A(prop = ^B(prop = $s))}->evaluateAndDeactivate().expressionSequence->toOne(), newMap(^Pair<String, List<Any>>(first = 's', second = ^List<Any>(values = 'dummy')))), |'');\n" +
"}\n");
this.execute("test():Boolean[1]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ native function meta::pure::functions::meta::deactivate(var:Any[*]):ValueSpecifi

native function meta::pure::functions::meta::reactivate(vs:ValueSpecification[1], vars:Map<String, List<Any>>[1]):Any[*];

native function meta::pure::functions::meta::canReactivateDynamically(vs:ValueSpecification[1]):Boolean[1];
function meta::pure::functions::meta::canReactivateDynamically(vs:ValueSpecification[1]):Boolean[1]
{
canReactivateDynamically($vs, ^Map<String, List<Any>>())
}

native function meta::pure::functions::meta::canReactivateDynamically(vs:ValueSpecification[1], vars:Map<String, List<Any>>[1]):Boolean[1];

native function meta::pure::functions::meta::evaluateAndDeactivate<T|m>(var:T[m]):T[m];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification;
import org.finos.legend.pure.m3.execution.ExecutionSupport;
import org.finos.legend.pure.runtime.java.compiled.generation.processors.natives.AbstractNativeFunctionGeneric;
import org.finos.legend.pure.runtime.java.compiled.generation.processors.support.map.PureMap;

public class CanReactivateDynamically extends AbstractNativeFunctionGeneric
{
public CanReactivateDynamically()
{
super("CoreGen.canReactivateWithoutJavaCompilation",new Class[]{ValueSpecification.class, ExecutionSupport.class},
false, true, false, "canReactivateDynamically_ValueSpecification_1__Boolean_1_");
super("CoreGen.canReactivateWithoutJavaCompilation",new Class[]{ValueSpecification.class, PureMap.class, ExecutionSupport.class},
false, true, false, "canReactivateDynamically_ValueSpecification_1__Map_1__Boolean_1_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public CoreExtensionCompiled()
" return Pure.canReactivateWithoutJavaCompilation(valueSpecification, es, new PureMap(UnifiedMap.newMap()), bridge);\n" +
" }\n" +
"\n" +
" public static boolean canReactivateWithoutJavaCompilation(ValueSpecification valueSpecification, ExecutionSupport es, PureMap lambdaOpenVariablesMap)\n" +
" public static boolean canReactivateWithoutJavaCompilation(ValueSpecification valueSpecification, PureMap lambdaOpenVariablesMap, ExecutionSupport es)\n" +
" {\n" +
" return Pure.canReactivateWithoutJavaCompilation(valueSpecification, es, lambdaOpenVariablesMap, bridge);\n" +
" }\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
import org.eclipse.collections.api.set.MutableSet;
import org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List;
import org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.*;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue;
Expand Down Expand Up @@ -87,6 +91,11 @@ private static boolean canReactivateWithoutJavaCompilationImpl(ValueSpecificatio
{
return canReactivateWithoutJavaCompilationImpl((Function<?>) o, es, false, lambdaOpenVariablesMap, bridge);
}
if (o instanceof KeyExpression)
{
return canReactivateWithoutJavaCompilationImpl(((KeyExpression) o)._key(), es, false, lambdaOpenVariablesMap, bridge) &&
canReactivateWithoutJavaCompilationImpl(((KeyExpression) o)._expression(), es, false, lambdaOpenVariablesMap, bridge);
}
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void init(PureRuntime runtime, Message message)
this.nativeFunctions.put("subTypeOf_Type_1__Type_1__Boolean_1_", new SubTypeOf(repository));
this.nativeFunctions.put("deactivate_Any_MANY__ValueSpecification_1_", new Deactivate());
this.nativeFunctions.put("reactivate_ValueSpecification_1__Map_1__Any_MANY_", new Reactivate(this));
this.nativeFunctions.put("canReactivateDynamically_ValueSpecification_1__Boolean_1_", new CanReactivateDynamically(repository));
this.nativeFunctions.put("canReactivateDynamically_ValueSpecification_1__Map_1__Boolean_1_", new CanReactivateDynamically(this));
this.nativeFunctions.put("evaluateAndDeactivate_T_m__T_m_", new EvaluateAndDeactivate());
this.nativeFunctions.put("toString_Any_1__String_1_", new ToString(repository, this));
this.nativeFunctions.put("mutateAdd_T_1__String_1__Any_MANY__T_1_", new MutateAdd());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.finos.legend.pure.m3.exception.PureExecutionException;
import org.finos.legend.pure.m3.compiler.Context;
import org.finos.legend.pure.m3.navigation.ValueSpecificationBootstrap;
import org.finos.legend.pure.m3.exception.PureExecutionException;
import org.finos.legend.pure.m3.navigation.ProcessorSupport;
import org.finos.legend.pure.m3.navigation.ValueSpecificationBootstrap;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;
import org.finos.legend.pure.m4.ModelRepository;
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.core.InstantiationContext;
import org.finos.legend.pure.runtime.java.interpreted.natives.core.NativeFunction;
Expand All @@ -32,17 +32,24 @@

public class CanReactivateDynamically extends NativeFunction
{
private final ModelRepository repository;
private final FunctionExecutionInterpreted functionExecution;

public CanReactivateDynamically(ModelRepository repository)
public CanReactivateDynamically(FunctionExecutionInterpreted functionExecution)
{
this.repository = repository;
this.functionExecution = functionExecution;
}

@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, VariableContext variableContext, CoreInstance functionExpressionToUseInStack, Profiler profiler, InstantiationContext instantiationContext, ExecutionSupport executionSupport, Context context, ProcessorSupport processorSupport) throws PureExecutionException
{
// return repository.newBooleanCoreInstance(true);
return ValueSpecificationBootstrap.newBooleanLiteral(repository, true, processorSupport);
try
{
ReactivateHelper.execute(this.functionExecution, params, resolvedTypeParameters, resolvedMultiplicityParameters, variableContext, functionExpressionToUseInStack, profiler, instantiationContext, executionSupport, context, processorSupport);
return ValueSpecificationBootstrap.newBooleanLiteral(this.functionExecution.getPureRuntime().getModelRepository(), true, processorSupport);
}
catch (PureExecutionException e)
{
return ValueSpecificationBootstrap.newBooleanLiteral(this.functionExecution.getPureRuntime().getModelRepository(), false, processorSupport);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,6 @@ public Reactivate(FunctionExecutionInterpreted functionExecution)
@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, VariableContext variableContext, CoreInstance functionExpressionToUseInStack, Profiler profiler, InstantiationContext instantiationContext, ExecutionSupport executionSupport, Context context, ProcessorSupport processorSupport) throws PureExecutionException
{
MapIterable<CoreInstance, CoreInstance> openVariablesMap = ((MapCoreInstance)Instance.getValueForMetaPropertyToManyResolved(params.get(1), M3Properties.values, processorSupport).getFirst()).getMap();
RichIterable<Pair<CoreInstance, CoreInstance>> openVariables = openVariablesMap.keyValuesView();
VariableContext newVarContext = VariableContext.newVariableContext();

for (Pair<CoreInstance, CoreInstance> pair: openVariables)
{
try
{
String name = pair.getOne().getName();
//todo: Should not need to do this, but there seem to be duplicates in the open variables
if (newVarContext.getValue(name) == null)
{
CoreInstance list = pair.getTwo();
CoreInstance inst = processorSupport.newEphemeralAnonymousCoreInstance(M3Paths.InstanceValue);
Instance.addValueToProperty(inst, M3Properties.genericType, list.getValueForMetaPropertyToOne(M3Properties.genericType) , processorSupport);
Instance.addValueToProperty(inst, M3Properties.multiplicity, list.getValueForMetaPropertyToOne(M3Properties.multiplicity), processorSupport);
Instance.addValueToProperty(inst, M3Properties.values, list.getValueForMetaPropertyToMany(M3Properties.values), processorSupport);
newVarContext.registerValue(name, inst);
}
}
catch (VariableContext.VariableNameConflictException e)
{
throw new PureExecutionException(e);
}
}

return this.functionExecution.executeValueSpecification(params.get(0).getValueForMetaPropertyToOne(M3Properties.values),
resolvedTypeParameters, resolvedMultiplicityParameters, functionExpressionToUseInStack, newVarContext, profiler, instantiationContext, executionSupport);
return ReactivateHelper.execute(this.functionExecution, params, resolvedTypeParameters, resolvedMultiplicityParameters, variableContext, functionExpressionToUseInStack, profiler, instantiationContext, executionSupport, context, processorSupport);
}
}
Loading