Beispiel #1
0
        public void Process(JObject command, Context context)
        {
            // get the query object
            // this will be used to match against the rules
            var query = JSONUtil.GetToken(command, "#assert");

            if (query == null)
            {
                query = JSONUtil.GetToken(command, "query");
            }

            // search through each Item in the Context
            var items = context.Items.Keys.ToList();

            foreach (var item in items)
            {
                // search the Item for matching rules
                // unifying any variables provided by the query
                var jtItem  = context.Items[item];
                var results = this.FindRules(jtItem, query, context);
                if (results == null)
                {
                    continue;
                }

                // if there were any results (rules that matched)
                foreach (var result in results)
                {
                    var jaResults = (JArray)result;

                    foreach (var jaResult in jaResults)
                    {
                        // grab the then (consequent) portion into an array of commands
                        var then = jaResult["then"];

                        // apply the unification that was determined when the rule was matched
                        var unification = (JObject)jaResult["#unification"];
                        unification.Add("?#", query);
                        then = Unification.ApplyUnification(then, unification);

                        // grab the array of commands (convert single to array)
                        var jCommands = new JArray();
                        if (then is JObject)
                        {
                            jCommands.Add(then);
                        }
                        else if (then is JArray)
                        {
                            jCommands = (JArray)then;
                        }

                        // run each command
                        foreach (JObject joTokenCommand in jCommands)
                        {
                            context.CommandEngine.RunCommand(joTokenCommand, context);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public void Process(JObject command, Context context)
        {
            string file = JSONUtil.GetText(command, "#run-script");

            if (file == null)
            {
                file = command["script"].ToString();
            }
            file = context.ReplaceVariables(file);

            System.IO.FileInfo fi = new System.IO.FileInfo(file);
            var scriptDir         = fi.Directory.FullName;

            JArray items = (JArray)JSONUtil.ReadFile(file);

            if (items == null)
            {
                Console.WriteLine($"RunScript: Unable to read file {file}.");
                return;
            }

            foreach (var item in items)
            {
                var commandItem = Unification.Replace(item, "#scriptDir", scriptDir);
                context.CommandEngine.RunCommand(commandItem, context);
            }
        }
Beispiel #3
0
 public void ConstantsUnification()
 {
     Assert.IsTrue(Unification.CarryOut(Atom("some-atom"), Atom("some-atom")).HasValue);
     Assert.IsFalse(Unification.CarryOut(Atom("some-atom"), Atom("some-other-atom")).HasValue);
     Assert.IsTrue(Unification.CarryOut(Number(42), Number(42)).HasValue);
     Assert.IsFalse(Unification.CarryOut(Number(42), Number(21)).HasValue);
     Assert.IsFalse(Unification.CarryOut(Atom("2"), Number(2)).HasValue);
 }
Beispiel #4
0
        public void Process(JObject command, Context context)
        {
            // get default property
            var query = JSONUtil.GetToken(command, "#run-rules");

            if (query == null)
            {
                query = JSONUtil.GetToken(command, "query");
            }

            var items = context.Items.Keys.ToList();

            foreach (var item in items)
            {
                var jtItem  = context.Items[item];
                var results = this.FindRules(jtItem, query);
                if (results == null)
                {
                    continue;
                }

                foreach (var result in results)
                {
                    var then = result["then"];

                    var unification = (JObject)result["#unification"];
                    unification.Add("?#", query);
                    then = Unification.ApplyUnification(then, unification);

                    if (then is JObject)
                    {
                        context.CommandEngine.RunCommand(then, context);
                    }
                    else if (then is JArray jCommands)
                    {
                        foreach (JObject joTokenCommand in jCommands)
                        {
                            context.CommandEngine.RunCommand(joTokenCommand, context);
                        }
                    }
                }
            }
        }
Beispiel #5
0
        internal JArray FindRules(JToken ruleSet, JToken query)
        {
            if (ruleSet.Type != JTokenType.Array)
            {
                return(null);
            }

            //var type = JSONUtil.GetText(ruleSet, "#type");
            //if (type != "rules") { return null; }

            var results = new JArray();

            //var rules = (JArray)ruleSet["rules"];
            var rules = (JArray)ruleSet;

            if (rules == null)
            {
                return(null);
            }

            //var testInput = query["input"].ToString();

            foreach (JObject rule in rules)
            {
                var when = rule["when"];
                if (when == null)
                {
                    continue;
                }

                //var unification = Text.UnifyStrings(when, query);
                var unification = Unification.Unify(when, query);
                if (unification != null)
                {
                    var clonedRule = (JObject)rule.DeepClone();
                    clonedRule.Add("#unification", unification);
                    results.Add(clonedRule);
                }
            }

            return(results);
        }
        public void UniqueCrossTest()
        {
            double[] input1 = new double[]
            {
                2, 3, 4, 5
            };

            double[] input2 = new double[]
            {
                4, 3, 5, 2
            };

            double[] output1 = new double[]
            {
                2, 3, 5, 4
            };

            double[] output2 = new double[]
            {
                4, 3, 2, 5
            };

            Unification cast = new Unification(new HashSet <double>()
            {
                2, 3, 4, 5
            });

            var rez = cast.Postworking(new Chromosome(4)
            {
                Gen = input1
            });

            var rez2 = cast.Postworking(new Chromosome(4)
            {
                Gen = input2
            });

            CollectionAssert.AreEqual(rez.Gen, output1);
            CollectionAssert.AreEqual(rez2.Gen, output2);
        }
Beispiel #7
0
        internal JObject Unify(JToken when, JToken query)
        {
            JObject result = null;

            if (when.Type == JTokenType.Array) // assume OR (try to prove any)
            {
                var jaWhens = (JArray)when;
                foreach (var jaWhen in jaWhens)
                {
                    var sub = this.Unify(jaWhen, query);
                    if (sub != null)
                    {
                        result = sub; break;
                    }
                }
            }
            else if (when.Type == JTokenType.Object)
            {
                result = Unification.Unify(when, query);
            }

            return(result);
        }
Beispiel #8
0
        private JArray RunSequenceRule(JToken query, JObject rule, Context context)
        {
            var results = new JArray();

            var whenSequence = rule["when-sequence"];

            if (whenSequence != null)
            {
                // get the object at the current sequence position
                int?pos = JSONUtil.GetInt32(rule, "#seq-pos");
                if (pos == null)
                {
                    pos = 0; rule.Add("#seq-pos", 0);
                }
                var jaWhenSequence = (JArray)whenSequence;
                var currentItem    = jaWhenSequence[pos];

                context.Trace($"Examining {JSONUtil.SingleLine(query)} against {JSONUtil.SingleLine(jaWhenSequence)} at pos = {pos} ({JSONUtil.SingleLine(currentItem)})");

                // try the unification at the current position - will return null if not able to unify
                var unification = Unification.Unify(currentItem, query);

                // if able to unify, instantiate constituent at that position
                if (unification != null)
                {
                    context.Trace($"Unified {JSONUtil.SingleLine(currentItem)} with {JSONUtil.SingleLine(query)}");

                    currentItem         = Unification.ApplyUnification(currentItem, unification);
                    jaWhenSequence[pos] = currentItem;
                    pos++;
                    rule["#seq-pos"] = pos;

                    // if sequence is complete, then return unification for the "then" portion to fire
                    if (pos == jaWhenSequence.Count)
                    {
                        context.Trace($"Detected completed sequence {JSONUtil.SingleLine(jaWhenSequence)}");

                        //var clonedRule = (JObject)rule.DeepClone();
                        rule.Add("#unification", unification); // include the unification information
                        results.Add(rule);
                    }
                    // if sequence is not complete, then throw a new arc on the open arcs for next round
                    else
                    {
                        context.Trace($"Adding extended sequence {JSONUtil.SingleLine(jaWhenSequence)} [{pos}] to open arcs");

                        var     arcSet   = context.Fetch("#seq-arcs");
                        JObject joArcSet = null;
                        JArray  arcs     = null;
                        if (arcSet == null)
                        {
                            joArcSet = new JObject();
                            arcs     = new JArray();
                            joArcSet.Add("rules", arcs);
                            context.Store("#seq-arcs", joArcSet);
                        }
                        else
                        {
                            joArcSet = (JObject)arcSet;
                            arcs     = (JArray)joArcSet["rules"];
                        }

                        arcs.Add(rule);
                        //context.Store("#seq-arcs", joArcSet);
                    }
                }
            }

            if (results.Count == 0)
            {
                return(null);
            }
            return(results);
        }