From fbac8379eba3877e7623ab8dd775254b584d5e5a Mon Sep 17 00:00:00 2001 From: Kevin Knight <57677197+kevin-m-knight-gs@users.noreply.github.com> Date: Fri, 12 Jan 2024 09:43:51 -0500 Subject: [PATCH] Improvements to PureModelBuilder (#745) --- legend-sdlc-language-pure-compiler/pom.xml | 33 ++++----- .../toPureGraph/PureModelBuilder.java | 72 ++++++++++++++++--- .../junit/LegendSDLCTestSuiteBuilder.java | 2 +- .../test/junit/pure/v1/PureTestHelper.java | 3 +- 4 files changed, 77 insertions(+), 33 deletions(-) diff --git a/legend-sdlc-language-pure-compiler/pom.xml b/legend-sdlc-language-pure-compiler/pom.xml index 6a7a509ffd..33f808bbe2 100644 --- a/legend-sdlc-language-pure-compiler/pom.xml +++ b/legend-sdlc-language-pure-compiler/pom.xml @@ -39,6 +39,13 @@ + + + org.finos.legend.pure + legend-pure-m3-core + + + org.finos.legend.engine @@ -48,6 +55,10 @@ org.finos.legend.engine legend-engine-protocol-pure + + org.finos.legend.engine + legend-engine-pure-code-compiled-core + org.finos.legend.engine legend-engine-language-pure-compiler @@ -88,26 +99,6 @@ test-jar test - - org.finos.legend.pure - legend-pure-m3-core - test - - - org.finos.legend.engine - legend-engine-pure-code-compiled-core - test - - - org.finos.legend.pure - legend-pure-runtime-java-engine-compiled - test - - - org.finos.legend.engine - legend-engine-language-pure-grammar - test - - \ No newline at end of file + diff --git a/legend-sdlc-language-pure-compiler/src/main/java/org/finos/legend/sdlc/language/pure/compiler/toPureGraph/PureModelBuilder.java b/legend-sdlc-language-pure-compiler/src/main/java/org/finos/legend/sdlc/language/pure/compiler/toPureGraph/PureModelBuilder.java index 838a8d1fe9..e6751db361 100644 --- a/legend-sdlc-language-pure-compiler/src/main/java/org/finos/legend/sdlc/language/pure/compiler/toPureGraph/PureModelBuilder.java +++ b/legend-sdlc-language-pure-compiler/src/main/java/org/finos/legend/sdlc/language/pure/compiler/toPureGraph/PureModelBuilder.java @@ -15,6 +15,9 @@ package org.finos.legend.sdlc.language.pure.compiler.toPureGraph; import org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel; +import org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModelProcessParameter; +import org.finos.legend.engine.language.pure.compiler.toPureGraph.extension.CompilerExtension; +import org.finos.legend.engine.language.pure.compiler.toPureGraph.extension.CompilerExtensions; import org.finos.legend.engine.protocol.Protocol; import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData; import org.finos.legend.engine.protocol.pure.v1.model.context.SDLC; @@ -23,11 +26,15 @@ import org.finos.legend.sdlc.domain.model.entity.Entity; import org.finos.legend.sdlc.protocol.pure.v1.PureModelContextDataBuilder; +import java.util.ServiceLoader; import java.util.stream.Stream; public class PureModelBuilder { private final PureModelContextDataBuilder contextDataBuilder = PureModelContextDataBuilder.newBuilder(); + private ClassLoader classLoader; + private CompilerExtensions extensions; + private String packagePrefix; private PureModelBuilder() { @@ -149,36 +156,81 @@ public PureModelBuilder withProtocol(Protocol protocol) return this; } - public PureModelWithContextData build() + public void setClassLoader(ClassLoader classLoader) { - return build(null); + this.classLoader = classLoader; } - public PureModelWithContextData build(ClassLoader classLoader) + public PureModelBuilder withClassLoader(ClassLoader classLoader) + { + setClassLoader(classLoader); + return this; + } + + public void setCompilerExtensions(CompilerExtensions extensions) + { + this.extensions = extensions; + } + + public PureModelBuilder withCompilerExtensions(CompilerExtensions extensions) + { + setCompilerExtensions(extensions); + return this; + } + + public void setPackagePrefix(String packagePrefix) + { + this.packagePrefix = packagePrefix; + } + + public PureModelBuilder withPackagePrefix(String packagePrefix) + { + setPackagePrefix(packagePrefix); + return this; + } + + public PureModelWithContextData build() { PureModelContextData pureModelContextData = this.contextDataBuilder.build(); - PureModel pureModel = buildPureModel(pureModelContextData, classLoader); + PureModel pureModel = buildPureModel(pureModelContextData); return new PureModelWithContextData(pureModel, pureModelContextData); } + @Deprecated + public PureModelWithContextData build(ClassLoader classLoader) + { + return withClassLoader(classLoader).build(); + } + public PureModel buildPureModel() { - return buildPureModel(null); + return buildPureModel(this.contextDataBuilder.build()); } + @Deprecated public PureModel buildPureModel(ClassLoader classLoader) { - return buildPureModel(this.contextDataBuilder.build(), classLoader); + return withClassLoader(classLoader).buildPureModel(); } - public static PureModelBuilder newBuilder() + private PureModel buildPureModel(PureModelContextData pureModelContextData) { - return new PureModelBuilder(); + return new PureModel(pureModelContextData, getExtensions(), null, this.classLoader, DeploymentMode.PROD, new PureModelProcessParameter(this.packagePrefix), null); + } + + private CompilerExtensions getExtensions() + { + if (this.extensions != null) + { + return this.extensions; + } + Iterable exts = (this.classLoader == null) ? ServiceLoader.load(CompilerExtension.class) : ServiceLoader.load(CompilerExtension.class, this.classLoader); + return CompilerExtensions.fromExtensions(exts); } - private static PureModel buildPureModel(PureModelContextData pureModelContextData, ClassLoader classLoader) + public static PureModelBuilder newBuilder() { - return new PureModel(pureModelContextData, null, classLoader, DeploymentMode.PROD); + return new PureModelBuilder(); } public static class PureModelWithContextData diff --git a/legend-sdlc-test-utils/src/main/java/org/finos/legend/sdlc/test/junit/LegendSDLCTestSuiteBuilder.java b/legend-sdlc-test-utils/src/main/java/org/finos/legend/sdlc/test/junit/LegendSDLCTestSuiteBuilder.java index 3b3ecb9850..ef1152852e 100644 --- a/legend-sdlc-test-utils/src/main/java/org/finos/legend/sdlc/test/junit/LegendSDLCTestSuiteBuilder.java +++ b/legend-sdlc-test-utils/src/main/java/org/finos/legend/sdlc/test/junit/LegendSDLCTestSuiteBuilder.java @@ -73,7 +73,7 @@ private LegendSDLCTestSuiteBuilder(String name, String pureVersion, ClassLoader this.pureVersion = pureVersion; this.testableClassifiers = TestableRunnerExtensionLoader.getClassifierPathToTestableRunnerMap(classLoader).keySet(); this.entities = getEntities(classLoader); - PureModelWithContextData pureModelWithContextData = PureModelBuilder.newBuilder().withEntitiesIfPossible(this.entities).build(classLoader); + PureModelWithContextData pureModelWithContextData = PureModelBuilder.newBuilder().withEntitiesIfPossible(this.entities).withClassLoader(classLoader).build(); this.pureModel = pureModelWithContextData.getPureModel(); this.pureModelContextData = pureModelWithContextData.getPureModelContextData(); this.protocolIndex = Iterate.groupByUniqueKey(this.pureModelContextData.getElements(), PackageableElement::getPath); diff --git a/legend-sdlc-test-utils/src/main/java/org/finos/legend/sdlc/test/junit/pure/v1/PureTestHelper.java b/legend-sdlc-test-utils/src/main/java/org/finos/legend/sdlc/test/junit/pure/v1/PureTestHelper.java index b736c14155..71b2ccaeb1 100644 --- a/legend-sdlc-test-utils/src/main/java/org/finos/legend/sdlc/test/junit/pure/v1/PureTestHelper.java +++ b/legend-sdlc-test-utils/src/main/java/org/finos/legend/sdlc/test/junit/pure/v1/PureTestHelper.java @@ -120,7 +120,8 @@ private static PureModelWithContextData buildPureModelWithContextData(ClassLoade { return PureModelBuilder.newBuilder() .withEntitiesIfPossible(entityLoader.getAllEntities()) - .build(classLoader); + .withClassLoader(classLoader) + .build(); } catch (Exception e) {