Search in sources :

Example 1 with Message

use of org.finos.legend.pure.m3.serialization.runtime.Message 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 Message

use of org.finos.legend.pure.m3.serialization.runtime.Message in project legend-pure by finos.

the class AbstractTestCast method checkInvalidTypeCastErrorMessage.

public void checkInvalidTypeCastErrorMessage(Exception e, String message, int line, int column) {
    PureException pe = PureException.findPureException(e);
    Assert.assertNotNull(pe);
    Assert.assertTrue(pe instanceof PureExecutionException);
    Assert.assertEquals(message, pe.getInfo());
    SourceInformation sourceInfo = pe.getSourceInformation();
    Assert.assertNotNull(sourceInfo);
    Assert.assertEquals(line, sourceInfo.getLine());
    Assert.assertEquals(column, sourceInfo.getColumn());
}
Also used : PureException(org.finos.legend.pure.m4.exception.PureException) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation)

Example 3 with Message

use of org.finos.legend.pure.m3.serialization.runtime.Message in project legend-pure by finos.

the class FunctionExecutionCompiled method executeFunction.

private Object executeFunction(CoreInstance functionDefinition, ListIterable<? extends CoreInstance> coreInstances, CompiledExecutionSupport executionSupport, ClassLoader cl, ProcessorSupport processorSupport) {
    // Manage Parameters ----------------------------
    ListIterable<? extends CoreInstance> parameters = Instance.getValueForMetaPropertyToManyResolved(processorSupport.function_getFunctionType(functionDefinition), M3Properties.parameters, processorSupport);
    Class[] paramClasses = new Class[parameters.size()];
    Object[] params = new Object[parameters.size()];
    Metadata metamodel = this.metadataCompilerEventHandler.getMetadata();
    int i = 0;
    if (parameters.size() != coreInstances.size()) {
        StringBuilder builder = new StringBuilder();
        org.finos.legend.pure.m3.navigation.function.Function.print(builder, functionDefinition, processorSupport);
        String message = "Error executing the function:" + builder + ". Mismatch between the number of function parameters (" + parameters.size() + ") and the number of supplied arguments (" + coreInstances.size() + ")";
        throw new PureExecutionException(message);
    }
    for (CoreInstance param : parameters) {
        Object val = GraphSerializer.valueSpecToJavaObject(coreInstances.get(i), this.context, this.getProcessorSupport(), metamodel);
        CoreInstance paramMult = Instance.getValueForMetaPropertyToOneResolved(param, M3Properties.multiplicity, processorSupport);
        if (Multiplicity.isToOne(paramMult, true)) {
            String t = TypeProcessor.typeToJavaPrimitiveSingle(Instance.getValueForMetaPropertyToOneResolved(param, M3Properties.genericType, processorSupport), processorSupport);
            paramClasses[i] = CompiledSupport.convertFunctionTypeStringToClass(t, cl);
            if (val instanceof MutableList) {
                MutableList valList = (MutableList) val;
                if (valList.size() != 1) {
                    throw new RuntimeException("Expected exactly one value, found " + valList.size());
                }
                val = valList.get(0);
            }
        } else if (Multiplicity.isToOne(paramMult, false)) {
            String className = TypeProcessor.typeToJavaObjectSingle(Instance.getValueForMetaPropertyToOneResolved(param, M3Properties.genericType, processorSupport), false, processorSupport);
            paramClasses[i] = CompiledSupport.loadClass(className, cl);
            if (val instanceof MutableList) {
                MutableList valList = (MutableList) val;
                switch(valList.size()) {
                    case 0:
                        {
                            val = null;
                            break;
                        }
                    case 1:
                        {
                            val = valList.get(0);
                            break;
                        }
                    default:
                        {
                            throw new RuntimeException("Expected at most one value, found " + valList.size());
                        }
                }
            }
        } else {
            paramClasses[i] = RichIterable.class;
            if (!(val instanceof MutableList)) {
                val = FastList.newListWith(val);
            }
        }
        params[i] = val;
        i++;
    }
    return CompiledSupport.executeFunction(functionDefinition, paramClasses, params, executionSupport);
// -----------------------------------------------
}
Also used : PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) Metadata(org.finos.legend.pure.runtime.java.compiled.metadata.Metadata) RichIterable(org.eclipse.collections.api.RichIterable) MutableList(org.eclipse.collections.api.list.MutableList) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 4 with Message

use of org.finos.legend.pure.m3.serialization.runtime.Message in project legend-pure by finos.

the class TestGraphIsSerialized method testInitializedFromGraphLoaderSerialization.

@Test
public void testInitializedFromGraphLoaderSerialization() {
    PureRuntime runtime = new PureRuntimeBuilder(getCodeStorage()).buildAndInitialize();
    PureGraphCache cache = new MemoryGraphLoaderPureGraphCache();
    cache.setPureRuntime(runtime);
    cache.cacheRepoAndSources();
    runtime = new PureRuntimeBuilder(getCodeStorage()).withCache(cache).buildAndTryToInitializeFromCache();
    new FunctionExecutionCompiledBuilder().build().init(runtime, new Message(""));
    Assert.assertTrue(cache.getCacheState().getLastStackTrace(), runtime.isInitialized());
    assertAllInstancesMarkedSerialized(runtime);
}
Also used : PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) FunctionExecutionCompiledBuilder(org.finos.legend.pure.runtime.java.compiled.execution.FunctionExecutionCompiledBuilder) Message(org.finos.legend.pure.m3.serialization.runtime.Message) PureRuntimeBuilder(org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder) PureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.PureGraphCache) MemoryPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.MemoryPureGraphCache) MemoryGraphLoaderPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.MemoryGraphLoaderPureGraphCache) MemoryGraphLoaderPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.MemoryGraphLoaderPureGraphCache) Test(org.junit.Test)

Example 5 with Message

use of org.finos.legend.pure.m3.serialization.runtime.Message in project legend-pure by finos.

the class TestGraphIsSerialized method testInitializedFromM4Serialization.

@Test
public void testInitializedFromM4Serialization() {
    PureRuntime runtime = new PureRuntimeBuilder(getCodeStorage()).buildAndInitialize();
    PureGraphCache cache = new MemoryPureGraphCache();
    cache.setPureRuntime(runtime);
    cache.cacheRepoAndSources();
    runtime = new PureRuntimeBuilder(getCodeStorage()).withCache(cache).buildAndTryToInitializeFromCache();
    new FunctionExecutionCompiledBuilder().build().init(runtime, new Message(""));
    Assert.assertTrue(cache.getCacheState().getLastStackTrace(), runtime.isInitialized());
    assertAllInstancesMarkedSerialized(runtime);
}
Also used : PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) FunctionExecutionCompiledBuilder(org.finos.legend.pure.runtime.java.compiled.execution.FunctionExecutionCompiledBuilder) Message(org.finos.legend.pure.m3.serialization.runtime.Message) MemoryPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.MemoryPureGraphCache) PureRuntimeBuilder(org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder) PureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.PureGraphCache) MemoryPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.MemoryPureGraphCache) MemoryGraphLoaderPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.MemoryGraphLoaderPureGraphCache) Test(org.junit.Test)

Aggregations

CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)45 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)29 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)19 RichIterable (org.eclipse.collections.api.RichIterable)15 MutableList (org.eclipse.collections.api.list.MutableList)15 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)13 Message (org.finos.legend.pure.m3.serialization.runtime.Message)13 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)12 PureRuntimeBuilder (org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder)12 ModelRepository (org.finos.legend.pure.m4.ModelRepository)12 PureRuntime (org.finos.legend.pure.m3.serialization.runtime.PureRuntime)11 ListIterable (org.eclipse.collections.api.list.ListIterable)10 SetIterable (org.eclipse.collections.api.set.SetIterable)10 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)10 Parser (org.finos.legend.pure.m3.serialization.grammar.Parser)10 Test (org.junit.Test)10 Lists (org.eclipse.collections.api.factory.Lists)9 MutableSet (org.eclipse.collections.api.set.MutableSet)9 Package (org.finos.legend.pure.m3.coreinstance.Package)9 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)9