use of org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData in project legend-sdlc by finos.
the class TestFileGenerationFactory method testFileGenerationFactory.
@Test
public void testFileGenerationFactory() {
PureModelContextData pureModelContextData = getPureModelContextDataFromPath("FileGenerationFactoryTestData.json");
GenerationSpecification generationSpecification = pureModelContextData.getElementsOfType(GenerationSpecification.class).get(0);
FileGenerationFactory factory = FileGenerationFactory.newFactory(generationSpecification, pureModelContextData);
MutableMap<FileGenerationSpecification, List<GenerationOutput>> result = factory.generateFiles();
MapIterable<String, FileGenerationSpecification> specifications = LazyIterate.selectInstancesOf(pureModelContextData.getElements(), FileGenerationSpecification.class).groupByUniqueKey(PackageableElement::getPath);
// avro
List<GenerationOutput> avroResult = result.get(specifications.get("generation::MyAvro"));
testAvroOutput(avroResult);
// protobuf
List<GenerationOutput> protoBufResult = result.get(specifications.get("generation::MyProtobuf"));
Assert.assertEquals(1, protoBufResult.size());
MapIterable<String, GenerationOutput> protobufOutputs = LazyIterate.adapt(protoBufResult).groupByUniqueKey(GenerationOutput::getFileName);
Assert.assertEquals("syntax = \"proto3\";\n" + "package model;\n" + "\n" + "message Firm {\n" + " Person employees = 1;\n" + "}\n" + "\n" + "message Person {\n" + " string first_name = 1;\n" + " string last_name = 2;\n" + "}", protobufOutputs.get("model.proto").getContent());
// rosetta
List<GenerationOutput> rosettaResult = result.get(specifications.get("generation::MyRosetta"));
Assert.assertEquals(1, rosettaResult.size());
Assert.assertEquals("rosettaTypes.txt", rosettaResult.get(0).getFileName());
}
use of org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData in project legend-sdlc by finos.
the class ModelGenerationFactory method generate.
public PureModelContextData generate() throws Exception {
if ((this.generationSpecification._package == null) || this.generationSpecification._package.isEmpty()) {
throw new RuntimeException("Invalid generation specifications, missing path '" + this.generationSpecification.name);
}
LOGGER.info("Generation generation specification '" + generationSpecification.getPath() + "'");
List<GenerationTreeNode> nodes = this.generationSpecification.generationNodes;
for (GenerationTreeNode node : nodes) {
LOGGER.info("Start generating generation model element '" + node.generationElement + "'");
List<Function3<String, SourceInformation, CompileContext, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement>> extraModelGenerationSpecificationResolvers = ListIterate.flatCollect(HelperGenerationSpecificationBuilder.getGenerationCompilerExtensions(this.pureModel.getContext()), GenerationCompilerExtension::getExtraModelGenerationSpecificationResolvers);
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement generationElement = extraModelGenerationSpecificationResolvers.stream().map(resolver -> resolver.value(node.generationElement, node.sourceInformation, this.pureModel.getContext())).filter(Objects::nonNull).findFirst().orElseThrow(() -> new EngineException("Can't find generation element '" + node.generationElement + "'", node.sourceInformation, EngineErrorType.COMPILATION));
ModelGenerator modelGenerator = ModelGenerator.newGenerator(generationElement, this.pureModel);
processModelGenerator(modelGenerator);
}
return validateAndBuildGeneratedModel();
}
use of org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData in project legend-sdlc by finos.
the class ModelGenerationFactory method validateGeneratedElements.
private void validateGeneratedElements(PureModelContextData generatedPureModelContextData) {
generatedPureModelContextData.getElements().forEach(e -> {
String path = e.getPath();
PackageableElement coreElement = this.coreModelElementIndex.get(path);
if (coreElement != null) {
throw new RuntimeException("Generated element '" + path + "' of type " + e.getClass().getSimpleName() + " can't override existing element of type " + coreElement.getClass().getSimpleName());
}
});
}
use of org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData in project legend-sdlc by finos.
the class TestModelGenerationFactory method testSimpleModelGenerationFactory.
@Test
public void testSimpleModelGenerationFactory() {
GenerationSpecification generationSpecification = this.getTestGenerationSpecification();
PureModelContextData.Builder builder = PureModelContextData.newBuilder();
builder.addElement(generationSpecification);
ModelGenerationFactory factory = ModelGenerationFactory.newFactory(generationSpecification, builder.build());
factory.processModelGenerator(simpleClassGenerator);
Assert.assertEquals(2, factory.getFullModel().getElements().size());
Assert.assertEquals(1, factory.getGeneratedModel().getElements().size());
PureModelContextData generatedModel = factory.validateAndBuildGeneratedModel();
Assert.assertEquals(1, generatedModel.getElements().size());
Class myClass = (Class) generatedModel.getElements().get(0);
Assert.assertEquals("MyClass", myClass.name);
}
use of org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData in project legend-sdlc by finos.
the class PureProtocolHelper method serializeForComparison.
public static String serializeForComparison(PackageableElement element) {
PureModelContextData pureModelContextData = PureModelContextData.newBuilder().withElement(element).build();
PureGrammarComposerContext composerContext = PureGrammarComposerContext.Builder.newInstance().withRenderStyle(RenderStyle.PRETTY).build();
String result = PureGrammarComposer.newInstance(composerContext).renderPureModelContextData(pureModelContextData);
if (result.startsWith("###")) {
result = result.substring(result.indexOf('\n') + 1);
}
return result;
}
Aggregations