Exemplo n.º 1
0
        public override async Task <EvaluateDoublesReply> EvaluateDoubles(EvaluateDoublesRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new EvaluateDoublesReply();

            try
            {
                var doubles = ROpsApi.EvaluateDoubles(request.Code);

                reply.Payload = new EvaluateDoublesPayload();

                foreach (var kvp in doubles)
                {
                    var doubleList = new DoubleList();
                    if (kvp.Value.IsCollection())
                    {
                        doubleList.Doubles.Add(kvp.Value);
                    }
                    reply.Payload.Doubles.Add(kvp.Key, doubleList);
                }
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(EvaluateDoubles));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 2
0
        public override async Task <SaveObjectToBinaryReply> SaveObjectToBinary(SaveObjectToBinaryRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new SaveObjectToBinaryReply();

            try
            {
                var binary = ROpsApi.SaveObjectToBinary(request.ObjectName);

                reply.Payload = new SaveObjectToBinaryPayload
                {
                    Binary = ByteString.CopyFrom(binary)
                };
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(SaveObjectToBinary));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 3
0
        public override async Task <InspectSymbolsReply> InspectSymbols(InspectSymbolsRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new InspectSymbolsReply();

            try
            {
                var symbolInfos = ROpsApi.InspectSymbols(request.PathToCode);

                using var memoryStream = new MemoryStream();
                Serializer.Serialize(memoryStream, symbolInfos);
                memoryStream.Position = 0;

                reply.Payload = new InspectSymbolsPayload
                {
                    SymbolInfos = ByteString.FromStream(memoryStream)
                };
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(InspectSymbols));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 4
0
        public override async Task <RversionReply> GetRversion(RversionRequest _, ServerCallContext __)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new RversionReply();

            try
            {
                var rVersion = ROpsApi.GetRversion();

                reply.Payload = new RversionPayload();
                reply.Payload.Rversion.Add(rVersion);
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(GetRversion));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 5
0
        public override async Task <EvaluateNumDataReply> EvaluateNumData(EvaluateNumDataRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new EvaluateNumDataReply();

            try
            {
                var numDataColumns = ROpsApi.EvaluateNumData(request.Code);

                reply.Payload = new EvaluateNumDataPayload();

                foreach (var numDataColumn in numDataColumns)
                {
                    var doubleColumn = new DoubleColumn
                    {
                        Name = numDataColumn.Name
                    };
                    doubleColumn.Doubles.Add(numDataColumn.Data);
                    reply.Payload.DoubleColumns.Add(doubleColumn);
                }
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(EvaluateNumData));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 6
0
        public override async Task <InstalledPackagesReply> GetInstalledPackages(InstalledPackagesRequest _, ServerCallContext __)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new InstalledPackagesReply();

            try
            {
                var installedPackages = ROpsApi.GetInstalledPackages();

                reply.Payload = new InstalledPackagesPayload();
                reply.Payload.InstalledPackages.Add(installedPackages);
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(GetInstalledPackages));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 7
0
        public override async Task <RunExecReply> RunExec(RunExecRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new RunExecReply();

            try
            {
                using var memoryStream = new MemoryStream(request.SimConfig.Length);
                request.SimConfig.WriteTo(memoryStream);
                memoryStream.Position = 0;
                var simConfig = Serializer.Deserialize <SimConfig>(memoryStream);

                ROpsApi.RunExec(request.PathToCode, simConfig.SimCode, simConfig.SimInput);

                reply.Payload = new RunExecPayload();
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(RunExec));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 8
0
        public void TestInspectExecSymbols()
        {
            // arrange
            var          memoryStream     = new MemoryStream();
            var          pathToSimLibrary = TestData.SimLibraryDirectory.FullName;
            var          pathToCode       = Path.Combine(pathToSimLibrary, "InspectExec", "inspect.R");
            const string execSymbol       = "run";
            const string parametersSymbol = "parameters";
            const string p1Symbol         = "p1";
            //const string p2Symbol = "p2";
            const string outputSymbol = "o";
            //const string o1Symbol = "o1";
            const string o2Symbol      = "o2";
            const string o3Symbol      = "o3";
            const string o3LocalSymbol = "Ro3";

            // act
            var symbolInfos = ROpsApi.InspectSymbols(pathToCode);
            var execSI      = symbolInfos.SingleOrDefault(si => si.Symbol == execSymbol && si.Level == 0);
            var parameterSI = symbolInfos.SingleOrDefault(si => si.Symbol == parametersSymbol && si.Level == 0);
            var pDefs       = parameterSI?.Names?.ToDictionary(n => n, n => symbolInfos.SingleOrDefault(si => si.Symbol == n && si.Level > 0));
            var outputSI    = symbolInfos.SingleOrDefault(si => si.Symbol == outputSymbol && si.Level == 0);
            var oDefs       = outputSI?.Names?.ToDictionary(n => n, n => symbolInfos.SingleOrDefault(si => si.Symbol == n && si.Level > 0));

            // assert
            Assert.IsNotNull(execSI);
            Assert.AreEqual("1st param", pDefs?[p1Symbol]?.Comment);
            Assert.AreEqual("u", pDefs?[p1Symbol]?.Unit);
            Assert.AreEqual("u.v", oDefs?[o2Symbol]?.Unit);
            Assert.AreEqual("2nd output", oDefs?[o2Symbol]?.Comment);
            Assert.IsFalse(oDefs?.ContainsKey(o3LocalSymbol) == true);
            Assert.IsNull(oDefs?[o3Symbol]);
        }
Exemplo n.º 9
0
        public void TestEvaluateNumData()
        {
            // arrange
            var expr01     = "2+2";
            var expected01 = new[] { 4.0 };

            var expr02 = "list(x = 1:4, y = 5:8)";
            var x      = Enumerable.Range(1, 4).Select(i => i * 1.0).ToArray();
            var y      = Enumerable.Range(5, 4).Select(i => i * 1.0).ToArray();

            var expr03     = "seq(0,1,by=0.1)";
            var expected03 = Enumerable.Range(0, 11).Select(i => i * 0.1);

            // act
            var actual01 = ROpsApi.EvaluateNumData(expr01);
            var actual02 = ROpsApi.EvaluateNumData(expr02);
            var actual03 = ROpsApi.EvaluateNumData(expr03);

            // assert
            Assert.IsTrue(actual01.Length == 1);
            Assert.IsTrue(expected01.SequenceEqual(actual01[0].Data));

            Assert.IsTrue(actual02[0].Name == nameof(x) && x.SequenceEqual(actual02[0].Data));
            Assert.IsTrue(actual02[1].Name == nameof(y) && y.SequenceEqual(actual02[1].Data));

            Assert.IsTrue(expected03.SequenceEqual(actual03[0].Data));
        }
Exemplo n.º 10
0
        public override async Task <CreateMatrixReply> CreateMatrix(CreateMatrixRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new CreateMatrixReply();

            try
            {
                var source = request.Source.Select(dl => dl.Doubles.ToArray()).ToArray();
                ROpsApi.CreateMatrix(source.ToMultidimensional(), request.ObjectName);

                reply.Payload = new CreateMatrixPayload();
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(CreateMatrix));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 11
0
        public override async Task <SerializeReply> Serialize(SerializeRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new SerializeReply();

            try
            {
                var serialized = ROpsApi.Serialize(request.ObjectName);

                reply.Payload = new SerializePayload
                {
                    Serialized = ByteString.CopyFrom(serialized)
                };
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(Serialize));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 12
0
        public void TestSourceLines()
        {
            // arrange
            var lines = new[]
            {
                "a <- 1",
                "if(a == 1) {",
                "a <- 2",
                "}"
            };

            // act
            ROpsApi.SourceLines(lines);
            var @out = ROpsApi.EvaluateNumData("a");

            // assert
            Assert.AreEqual(@out.First().Data[0], 2.0);
        }
Exemplo n.º 13
0
        public void TestEvaluate()
        {
            // arrange
            var expr01     = @"list(x = ""abc"", y = ""def"")";
            var expected01 = new Dictionary <string, object[]> {
                ["x"] = new[] { "abc" }, ["y"] = new[] { "def" }
            };

            // act
            var actual01 = ROpsApi.Evaluate(expr01);

            // assert
            Assert.AreEqual(expected01.Count, actual01.Count);

            var areEqual = expected01.Keys.All(
                k => actual01.ContainsKey(k) && actual01[k]?[0] as string == expected01[k][0] as string
                );

            Assert.IsTrue(areEqual);
        }
Exemplo n.º 14
0
        public void TestBinaryRoundTrip()
        {
            // arrange
            var lines = new[]
            {
                $"list_{nameof(TestBinaryRoundTrip)} <- list(ints = c(1,2,3), nums = c(1.0, 2.0, 3.), alpha = c(\"a\",\"b\",\"c\"))",
                $"copy_of_list_{nameof(TestBinaryRoundTrip)} <- list_{nameof(TestBinaryRoundTrip)}"
            };

            // act
            ROpsApi.SourceLines(lines);
            var bytes = ROpsApi.SaveObjectToBinary($"list_{nameof(TestBinaryRoundTrip)}");

            ROpsApi.EvaluateNonQuery($"rm(list_{nameof(TestBinaryRoundTrip)})");
            ROpsApi.LoadFromBinary(bytes);
            var evaluated = ROpsApi.Evaluate($"all.equal(copy_of_list_{nameof(TestBinaryRoundTrip)}, list_{nameof(TestBinaryRoundTrip)})");

            // assert
            Assert.IsNotNull(evaluated.First().Value);
            Assert.IsTrue(evaluated.First().Value !.First().Resolve(out bool allEqual) && allEqual);
        }
Exemplo n.º 15
0
        public override async Task <TabulateTmplOutputReply> TabulateTmplOutput(TabulateTmplOutputRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new TabulateTmplOutputReply();

            try
            {
                using var memoryStream = new MemoryStream(request.SimConfig.Length);
                request.SimConfig.WriteTo(memoryStream);
                memoryStream.Position = 0;
                var simConfig = Serializer.Deserialize <SimConfig>(memoryStream);

                var numDataColumns = ROpsApi.TabulateTmplOutput(simConfig);

                reply.Payload = new TabulateTmplOutputPayload();

                foreach (var numDataColumn in numDataColumns)
                {
                    var doubleColumn = new DoubleColumn
                    {
                        Name = numDataColumn.Name
                    };
                    doubleColumn.Doubles.Add(numDataColumn.Data);
                    reply.Payload.DoubleColumns.Add(doubleColumn);
                }
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(TabulateTmplOutput));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 16
0
        public override async Task <ClearGlobalEnvironmentReply> ClearGlobalEnvironment(ClearGlobalEnvironmentRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new ClearGlobalEnvironmentReply();

            try
            {
                ROpsApi.ClearGlobalEnvironment();
                reply.Payload = new ClearGlobalEnvironmentPayload();
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(ClearGlobalEnvironment));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 17
0
        public override async Task <GarbageCollectReply> GarbageCollect(GarbageCollectRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new GarbageCollectReply();

            try
            {
                GC.Collect();
                ROpsApi.GarbageCollect();
                reply.Payload = new GarbageCollectPayload();
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(GarbageCollect));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 18
0
        public override async Task <EvaluateNonQueryReply> EvaluateNonQuery(EvaluateNonQueryRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new EvaluateNonQueryReply();

            try
            {
                ROpsApi.EvaluateNonQuery(request.Code);

                reply.Payload = new EvaluateNonQueryPayload();
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(EvaluateNonQuery));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 19
0
        public override async Task <SourceFileReply> SourceFile(SourceFileRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new SourceFileReply();

            try
            {
                ROpsApi.SourceFile(request.PathToCode);

                reply.Payload = new SourceFilePayload();
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(RunExec));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 20
0
        public override async Task <UnserializeReply> Unserialize(UnserializeRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new UnserializeReply();

            try
            {
                ROpsApi.Unserialize(request.Raw.ToByteArray(), request.ObjectName);

                reply.Payload = new UnserializePayload();
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(Unserialize));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }
Exemplo n.º 21
0
        public override async Task <LoadFromBinaryReply> LoadFromBinary(LoadFromBinaryRequest request, ServerCallContext context)
        {
            await _semaphoreSlim.WaitAsync();

            var reply = new LoadFromBinaryReply();

            try
            {
                ROpsApi.LoadFromBinary(request.Raw.ToArray());

                reply.Payload = new LoadFromBinaryPayload();
            }
            catch (Exception ex)
            {
                reply.Error = PopulateError(ex);
                _logger.LogError(ex, nameof(LoadFromBinary));
            }
            finally
            {
                _semaphoreSlim.Release();
            }

            return(await Task.FromResult(reply));
        }