예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
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);
        }