Exemple #1
0
        public static bool TryConvertBack(REngine engine, RDotNetConverter dataConverter, object data, out SymbolicExpression result)
        {
            var type = data.GetType();

            if (type.IsGenericType)
            {
                var genericTypeDefinition = type.GetGenericTypeDefinition();
                if (dictionaryTypeDefinitions.Contains(genericTypeDefinition))
                {
                    var genericArguments = type.GetGenericArguments();
                    if (genericArguments[0] == typeof(string))
                    {
                        if (dicoToSexpMethod.MakeGenericMethod(new[] { genericArguments[1] })
                            .Invoke(null, new[] { engine, dataConverter, data }) is SymbolicExpression sexp)
                        {
                            result = sexp;
                            return(true);
                        }
                    }
                }

                if (listTypeDefinitions.Contains(genericTypeDefinition))
                {
                    if (data is IEnumerable enumerable)
                    {
                        result = new GenericVector(engine, ListToSexp(engine, dataConverter, enumerable));
                        return(true);
                    }
                }
            }

            if (type.IsArray)
            {
                if (data is IEnumerable array)
                {
                    result = new GenericVector(engine, ListToSexp(engine, dataConverter, array));
                    return(true);
                }
            }

            result = engine.NilValue;
            return(false);
        }
Exemple #2
0
 private static IEnumerable <SymbolicExpression> ListToSexp(REngine engine, RDotNetConverter dataConverter, IEnumerable enumerable)
 {
     foreach (var item in enumerable)
     {
         if (item == null)
         {
             yield return(engine.NilValue);
         }
         else
         {
             var sexp = dataConverter.ConvertToSexp(item.GetType(), item);
             if (sexp == null)
             {
                 yield return(engine.NilValue);
             }
             else
             {
                 yield return(sexp);
             }
         }
     }
 }
Exemple #3
0
        private static SymbolicExpression DicoToSexp <T>(REngine engine, RDotNetConverter dataConverter, IEnumerable <KeyValuePair <string, T> > enumerable)
        // ReSharper restore UnusedMember.Local
        {
            var type   = typeof(T);
            var array  = enumerable.ToArray();
            var length = array.Length;

            var values = new SymbolicExpression[length];
            var names  = new string[length];

            for (var i = 0; i < length; i++)
            {
                names[i] = array[i].Key;
                var sexp = dataConverter.ConvertToSexp(type, array[i].Value);
                values[i] = sexp ?? engine.NilValue;
            }

            var vector = new GenericVector(engine, values);

            vector.SetNames(names);
            return(vector);
        }