예제 #1
0
        /// <summary>
        /// Compiles the action set into a set of rules. Should not be called if validation failed.
        /// </summary>
        public bool Compile(RuleTable rules, List <Flag> flags, CancellationToken cancel)
        {
            bool result = true;

            if (IsCompiled != LiftedBool.Unknown)
            {
                return((bool)IsCompiled);
            }
            else if (myComprData == null)
            {
                foreach (var a in actions)
                {
                    result = a.Compile(rules, flags, cancel) && result;
                }

                return((bool)(IsCompiled = result));
            }

            //// For a comprehension need to compile the bodies of the actions
            //// before compiling the actions, so a representation for the comprehension is known.
            FindData[] parts;
            var        bodies = new LinkedList <Term>();

            foreach (var a in actions)
            {
                result = a.Body.Compile(rules, out parts, flags, cancel) && result;
                if (result)
                {
                    bodies.AddLast(rules.MkBodyTerm(parts));
                }
            }

            if (!result)
            {
                return((bool)(IsCompiled = result));
            }

            bool wasAdded;
            Term reads       = Index.TrueValue;
            var  comprSymbol = Index.SymbolTable.GetOpSymbol(ReservedOpKind.Compr);
            var  conjSymbol  = Index.SymbolTable.GetOpSymbol(ReservedOpKind.Conj);

            foreach (var kv in myComprData.ReadVars.Reverse)
            {
                reads = Index.MkApply(conjSymbol, new Term[] { kv.Key, reads }, out wasAdded);
            }

            var headSet = new Set <Term>(Term.Compare);

            foreach (var a in actions)
            {
                headSet.Add(a.HeadTerm);
            }

            Term heads = Index.TrueValue;

            foreach (var h in headSet.Reverse)
            {
                heads = Index.MkApply(conjSymbol, new Term[] { h, heads }, out wasAdded);
            }

            myComprData.Representation = Index.MkApply(comprSymbol, new Term[] { heads, reads, rules.MkBodiesTerm(bodies) }, out wasAdded);
            foreach (var a in actions)
            {
                result = a.Compile(rules, flags, cancel) && result;
            }

            return((bool)(IsCompiled = result));
        }