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;
});
}
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;
}
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;
}
Aggregations