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!");
}
});
}
}
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;
}
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());
}
}
}
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);
}
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;
}
Aggregations