Search in sources :

Example 1 with PureGrammarParser

use of org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser in project legend-engine by finos.

the class TransformGrammarToJson method transformGrammarToJson.

@Deprecated
@POST
@Path("transformGrammarToJson")
@ApiOperation(value = "Generates Pure protocol JSON from Pure language text")
@Consumes({ MediaType.APPLICATION_JSON, APPLICATION_ZLIB })
public Response transformGrammarToJson(GrammarToJsonInput grammarInput, @ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> pm, @DefaultValue("true") @QueryParam("returnSourceInfo") boolean returnSourceInfo) {
    MutableList<CommonProfile> profiles = ProfileManagerHelper.extractProfiles(pm);
    try (Scope scope = GlobalTracer.get().buildSpan("Service: transformJsonToGrammar").startActive(true)) {
        PureGrammarParserExtensions.logExtensionList();
        PureGrammarParser parser = PureGrammarParser.newInstance();
        Map<String, Lambda> lambdas = new HashMap<>();
        Map<String, ParserError> lambdaErrors = new HashMap<>();
        grammarInput.isolatedLambdas.forEach((key, value) -> {
            try {
                Lambda lambda = parser.parseLambda(value, key, returnSourceInfo);
                lambdas.put(key, lambda);
            } catch (Exception e) {
                if (e instanceof EngineException) {
                    EngineException exception = (EngineException) e;
                    if (!EngineErrorType.PARSER.equals(exception.getErrorType())) {
                        // throw non-parser error
                        throw e;
                    }
                    lambdaErrors.put(key, new ParserError(exception.getMessage(), exception.getSourceInformation()));
                }
                ExceptionTool.exceptionManager(e, LoggingEventType.TRANSFORM_GRAMMAR_TO_JSON_ERROR, profiles);
            }
        });
        JsonToGrammarInput symmetricResult = new JsonToGrammarInput();
        symmetricResult.isolatedLambdas = new LambdaInput();
        symmetricResult.isolatedLambdas.lambdas = lambdas;
        // only show object is errors exist
        symmetricResult.isolatedLambdas.lambdaErrors = lambdaErrors.size() > 0 ? lambdaErrors : null;
        if (grammarInput.code != null) {
            try {
                symmetricResult.modelDataContext = parser.parseModel(grammarInput.code, returnSourceInfo);
            } catch (Exception e) {
                if (e instanceof EngineException) {
                    EngineException exception = (EngineException) e;
                    if (!EngineErrorType.PARSER.equals(exception.getErrorType())) {
                        // throw non-parser error
                        throw e;
                    }
                    symmetricResult.codeError = new ParserError(exception.getMessage(), exception.getSourceInformation());
                }
                ExceptionTool.exceptionManager(e, LoggingEventType.TRANSFORM_GRAMMAR_TO_JSON_ERROR, profiles);
            }
        }
        return ManageConstantResult.manageResult(profiles, symmetricResult, objectMapper);
    } catch (Exception ex) {
        return ExceptionTool.exceptionManager(ex, LoggingEventType.TRANSFORM_GRAMMAR_TO_JSON_ERROR, profiles);
    }
}
Also used : JsonToGrammarInput(org.finos.legend.engine.language.pure.grammar.api.jsonToGrammar.JsonToGrammarInput) HashMap(java.util.HashMap) ParserError(org.finos.legend.engine.shared.core.api.grammar.ParserError) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) PureGrammarParser(org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Scope(io.opentracing.Scope) CommonProfile(org.pac4j.core.profile.CommonProfile) Lambda(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda) LambdaInput(org.finos.legend.engine.language.pure.grammar.api.jsonToGrammar.LambdaInput) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with PureGrammarParser

use of org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser in project legend-engine by finos.

the class TestExternalFormatQueries method runTest.

protected String runTest(PureModelContextData generated, String grammar, String query, String mappingPath, String runtimePath, InputStream input) {
    try {
        PureModelContextData parsed = PureGrammarParser.newInstance().parseModel(grammar);
        PureModelContextData modelData = generated == null ? parsed : parsed.combine(generated);
        ObjectMapper objectMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports();
        String json = objectMapper.writeValueAsString(modelData);
        modelData = objectMapper.readValue(json, PureModelContextData.class);
        PureModel model = Compiler.compile(modelData, DeploymentMode.TEST, null);
        PureGrammarParser parser = PureGrammarParser.newInstance();
        Lambda lambdaProtocol = parser.parseLambda(query, "query", true);
        LambdaFunction<?> lambda = HelperValueSpecificationBuilder.buildLambda(lambdaProtocol.body, Lists.fixedSize.<Variable>empty(), model.getContext());
        ExecutionContext context = new Root_meta_pure_runtime_ExecutionContext_Impl(" ")._enableConstraints(true);
        RichIterable<Root_meta_external_shared_format_ExternalFormatExtension> planGenerationExtensions = LazyIterate.collect(ExternalFormatPlanGenerationExtensionLoader.extensions().values(), ext -> ext.getPureExtension(model.getExecutionSupport()));
        RichIterable<? extends Root_meta_pure_router_extension_RouterExtension> extensions = core_external_shared_extension.Root_meta_external_shared_format_routerExtensions_String_1__ExternalFormatExtension_MANY__RouterExtension_MANY_("externalFormat", planGenerationExtensions, model.getExecutionSupport());
        Mapping mapping = model.getMapping(mappingPath);
        Runtime runtime = model.getRuntime(runtimePath);
        String plan = PlanGenerator.generateExecutionPlanAsString(lambda, mapping, runtime, context, model, "vX_X_X", PlanPlatform.JAVA, "test", extensions, LegendPlanTransformers.transformers);
        PlanExecutor executor = PlanExecutor.newPlanExecutorWithAvailableStoreExecutors(true);
        Result result = executor.execute(plan, input);
        StreamingResult streamingResult = (StreamingResult) result;
        return streamingResult.flush(streamingResult.getSerializer(SerializationFormat.DEFAULT));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Root_meta_external_shared_format_ExternalFormatExtension(org.finos.legend.pure.generated.Root_meta_external_shared_format_ExternalFormatExtension) PlanExecutor(org.finos.legend.engine.plan.execution.PlanExecutor) Mapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) PureGrammarParser(org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser) Result(org.finos.legend.engine.plan.execution.result.Result) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) Runtime(org.finos.legend.pure.m3.coreinstance.meta.pure.runtime.Runtime) ExecutionContext(org.finos.legend.pure.m3.coreinstance.meta.pure.runtime.ExecutionContext) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) Root_meta_pure_runtime_ExecutionContext_Impl(org.finos.legend.pure.generated.Root_meta_pure_runtime_ExecutionContext_Impl) Lambda(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

PureGrammarParser (org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser)2 Lambda (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Scope (io.opentracing.Scope)1 ApiOperation (io.swagger.annotations.ApiOperation)1 HashMap (java.util.HashMap)1 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)1 JsonToGrammarInput (org.finos.legend.engine.language.pure.grammar.api.jsonToGrammar.JsonToGrammarInput)1 LambdaInput (org.finos.legend.engine.language.pure.grammar.api.jsonToGrammar.LambdaInput)1 PlanExecutor (org.finos.legend.engine.plan.execution.PlanExecutor)1 Result (org.finos.legend.engine.plan.execution.result.Result)1 StreamingResult (org.finos.legend.engine.plan.execution.result.StreamingResult)1 PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)1 ParserError (org.finos.legend.engine.shared.core.api.grammar.ParserError)1 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)1 Root_meta_external_shared_format_ExternalFormatExtension (org.finos.legend.pure.generated.Root_meta_external_shared_format_ExternalFormatExtension)1 Root_meta_pure_runtime_ExecutionContext_Impl (org.finos.legend.pure.generated.Root_meta_pure_runtime_ExecutionContext_Impl)1 Mapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping)1 ExecutionContext (org.finos.legend.pure.m3.coreinstance.meta.pure.runtime.ExecutionContext)1 Runtime (org.finos.legend.pure.m3.coreinstance.meta.pure.runtime.Runtime)1