Search in sources :

Example 1 with Function

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

the class TypeInference method storeInferredTypeParametersInFunctionExpression.

public static void storeInferredTypeParametersInFunctionExpression(FunctionExpression functionExpression, ProcessorState state, ProcessorSupport processorSupport, Function<?> foundFunction) throws PureCompilationException {
    // Store the inferred params in the FunctionExpression
    if (!(foundFunction instanceof QualifiedProperty)) {
        TypeInferenceContext typeInferenceContext = state.getTypeInferenceContext();
        FunctionType functionType = (FunctionType) processorSupport.function_getFunctionType(foundFunction);
        functionType._typeParameters().forEach(typeParameter -> {
            CoreInstance value = typeInferenceContext.getTypeParameterValue(typeParameter._name());
            if (value != null) {
                functionExpression._resolvedTypeParametersAdd((GenericType) value);
            } else if (typeInferenceContext.getParent() == null) {
                StringBuilder builder = new StringBuilder("The type parameter ").append(typeParameter._name()).append(" was not resolved (").append(foundFunction._functionName()).append(" / ");
                org.finos.legend.pure.m3.navigation.function.FunctionType.print(builder, functionType, processorSupport).append(")!");
                throw new PureCompilationException(functionExpression.getSourceInformation(), builder.toString());
            }
        });
        functionType._multiplicityParameters().forEach(multiplicityParameter -> {
            String parameterName = multiplicityParameter._valuesCoreInstance().getFirst().getName();
            CoreInstance value = typeInferenceContext.getMultiplicityParameterValue(parameterName);
            if (value != null) {
                functionExpression._resolvedMultiplicityParametersAdd((Multiplicity) value);
            } else {
                throw new PureCompilationException(functionExpression.getSourceInformation(), "The multiplicity parameter " + parameterName + " was not resolved!");
            }
        });
    }
}
Also used : FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 2 with Function

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function 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;
}
Also used : FunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.FunctionExpression) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 3 with Function

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

the class ConcreteFunctionDefinitionNameProcessor method process.

public static void process(Function<?> function, ModelRepository repository, ProcessorSupport processorSupport) throws PureCompilationException {
    Package parent = function._package();
    if (parent != null) {
        // Make sure we have a unique name for overloaded functions.
        String signature = getSignatureAndResolveImports(function, repository, processorSupport);
        parent._childrenRemove(function);
        function.setName(signature);
        parent._childrenAdd(function);
        if (function._name() == null) {
            function._name(signature);
        }
        if (parent._children().count(c -> signature.equals(c.getName())) > 1) {
            ListIterable<SourceInformation> sourceInfos = parent._children().collectIf(c -> signature.equals(c.getName()), CoreInstance::getSourceInformation, Lists.mutable.empty()).sortThis();
            String pkg = PackageableElement.getUserPathForPackageableElement(parent);
            if (M3Paths.Root.equals(pkg)) {
                pkg = "::";
            }
            StringBuilder message = new StringBuilder("The function '").append(signature).append("' is defined more than once in the package '").append(pkg).append("' at: ");
            boolean first = true;
            for (SourceInformation sourceInfo : sourceInfos) {
                if (first) {
                    first = false;
                } else {
                    message.append(", ");
                }
                message.append(sourceInfo.getSourceId()).append(" (line:").append(sourceInfo.getLine()).append(" column:").append(sourceInfo.getColumn()).append(')');
            }
            throw new PureCompilationException(function.getSourceInformation(), message.toString());
        }
    }
}
Also used : Multiplicity(org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) PackageableElement(org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement) ModelRepository(org.finos.legend.pure.m4.ModelRepository) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) Lists(org.eclipse.collections.api.factory.Lists) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) MutableList(org.eclipse.collections.api.list.MutableList) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) M3Paths(org.finos.legend.pure.m3.navigation.M3Paths) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) Function(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) ImportStub(org.finos.legend.pure.m3.navigation.importstub.ImportStub) Package(org.finos.legend.pure.m3.coreinstance.Package) ListIterable(org.eclipse.collections.api.list.ListIterable) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) Package(org.finos.legend.pure.m3.coreinstance.Package) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 4 with Function

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

the class TestSimpleGrammar method testMappingIncludes.

@Test
public void testMappingIncludes() {
    String pureCode = "import other::*;\n" + "\n" + "Class other::Person\n" + "{\n" + "    name:String[1];\n" + "    firm:Firm[1];\n" + "}\n" + "Class other::Firm\n" + "{\n" + "    legalName:String[1];\n" + "    employees:Person[1];\n" + "}\n" + "###Relational\n" + "Database mapping::db(\n" + "   Table employeeFirmDenormTable\n" + "   (\n" + "    id INT PRIMARY KEY,\n" + "    name VARCHAR(200),\n" + "    firmId INT,\n" + "    legalName VARCHAR(200)\n" + "   )\n" + "   Join firmJoin(employeeFirmDenormTable.firmId = {target}.firmId)\n" + ")\n" + "###Mapping\n" + "import other::*;\n" + "import mapping::*;\n" + "import mappingPackage::*;\n" + "Mapping mappingPackage::subMapping1\n" + "(\n" + "    Person: Relational\n" + "    {\n" + "        name : [db]employeeFirmDenormTable.name\n" + "    }\n" + ")\n" + "Mapping mappingPackage::subMapping2\n" + "(\n" + "    Firm: Relational\n" + "    {\n" + "        legalName : [db]employeeFirmDenormTable.legalName\n" + "    }\n" + ")\n" + "Mapping mappingPackage::myMapping\n" + "(\n" + "    include mappingPackage::subMapping1\n" + "    include subMapping2\n" + ")\n";
    Loader.parseM3(pureCode, this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
    this.runtime.compile();
    CoreInstance mapping = this.graphWalker.getMapping("mappingPackage::myMapping");
    Assert.assertNotNull(mapping);
    ListIterable<? extends CoreInstance> includes = this.graphWalker.getMany(mapping, M3Properties.includes);
    Assert.assertEquals(2, includes.size());
    MutableList<String> includedMappingPaths = includes.collect(new Function<CoreInstance, String>() {

        @Override
        public String valueOf(CoreInstance include) {
            return PackageableElement.getUserPathForPackageableElement(graphWalker.getOne(include, M3Properties.included));
        }
    }, FastList.<String>newList(2));
    Verify.assertListsEqual(Lists.fixedSize.with("mappingPackage::subMapping1", "mappingPackage::subMapping2"), includedMappingPaths);
}
Also used : EnumerationMappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser) MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser) ParserLibrary(org.finos.legend.pure.m3.serialization.grammar.ParserLibrary) M3AntlrParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) RelationalParser(org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser) Test(org.junit.Test)

Example 5 with Function

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

the class FunctionExpressionMatcher method getFunctionsWithMatchingName.

private static RichIterable<Function<?>> getFunctionsWithMatchingName(String functionName, ListIterable<String> functionPackage, FunctionExpression functionExpression, ProcessorSupport processorSupport) {
    SetIterable<org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement> packages = getValidPackages(functionPackage, functionExpression, processorSupport);
    MutableList<Function<?>> functions = Lists.mutable.empty();
    for (CoreInstance function : processorSupport.function_getFunctionsForName(functionName)) {
        if (packages.contains(((Function<?>) function)._package())) {
            functions.add((Function<?>) function);
        }
    }
    return functions;
}
Also used : Function(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function) PackageableElement(org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Aggregations

CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)119 Test (org.junit.Test)114 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)47 RuntimeTestScriptBuilder (org.finos.legend.pure.m3.RuntimeTestScriptBuilder)41 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)41 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)33 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)32 RichIterable (org.eclipse.collections.api.RichIterable)31 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)31 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)30 ListIterable (org.eclipse.collections.api.list.ListIterable)29 MutableList (org.eclipse.collections.api.list.MutableList)28 InstanceValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue)24 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)24 Function (org.eclipse.collections.api.block.function.Function)23 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)22 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)21 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)20 Pair (org.eclipse.collections.api.tuple.Pair)16 Function (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function)16