use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ConstraintContext in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method constraint.
private Constraint constraint(CoreInstance owner, ConstraintContext ctx, int position, ImportGroup importId, LambdaContext lambdaContext, boolean addLines, boolean postConstraint) {
String constraintName;
String constraintOwner = null;
String constraintExternalId = null;
CoreInstance constraintFunctionDefinition;
String constraintLevel = null;
LambdaFunction<?> constraintMessageFunction = null;
SourceInformation constraintSourceInformation;
if (ctx.simpleConstraint() != null) {
org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SimpleConstraintContext simpleConstraintContext = ctx.simpleConstraint();
constraintName = simpleConstraintContext.constraintId() != null ? simpleConstraintContext.constraintId().VALID_STRING().getText() : String.valueOf(position);
constraintFunctionDefinition = this.combinedExpression(simpleConstraintContext.combinedExpression(), "", Lists.mutable.empty(), lambdaContext, "", true, importId, addLines);
constraintSourceInformation = this.sourceInformation.getPureSourceInformation(simpleConstraintContext.getStart(), simpleConstraintContext.getStart(), simpleConstraintContext.getStop());
} else {
org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ComplexConstraintContext complexConstraintContext = ctx.complexConstraint();
constraintSourceInformation = this.sourceInformation.getPureSourceInformation(complexConstraintContext.getStart(), complexConstraintContext.VALID_STRING().getSymbol(), complexConstraintContext.getStop());
if (this.processorSupport.instance_instanceOf(owner, M3Paths.Class)) {
constraintName = complexConstraintContext.VALID_STRING().getText();
if (complexConstraintContext.constraintOwner() != null) {
constraintOwner = complexConstraintContext.constraintOwner().VALID_STRING().getText();
}
if (complexConstraintContext.constraintExternalId() != null) {
constraintExternalId = this.removeQuotes(complexConstraintContext.constraintExternalId().STRING());
}
constraintFunctionDefinition = this.combinedExpression(complexConstraintContext.constraintFunction().combinedExpression(), "", Lists.mutable.empty(), lambdaContext, "", true, importId, addLines);
if (complexConstraintContext.constraintEnforcementLevel() != null) {
constraintLevel = complexConstraintContext.constraintEnforcementLevel().ENFORCEMENT_LEVEL().getText();
}
if (complexConstraintContext.constraintMessage() != null) {
CoreInstance messageFunction = this.combinedExpression(complexConstraintContext.constraintMessage().combinedExpression(), "", Lists.mutable.empty(), lambdaContext, "", true, importId, addLines);
SourceInformation messageSourceInformation = messageFunction.getSourceInformation();
CoreInstance messageFunctionType = this.repository.newAnonymousCoreInstance(messageSourceInformation, this.processorSupport.package_getByUserPath(M3Paths.FunctionType), true);
CoreInstance param = VariableExpressionInstance.createPersistent(this.repository, messageSourceInformation, (GenericType) org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(owner, this.processorSupport), this.pureOne, "this");
Instance.addValueToProperty(messageFunctionType, M3Properties.parameters, param, this.processorSupport);
Instance.setValueForProperty(messageFunctionType, M3Properties.returnMultiplicity, this.getPureOne(), this.processorSupport);
Instance.setValueForProperty(messageFunctionType, M3Properties.returnType, org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(this.processorSupport.package_getByUserPath(M3Paths.String), this.processorSupport), this.processorSupport);
CoreInstance messageFunctionTypeGt = this.repository.newAnonymousCoreInstance(messageSourceInformation, this.processorSupport.package_getByUserPath(M3Paths.GenericType), true);
Instance.setValueForProperty(messageFunctionTypeGt, M3Properties.rawType, messageFunctionType, this.processorSupport);
CoreInstance lambdaFunctionClass = this.processorSupport.package_getByUserPath(M3Paths.LambdaFunction);
CoreInstance lambdaGenericType = org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(lambdaFunctionClass, this.processorSupport);
Instance.setValueForProperty(lambdaGenericType, M3Properties.typeArguments, messageFunctionTypeGt, this.processorSupport);
constraintMessageFunction = LambdaFunctionInstance.createPersistent(this.repository, lambdaContext.getLambdaFunctionUniqueName(), messageSourceInformation);
Instance.setValueForProperty(constraintMessageFunction, M3Properties.expressionSequence, messageFunction, this.processorSupport);
Instance.setValueForProperty(constraintMessageFunction, M3Properties.classifierGenericType, lambdaGenericType, this.processorSupport);
Instance.setValueForProperty(messageFunctionType, M3Properties.function, constraintMessageFunction, this.processorSupport);
}
} else {
throw new PureParserException(constraintSourceInformation, "Complex constraint specifications are supported only for class definitions");
}
}
SourceInformation functionSourceInformation = constraintFunctionDefinition.getSourceInformation();
CoreInstance functionType = this.repository.newAnonymousCoreInstance(functionSourceInformation, this.processorSupport.package_getByUserPath(M3Paths.FunctionType), true);
if (this.processorSupport.instance_instanceOf(owner, M3Paths.ElementWithConstraints)) {
CoreInstance param = VariableExpressionInstance.createPersistent(this.repository, functionSourceInformation, (GenericType) org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(owner, this.processorSupport), this.pureOne, "this");
Instance.addValueToProperty(functionType, M3Properties.parameters, param, this.processorSupport);
}
if (this.processorSupport.instance_instanceOf(owner, M3Paths.FunctionDefinition)) {
FunctionType ft = (FunctionType) this.processorSupport.function_getFunctionType(owner);
MutableList<CoreInstance> params = Lists.mutable.empty();
params.addAll(Instance.getValueForMetaPropertyToManyResolved(ft, M3Properties.parameters, this.processorSupport).toList());
if (postConstraint) {
CoreInstance returnParam = VariableExpressionInstance.createPersistent(this.repository, functionSourceInformation, (GenericType) ft.getValueForMetaPropertyToOne(M3Properties.returnType), (Multiplicity) ft.getValueForMetaPropertyToOne(M3Properties.returnMultiplicity), "return");
params.add(returnParam);
}
Instance.addValueToProperty(functionType, M3Properties.parameters, params, this.processorSupport);
}
Instance.setValueForProperty(functionType, M3Properties.returnMultiplicity, this.getPureOne(), this.processorSupport);
Instance.setValueForProperty(functionType, M3Properties.returnType, org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(this.processorSupport.package_getByUserPath(M3Paths.Boolean), this.processorSupport), this.processorSupport);
CoreInstance functionTypeGt = this.repository.newAnonymousCoreInstance(functionSourceInformation, this.processorSupport.package_getByUserPath(M3Paths.GenericType), true);
Instance.setValueForProperty(functionTypeGt, M3Properties.rawType, functionType, this.processorSupport);
CoreInstance lambdaFunctionClass = this.processorSupport.package_getByUserPath(M3Paths.LambdaFunction);
CoreInstance lambdaGenericType = org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(lambdaFunctionClass, this.processorSupport);
Instance.setValueForProperty(lambdaGenericType, M3Properties.typeArguments, functionTypeGt, this.processorSupport);
LambdaFunction<?> constraintFunctionLambda = LambdaFunctionInstance.createPersistent(this.repository, lambdaContext.getLambdaFunctionUniqueName(), functionSourceInformation);
Instance.setValueForProperty(constraintFunctionLambda, M3Properties.expressionSequence, constraintFunctionDefinition, this.processorSupport);
Instance.setValueForProperty(constraintFunctionLambda, M3Properties.classifierGenericType, lambdaGenericType, this.processorSupport);
Instance.setValueForProperty(functionType, M3Properties.function, constraintFunctionLambda, this.processorSupport);
Constraint constraint = ConstraintInstance.createPersistent(this.repository, null, null, null);
constraint.setSourceInformation(constraintSourceInformation);
constraint._name(constraintName);
if (constraintOwner != null) {
constraint._owner(constraintOwner);
}
if (constraintExternalId != null) {
constraint._externalId(constraintExternalId);
}
constraint._functionDefinition(constraintFunctionLambda);
if (constraintLevel != null) {
constraint._enforcementLevel(constraintLevel);
}
if (constraintMessageFunction != null) {
constraint._messageFunction(constraintMessageFunction);
}
return constraint;
}
use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ConstraintContext in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method constraints.
private MutableList<Constraint> constraints(CoreInstance owner, ConstraintsContext ctx, ImportGroup importId, LambdaContext lambdaContext, boolean addLines) {
MutableList<Constraint> constraints = Lists.mutable.empty();
if (ctx != null) {
int i = 0;
for (ConstraintContext cCtx : ctx.constraint()) {
constraints.add(this.constraint(owner, cCtx, i, importId, lambdaContext, addLines, false));
i++;
}
}
return constraints;
}
Aggregations