Skip to content

Commit

Permalink
Improvements to PureModelBuilder (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-m-knight-gs authored Jan 12, 2024
1 parent cf3e8e2 commit fbac837
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 33 deletions.
33 changes: 12 additions & 21 deletions legend-sdlc-language-pure-compiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
</dependency>
<!-- SDLC -->

<!-- PURE -->
<dependency>
<groupId>org.finos.legend.pure</groupId>
<artifactId>legend-pure-m3-core</artifactId>
</dependency>
<!-- PURE -->

<!-- ENGINE -->
<dependency>
<groupId>org.finos.legend.engine</groupId>
Expand All @@ -48,6 +55,10 @@
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-protocol-pure</artifactId>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-pure-code-compiled-core</artifactId>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-language-pure-compiler</artifactId>
Expand Down Expand Up @@ -88,26 +99,6 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.finos.legend.pure</groupId>
<artifactId>legend-pure-m3-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-pure-code-compiled-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.finos.legend.pure</groupId>
<artifactId>legend-pure-runtime-java-engine-compiled</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-language-pure-grammar</artifactId>
<scope>test</scope>
</dependency>
<!-- TEST -->
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
Expand Down Expand Up @@ -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<? extends CompilerExtension> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ private static PureModelWithContextData buildPureModelWithContextData(ClassLoade
{
return PureModelBuilder.newBuilder()
.withEntitiesIfPossible(entityLoader.getAllEntities())
.build(classLoader);
.withClassLoader(classLoader)
.build();
}
catch (Exception e)
{
Expand Down

0 comments on commit fbac837

Please sign in to comment.