Esempio n. 1
0
        internal static int Compare(BuilderRef r1, BuilderRef r2)
        {
            if (r1.refId < r2.refId)
            {
                return(-1);
            }
            else if (r1.refId > r2.refId)
            {
                return(1);
            }

            return(0);
        }
Esempio n. 2
0
        private void BuildFactBody(
            Set <Term> facts,
            Term t,
            Builder bldr,
            BuilderRef modelRef,
            string aliasPrefix,
            Map <Term, string> aliases,
            bool removeRenaming)
        {
            Contract.Assert(t.Symbol.Kind == SymbolKind.ConSymb || t.Symbol.Kind == SymbolKind.MapSymb);
            string myAlias;

            if (aliasPrefix == null)
            {
                myAlias = null;
            }
            else
            {
                myAlias = ToAliasName((UserSymbol)t.Symbol, removeRenaming, aliasPrefix, aliases.Count);
                bldr.PushId(myAlias);
            }

            string       alias;
            BaseCnstSymb bc;
            UserCnstSymb uc;
            var          nsStack = new Stack <Namespace>();

            t.Compute <Unit>(
                (x, s) =>
            {
                if (aliases.ContainsKey(x))
                {
                    return(null);
                }
                else
                {
                    if (x.Symbol.Kind == SymbolKind.ConSymb || x.Symbol.Kind == SymbolKind.MapSymb)
                    {
                        nsStack.Push(((UserSymbol)x.Symbol).Namespace);
                    }

                    return(x.Args);
                }
            },
                (x, ch, s) =>
            {
                switch (x.Symbol.Kind)
                {
                case SymbolKind.BaseCnstSymb:
                    {
                        bc = (BaseCnstSymb)x.Symbol;
                        switch (bc.CnstKind)
                        {
                        case CnstKind.Numeric:
                            bldr.PushCnst((Rational)bc.Raw);
                            break;

                        case CnstKind.String:
                            bldr.PushCnst((string)bc.Raw);
                            break;

                        default:
                            throw new NotImplementedException();
                        }

                        break;
                    }

                case SymbolKind.UserCnstSymb:
                    {
                        uc = (UserCnstSymb)x.Symbol;
                        Contract.Assert(uc.IsNewConstant && !uc.IsSymbolicConstant);
                        bldr.PushId(ToIdString(uc, nsStack.Peek()));
                        break;
                    }

                case SymbolKind.ConSymb:
                case SymbolKind.MapSymb:
                    {
                        if (aliases.TryFindValue(x, out alias))
                        {
                            bldr.PushId(alias);
                        }
                        else
                        {
                            nsStack.Pop();
                            bldr.PushId(x != t ? ToIdString((UserSymbol)x.Symbol, nsStack.Peek()) : ToIdString((UserSymbol)x.Symbol, removeRenaming));
                            bldr.PushFuncTerm();
                            for (int i = 0; i < x.Args.Length; ++i)
                            {
                                bldr.AddFuncTermArg();
                            }
                        }

                        break;
                    }

                default:
                    throw new NotImplementedException();
                }

                return(default(Unit));
            });

            if (aliasPrefix == null)
            {
                bldr.PushAnonModelFact();
                bldr.Load(modelRef);
                bldr.AddModelFact(true);
                bldr.Pop();
            }
            else
            {
                bldr.PushModelFact();
                bldr.Load(modelRef);
                bldr.AddModelFact(true);
                bldr.Pop();
                aliases.Add(t, myAlias);
            }
        }