Search in sources :

Example 1 with PureModelContextData

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());
}
Also used : FileGenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.fileGeneration.FileGenerationSpecification) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) FileGenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.fileGeneration.FileGenerationSpecification) GenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.generationSpecification.GenerationSpecification) List(java.util.List) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Test(org.junit.Test)

Example 2 with PureModelContextData

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();
}
Also used : PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) Logger(org.slf4j.Logger) GenerationCompilerExtension(org.finos.legend.engine.language.pure.dsl.generation.compiler.toPureGraph.GenerationCompilerExtension) EngineErrorType(org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType) GenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.generationSpecification.GenerationSpecification) Iterate(org.eclipse.collections.impl.utility.Iterate) ListIterate(org.eclipse.collections.impl.utility.ListIterate) SourceInformation(org.finos.legend.engine.protocol.pure.v1.model.SourceInformation) Objects(java.util.Objects) Function3(org.eclipse.collections.api.block.function.Function3) List(java.util.List) MutableMap(org.eclipse.collections.api.map.MutableMap) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) CompileContext(org.finos.legend.engine.language.pure.compiler.toPureGraph.CompileContext) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) DeploymentMode(org.finos.legend.engine.shared.core.deployment.DeploymentMode) HelperGenerationSpecificationBuilder(org.finos.legend.engine.language.pure.dsl.generation.compiler.toPureGraph.HelperGenerationSpecificationBuilder) GenerationTreeNode(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.generationSpecification.GenerationTreeNode) GenerationCompilerExtension(org.finos.legend.engine.language.pure.dsl.generation.compiler.toPureGraph.GenerationCompilerExtension) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Function3(org.eclipse.collections.api.block.function.Function3) Objects(java.util.Objects) GenerationTreeNode(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.generationSpecification.GenerationTreeNode)

Example 3 with PureModelContextData

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());
        }
    });
}
Also used : PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement)

Example 4 with PureModelContextData

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);
}
Also used : GenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.generationSpecification.GenerationSpecification) Class(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Test(org.junit.Test)

Example 5 with PureModelContextData

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;
}
Also used : PureGrammarComposerContext(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposerContext) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)

Aggregations

PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)162 Test (org.junit.Test)112 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)61 SchemaToModelGenerationTest (org.finos.legend.engine.external.shared.format.model.test.SchemaToModelGenerationTest)43 ModelToSchemaGenerationTest (org.finos.legend.engine.external.shared.format.model.test.ModelToSchemaGenerationTest)19 PackageableElement (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement)17 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 List (java.util.List)12 Service (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service)12 Scope (io.opentracing.Scope)11 PureGrammarComposer (org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer)11 CommonProfile (org.pac4j.core.profile.CommonProfile)11 IOException (java.io.IOException)10 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)9 ApiOperation (io.swagger.annotations.ApiOperation)8 RichIterable (org.eclipse.collections.api.RichIterable)8 MutableList (org.eclipse.collections.api.list.MutableList)8 CompileContext (org.finos.legend.engine.language.pure.compiler.toPureGraph.CompileContext)8 FileGenerationSpecification (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.fileGeneration.FileGenerationSpecification)8 Lambda (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda)8