Search in sources :

Example 1 with Package

use of org.finos.legend.pure.m3.coreinstance.Package 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 2 with Package

use of org.finos.legend.pure.m3.coreinstance.Package 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 3 with Package

use of org.finos.legend.pure.m3.coreinstance.Package in project legend-pure by finos.

the class CompiledProcessorSupport method package_getByUserPath.

@Override
public CoreInstance package_getByUserPath(String path) {
    // Check top level elements
    if (M3Paths.Root.equals(path) || "::".equals(path)) {
        return this.metadataAccessor.getPackage(M3Paths.Root);
    }
    if (M3Paths.Package.equals(path)) {
        return this.metadataAccessor.getClass(M3Paths.Package);
    }
    if (PrimitiveUtilities.isPrimitiveTypeName(path)) {
        return this.metadataAccessor.getPrimitiveType(path);
    }
    int lastColon = path.lastIndexOf(':');
    if (lastColon == -1) {
        // An element in Root - probably a package
        try {
            CoreInstance element = this.metadataAccessor.getPackage(M3Paths.Root + "::" + path);
            if (element != null) {
                return element;
            }
        } catch (Exception ignore) {
        // Perhaps it's not a package? Fall back to general method
        }
        // Get the Root package, then search its children
        Package pkg = this.metadataAccessor.getPackage(M3Paths.Root);
        return pkg._children().detect(c -> path.equals(c.getName()));
    }
    // Perhaps the element is a class?
    try {
        CoreInstance element = this.metadataAccessor.getClass(M3Paths.Root + "::" + path);
        if (element != null) {
            return element;
        }
    } catch (Exception ignore) {
    // Perhaps it's not a class? Fall back to general method
    }
    // Get the element's package, then search in the package
    Package pkg;
    try {
        pkg = this.metadataAccessor.getPackage(M3Paths.Root + "::" + path.substring(0, lastColon - 1));
    } catch (Exception ignore) {
        pkg = null;
    }
    if (pkg == null) {
        // Package doesn't exist, so the element
        return null;
    }
    // Search the children of the package
    String name = path.substring(lastColon + 1);
    return pkg._children().detect(c -> name.equals(c.getName()));
}
Also used : ValCoreInstance(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.coreinstance.ValCoreInstance) BaseCoreInstance(org.finos.legend.pure.m3.coreinstance.BaseCoreInstance) ReflectiveCoreInstance(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.coreinstance.ReflectiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) Package(org.finos.legend.pure.m3.coreinstance.Package) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException)

Example 4 with Package

use of org.finos.legend.pure.m3.coreinstance.Package in project legend-pure by finos.

the class PackageableElementUnloaderWalk method run.

@Override
public void run(CoreInstance element, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
    PackageableElement packageableElement = PackageableElementCoreInstanceWrapper.toPackageableElement(element);
    for (ReferenceUsage referenceUsage : packageableElement._referenceUsages()) {
        matcher.fullMatch(referenceUsage._ownerCoreInstance(), state);
    }
    Package pkg = packageableElement._package();
    if ((pkg != null) && (pkg.getSourceInformation() == null)) {
        matcher.fullMatch(pkg, state);
    }
}
Also used : PackageableElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement) Package(org.finos.legend.pure.m3.coreinstance.Package) ReferenceUsage(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.ReferenceUsage)

Example 5 with Package

use of org.finos.legend.pure.m3.coreinstance.Package in project legend-pure by finos.

the class TestPureRuntimeClass_FunctionExpressionParam method testPureRuntimeClassParameterUsageCleanUp.

@Test
public void testPureRuntimeClassParameterUsageCleanUp() throws Exception {
    RuntimeVerifier.verifyOperationIsStable(new RuntimeTestScriptBuilder().createInMemorySource("sourceId.pure", "function f(c:Class<Any>[1]):Any[1]{$c} function k():Nil[0]{f(A);[];}").createInMemorySource("userId.pure", "Class A{}").compile(), new RuntimeTestScriptBuilder().deleteSource("sourceId.pure").compile().createInMemorySource("sourceId.pure", "function f(c:Class<Any>[1]):Any[1]{$c} function k():Nil[0]{f(A);[];}").compile(), this.runtime, this.functionExecution, this.getAdditionalVerifiers());
    Assert.assertEquals("A instance Class\n" + "    classifierGenericType(Property):\n" + "        Anonymous_StripedId instance GenericType\n" + "            [... >0]\n" + "    generalizations(Property):\n" + "        Anonymous_StripedId instance Generalization\n" + "            [... >0]\n" + "    name(Property):\n" + "        A instance String\n" + "    package(Property):\n" + "        Root instance Package\n" + "    referenceUsages(Property):\n" + "        Anonymous_StripedId instance ReferenceUsage\n" + "            [... >0]", this.runtime.getCoreInstance("A").printWithoutDebug("", 0));
}
Also used : RuntimeTestScriptBuilder(org.finos.legend.pure.m3.RuntimeTestScriptBuilder) Test(org.junit.Test)

Aggregations

Package (org.finos.legend.pure.m3.coreinstance.Package)29 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)18 Test (org.junit.Test)11 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)9 MutableList (org.eclipse.collections.api.list.MutableList)8 RichIterable (org.eclipse.collections.api.RichIterable)7 Lists (org.eclipse.collections.api.factory.Lists)7 ListIterable (org.eclipse.collections.api.list.ListIterable)7 Pair (org.eclipse.collections.api.tuple.Pair)7 Tuples (org.eclipse.collections.impl.tuple.Tuples)7 ModelRepository (org.finos.legend.pure.m4.ModelRepository)7 StandardCharsets (java.nio.charset.StandardCharsets)6 Collection (java.util.Collection)6 Map (java.util.Map)6 Maps (org.eclipse.collections.api.factory.Maps)6 Sets (org.eclipse.collections.api.factory.Sets)6 MutableMap (org.eclipse.collections.api.map.MutableMap)6 MutableSet (org.eclipse.collections.api.set.MutableSet)6 SetIterable (org.eclipse.collections.api.set.SetIterable)6 PackageableElement (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement)6