Search in sources :

Example 1 with SchemaToModelGenerator

use of org.finos.legend.engine.external.shared.format.model.toModel.SchemaToModelGenerator in project legend-engine by finos.

the class SchemaToModelGenerationTest method generateModel.

public static PureModelContextData generateModel(String schemaCode, SchemaToModelConfiguration config) {
    PureModelContextData modelData = null;
    PureModel pureModel = null;
    try {
        modelData = PureGrammarParser.newInstance().parseModel(schemaCode);
        pureModel = Compiler.compile(modelData, DeploymentMode.TEST, null);
    } catch (Exception e) {
        Assert.fail("The schema code cannot be compiled: " + e.getMessage());
    }
    PureModelContextData generated = new SchemaToModelGenerator(pureModel, "vX_X_X").generate(config);
    PureModelContextData combined = modelData.combine(generated);
    try {
        Compiler.compile(combined, DeploymentMode.TEST, null);
    } catch (Exception e) {
        Assert.fail("The generated model cannot be compiled: " + e.getMessage());
    }
    return generated;
}
Also used : SchemaToModelGenerator(org.finos.legend.engine.external.shared.format.model.toModel.SchemaToModelGenerator) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) IOException(java.io.IOException)

Example 2 with SchemaToModelGenerator

use of org.finos.legend.engine.external.shared.format.model.toModel.SchemaToModelGenerator in project legend-engine by finos.

the class ExternalFormats method generateModel.

@POST
@Path("generateModel")
@ApiOperation(value = "Generates a model from a schema for the external format")
@Consumes({ MediaType.APPLICATION_JSON, APPLICATION_ZLIB })
public Response generateModel(GenerateModelInput generateModelInput, @ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> pm) {
    MutableList<CommonProfile> profiles = ProfileManagerHelper.extractProfiles(pm);
    boolean interactive = generateModelInput.model instanceof PureModelContextData;
    try (Scope scope = GlobalTracer.get().buildSpan("Service: Generate Model From External Format Schema").startActive(true)) {
        long start = System.currentTimeMillis();
        LOGGER.info(new LogInfo(profiles, interactive ? LoggingEventType.GENERATE_EXTERNAL_FORMAT_MODEL_INTERACTIVE_START : LoggingEventType.GENERATE_EXTERNAL_FORMAT_MODEL_START).toString());
        ExternalFormatExtension extension = extensions.get(generateModelInput.config.format);
        if (!extension.supportsModelGeneration()) {
            throw new UnsupportedOperationException("Model generation not supported for " + extension.getFormat());
        }
        PureModel pureModel = this.modelManager.loadModel(generateModelInput.model, generateModelInput.clientVersion, profiles, null);
        SchemaToModelGenerator generator = new SchemaToModelGenerator(pureModel, generateModelInput.clientVersion);
        PureModelContextData generated = generator.generate(generateModelInput.config);
        LOGGER.info(new LogInfo(profiles, interactive ? LoggingEventType.GENERATE_EXTERNAL_FORMAT_MODEL_INTERACTIVE_STOP : LoggingEventType.GENERATE_EXTERNAL_FORMAT_MODEL_STOP, (double) System.currentTimeMillis() - start).toString());
        return ManageConstantResult.manageResult(profiles, generated, objectMapper);
    } catch (Exception ex) {
        return ExceptionTool.exceptionManager(ex, interactive ? LoggingEventType.GENERATE_EXTERNAL_FORMAT_MODEL_INTERACTIVE_ERROR : LoggingEventType.GENERATE_EXTERNAL_FORMAT_MODEL_ERROR, profiles);
    }
}
Also used : LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) Scope(io.opentracing.Scope) CommonProfile(org.pac4j.core.profile.CommonProfile) SchemaToModelGenerator(org.finos.legend.engine.external.shared.format.model.toModel.SchemaToModelGenerator) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) ExternalFormatExtension(org.finos.legend.engine.external.shared.format.model.ExternalFormatExtension) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SchemaToModelGenerator (org.finos.legend.engine.external.shared.format.model.toModel.SchemaToModelGenerator)2 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)2 PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)2 Scope (io.opentracing.Scope)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 ExternalFormatExtension (org.finos.legend.engine.external.shared.format.model.ExternalFormatExtension)1 LogInfo (org.finos.legend.engine.shared.core.operational.logs.LogInfo)1 CommonProfile (org.pac4j.core.profile.CommonProfile)1