Search in sources :

Example 1 with Constraint

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

the class HelperModelBuilder method processConstraints.

public static RichIterable<org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint> processConstraints(List<org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Constraint> constraints, CompileContext context, String _package, String _name, ValueSpecificationContext vsContext, ProcessingContext ctx) {
    String constraintSourceId = context.pureModel.buildPackageString(_package, _name).replace("::", "_") + "_Constraint$";
    SourceInformation si = new SourceInformation(constraintSourceId, 0, 0, 0, 0);
    return ListIterate.collect(constraints, c -> {
        ctx.push("Constraint " + c.name);
        org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction<?> lf;
        try {
            lf = HelperValueSpecificationBuilder.buildLambdaWithContext(c.name, c.functionDefinition.body, c.functionDefinition.parameters, context, ctx);
            lf._expressionSequence().forEach((Procedure<ValueSpecification>) es -> es._usageContext(vsContext));
            lf.setSourceInformation(si);
        } catch (Exception e) {
            LOGGER.warn(new LogInfo(null, LoggingEventType.GRAPH_EXPRESSION_ERROR, "Can't build constraint '" + c.name + "' of class '" + context.pureModel.buildPackageString(_package, _name) + "- stack: " + ctx.getStack()).toString());
            System.out.println(e.getMessage());
            if (e instanceof EngineException) {
                throw e;
            }
            throw new EngineException(e.getMessage(), c.sourceInformation, EngineErrorType.COMPILATION);
        }
        org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction<?> mf = null;
        try {
            if (c.messageFunction != null) {
                mf = HelperValueSpecificationBuilder.buildLambdaWithContext(c.name, c.messageFunction.body, c.messageFunction.parameters, context, ctx);
                mf._expressionSequence().forEach((Procedure<ValueSpecification>) es -> es._usageContext(vsContext));
                mf.setSourceInformation(si);
            }
        } catch (Exception e) {
            mf = null;
            LOGGER.warn(new LogInfo(null, LoggingEventType.GRAPH_EXPRESSION_ERROR, "Can't build the message function for constraint '" + c.name + "' of class '" + context.pureModel.buildPackageString(_package, _name) + "' - stack: " + ctx.getStack()).toString());
        /* We let these through as a warning because there are invalid message functions that are not properly caught by the PURE compiler .
                   For example:
                      ~message: 'String ' + $this.maybe
                      where maybe is String[0..1]

                   which should cause a multiplicity error as arguments to plus should all be [1] (to become elements of to plus(parts:String[*]))
                   In the absence of a message the system will fall back on the default failure message.
                 */
        }
        org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint pureConstraint = new Root_meta_pure_metamodel_constraint_Constraint_Impl(constraintSourceId);
        pureConstraint.setSourceInformation(si);
        pureConstraint._functionDefinition(lf);
        pureConstraint._name(c.name);
        pureConstraint._externalId(c.externalId);
        pureConstraint._enforcementLevel(c.enforcementLevel);
        pureConstraint._messageFunction(mf);
        ctx.pop();
        if (!lf._expressionSequence().getLast()._genericType()._rawType().equals(context.pureModel.getType("Boolean"))) {
            throw new EngineException("Constraint must be of type 'Boolean'", c.functionDefinition.body.get(c.functionDefinition.body.size() - 1).sourceInformation, EngineErrorType.COMPILATION);
        }
        return pureConstraint;
    });
}
Also used : LoggingEventType(org.finos.legend.engine.shared.core.operational.logs.LoggingEventType) Property(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Property) Assert(org.finos.legend.engine.shared.core.operational.Assert) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) AbstractProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty) EngineErrorType(org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType) Function(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Function) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) Procedure(org.eclipse.collections.api.block.procedure.Procedure) Root_meta_pure_mapping_modelToModel_ModelStore_Impl(org.finos.legend.pure.generated.Root_meta_pure_mapping_modelToModel_ModelStore_Impl) PropertyOwner(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PropertyOwner) ValueSpecificationContext(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecificationContext) Root_meta_pure_metamodel_extension_TaggedValue_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_extension_TaggedValue_Impl) RichIterable(org.eclipse.collections.api.RichIterable) Variable(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.Variable) Root_meta_pure_metamodel_type_FunctionType_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_type_FunctionType_Impl) Multiplicity(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity) Class(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class) Root_meta_pure_metamodel_constraint_Constraint_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_constraint_Constraint_Impl) Root_meta_pure_metamodel_type_generics_GenericType_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_type_generics_GenericType_Impl) Logger(org.slf4j.Logger) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) MilestoningFunctions(org.finos.legend.pure.m3.compiler.postprocessing.processor.milestoning.MilestoningFunctions) Root_meta_pure_metamodel_valuespecification_VariableExpression_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_valuespecification_VariableExpression_Impl) Root_meta_pure_metamodel_function_property_Property_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_function_property_Property_Impl) ListIterate(org.eclipse.collections.impl.utility.ListIterate) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) Root_meta_pure_metamodel_valuespecification_ExpressionSequenceValueSpecificationContext_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_valuespecification_ExpressionSequenceValueSpecificationContext_Impl) LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) LambdaFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction) Objects(java.util.Objects) List(java.util.List) CompiledExecutionSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) Root_meta_pure_metamodel_function_property_QualifiedProperty_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_function_property_QualifiedProperty_Impl) Root_meta_pure_metamodel_valuespecification_ClassConstraintValueSpecificationContext_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_valuespecification_ClassConstraintValueSpecificationContext_Impl) PackageableElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) Lists(org.eclipse.collections.impl.factory.Lists) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) org.finos.legend.pure.generated.platform_pure_corefunctions_meta(org.finos.legend.pure.generated.platform_pure_corefunctions_meta) Optional(java.util.Optional) Constraint(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint) LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) Root_meta_pure_metamodel_constraint_Constraint_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_constraint_Constraint_Impl) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Constraint(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint)

Example 2 with Constraint

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

the class DomainParseTreeWalker method visitConstraint.

private Constraint visitConstraint(List<DomainParserGrammar.ConstraintContext> constraintContexts, DomainParserGrammar.ConstraintContext ctx) {
    Constraint constraint = new Constraint();
    constraint.functionDefinition = new Lambda();
    List<String> typeParametersNames = new ArrayList<>();
    if (ctx.simpleConstraint() != null) {
        DomainParserGrammar.SimpleConstraintContext simpleConstraintContext = ctx.simpleConstraint();
        constraint.name = simpleConstraintContext.constraintId() == null ? String.valueOf(constraintContexts.indexOf((ctx))) : PureGrammarParserUtility.fromIdentifier(simpleConstraintContext.constraintId().identifier());
        DomainParseTreeWalker.LambdaContext lambdaContext = new DomainParseTreeWalker.LambdaContext(constraint.name.replace("::", "_"));
        ValueSpecification valueSpecification = this.combinedExpression(simpleConstraintContext.combinedExpression(), "line", typeParametersNames, lambdaContext, "", true, false);
        constraint.functionDefinition.body = Collections.singletonList(valueSpecification);
        constraint.sourceInformation = this.walkerSourceInformation.getSourceInformation(ctx);
    } else if (ctx.complexConstraint() != null) {
        DomainParserGrammar.ComplexConstraintContext complexConstraintContext = ctx.complexConstraint();
        constraint.name = PureGrammarParserUtility.fromIdentifier(complexConstraintContext.identifier());
        constraint.enforcementLevel = complexConstraintContext.constraintEnforcementLevel() != null ? complexConstraintContext.constraintEnforcementLevel().constraintEnforcementLevelType().getText() : null;
        constraint.externalId = complexConstraintContext.constraintExternalId() != null ? PureGrammarParserUtility.fromGrammarString(complexConstraintContext.constraintExternalId().STRING().getText(), true) : null;
        DomainParseTreeWalker.LambdaContext lambdaContext = new DomainParseTreeWalker.LambdaContext(constraint.name.replace("::", "_"));
        constraint.functionDefinition.body = Collections.singletonList(this.combinedExpression(complexConstraintContext.constraintFunction().combinedExpression(), "constraint", typeParametersNames, lambdaContext, "", true, false));
        if (complexConstraintContext.constraintMessage() != null) {
            constraint.messageFunction = new Lambda();
            constraint.messageFunction.body = Collections.singletonList(this.combinedExpression(complexConstraintContext.constraintMessage().combinedExpression(), "message", typeParametersNames, lambdaContext, "", true, false));
        }
        constraint.sourceInformation = this.walkerSourceInformation.getSourceInformation(ctx);
    } else {
        throw new UnsupportedOperationException();
    }
    return constraint;
}
Also used : Constraint(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Constraint) ArrayList(java.util.ArrayList) ValueSpecification(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification) CString(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.CString) DomainParserGrammar(org.finos.legend.engine.language.pure.grammar.from.antlr4.domain.DomainParserGrammar) Lambda(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda)

Example 3 with Constraint

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

the class DomainParseTreeWalker method visitClass.

// ----------------------------------------------- CLASS -----------------------------------------------
private Class visitClass(DomainParserGrammar.ClassDefinitionContext ctx) {
    // TODO: break if use of generics!
    Class _class = new Class();
    _class._package = ctx.qualifiedName().packagePath() == null ? "" : PureGrammarParserUtility.fromPath(ctx.qualifiedName().packagePath().identifier());
    _class.name = PureGrammarParserUtility.fromIdentifier(ctx.qualifiedName().identifier());
    _class.stereotypes = ctx.stereotypes() == null ? Lists.mutable.empty() : this.visitStereotypes(ctx.stereotypes());
    _class.constraints = ctx.constraints() == null ? Lists.mutable.empty() : ListIterate.collect(ctx.constraints().constraint(), c -> this.visitConstraint(ctx.constraints().constraint(), c));
    // TODO ? add source info specific to each superType
    _class.superTypes = ctx.EXTENDS() == null ? Lists.mutable.empty() : ListIterate.collect(ctx.type(), t -> PureGrammarParserUtility.fromQualifiedName(t.qualifiedName().packagePath() == null ? Collections.emptyList() : t.qualifiedName().packagePath().identifier(), t.qualifiedName().identifier()));
    _class.taggedValues = ctx.taggedValues() == null ? Lists.mutable.empty() : this.visitTaggedValues(ctx.taggedValues());
    _class.properties = ctx.classBody().properties().property() == null ? new ArrayList<>() : ListIterate.collect(ctx.classBody().properties().property(), this::visitSimpleProperty);
    _class.qualifiedProperties = ctx.classBody().properties().qualifiedProperty() == null ? new ArrayList<>() : ListIterate.collect(ctx.classBody().properties().qualifiedProperty(), this::visitDerivedProperty);
    _class.sourceInformation = this.walkerSourceInformation.getSourceInformation(ctx);
    return _class;
}
Also used : ArrayList(java.util.ArrayList) Class(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class) HackedClass(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.HackedClass)

Aggregations

ArrayList (java.util.ArrayList)2 Class (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class)2 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 RichIterable (org.eclipse.collections.api.RichIterable)1 Procedure (org.eclipse.collections.api.block.procedure.Procedure)1 Lists (org.eclipse.collections.impl.factory.Lists)1 LazyIterate (org.eclipse.collections.impl.utility.LazyIterate)1 ListIterate (org.eclipse.collections.impl.utility.ListIterate)1 DomainParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.domain.DomainParserGrammar)1 EngineErrorType (org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType)1 Constraint (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Constraint)1 Function (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Function)1 Multiplicity (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity)1 Property (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Property)1 ValueSpecification (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification)1 Variable (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.Variable)1 CString (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.CString)1 HackedClass (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.HackedClass)1