Search in sources :

Example 1 with PureModelBuilder

use of org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder 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)

Example 2 with PureModelBuilder

use of org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder in project legend-sdlc by finos.

the class ModelGenerationMojo 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);
    }
    PureModelBuilder.PureModelWithContextData pureModelWithContextData = pureModelBuilder.build();
    PureModelContextData pureModelContextData = pureModelWithContextData.getPureModelContextData();
    PureModel pureModel = pureModelWithContextData.getPureModel();
    long modelEnd = System.nanoTime();
    getLog().info(String.format("Finished loading model (%.9fs)", (modelEnd - modelStart) / 1_000_000_000.0));
    // Checks/Filters on generation specifications
    Map<String, GenerationSpecification> generationSpecificationMap = Iterate.groupByUniqueKey(pureModelContextData.getElementsOfType(GenerationSpecification.class), PackageableElement::getPath);
    filterGenerationSpecsByIncludes(generationSpecificationMap);
    filterGenerationSpecsByExcludes(generationSpecificationMap);
    if (generationSpecificationMap.isEmpty()) {
        getLog().info("No generation specification found, nothing to generate");
        return;
    }
    if (generationSpecificationMap.size() > 1) {
        throw new MojoExecutionException("Only one generation specification allowed, found: " + generationSpecificationMap.size());
    }
    try {
        // Start generating
        GenerationSpecification generationSpecification = generationSpecificationMap.values().iterator().next();
        ModelGenerationFactory modelGenerationFactory = ModelGenerationFactory.newFactory(generationSpecification, pureModelContextData, pureModel);
        PureModelContextData fullGeneratedModel = modelGenerationFactory.generate();
        serializePureModelContextData(fullGeneratedModel);
        getLog().info(String.format("Done (%.9fs)", (System.nanoTime() - generateStart) / 1_000_000_000.0));
    } catch (Exception e) {
        throw new MojoExecutionException("Error generating model generation: " + e.getMessage(), e);
    }
}
Also used : PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) 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) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureModelBuilder(org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder)

Example 3 with PureModelBuilder

use of org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder in project legend-sdlc by finos.

the class ServicesGenerationMojo method execute.

@Override
public void execute() throws MojoExecutionException {
    if (this.inclusions != null) {
        getLog().info("include service paths: " + this.inclusions.servicePaths);
        getLog().info("include service packages: " + this.inclusions.packages);
        getLog().info("include service directories: " + Arrays.toString(this.inclusions.directories));
    }
    if (this.exclusions != null) {
        getLog().info("exclude service paths: " + this.exclusions.servicePaths);
        getLog().info("exclude service packages: " + this.exclusions.packages);
        getLog().info("exclude service directories: " + Arrays.toString(this.exclusions.directories));
    }
    getLog().info("package prefix: " + ((this.packagePrefix == null) ? null : ('"' + this.packagePrefix + '"')));
    getLog().info("Java source output directory: " + this.javaSourceOutputDirectory);
    getLog().info("resource output directory: " + this.resourceOutputDirectory);
    if ((this.packagePrefix != null) && !SourceVersion.isName(this.packagePrefix)) {
        throw new MojoExecutionException("Invalid package prefix: " + this.packagePrefix);
    }
    getLog().info("Loading model");
    long modelStart = System.nanoTime();
    PureModelBuilder pureModelBuilder = PureModelBuilder.newBuilder();
    try (EntityLoader allEntities = EntityLoader.newEntityLoader(Thread.currentThread().getContextClassLoader())) {
        pureModelBuilder.addEntitiesIfPossible(allEntities.getAllEntities());
    } catch (Exception e) {
        throw new MojoExecutionException("Error loading entities from model", e);
    }
    int elementCount = pureModelBuilder.getElementCount();
    getLog().info("Found " + elementCount + " elements in the model");
    if (elementCount == 0) {
        long modelEnd = System.nanoTime();
        getLog().info(String.format("Finished loading model (%.9fs)", (modelEnd - modelStart) / 1_000_000_000.0));
        getLog().info("No execution artifacts to generate");
        return;
    }
    PureModelBuilder.PureModelWithContextData pureModelWithContextData;
    try {
        pureModelWithContextData = pureModelBuilder.build();
    } catch (Exception e) {
        throw new MojoExecutionException("Error building Pure model", e);
    }
    PureModelContextData pureModelContextData = pureModelWithContextData.getPureModelContextData();
    PureModel pureModel = pureModelWithContextData.getPureModel();
    long modelEnd = System.nanoTime();
    getLog().info(String.format("Finished loading model (%.9fs)", (modelEnd - modelStart) / 1_000_000_000.0));
    JsonMapper jsonMapper = PureProtocolObjectMapperFactory.withPureProtocolExtensions(JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS).enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY).disable(StreamWriteFeature.AUTO_CLOSE_TARGET).disable(StreamReadFeature.AUTO_CLOSE_SOURCE).serializationInclusion(JsonInclude.Include.NON_NULL).build());
    long start = System.nanoTime();
    Map<String, Service> servicesByPath = pureModelContextData.getElementsOfType(Service.class).stream().collect(Collectors.toMap(PackageableElement::getPath, el -> el));
    filterServicesByIncludes(servicesByPath);
    filterServicesByExcludes(servicesByPath);
    if (servicesByPath.isEmpty()) {
        getLog().info("Found 0 services for generation");
    } else if (getLog().isInfoEnabled()) {
        getLog().info(servicesByPath.keySet().stream().sorted().collect(Collectors.joining(", ", "Found " + servicesByPath.size() + " services for generation: ", "")));
    }
    for (Service service : servicesByPath.values()) {
        getLog().info("Generating execution artifacts for " + service.getPath());
        long serviceStart = System.nanoTime();
        try {
            MutableList<PlanGeneratorExtension> extensions = Lists.mutable.withAll(ServiceLoader.load(PlanGeneratorExtension.class));
            RichIterable<? extends Root_meta_pure_router_extension_RouterExtension> routerExtensions = extensions.flatCollect(e -> e.getExtraRouterExtensions(pureModel));
            MutableList<PlanTransformer> planTransformers = extensions.flatCollect(PlanGeneratorExtension::getExtraPlanTransformers);
            ServiceExecutionGenerator.newGenerator(service, pureModel, this.packagePrefix, this.javaSourceOutputDirectory.toPath(), this.resourceOutputDirectory.toPath(), jsonMapper, routerExtensions, planTransformers, null).generate();
        } catch (Exception e) {
            throw new MojoExecutionException("Error generating execution artifacts for " + service.getPath(), e);
        }
        long serviceEnd = System.nanoTime();
        getLog().info(String.format("Finished generating execution artifacts for %s (%.9fs)", service.getPath(), (serviceEnd - serviceStart) / 1_000_000_000.0));
    }
    long end = System.nanoTime();
    getLog().info(String.format("Finished generating execution artifacts for %d services (%.9fs)", servicesByPath.size(), (end - start) / 1_000_000_000.0));
}
Also used : Arrays(java.util.Arrays) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) Lists(org.eclipse.collections.api.factory.Lists) Parameter(org.apache.maven.plugins.annotations.Parameter) MutableList(org.eclipse.collections.api.list.MutableList) EntityToPureConverter(org.finos.legend.sdlc.protocol.pure.v1.EntityToPureConverter) ArrayList(java.util.ArrayList) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Mojo(org.apache.maven.plugins.annotations.Mojo) RichIterable(org.eclipse.collections.api.RichIterable) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Map(java.util.Map) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) PlanTransformer(org.finos.legend.engine.plan.generation.transformers.PlanTransformer) PureModelBuilder(org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) StreamWriteFeature(com.fasterxml.jackson.core.StreamWriteFeature) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StreamReadFeature(com.fasterxml.jackson.core.StreamReadFeature) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) Collectors(java.util.stream.Collectors) Root_meta_pure_router_extension_RouterExtension(org.finos.legend.pure.generated.Root_meta_pure_router_extension_RouterExtension) File(java.io.File) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) PureProtocolObjectMapperFactory(org.finos.legend.engine.protocol.pure.v1.PureProtocolObjectMapperFactory) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Comparator(java.util.Comparator) AbstractMojo(org.apache.maven.plugin.AbstractMojo) PlanGeneratorExtension(org.finos.legend.engine.plan.generation.extension.PlanGeneratorExtension) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) PlanTransformer(org.finos.legend.engine.plan.generation.transformers.PlanTransformer) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) PlanGeneratorExtension(org.finos.legend.engine.plan.generation.extension.PlanGeneratorExtension) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureModelBuilder(org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder)

Aggregations

MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)3 PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)3 PackageableElement (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement)3 PureModelBuilder (org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder)3 EntityLoader (org.finos.legend.sdlc.serialization.EntityLoader)3 IOException (java.io.IOException)2 List (java.util.List)2 GenerationSpecification (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.generationSpecification.GenerationSpecification)2 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 StreamReadFeature (com.fasterxml.jackson.core.StreamReadFeature)1 StreamWriteFeature (com.fasterxml.jackson.core.StreamWriteFeature)1 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 JsonMapper (com.fasterxml.jackson.databind.json.JsonMapper)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1