Search in sources :

Example 1 with PackageableElement

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement 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 PackageableElement

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement 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 3 with PackageableElement

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement 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)

Example 4 with PackageableElement

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement in project legend-sdlc by finos.

the class TestEntityToPureConverter method testFromEntity.

@Test
public void testFromEntity() {
    Entity entity = TestTools.newClassEntity("EmptyClass", "model::test");
    PackageableElement result = this.converter.fromEntity(entity);
    Assert.assertTrue(result instanceof Class);
    Class resultClass = (Class) result;
    Assert.assertEquals("EmptyClass", resultClass.name);
    Assert.assertEquals("model::test", resultClass._package);
    Assert.assertEquals(Collections.singletonList("meta::pure::metamodel::type::Any"), resultClass.superTypes);
    Assert.assertEquals(Collections.emptyList(), resultClass.properties);
    IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> this.converter.fromEntity(Entity.newEntity("not::a::real::PureEntity", "meta::unknown::NotAClassifier", Collections.emptyMap())));
    String message = e.getMessage();
    Assert.assertNotNull(message);
    String expectedPrefix = "Could not convert entity not::a::real::PureEntity with classifier meta::unknown::NotAClassifier to class PackageableElement";
    if (!message.startsWith(expectedPrefix)) {
        Assert.assertEquals(expectedPrefix, message);
    }
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) Class(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class) Test(org.junit.Test)

Example 5 with PackageableElement

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement in project legend-sdlc by finos.

the class FileGenerationMojo method execute.

@Override
public void execute() throws MojoExecutionException {
    long generateStart = System.nanoTime();
    if (this.inclusions != null) {
        getLog().info("include generation specification paths: " + this.inclusions.paths);
        getLog().info("include generation specification packages: " + this.inclusions.packages);
        getLog().info("include generation specification directories: " + Arrays.toString(this.inclusions.directories));
    }
    if (this.exclusions != null) {
        getLog().info("exclude generation specification paths: " + this.exclusions.paths);
        getLog().info("exclude generation specification packages: " + this.exclusions.packages);
        getLog().info("exclude generation specification directories: " + Arrays.toString(this.exclusions.directories));
    }
    getLog().info("Output directory: " + this.outputDirectory);
    // Load Model
    long modelStart = System.nanoTime();
    getLog().info("Start loading model");
    PureModelBuilder pureModelBuilder = PureModelBuilder.newBuilder();
    try (EntityLoader allEntities = EntityLoader.newEntityLoader(Thread.currentThread().getContextClassLoader())) {
        pureModelBuilder.addEntitiesIfPossible(allEntities.getAllEntities());
        int entityCount = pureModelBuilder.getElementCount();
        getLog().info("Found " + entityCount + " entities");
        if (entityCount == 0) {
            long modelEnd = System.nanoTime();
            getLog().info(String.format("Finished loading model (%.9fs)", (modelEnd - modelStart) / 1_000_000_000.0));
            getLog().info("No elements found to generate");
            return;
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error loading entities from model", e);
    }
    getLog().info("Compiling model");
    PureModelBuilder.PureModelWithContextData pureModelWithContextData = pureModelBuilder.build();
    PureModelContextData pureModelContextData = pureModelWithContextData.getPureModelContextData();
    PureModel pureModel = pureModelWithContextData.getPureModel();
    long modelEnd = System.nanoTime();
    getLog().info(String.format("Finished loading and compiling model (%.9fs)", (modelEnd - modelStart) / 1_000_000_000.0));
    Map<String, GenerationSpecification> generationSpecificationMap = LazyIterate.selectInstancesOf(pureModelContextData.getElements(), GenerationSpecification.class).groupByUniqueKey(PackageableElement::getPath, Maps.mutable.empty());
    filterGenerationSpecsByIncludes(generationSpecificationMap);
    filterGenerationSpecsByExcludes(generationSpecificationMap);
    if (generationSpecificationMap.isEmpty()) {
        getLog().info("No generation specification found, nothing to generate");
        return;
    }
    if (generationSpecificationMap.size() > 1) {
        throw new MojoExecutionException(Iterate.toSortedList(generationSpecificationMap.keySet()).makeString("Only 1 generation specification allowed, found " + generationSpecificationMap.size() + ": ", ", ", ""));
    }
    try {
        // Start generating
        GenerationSpecification generationSpecification = generationSpecificationMap.values().iterator().next();
        getLog().info(String.format("Start generating file generations for generation specification '%s', %,d file generations found", generationSpecification.getPath(), generationSpecification.fileGenerations.size()));
        FileGenerationFactory fileGenerationFactory = FileGenerationFactory.newFactory(generationSpecification, pureModelContextData, pureModel);
        MutableMap<FileGenerationSpecification, List<GenerationOutput>> outputs = fileGenerationFactory.generateFiles();
        serializeOutput(outputs);
        getLog().info(String.format("Done (%.9fs)", (System.nanoTime() - generateStart) / 1_000_000_000.0));
    } catch (Exception e) {
        throw new MojoExecutionException("Error generating files: " + e.getMessage(), e);
    }
}
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) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) FileGenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.fileGeneration.FileGenerationSpecification) GenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.generationSpecification.GenerationSpecification) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) List(java.util.List) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureModelBuilder(org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder)

Aggregations

PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)18 PackageableElement (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement)18 List (java.util.List)13 Class (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class)13 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)13 SectionIndex (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex)12 PackageableElement (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement)12 ListIterate (org.eclipse.collections.impl.utility.ListIterate)11 ImportAwareCodeSection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.ImportAwareCodeSection)11 PackageableConnection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.PackageableConnection)10 PackageableRuntime (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.PackageableRuntime)10 Test (org.junit.Test)10 Lists (org.eclipse.collections.impl.factory.Lists)9 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)9 EngineErrorType (org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType)9 PackageableElementVisitor (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElementVisitor)9 DataElement (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.data.DataElement)9 org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain)9 Mapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.Mapping)9 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)9