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

Default value fixes in M3 compiler and reactivator #827

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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 @@ -55,6 +55,10 @@ public void run(Association association, MatcherState state, Matcher matcher, Mo
{
throw new PureCompilationException(property.getSourceInformation(), "Association properties must have concrete types");
}
if (property._defaultValue() != null)
{
throw new PureCompilationException(property.getSourceInformation(), "Association properties must not have default values defined");
}
Validator.validate(propertyRawType, validatorState, matcher, processorSupport);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package org.finos.legend.pure.m3.tests.elements.property;

import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiledPlatform;
import org.finos.legend.pure.m4.exception.PureCompilationException;
import org.junit.Assert;
import org.junit.Test;

public abstract class AbstractTestDefaultValue extends AbstractPureTestWithCoreCompiledPlatform
Expand Down Expand Up @@ -126,5 +128,18 @@ public void testDefaultValueWithQualifiedProperty()
+ "}\n"
);
execute("test():Boolean[1]");
}
}

@Test
public void testDefaultValueAssociationFailsCompile()
{
PureCompilationException e = Assert.assertThrows(PureCompilationException.class, () -> compileTestSource(
"defaultValueSource.pure",
"Class test::A { i:Integer[1]; }\n" +
"Class test::B { j:Integer[1]; }\n" +
"Association test::AB { a : test::A[1]; b : test::B[1] = ^test::B(j=2);}"
));

assertPureException(PureCompilationException.class, "Association properties must not have default values defined", "defaultValueSource.pure", 3, 40, 3, 70, e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package org.finos.legend.pure.m3.tests.function.base.meta;

import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue;
import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiled;
import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiledPlatform;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

public abstract class AbstractTestReactivate extends AbstractPureTestWithCoreCompiled
public abstract class AbstractTestReactivate extends AbstractPureTestWithCoreCompiledPlatform
{
@After
public void cleanRuntime()
Expand Down Expand Up @@ -186,4 +186,82 @@ public void testReactivateFunctionExpressionWithPackageArg()
"}\n");
Assert.assertEquals(runtime.getCoreInstance("test::pkg1"), ((InstanceValue) execute("test::pkg1::test():Any[1]"))._values().getOnly());
}

@Test
public void testReactivateNewDefaultValues()
{
compileTestSource("testSource.pure",
"Class test::A\n" +
"{\n" +
" a:Boolean[1] = true;\n" +
" i:Integer[1] = 10;\n" +
" enumProperty:test::EnumWithDefault[1] = test::EnumWithDefault.DefaultValue;\n" +
"}\n" +
"\n" +
"Enum test::EnumWithDefault\n" +
"{\n" +
" DefaultValue,\n" +
" AnotherValue\n" +
"}\n" +
"\n" +
"function test::f():Boolean[1]\n" +
"{\n" +
" let l = {| ^test::A()};\n" +
" let a = $l.expressionSequence->evaluateAndDeactivate()->toOne()->reactivate()->cast(@test::A)->toOne();\n" +
" assert($a.a, | 'Default value for property a set to wrong value');\n" +
" assertEquals($a.i, 10);\n" +
" assertEquals($a.enumProperty, test::EnumWithDefault.DefaultValue);\n" +
"}\n");
execute("test::f():Boolean[1]");
}

@Test
public void testReactivateNewOverrideDefaultValues()
{
compileTestSource("testSourceDefaultOverride.pure",
"Class test::A1\n" +
"{\n" +
" a:Boolean[1] = true;\n" +
" i:Integer[1] = 10;\n" +
" enumProperty:test::EnumWithDefault1[1] = test::EnumWithDefault1.DefaultValue;\n" +
"}\n" +
"\n" +
"Enum test::EnumWithDefault1\n" +
"{\n" +
" DefaultValue,\n" +
" AnotherValue\n" +
"}\n" +
"\n" +
"function test::f1():Boolean[1]\n" +
"{\n" +
" let l = {| ^test::A1(a=false)};\n" +
" let a = $l.expressionSequence->evaluateAndDeactivate()->toOne()->reactivate()->cast(@test::A1)->toOne();\n" +
" assert(!$a.a, | 'Default value for property a set to wrong value');\n" +
" assertEquals($a.i, 10);\n" +
" assertEquals($a.enumProperty, test::EnumWithDefault1.DefaultValue);\n" +
"}\n");
execute("test::f1():Boolean[1]");
}

@Test
public void testReactivateNewDefaultValueInheritance()
{
compileTestSource("testSource.pure",
"Class test::A\n" +
"{\n" +
" a:Boolean[1] = true;\n" +
"}\n" +
"\n" +
"Class test::B extends test::A\n" +
"{\n" +
" b:Boolean[1] = true;\n" +
"}\n" +
"function test::f():Boolean[1]\n" +
"{\n" +
" let l = {| ^test::B()};\n" +
" let b = $l.expressionSequence->evaluateAndDeactivate()->toOne()->reactivate()->cast(@test::B)->toOne();\n" +
" assert($b.a, | 'Default value for property b set to wrong value');\n" +
"}\n");
execute("test::f():Boolean[1]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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.DefaultValue;
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.multiplicity.Multiplicity;
Expand All @@ -55,6 +56,7 @@
import org.finos.legend.pure.m3.navigation.Instance;
import org.finos.legend.pure.m3.navigation.M3Paths;
import org.finos.legend.pure.m3.navigation.ProcessorSupport;
import org.finos.legend.pure.m3.navigation._class._Class;
import org.finos.legend.pure.m3.navigation.generictype.GenericType;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;
import org.finos.legend.pure.m4.coreinstance.SourceInformation;
Expand Down Expand Up @@ -721,13 +723,17 @@ public static Object newObject(Bridge bridge, org.finos.legend.pure.m3.coreinsta
{
Class<?> c = ((CompiledExecutionSupport) es).getClassLoader().loadClass(JavaPackageAndImportBuilder.platformJavaPackage() + "." + Pure.elementToPath(aClass, "_", true) + "_Impl");
Any result = (Any) c.getConstructor(String.class).newInstance(name);
MutableList<String> userAssignedParameters = Lists.mutable.empty();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
MutableList<String> userAssignedParameters = Lists.mutable.empty();
MutableSet<String> userAssignedParameters = Sets.mutable.empty();


root_meta_pure_functions_lang_keyExpressions.forEach(new CheckedProcedure<KeyExpression>()
{
@Override
public void safeValue(KeyExpression o) throws Exception
{
Object res = reactivate(o._expression(), new PureMap(Maps.fixedSize.empty()), bridge, es);
Method m = c.getMethod("_" + o._key()._values().getFirst(), RichIterable.class);
String key = (String) o._key()._values().getFirst();
userAssignedParameters.add(key);
Method m = c.getMethod("_" + key, RichIterable.class);
if (res instanceof RichIterable)
{
m.invoke(result, res);
Expand All @@ -736,9 +742,38 @@ public void safeValue(KeyExpression o) throws Exception
{
m.invoke(result, Lists.fixedSize.of(res));
}

}
});

RichIterable<CoreInstance> classProperties = _Class.getSimpleProperties(aClass, ((CompiledExecutionSupport) es).getProcessorSupport());
classProperties.forEach(new CheckedProcedure<CoreInstance>()
{
@Override
public void safeValue(CoreInstance p) throws Exception
{
if (!userAssignedParameters.contains(p.getName()))
{
Property<?, ?> prop = (Property<?, ?>) p;
DefaultValue defaultValue = prop._defaultValue();
if (defaultValue != null)
{
Object res = reactivate(defaultValue._functionDefinition()._expressionSequence().getFirst(), new PureMap(Maps.fixedSize.empty()), bridge, es);
Method method = c.getMethod("_" + prop._name(), RichIterable.class);
if (res instanceof RichIterable)
{
method.invoke(result, res);
}
else
{
method.invoke(result, Lists.fixedSize.of(res));
}
}
}
Comment on lines +749 to +772
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have methods now on AbstractCompiledCoreInstance for dealing with default values. Maybe result above can be cast directly to AbstractCompiledCoreInstance? In case it needs to be cast to Any above, I'm casting it in the suggestion below.

Suggested change
RichIterable<CoreInstance> classProperties = _Class.getSimpleProperties(aClass, ((CompiledExecutionSupport) es).getProcessorSupport());
classProperties.forEach(new CheckedProcedure<CoreInstance>()
{
@Override
public void safeValue(CoreInstance p) throws Exception
{
if (!userAssignedParameters.contains(p.getName()))
{
Property<?, ?> prop = (Property<?, ?>) p;
DefaultValue defaultValue = prop._defaultValue();
if (defaultValue != null)
{
Object res = reactivate(defaultValue._functionDefinition()._expressionSequence().getFirst(), new PureMap(Maps.fixedSize.empty()), bridge, es);
Method method = c.getMethod("_" + prop._name(), RichIterable.class);
if (res instanceof RichIterable)
{
method.invoke(result, res);
}
else
{
method.invoke(result, Lists.fixedSize.of(res));
}
}
}
AbstractCompiledCoreInstance instance = (AbstractCompiledCoreInstance) result;
instance.getDefaultValueKeys().forEach(new CheckedProcedure<String>()
{
@Override
public void safeValue(String p) throws Exception
{
if (!userAssignedParameters.contains(p))
{
RichIterable<?> defaultValues = instance.getDefaultValue(p, es);
if ((defaultValues != null) && defaultValues.notEmpty())
{
Method method = c.getMethod("_" + prop._name(), RichIterable.class);
method.invoke(result, defaultValues);
}
}

}
});


return result;
}
catch (RuntimeException e)
Expand Down
Loading