Search in sources :

Example 1 with CoreInstanceFactoryRegistry

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

the class FunctionExecutionCompiled method init.

@Override
public void init(PureRuntime runtime, Message message) {
    this.runtime = runtime;
    this.repository = runtime.getModelRepository();
    this.context = runtime.getContext();
    this.sourceRegistry = runtime.getSourceRegistry();
    this.javaCompilerEventHandler = new JavaCompilerEventHandler(runtime, message, this.includePureStackTrace, this.javaCompilerEventObserver, this.extensions);
    this.metadataCompilerEventHandler = new MetadataEagerCompilerEventHandler(runtime.getModelRepository(), (MetadataEventObserver) this.javaCompilerEventObserver, message, runtime.getProcessorSupport());
    runtime.addEventHandler(this);
    runtime.getIncrementalCompiler().addCompilerEventHandler(this.javaCompilerEventHandler);
    runtime.getIncrementalCompiler().addCompilerEventHandler(this.metadataCompilerEventHandler);
    initializeFromRuntimeState();
    this.extraSupportedTypes = runtime.getIncrementalCompiler().getParserLibrary().getParsers().flatCollect(CoreInstanceFactoriesRegistry::getCoreInstanceFactoriesRegistry).flatCollect(CoreInstanceFactoryRegistry::allManagedTypes).toSet();
}
Also used : CoreInstanceFactoryRegistry(org.finos.legend.pure.m3.coreinstance.CoreInstanceFactoryRegistry) MetadataEventObserver(org.finos.legend.pure.runtime.java.compiled.metadata.MetadataEventObserver) MetadataEagerCompilerEventHandler(org.finos.legend.pure.runtime.java.compiled.delta.MetadataEagerCompilerEventHandler)

Example 2 with CoreInstanceFactoryRegistry

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

the class M3ToJavaGenerator method createFactory.

private void createFactory(MapIterable<String, CoreInstance> packageToCoreInstance, SetIterable<CoreInstance> m3Enumerations) {
    String factoryName = this.factoryNamePrefix + "CoreInstanceFactoryRegistry";
    RichIterable<CoreInstance> allInstances = m3Enumerations.toList().withAll(packageToCoreInstance.valuesView());
    String result = "package org.finos.legend.pure.m3.coreinstance;\n" + "\n" + "import org.eclipse.collections.api.map.MutableMap;\n" + "import org.eclipse.collections.api.map.primitive.MutableIntObjectMap;\n" + "import org.eclipse.collections.impl.map.mutable.UnifiedMap;\n" + "import org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap;\n" + "import org.eclipse.collections.api.set.SetIterable;\n" + "import org.eclipse.collections.impl.factory.Sets;\n" + packageToCoreInstance.keysView().collect(_package -> "import " + _package + ";").makeString("\n") + packageToCoreInstance.keysView().collect(_package -> "import " + _package.substring(0, _package.lastIndexOf("Instance")) + ";").makeString("\n") + "\n" + "import org.finos.legend.pure.m4.coreinstance.CoreInstance;\n" + "import org.finos.legend.pure.m4.coreinstance.factory.CoreInstanceFactory;\n" + "\n" + "\n" + "public class " + factoryName + "\n" + "{\n" + "    public static final CoreInstanceFactoryRegistry REGISTRY;\n" + "    public static final SetIterable<String> ALL_PATHS = Sets.mutable.of" + allInstances.collect(object -> "\"" + getUserObjectPathForPackageableElement(object, false).makeString("::") + "\"").makeString("(", ",", ")") + ";\n\n" + "    static\n" + "    {\n" + "        MutableMap<String, java.lang.Class> interfaceByPath = UnifiedMap.newMap(" + packageToCoreInstance.size() + ");\n" + allInstances.collect(object -> {
        String classString = isEnum(object) ? "org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum" : Objects.requireNonNull(object).getName();
        return "        interfaceByPath.put(\"" + getUserObjectPathForPackageableElement(object, false).makeString("::") + "\", " + classString + ".class);";
    }).makeString("\n") + "\n";
    if (this.generateTypeFactoriesById) {
        result += "        MutableIntObjectMap<CoreInstanceFactory> typeFactoriesById = IntObjectHashMap.newMap();\n" + allInstances.collect(object -> {
            String classString = isEnum(object) ? "org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum" : Objects.requireNonNull(object).getName();
            return "        typeFactoriesById.put(" + object.getSyntheticId() + ", " + classString + "Instance.FACTORY);";
        }).makeString("", "\n", "\n");
    }
    result += "        MutableMap<String, CoreInstanceFactory> typeFactoriesByPath = UnifiedMap.newMap(" + packageToCoreInstance.size() + ");\n" + allInstances.collect(object -> {
        String classString = isEnum(object) ? "org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum" : Objects.requireNonNull(object).getName();
        return "        typeFactoriesByPath.put(" + getUserObjectPathForPackageableElement(object, false).makeString("\"", "::", "\"") + ", " + classString + "Instance.FACTORY);";
    }).makeString("", "\n", "\n") + "        REGISTRY = new CoreInstanceFactoryRegistry(" + (this.generateTypeFactoriesById ? "typeFactoriesById.toImmutable()" : "new IntObjectHashMap<CoreInstanceFactory>().toImmutable()") + ", typeFactoriesByPath.toImmutable(), interfaceByPath.toImmutable());\n" + "    }\n" + "    public static java.lang.Class getClassForPath(String path)\n" + "    {\n" + "        return REGISTRY.getClassForPath(path);\n" + "    }\n" + "}\n";
    try {
        Path p = Paths.get(this.outputDir + ROOT_PACKAGE.replace(".", "/") + "/" + factoryName + ".java");
        Files.write(p, result.getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
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) Path(java.nio.file.Path) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) IOException(java.io.IOException)

Example 3 with CoreInstanceFactoryRegistry

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

the class AbstractPureTestWithCoreCompiled method setUpRuntime.

// @Before
public static final void setUpRuntime(FunctionExecution execution, MutableCodeStorage codeStorage, CoreInstanceFactoryRegistry registry, RuntimeOptions options, Pair<String, String> extra, RichIterable<? extends CodeRepository> repositories) {
    codeRepositories = repositories;
    functionExecution = execution;
    runtime = new PureRuntimeBuilder(codeStorage).withRuntimeStatus(getPureRuntimeStatus()).withFactoryRegistryOverride(registry).setTransactionalByDefault(isTransactionalByDefault()).withOptions(options).build();
    functionExecution.init(runtime, new Message(""));
    runtime.loadAndCompileCore();
    if (extra != null) {
        runtime.createInMemoryAndCompile(extra);
    }
    repository = runtime.getModelRepository();
    context = runtime.getContext();
    processorSupport = functionExecution.getProcessorSupport() == null ? runtime.getProcessorSupport() : functionExecution.getProcessorSupport();
    if (functionExecution.getConsole() != null) {
        functionExecution.getConsole().enableBufferLines();
    }
}
Also used : Message(org.finos.legend.pure.m3.serialization.runtime.Message) PureRuntimeBuilder(org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder)

Aggregations

IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Objects (java.util.Objects)1 RichIterable (org.eclipse.collections.api.RichIterable)1 Lists (org.eclipse.collections.api.factory.Lists)1 Maps (org.eclipse.collections.api.factory.Maps)1 Sets (org.eclipse.collections.api.factory.Sets)1 ListIterable (org.eclipse.collections.api.list.ListIterable)1 MutableList (org.eclipse.collections.api.list.MutableList)1 MapIterable (org.eclipse.collections.api.map.MapIterable)1 MutableMap (org.eclipse.collections.api.map.MutableMap)1 PartitionIterable (org.eclipse.collections.api.partition.PartitionIterable)1 MutableSet (org.eclipse.collections.api.set.MutableSet)1 SetIterable (org.eclipse.collections.api.set.SetIterable)1 Pair (org.eclipse.collections.api.tuple.Pair)1