use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint in project legend-pure by finos.
the class FunctionDefinitionProcessor method process.
public static void process(FunctionDefinition<?> functionDefinition, ProcessorState state, Matcher matcher, ModelRepository repository) {
ProcessorSupport processorSupport = state.getProcessorSupport();
VariableContext variableContext = state.getVariableContext();
FunctionType functionType = (FunctionType) processorSupport.function_getFunctionType(functionDefinition);
state.getObserver().startProcessingFunction(functionDefinition, functionType);
URLPatternLibrary urlPatternLibrary = state.getURLPatternLibrary();
if (urlPatternLibrary != null) {
urlPatternLibrary.possiblyRegister(functionDefinition, processorSupport);
}
boolean shouldSetTypeInferenceContext = (functionDefinition instanceof ConcreteFunctionDefinition) && (functionDefinition._classifierGenericType() != null) && (functionDefinition._classifierGenericType()._rawTypeCoreInstance() != null) && "ConcreteFunctionDefinition".equals(functionDefinition._classifierGenericType()._rawTypeCoreInstance().getName());
if (shouldSetTypeInferenceContext) {
state.newTypeInferenceContext(functionType);
}
functionType._parameters().forEach(var -> {
try {
variableContext.registerValue(var._name(), var);
} catch (VariableNameConflictException e) {
throw new PureCompilationException(functionDefinition.getSourceInformation(), e.getMessage());
}
GenericType propertyType = var._genericType();
// The property type may be null if it's a lambda expression...
if (propertyType != null) {
// We resolve because we want to fail fast if a given type is unknown...
org.finos.legend.pure.m3.navigation.generictype.GenericType.resolveGenericTypeUsingImports(propertyType, repository, processorSupport);
}
});
ListIterable<? extends ValueSpecification> expressions = functionDefinition._expressionSequence().toList();
if (expressions.isEmpty()) {
throw new PureCompilationException(functionDefinition.getSourceInformation(), "Function definition must contain at least one expression");
}
// The function is going to be processed again after inference
if (TypeInference.canProcessLambda(functionDefinition, state, processorSupport)) {
state.getObserver().shiftTab();
state.getObserver().startProcessingFunctionBody();
processExpressions(functionDefinition, expressions, matcher, state, processorSupport);
findReturnTypesForLambda(functionDefinition, functionType, processorSupport);
FunctionDefinitionValidator.validateFunctionReturnType(functionDefinition, functionType, processorSupport);
state.getObserver().finishedProcessingFunctionBody();
state.getObserver().unShiftTab();
state.addFunctionDefinition(functionDefinition);
}
if (shouldSetTypeInferenceContext) {
state.deleteTypeInferenceContext();
}
state.getVariableContext().buildAndRegister("return", functionType._returnType(), functionType._returnMultiplicity(), processorSupport);
RichIterable<? extends Constraint> constraints = functionDefinition._preConstraints();
if (constraints.notEmpty()) {
processConstraints(functionDefinition, constraints.toList(), matcher, state, processorSupport);
}
RichIterable<? extends Constraint> postConstraints = functionDefinition._postConstraints();
if (postConstraints.notEmpty()) {
processConstraints(functionDefinition, postConstraints.toList(), matcher, state, processorSupport);
}
state.getObserver().finishedProcessingFunction(functionType);
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint 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.coreinstance.meta.pure.metamodel.constraint.Constraint in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method classParser.
private CoreInstance classParser(ClassDefinitionContext ctx, ImportGroup importId, boolean addLines) throws PureParserException {
MutableList<Property<? extends CoreInstance, ?>> properties = Lists.mutable.empty();
MutableList<QualifiedProperty<? extends CoreInstance>> qualifiedProperties = Lists.mutable.empty();
MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
boolean isDirectSubTypeofAny = false;
MutableList<String> typeParameterNames = Lists.mutable.empty();
MutableList<Boolean> contravariants = Lists.mutable.empty();
MutableList<String> multiplicityParameterNames = Lists.mutable.empty();
ImportStubInstance ownerType;
ListIterable<CoreInstance> stereotypes = null;
ListIterable<TaggedValue> tags = null;
ClassInstance classInstance;
if (ctx.stereotypes() != null) {
stereotypes = this.stereotypes(ctx.stereotypes(), importId);
}
if (ctx.taggedValues() != null) {
tags = this.taggedValues(ctx.taggedValues(), importId);
}
this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
if (ctx.typeParametersWithContravarianceAndMultiplicityParameters() != null) {
this.typeParametersWithContravarianceAndMultiplicityParameters(ctx.typeParametersWithContravarianceAndMultiplicityParameters(), typeParameterNames, contravariants, multiplicityParameterNames);
}
if (ctx.projection() != null) {
return this.projectionParser(ctx, importId, addLines, stereotypes, tags);
} else {
if (ctx.EXTENDS() != null) {
for (TypeContext typeCtx : ctx.type()) {
superTypesGenericTypes.add(this.type(typeCtx, typeParameterNames, "", importId, addLines));
}
}
String className = ctx.qualifiedName().identifier().getText();
classInstance = ClassInstance.createPersistent(this.repository, className);
PackageInstance packageInstance = this.buildPackage(ctx.qualifiedName().packagePath());
classInstance._package(packageInstance);
packageInstance._childrenAdd(classInstance);
String fullName = this.getQualifiedNameString(ctx.qualifiedName());
ownerType = ImportStubInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.qualifiedName().identifier().getStart()), fullName, importId);
LambdaContext lambdaContext = new LambdaContext(fullName.replace("::", "_"));
MutableList<Constraint> constraints = this.constraints(classInstance, ctx.constraints(), importId, lambdaContext, addLines);
this.propertyParser(ctx.classBody().properties(), properties, qualifiedProperties, typeParameterNames, multiplicityParameterNames, ownerType, importId, 0);
classInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
if (superTypesGenericTypes.isEmpty()) {
isDirectSubTypeofAny = true;
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstance._rawTypeCoreInstance(this.processorSupport.package_getByUserPath(M3Paths.Any));
superTypesGenericTypes.add(genericTypeInstance);
}
GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
ClassInstance classType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Class);
classifierGT._rawTypeCoreInstance(classType);
GenericTypeInstance classifierGTTA = GenericTypeInstance.createPersistent(this.repository);
classifierGTTA._rawTypeCoreInstance(classInstance);
if (!typeParameterNames.isEmpty()) {
MutableList<TypeParameter> typeParameters = Lists.mutable.of();
MutableList<Pair<String, Boolean>> tps = typeParameterNames.zip(contravariants);
for (Pair<String, Boolean> typeParam : tps) {
TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParam.getOne());
tp._contravariant(typeParam.getTwo());
typeParameters.add(tp);
}
classInstance._typeParameters(typeParameters);
MutableList<GenericType> typeArgs = Lists.mutable.of();
for (String typeParamName : typeParameterNames) {
TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParamName);
GenericTypeInstance gt = GenericTypeInstance.createPersistent(this.repository);
gt._typeParameter(tp);
typeArgs.add(gt);
}
classifierGTTA._typeArguments(typeArgs);
}
if (!multiplicityParameterNames.isEmpty()) {
MutableList<Multiplicity> multParameters = Lists.mutable.of();
for (String multiplicityParam : multiplicityParameterNames) {
MultiplicityInstance mult = MultiplicityInstance.createPersistent(this.repository, null, null);
mult._multiplicityParameter(multiplicityParam);
multParameters.add(mult);
}
classInstance._multiplicityParameters(this.processMultiplicityParametersInstance(multiplicityParameterNames));
classifierGTTA._multiplicityArguments(multParameters);
}
classifierGT._typeArguments(Lists.mutable.<GenericType>of(classifierGTTA));
classInstance._classifierGenericType(classifierGT);
if (properties.notEmpty()) {
classInstance._properties(properties);
}
if (qualifiedProperties.notEmpty()) {
classInstance._qualifiedProperties(qualifiedProperties);
}
classInstance._name(ctx.qualifiedName().identifier().getText());
if (stereotypes != null) {
classInstance._stereotypesCoreInstance(stereotypes);
}
if (tags != null) {
classInstance._taggedValues(tags);
}
if (constraints.notEmpty()) {
classInstance._constraints(constraints);
}
MutableList<Generalization> generalizations = Lists.mutable.empty();
for (GenericType superType : superTypesGenericTypes) {
GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, classInstance);
generalizations.add(generalizationInstance);
}
classInstance._generalizations(generalizations);
if (isDirectSubTypeofAny) {
MilestoningClassProcessor.addMilestoningProperty(classInstance, this.context, this.processorSupport, this.repository);
}
return classInstance;
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint in project legend-pure by finos.
the class TestBinaryModelSourceSerializer method testPlatformSerialization.
@Test
public void testPlatformSerialization() {
MutableListMultimap<String, String> importGroupsByBaseName = Multimaps.mutable.list.empty();
CoreInstance systemImports = this.runtime.getCoreInstance("system::imports");
if (systemImports != null) {
for (CoreInstance importGroup : systemImports.getValueForMetaPropertyToMany(M3Properties.children)) {
importGroupsByBaseName.put(Imports.getImportGroupBaseName(importGroup), "system::imports::" + importGroup.getName());
}
}
for (Source source : this.runtime.getSourceRegistry().getSources()) {
MutableSet<String> expectedInstances = this.getInstancePaths(source.getNewInstances());
expectedInstances.addAllIterable(importGroupsByBaseName.get(Source.formatForImportGroupId(source.getId())));
if ("/platform/pure/m3.pure".equals(source.getId())) {
expectedInstances.addAllIterable(Lists.mutable.with("meta", "meta::pure", "meta::pure::functions", "meta::pure::functions::lang", "meta::pure::metamodel", "meta::pure::metamodel::constraint", "meta::pure::metamodel::extension", "meta::pure::metamodel::function", "meta::pure::metamodel::function::property", "meta::pure::metamodel::import", "meta::pure::metamodel::multiplicity", "meta::pure::metamodel::relationship", "meta::pure::metamodel::treepath", "meta::pure::metamodel::type", "meta::pure::metamodel::type::generics", "meta::pure::metamodel::valuespecification", "meta::pure::router", "meta::pure::tools", "system", "system::imports"));
}
Pair<SourceSerializationResult, byte[]> serializationResult = this.serializeSource(source);
SourceSerializationResult result = serializationResult.getOne();
byte[] serialized = serializationResult.getTwo();
this.assertSetsEqual(expectedInstances, result.getSerializedInstances());
this.assertExternalReferencePathsResolvable(result.getExternalReferences(), source.getId());
SourceDeserializationResult deserializationResult = BinaryModelSourceDeserializer.readIndexes(BinaryReaders.newBinaryReader(serialized));
this.assertSetsEqual(expectedInstances, deserializationResult.getInstances().toSet());
this.assertSetsEqual(result.getExternalReferences(), deserializationResult.getExternalReferences());
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint in project legend-pure by finos.
the class TestBinaryModelSourceSerializer method testM3Serialization.
@Test
public void testM3Serialization() {
Source source = this.runtime.getSourceById("/platform/pure/m3.pure");
SetIterable<String> expectedInstances = this.collectInstancePaths(source.getNewInstances(), Sets.mutable.with("meta", "meta::pure", "meta::pure::functions", "meta::pure::functions::lang", "meta::pure::metamodel", "meta::pure::metamodel::extension", "meta::pure::metamodel::function", "meta::pure::metamodel::function::property", "meta::pure::metamodel::import", "meta::pure::metamodel::multiplicity", "meta::pure::metamodel::relationship", "meta::pure::metamodel::treepath", "meta::pure::metamodel::type", "meta::pure::metamodel::type::generics", "meta::pure::metamodel::constraint", "meta::pure::metamodel::valuespecification", "meta::pure::router", "meta::pure::tools", "system", "system::imports"));
SetIterable<String> expectedReferences = Sets.mutable.with();
Pair<SourceSerializationResult, byte[]> serializationResult = this.serializeSource(source);
SourceSerializationResult result = serializationResult.getOne();
byte[] serialized = serializationResult.getTwo();
this.assertSetsEqual(expectedInstances, result.getSerializedInstances());
this.assertSetsEqual(expectedReferences, result.getExternalReferences());
this.assertExternalReferencePathsResolvable(result.getExternalReferences());
SourceDeserializationResult deserializationResult = BinaryModelSourceDeserializer.readIndexes(BinaryReaders.newBinaryReader(serialized));
this.assertSetsEqual(expectedInstances, deserializationResult.getInstances().toSet());
this.assertSetsEqual(expectedReferences, deserializationResult.getExternalReferences());
}
Aggregations