Search in sources :

Example 1 with ConcreteFunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition 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);
}
Also used : ConcreteFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) URLPatternLibrary(org.finos.legend.pure.m3.serialization.runtime.pattern.URLPatternLibrary) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) VariableContext(org.finos.legend.pure.m3.compiler.postprocessing.VariableContext) VariableNameConflictException(org.finos.legend.pure.m3.compiler.postprocessing.VariableContext.VariableNameConflictException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 2 with ConcreteFunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition in project legend-pure by finos.

the class TestMultiplicity method testSingleLiteralMultiplicity.

@Test
public void testSingleLiteralMultiplicity() {
    compileTestSource("fromString.pure", "function test::testFn():Any[*]\n" + "{\n" + "  1\n" + "}");
    ConcreteFunctionDefinition testFn = (ConcreteFunctionDefinition) this.runtime.getFunction("test::testFn():Any[*]");
    Assert.assertNotNull(testFn);
    ListIterable<ValueSpecification> expressions = (ListIterable<ValueSpecification>) testFn._expressionSequence();
    Verify.assertSize(1, expressions);
    ValueSpecification expression = expressions.get(0);
    CoreInstance multiplicity = expression._multiplicity();
    Assert.assertEquals(1, Multiplicity.multiplicityLowerBoundToInt(multiplicity));
    Assert.assertEquals(1, Multiplicity.multiplicityUpperBoundToInt(multiplicity));
}
Also used : ConcreteFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition) ListIterable(org.eclipse.collections.api.list.ListIterable) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Test(org.junit.Test)

Example 3 with ConcreteFunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition in project legend-pure by finos.

the class TestMultiplicity method testSingleFunctionExpressionMultiplicity.

@Test
public void testSingleFunctionExpressionMultiplicity() {
    compileTestSource("fromString.pure", "function test::testFn():Any[*]\n" + "{\n" + "  [1, 2, 3]->first();\n" + "}");
    ConcreteFunctionDefinition testFn = (ConcreteFunctionDefinition) this.runtime.getFunction("test::testFn():Any[*]");
    Assert.assertNotNull(testFn);
    ListIterable<ValueSpecification> expressions = (ListIterable<ValueSpecification>) testFn._expressionSequence();
    Verify.assertSize(1, expressions);
    ValueSpecification expression = expressions.get(0);
    CoreInstance multiplicity = expression._multiplicity();
    Assert.assertEquals(0, Multiplicity.multiplicityLowerBoundToInt(multiplicity));
    Assert.assertEquals(1, Multiplicity.multiplicityUpperBoundToInt(multiplicity));
}
Also used : ConcreteFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition) ListIterable(org.eclipse.collections.api.list.ListIterable) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Test(org.junit.Test)

Example 4 with ConcreteFunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition in project legend-pure by finos.

the class TestMultiplicity method testSingleFunctionExpressionCollectionMultiplicity.

@Test
public void testSingleFunctionExpressionCollectionMultiplicity() {
    compileTestSource("fromString.pure", "function test::testFn():Any[*]\n" + "{\n" + "  [[1, 2, 3]->first()];\n" + "}");
    ConcreteFunctionDefinition testFn = (ConcreteFunctionDefinition) this.runtime.getFunction("test::testFn():Any[*]");
    Assert.assertNotNull(testFn);
    ListIterable<ValueSpecification> expressions = (ListIterable<ValueSpecification>) testFn._expressionSequence();
    Verify.assertSize(1, expressions);
    ValueSpecification expression = expressions.get(0);
    CoreInstance multiplicity = expression._multiplicity();
    Assert.assertEquals(0, Multiplicity.multiplicityLowerBoundToInt(multiplicity));
    Assert.assertEquals(1, Multiplicity.multiplicityUpperBoundToInt(multiplicity));
}
Also used : ConcreteFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition) ListIterable(org.eclipse.collections.api.list.ListIterable) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Test(org.junit.Test)

Example 5 with ConcreteFunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition in project legend-pure by finos.

the class TestMultiplicity method testLiteralCollectionMultiplicity.

@Test
public void testLiteralCollectionMultiplicity() {
    compileTestSource("fromString.pure", "function test::testFn():Any[*]\n" + "{\n" + "  [1, 2, 3]\n" + "}");
    ConcreteFunctionDefinition testFn = (ConcreteFunctionDefinition) this.runtime.getFunction("test::testFn():Any[*]");
    Assert.assertNotNull(testFn);
    ListIterable<ValueSpecification> expressions = (ListIterable<ValueSpecification>) testFn._expressionSequence();
    Verify.assertSize(1, expressions);
    ValueSpecification expression = expressions.get(0);
    CoreInstance multiplicity = expression._multiplicity();
    Assert.assertEquals(3, Multiplicity.multiplicityLowerBoundToInt(multiplicity));
    Assert.assertEquals(3, Multiplicity.multiplicityUpperBoundToInt(multiplicity));
}
Also used : ConcreteFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition) ListIterable(org.eclipse.collections.api.list.ListIterable) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Test(org.junit.Test)

Aggregations

ConcreteFunctionDefinition (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition)13 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)9 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)8 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)7 ListIterable (org.eclipse.collections.api.list.ListIterable)6 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)6 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)6 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)6 Test (org.junit.Test)6 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)4 QualifiedProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)4 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)4 RichIterable (org.eclipse.collections.api.RichIterable)3 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)3 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)3 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)3 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)3 PureLambdaFunction (org.finos.legend.pure.runtime.java.compiled.generation.processors.support.function.PureLambdaFunction)3 JavaMethodWithParamsSharedPureFunction (org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction)3 JSONObject (org.json.simple.JSONObject)3