Skip to content

Commit

Permalink
Better support for Function lookups on PureModel (#3233)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey authored Nov 6, 2024
1 parent b63158e commit de78a32
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ private PackageableElement functionFirstPass(Function function, CompileContext c

private void functionThirdPass(Function function, CompileContext context)
{
String packageString = context.pureModel.buildPackageString(function._package, HelperModelBuilder.getSignature(function));
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition<?> targetFunc = context.pureModel.getConcreteFunctionDefinition(packageString, function.sourceInformation);
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition<?> targetFunc = context.pureModel.getConcreteFunctionDefinition(function);
String packageString = context.pureModel.buildPackageString(function._package, targetFunc._name());
ProcessingContext ctx = new ProcessingContext("Function '" + packageString + "' Third Pass");
MutableList<ValueSpecification> body;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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.PureModelContextData;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Function;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Profile;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.Section;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex;
Expand Down Expand Up @@ -667,7 +668,14 @@ private <T> T lookupAndCastPackageableElement(String fullPath, java.lang.Class<T

public org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement getPackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement element)
{
return this.getPackageableElement(element.getPath(), element.sourceInformation);
if (element instanceof Function)
{
return this.getConcreteFunctionDefinition((Function) element);
}
else
{
return this.getPackageableElement(element.getPath(), element.sourceInformation);
}
}

public org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement getPackageableElement(String fullPath)
Expand Down Expand Up @@ -998,6 +1006,11 @@ public org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.Profi
return profile;
}

public org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition<?> getConcreteFunctionDefinition(Function function)
{
String packageString = this.buildPackageString(function._package, HelperModelBuilder.getSignature(function));
return this.getConcreteFunctionDefinition(packageString, function.sourceInformation);
}

public org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition<?> getConcreteFunctionDefinition(String fullPath, SourceInformation sourceInformation)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public void validateFunction(Function func, CompileContext compileContext)
if (func.tests != null && !func.tests.isEmpty())
{
PureModel pureModel = compileContext.pureModel;
String packageString = pureModel.buildPackageString(func._package, HelperModelBuilder.getSignature(func));
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition<?> targetFunc = pureModel.getConcreteFunctionDefinition(packageString, func.sourceInformation);
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition<?> targetFunc = pureModel.getConcreteFunctionDefinition(func);
for (org.finos.legend.pure.m3.coreinstance.meta.pure.test.Test test: targetFunc._tests())
{
Root_meta_legend_function_metamodel_FunctionTestSuite metamodelSuite = (Root_meta_legend_function_metamodel_FunctionTestSuite) test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,7 @@ public static Pair<PureModelContextData, PureModel> test(String str, String expe
modelData = objectMapper.readValue(json, PureModelContextData.class);
}
PureModel pureModel = Compiler.compile(modelData, DeploymentMode.TEST, Identity.getAnonymousIdentity().getName());
modelData.getElements().parallelStream().forEach(element ->
{
String fullPath;
if (element instanceof Function)
{
Function function = (Function) element;
String functionSignature = HelperModelBuilder.getSignature(function);
fullPath = pureModel.buildPackageString(function._package, functionSignature);
}
else
{
fullPath = element.getPath();
}
pureModel.getPackageableElement(fullPath, element.sourceInformation);
});
modelData.getElements().parallelStream().forEach(pureModel::getPackageableElement);
if (expectedErrorMsg != null)
{
Assert.fail("Expected compilation error with message: " + expectedErrorMsg + "; but no error occurred");
Expand Down Expand Up @@ -157,21 +143,7 @@ public static Pair<PureModelContextData, PureModel> partialCompilationTest(Strin
}
PureModelProcessParameter pureModelProcessParameter = PureModelProcessParameter.newBuilder().withEnablePartialCompilation(true).build();
PureModel pureModel = Compiler.compile(modelData, DeploymentMode.TEST, Identity.getAnonymousIdentity().getName(), null, pureModelProcessParameter);
modelData.getElements().parallelStream().forEach(element ->
{
String fullPath;
if (element instanceof Function)
{
Function function = (Function) element;
String functionSignature = HelperModelBuilder.getSignature(function);
fullPath = pureModel.buildPackageString(function._package, functionSignature);
}
else
{
fullPath = element.getPath();
}
pureModel.getPackageableElement(fullPath, element.sourceInformation);
});
modelData.getElements().parallelStream().forEach(pureModel::getPackageableElement);

Set<String> engineExceptions = pureModel.getEngineExceptions().stream().map(EngineException::toPretty).collect(Collectors.toSet());
if (expectedEngineExceptions != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,7 @@ public void testWithJson(String pureModelContextDataJsonStr, String expectedErro
{
PureModelContextData pureModelContextData = objectMapper.readValue(pureModelContextDataJsonStr, PureModelContextData.class);
PureModel pureModel = Compiler.compile(pureModelContextData, null, Identity.getAnonymousIdentity().getName());
pureModelContextData.getElements().parallelStream().forEach(element ->
{
String fullPath;
if (element instanceof Function)
{
Function function = (Function) element;
String functionSignature = HelperModelBuilder.getSignature(function);
fullPath = pureModel.buildPackageString(function._package, functionSignature);
}
else
{
fullPath = element.getPath();
}
pureModel.getPackageableElement(fullPath, element.sourceInformation);
});
pureModelContextData.getElements().parallelStream().forEach(pureModel::getPackageableElement);
if (expectedErrorMsg != null)
{
Assert.fail("Expected compilation error with message: " + expectedErrorMsg + "; but no error occurred");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.impl.utility.ListIterate;
import org.finos.legend.engine.language.pure.compiler.toPureGraph.HelperModelBuilder;
import org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel;
import org.finos.legend.engine.language.pure.dsl.generation.extension.Artifact;
import org.finos.legend.engine.language.pure.dsl.generation.extension.ArtifactGenerationExtension;
import org.finos.legend.engine.language.pure.dsl.generation.extension.ArtifactGenerationExtensionLoader;
import org.finos.legend.engine.protocol.pure.PureClientVersions;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -84,7 +82,7 @@ public MutableMap<ArtifactGenerationExtension, List<ArtifactGenerationResult>> g
MutableMap<ArtifactGenerationExtension, List<ArtifactGenerationResult>> results = Maps.mutable.empty();
for (PackageableElement element : this.elements)
{
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement packageableElement = this.findPackageableElement(pureModel, element);
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement packageableElement = pureModel.getPackageableElement(element);
if (packageableElement != null)
{
for (ArtifactGenerationExtension extension : this.extensions)
Expand All @@ -102,26 +100,6 @@ public MutableMap<ArtifactGenerationExtension, List<ArtifactGenerationResult>> g
return results;
}

private org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement findPackageableElement(PureModel pureModel, PackageableElement packageableElement)
{
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement element = pureModel.getPackageableElement_safe(packageableElement.getPath());
if (element != null)
{
return element;
}
if (packageableElement instanceof Function)
{
Function elementFunc = (Function) packageableElement;
String fullPath = pureModel.buildPackageString(packageableElement._package, HelperModelBuilder.getSignature(elementFunc));
element = pureModel.getPackageableElement_safe(fullPath);
}
if (element == null)
{
LOGGER.debug("Unable to find element '{}' in Pure Model", packageableElement.getPath());
}
return element;
}

private List<Artifact> generateArtifacts(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement packageableElement, PackageableElement element, ArtifactGenerationExtension extension)
{
try
Expand Down

0 comments on commit de78a32

Please sign in to comment.