コード例 #1
0
        public object Eval(object code)
        {
            TermList partList = code as TermList;

            if (partList == null)
            {
                if (code is SIProlog.Part)
                {
                    partList = new TermList((SIProlog.Part)code);
                }
            }
            if (code is string)
            {
                partList = prologEngine.ParseQuery(code.ToString(), MT);
            }
            List <Dictionary <string, SIProlog.Part> > results = new List <Dictionary <string, SIProlog.Part> >();

            prologEngine.askQuery(partList, MT, true, results, null);
            return(results);
        }
コード例 #2
0
        public bool CanInvokeObject(object o, string method, PartList methodArgs, out object result, TextWriter helpfulWarnings)
        {
            object[] invokeargs = new object[methodArgs.Count];
            Type     t          = o.GetType();
            string   extraInfo  = "";

            foreach (var s in
                     t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
                                  BindingFlags.Static))
            {
                if (s.Name.ToLower() == method.ToLower())
                {
                    var  ps       = s.GetParameters();
                    bool mismatch = false;
                    if (ps.Length == methodArgs.Count)
                    {
                        for (int i = 0; i < ps.Length; i++)
                        {
                            ParameterInfo parameterInfo = ps[i];
                            var           pt            = parameterInfo.ParameterType;
                            if (invokeargs[i] != null)
                            {
                                if (pt.IsInstanceOfType(invokeargs[i]))
                                {
                                    continue;
                                }
                            }
                            object op;
                            if (!ConvertPart(methodArgs[i], pt, out op))
                            {
                                mismatch = true;
                                break;
                            }
                            invokeargs[i] = op;
                        }
                    }
                    else
                    {
                        if (extraInfo == "")
                        {
                            extraInfo = " except with " + s;
                        }
                        continue;
                    }
                    if (!mismatch)
                    {
                        object io = s.IsStatic ? null : o;
                        result = s.Invoke(io, invokeargs);
                        if (result == null && s.ReturnType == typeof(void))
                        {
                            result = Atom.PrologNIL;
                        }
                        return(true);
                    }
                    if (helpfulWarnings != null)
                    {
                        helpfulWarnings.WriteLine("mismatched " + s);
                    }
                }
            }
            result = null;
            if (helpfulWarnings != null && extraInfo != "")
            {
                helpfulWarnings.WriteLine(extraInfo);
            }
            return(false);
        }
コード例 #3
0
        public bool CanInvokeObjectByName(string name, string method, PartList methodArgs, bool searchDownMembers, out object result, TextWriter helpfulWarningsOrNull)
        {
            List <object> objs;

            lock (NameToObjects)
            {
                if (!NameToObjects.TryGetValue(name, out objs) || objs.Count == 0)
                {
                    if (helpfulWarningsOrNull != null)
                    {
                        helpfulWarningsOrNull.WriteLine("no such object named '" + name + "'");
                    }
                    result = null;
                    return(false);
                }
            }
            object[] os;
            lock (objs)
            {
                os = objs.ToArray();
            }
            foreach (object o in os)
            {
                try
                {
                    if (CanInvokeObject(o, method, methodArgs, out result, helpfulWarningsOrNull))
                    {
                        return(true);
                    }
                }
                catch (NotSupportedException nse)
                {
                    continue;
                }
            }
            if (searchDownMembers)
            {
                HashSet <object> tryPlaces = new HashSet <object>();
                foreach (object o in os)
                {
                    foreach (
                        var membs in
                        o.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.NonPublic | BindingFlags.Static |
                                              BindingFlags.Instance))
                    {
                        object io  = membs.IsStatic ? null : o;
                        var    tat = membs.GetValue(io);
                        if (tat is IConvertible || tat is ValueType || tat == null)
                        {
                            continue;
                        }
                        tryPlaces.Add(tat);
                    }
                    if (false)
                    {
                        foreach (
                            var membs in
                            o.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.NonPublic |
                                                      BindingFlags.Static | BindingFlags.Instance))
                        {
                            var ggm = membs.GetGetMethod();
                            if (ggm == null)
                            {
                                continue;
                            }
                            object io  = ggm.IsStatic ? null : o;
                            var    tat = ggm.Invoke(io, null);
                            if (tat is IConvertible || tat is ValueType || tat == null)
                            {
                                continue;
                            }
                            tryPlaces.Add(tat);
                        }
                    }
                }
                foreach (object o in os)
                {
                    tryPlaces.Remove(o);
                }
                foreach (object o in tryPlaces)
                {
                    if (CanInvokeObject(o, method, methodArgs, out result, helpfulWarningsOrNull))
                    {
                        return(true);
                    }
                }
            }
            if (helpfulWarningsOrNull != null)
            {
                helpfulWarningsOrNull.WriteLine("no such method '" + method + "' on an object named '" + name + "'");
            }
            result = null;
            return(false);
        }