Search in sources :

Example 6 with org.finos.legend.pure.m3.navigation._class._Class

use of org.finos.legend.pure.m3.navigation._class._Class in project legend-pure by finos.

the class AssociationProcessor method updateClassifierGenericTypeForQualifiedPropertiesThisVarExprParams.

private static void updateClassifierGenericTypeForQualifiedPropertiesThisVarExprParams(Association association, QualifiedProperty qualifiedProperty, final Class qualifiedPropertyReturnType, ListIterable<? extends Property> assnProperties, final Context context, final ProcessorSupport processorSupport) {
    validateQualifiedPropertyLeftSideOfFilterByPropertyName(association, qualifiedProperty, qualifiedPropertyReturnType, assnProperties, context, processorSupport);
    Property leftSideOfFilterProperty = getQualifiedPropertiesFilterLeftSideParam(qualifiedPropertyReturnType, assnProperties, processorSupport);
    Property leftSideOfFilterOtherProperty = assnProperties.detect(Predicates.notEqual(leftSideOfFilterProperty));
    GenericType leftSideOfFilterOtherPropertyGenericType = leftSideOfFilterOtherProperty._genericType();
    FunctionType functionType = (FunctionType) qualifiedProperty._classifierGenericType()._typeArguments().toList().getFirst()._rawTypeCoreInstance();
    Iterable<? extends VariableExpression> functionTypeParams = functionType._parameters();
    for (VariableExpression functionTypeParam : functionTypeParams) {
        if (functionTypeParam != null && "this".equals(functionTypeParam._name())) {
            GenericType genericTypeCopy = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericType(leftSideOfFilterOtherPropertyGenericType, processorSupport);
            functionTypeParam._genericType(genericTypeCopy);
            context.update(functionTypeParam);
            if (functionTypeParam.hasBeenValidated()) {
                functionTypeParam.markNotValidated();
                context.update(functionTypeParam);
            }
        }
    }
}
Also used : GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) AbstractProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)

Example 7 with org.finos.legend.pure.m3.navigation._class._Class

use of org.finos.legend.pure.m3.navigation._class._Class in project legend-pure by finos.

the class M3ToJavaGenerator method createWrapperClass.

private String createWrapperClass(final CoreInstance instance, String javaPackage, MutableSet<CoreInstance> properties, MutableSet<CoreInstance> propertiesFromAssociations, MutableSet<CoreInstance> qualifiedProperties, Imports imports, MutableMap<String, CoreInstance> propertyOwners) {
    imports.addImports(Lists.mutable.of("org.finos.legend.pure.m4.coreinstance.simple.ValueHolder", "org.finos.legend.pure.m3.coreinstance.helper.PrimitiveHelper"));
    final String interfaceName = getInterfaceName(instance);
    String wrapperName = getWrapperName(instance);
    String typeParamsWithExtendsCoreInstance = getTypeParams(instance, true);
    final CoreInstance classGenericType = getClassGenericType(instance);
    imports.setThisClassName(wrapperName);
    PartitionIterable<CoreInstance> partition = properties.partition(M3ToJavaGenerator::isToOne);
    RichIterable<CoreInstance> toOneProperties = partition.getSelected();
    RichIterable<CoreInstance> toManyProperties = partition.getRejected();
    RichIterable<CoreInstance> mandatoryToOneProps = toOneProperties.select(M3ToJavaGenerator::isMandatoryProperty);
    RichIterable<Pair<String, String>> typesForMandatoryProps = buildMandatoryProperties(classGenericType, mandatoryToOneProps, imports).toSortedSetBy(Pair::getOne);
    MutableList<String> mandatoryTypes = Lists.mutable.of();
    MutableList<String> mandatoryProps = Lists.mutable.of();
    for (Pair<String, String> pair : typesForMandatoryProps) {
        mandatoryTypes.add(pair.getTwo() + " " + pair.getOne());
        mandatoryProps.add(pair.getOne());
    }
    final String maybeFullyQualifiedInterfaceName = (imports.shouldFullyQualify(javaPackage + "." + interfaceName) ? javaPackage + "." + interfaceName : interfaceName);
    String maybeFullyQualifiedInterfaceNameWithTypeParams = maybeFullyQualifiedInterfaceName + typeParamsWithExtendsCoreInstance;
    String systemPathForPackageableElement = getUserObjectPathForPackageableElement(instance, true).makeString("::");
    String value = "\n" + "package " + javaPackage + ";\n" + "\n" + "import org.eclipse.collections.api.RichIterable;\n" + "import org.eclipse.collections.api.block.predicate.Predicate;\n" + "import org.eclipse.collections.api.list.ListIterable;\n" + "import org.eclipse.collections.api.list.MutableList;\n" + "import org.eclipse.collections.api.set.SetIterable;\n" + "import org.eclipse.collections.impl.factory.Lists;\n" + "import org.eclipse.collections.impl.factory.Sets;\n" + "import org.finos.legend.pure.m3.coreinstance.BaseCoreInstance;\n" + "import org.finos.legend.pure.m4.coreinstance.AbstractCoreInstanceWrapper;\n" + "import org.finos.legend.pure.m3.coreinstance.BaseM3CoreInstanceFactory;\n" + "import org.finos.legend.pure.m4.coreinstance.AbstractCoreInstance;\n" + imports.toImportString() + "\n" + getPrimitiveImports() + "\n" + "import org.finos.legend.pure.m4.coreinstance.CoreInstance;\n" + "\n" + "public class " + wrapperName + " extends AbstractCoreInstanceWrapper implements " + maybeFullyQualifiedInterfaceNameWithTypeParams + "\n" + "{\n" + "    public static final CoreInstanceFunction FROM_CORE_INSTANCE_FN = new CoreInstanceFunction();\n" + "    public " + wrapperName + "(CoreInstance instance)\n" + "    {\n" + "        super(instance);\n" + "    }\n" + "\n" + toOneProperties.collect(property -> {
        CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
        return createWrapperPropertyGetterToOne(interfaceName, property, propertyReturnGenericType, imports);
    }).makeString("") + toManyProperties.collect(property -> {
        CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
        return createWrapperPropertyGetterToMany(interfaceName, property, propertyReturnGenericType, imports);
    }).makeString("") + "\n" + toOneProperties.collect(property -> {
        CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
        String sub = getSubstituteType(property, propertyReturnGenericType);
        return sub == null ? "" : "    public " + maybeFullyQualifiedInterfaceName + " " + getUnifiedMethodName(property) + "(" + sub + " value)\n" + "    {\n" + "        instance.setKeyValues(" + createPropertyKeyNameReference(property.getName(), propertyOwners.get(property.getName()), instance) + ", Lists.immutable.<CoreInstance>with((CoreInstance)value));\n" + "        return this;\n" + "    }\n" + "\n";
    }).makeString("") + properties.collect(property -> {
        CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
        return createWrapperPropertySetter(property, propertyReturnGenericType, imports, maybeFullyQualifiedInterfaceName, propertyOwners, instance);
    }).makeString("") + propertiesFromAssociations.collect(property -> {
        CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
        return createPropertyReverse(property, propertyReturnGenericType, imports, false, true) + createPropertyReverse(property, propertyReturnGenericType, imports, true, true);
    }).makeString("") + qualifiedProperties.collect(property -> {
        CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
        return createWrapperQualifiedPropertyGetter(property, propertyReturnGenericType, imports);
    }).makeString("") + "\n" + "    public static " + maybeFullyQualifiedInterfaceName + " to" + interfaceName + "(CoreInstance instance)\n" + "    {\n" + "        if (instance == null) { return null; }\n" + "        return " + maybeFullyQualifiedInterfaceName + ".class.isInstance(instance) ? (" + maybeFullyQualifiedInterfaceName + ") instance : new " + wrapperName + "(instance);\n" + "    }\n" + createWrapperStaticConversionFunction(interfaceName, wrapperName, maybeFullyQualifiedInterfaceNameWithTypeParams, getTypeParams(instance, false)) + createWrapperClassCopyMethod(instance, maybeFullyQualifiedInterfaceName) + createClassPackageableElement(systemPathForPackageableElement) + "}\n";
    return value;
}
Also used : ArrayAdapter(org.eclipse.collections.impl.list.fixed.ArrayAdapter) Lists(org.eclipse.collections.api.factory.Lists) SetIterable(org.eclipse.collections.api.set.SetIterable) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) Maps(org.eclipse.collections.api.factory.Maps) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) Tuples(org.eclipse.collections.impl.tuple.Tuples) Pair(org.eclipse.collections.api.tuple.Pair) StringIterate(org.eclipse.collections.impl.utility.StringIterate) Path(java.nio.file.Path) Sets(org.eclipse.collections.api.factory.Sets) JavaTools(org.finos.legend.pure.m3.tools.JavaTools) ArrayIterate(org.eclipse.collections.impl.utility.ArrayIterate) ModelRepository(org.finos.legend.pure.m4.ModelRepository) Files(java.nio.file.Files) Collection(java.util.Collection) IOException(java.io.IOException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Paths(java.nio.file.Paths) ListIterable(org.eclipse.collections.api.list.ListIterable) MapIterable(org.eclipse.collections.api.map.MapIterable) PartitionIterable(org.eclipse.collections.api.partition.PartitionIterable) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Pair(org.eclipse.collections.api.tuple.Pair)

Example 8 with org.finos.legend.pure.m3.navigation._class._Class

use of org.finos.legend.pure.m3.navigation._class._Class 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)

Example 9 with org.finos.legend.pure.m3.navigation._class._Class

use of org.finos.legend.pure.m3.navigation._class._Class 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);
    }
}
Also used : Enumeration(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enumeration) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)

Example 10 with org.finos.legend.pure.m3.navigation._class._Class

use of org.finos.legend.pure.m3.navigation._class._Class in project legend-pure by finos.

the class FunctionExecutionCompiled method executeFunction.

private Object executeFunction(CoreInstance functionDefinition, ListIterable<? extends CoreInstance> arguments, CompiledExecutionSupport executionSupport) {
    ProcessorSupport processorSupport = new M3ProcessorSupport(this.context, this.repository);
    Object result;
    try {
        result = this.executeFunction(functionDefinition, arguments, executionSupport, this.javaCompilerEventHandler.getJavaCompiler().getClassLoader(), processorSupport);
    } catch (PureException pe) {
        // Rethrow as is to keep the original error
        throw pe;
    } catch (Exception e) {
        StringBuilder builder = new StringBuilder("Error executing ");
        try {
            org.finos.legend.pure.m3.navigation.function.Function.print(builder, functionDefinition, processorSupport);
        } catch (Exception ignore) {
            builder = new StringBuilder("Error executing ");
            builder.append(functionDefinition);
        }
        builder.append(". ");
        if (e.getMessage() != null) {
            builder.append(e.getMessage());
        }
        throw new RuntimeException(builder.toString(), e);
    }
    if (result == null) {
        result = Lists.immutable.empty();
    }
    return result;
}
Also used : PureException(org.finos.legend.pure.m4.exception.PureException) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) M3ProcessorSupport(org.finos.legend.pure.m3.navigation.M3ProcessorSupport) M3ProcessorSupport(org.finos.legend.pure.m3.navigation.M3ProcessorSupport) PureExecutionStreamingException(org.finos.legend.pure.m3.exception.PureExecutionStreamingException) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) IOException(java.io.IOException) PureException(org.finos.legend.pure.m4.exception.PureException)

Aggregations

GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)98 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)95 RichIterable (org.eclipse.collections.api.RichIterable)54 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)52 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)51 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)51 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)47 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)47 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)40 MutableList (org.eclipse.collections.api.list.MutableList)38 ListIterate (org.eclipse.collections.impl.utility.ListIterate)37 ListIterable (org.eclipse.collections.api.list.ListIterable)34 FastList (org.eclipse.collections.impl.list.mutable.FastList)34 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)34 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)34 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)34 Class (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)33 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)33 List (java.util.List)32 EngineErrorType (org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType)32