use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification in project legend-pure by finos.
the class TypeInference method processParamTypesOfLambdaUsedAsAFunctionExpressionParamValue.
public static boolean processParamTypesOfLambdaUsedAsAFunctionExpressionParamValue(ValueSpecification instanceValueContainer, LambdaFunction<?> lambdaFunction, VariableExpression templateToMatchLambdaTo, Matcher matcher, ProcessorState state, ModelRepository repository, ProcessorSupport processorSupport) throws PureCompilationException {
GenericType templateGenericType = templateToMatchLambdaTo._genericType();
Type templateFunctionType = templateGenericType._typeArguments().notEmpty() ? (Type) ImportStub.withImportStubByPass(templateGenericType._typeArguments().getFirst()._rawTypeCoreInstance(), processorSupport) : null;
FunctionType lambdaFunctionType = (FunctionType) ImportStub.withImportStubByPass(lambdaFunction._classifierGenericType()._typeArguments().getFirst()._rawTypeCoreInstance(), processorSupport);
if (org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(templateGenericType) && org.finos.legend.pure.m3.navigation.type.Type.subTypeOf(ImportStub.withImportStubByPass(templateGenericType._rawTypeCoreInstance(), processorSupport), processorSupport.package_getByUserPath(M3Paths.Function), processorSupport)) {
ListIterable<? extends VariableExpression> parameters = ListHelper.wrapListIterable(lambdaFunctionType._parameters());
for (int j = 0; j < parameters.size(); j++) {
VariableExpression param = parameters.get(j);
if (param._genericType() == null) {
if (org.finos.legend.pure.m3.navigation.type.Type.isBottomType(templateFunctionType, processorSupport) || org.finos.legend.pure.m3.navigation.type.Type.isTopType(templateFunctionType, processorSupport)) {
throw new PureCompilationException(lambdaFunction.getSourceInformation(), "Can't infer the parameters' types for the lambda. Please specify it in the signature.");
}
VariableExpression templateParam = ListHelper.wrapListIterable(((FunctionType) Objects.requireNonNull(templateFunctionType))._parameters()).get(j);
CoreInstance genericType = org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(templateParam._genericType(), state.getTypeInferenceContext().getTypeParameterToGenericType(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity(), processorSupport);
if (state.getTypeInferenceContext().isTypeParameterResolved(genericType)) {
genericType = state.getTypeInferenceContext().resolve(genericType);
} else {
return true;
}
CoreInstance multiplicity = org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.makeMultiplicityAsConcreteAsPossible(templateParam._multiplicity(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity());
param._genericType((GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericTypeAsInferredGenericType(genericType, param.getSourceInformation(), processorSupport));
param._multiplicity((Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.copyMultiplicity(multiplicity, param.getSourceInformation(), processorSupport));
}
}
state.pushVariableContext();
FunctionDefinitionProcessor.process(lambdaFunction, state, matcher, repository);
LambdaFunctionProcessor.process(lambdaFunction, state, matcher, repository);
state.popVariableContext();
} else {
throw new PureCompilationException(lambdaFunction.getSourceInformation(), "Can't infer the parameters' types for the lambda. Please specify it in the signature.");
}
instanceValueContainer._genericTypeRemove();
InstanceValueProcessor.updateInstanceValue(instanceValueContainer, processorSupport);
return false;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification in project legend-pure by finos.
the class AssociationProcessor method getPropertyNameForLeftSideOfQualifiedPropertyFilter.
private static String getPropertyNameForLeftSideOfQualifiedPropertyFilter(Association association, QualifiedProperty qualifiedProperty, ValueSpecification instance, Context context, ProcessorSupport processorSupport) {
String functionName = instance instanceof FunctionExpression ? ((FunctionExpression) instance)._functionName() : null;
String propertyNameForLeftSideOfQualifiedPropertyFilter;
if ("filter".equals(functionName)) {
ValueSpecification leftSideOfFilter = ((FunctionExpression) instance)._parametersValues().toList().getFirst();
CoreInstance propertyName = leftSideOfFilter instanceof FunctionExpression ? ((FunctionExpression) leftSideOfFilter)._propertyName()._valuesCoreInstance().toList().getFirst() : null;
ValueSpecification variableExpression = leftSideOfFilter instanceof FunctionExpression ? ((FunctionExpression) leftSideOfFilter)._parametersValues().toList().getFirst() : null;
String variableExpressionName = variableExpression instanceof VariableExpression ? ((VariableExpression) variableExpression)._name() : null;
if (!"this".equals(variableExpressionName)) {
throw new PureCompilationException(instance.getSourceInformation(), validQualifiedPropertyInAssociationMsg() + qualifiedPropertyCompileErrorMsgPrefix(association, qualifiedProperty) + " left side of filter should refer to '$this' not '" + variableExpressionName + "'");
}
propertyNameForLeftSideOfQualifiedPropertyFilter = Objects.requireNonNull(propertyName).getName();
} else {
ValueSpecification firstParamValue = instance instanceof FunctionExpression ? ((FunctionExpression) instance)._parametersValues().toList().getFirst() : null;
if (firstParamValue != null) {
propertyNameForLeftSideOfQualifiedPropertyFilter = getPropertyNameForLeftSideOfQualifiedPropertyFilter(association, qualifiedProperty, firstParamValue, context, processorSupport);
} else {
throw new PureCompilationException(qualifiedProperty.getSourceInformation(), validQualifiedPropertyInAssociationMsg() + qualifiedPropertyCompileErrorMsgPrefix(association, qualifiedProperty) + " does not use the 'filter' function");
}
}
return propertyNameForLeftSideOfQualifiedPropertyFilter;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification in project legend-pure by finos.
the class FunctionDefinitionProcessor method processExpression.
private static void processExpression(FunctionDefinition<?> functionDefinition, Matcher matcher, ProcessorState processorState, ProcessorSupport processorSupport, int i, ValueSpecification expression) {
processorState.resetVariables();
PostProcessor.processElement(matcher, expression, processorState, processorSupport);
if (expression._usageContext() == null) {
ExpressionSequenceValueSpecificationContext usageContext = (ExpressionSequenceValueSpecificationContext) processorSupport.newAnonymousCoreInstance(null, M3Paths.ExpressionSequenceValueSpecificationContext);
usageContext._offset(i);
usageContext._functionDefinition(functionDefinition);
expression._usageContext(usageContext);
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification in project legend-pure by finos.
the class CompiledProcessorSupport method valueSpecification_instanceOf.
@Override
public boolean valueSpecification_instanceOf(CoreInstance valueSpecification, String type) {
try {
ValueSpecification valueSpec = ValueSpecificationCoreInstanceWrapper.toValueSpecification(valueSpecification);
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type rawType = valueSpec._genericType()._rawType();
Class<?> valueSpecType = (rawType instanceof Enumeration) ? this.globalClassLoader.loadClass(FullJavaPaths.Enum) : this.pureClassToJavaClass(fullName(rawType));
Class<?> typeCl = this.pureClassToJavaClass(type);
return typeCl.isAssignableFrom(valueSpecType);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification in project legend-pure by finos.
the class FunctionExpressionProcessor method firstPassTypeInference.
private boolean firstPassTypeInference(FunctionExpression functionExpression, ListIterable<? extends ValueSpecification> parametersValues, ProcessorState processorState, Matcher matcher, ModelRepository repository, Context context, ProcessorSupport processorSupport) throws PureCompilationException {
TypeInferenceObserver observer = processorState.getObserver();
int i = 0;
observer.startProcessingFunctionExpression(functionExpression);
boolean inferenceSuccess = true;
observer.shiftTab();
observer.startFirstPassParametersProcessing();
observer.shiftTab();
observer.shiftTab();
org.eclipse.collections.api.block.function.Function<CoreInstance, Void> processElement = coreInstance -> {
PostProcessor.processElement(matcher, coreInstance, processorState, processorSupport);
return null;
};
for (ValueSpecification boundVariable : parametersValues) {
observer.processingParameter(functionExpression, i, boundVariable);
observer.shiftTab();
MilestoningDatesPropagationFunctions.possiblyExecuteInNewMilestoningDateContext(functionExpression, boundVariable, processElement, processorState, repository, context, processorSupport);
boolean success = this.isInferenceSuccess(boundVariable, processorSupport);
observer.inferenceResult(success);
inferenceSuccess = inferenceSuccess && success;
addTraceForParameterValue(functionExpression, i, boundVariable, processorSupport);
i++;
observer.unShiftTab();
}
observer.unShiftTab();
observer.unShiftTab();
return inferenceSuccess;
}
Aggregations