Beispiel #1
0
 private void FixAgInIAandFunctions(Agent a, Literal l)
 {
     // if l is internal action/function
     if (l.GetType() == typeof(InternalActionLiteral))
     {
         ((InternalActionLiteral)l).SetIA(null);  // reset the IA in the literal, the IA there will be updated next getIA call
     }
     if (l.GetType() == typeof(ArithFunctionTerm))
     {
         ((ArithFunctionTerm)l).SetAgent(a);
     }
     if (l.GetType() == typeof(Rule))
     {
         ILogicalFormula f = ((Rule)l).GetBody();
         if (f.GetType() == typeof(Literal))
         {
             FixAgInIAandFunctions(a, (Literal)f);
         }
     }
     for (int i = 0; i < l.GetArity(); i++)
     {
         if (l.GetTerm(i).GetType() == typeof(Literal))
         {
             FixAgInIAandFunctions(a, (Literal)l.GetTerm(i));
         }
     }
 }
Beispiel #2
0
        private ArithFunction GetArithFunction(Literal l)
        {
            ArithFunction af = null;

            if (curAg != null)
            {
                // try to find the function in agent register
                af = curAg.GetFunction(l.GetFunctor(), l.GetArity());
            }
            if (af == null)
            {
                // try global function
                af = FunctionRegister.GetFunction(l.GetFunctor(), l.GetArity());
            }
            return(af);
        }
Beispiel #3
0
 public virtual ITerm[] PrepareArguments(Literal body, Unifier un)
 {
     ITerm[] terms = new ITerm[body.GetArity()];
     for (int i = 0; i < terms.Length; i++)
     {
         terms[i] = body.GetTerm(i).CApply(un);
     }
     return(terms);
 }
Beispiel #4
0
        public void MakeVarsAnnon(Literal l, Unifier un)
        {
            try
            {
                for (int i = 0; i < l.GetArity(); i++)
                {
                    ITerm t = l.GetTerm(i);
                    if (t.IsString())
                    {
                        IStringTerm     st        = (IStringTerm)t;
                        MatchCollection matcher   = rx.Matches(st.GetString());
                        StringBuilder   sb        = new StringBuilder();
                        int             last      = 0;
                        var             userBlock = new List <string>();

                        foreach (Match item in matcher)
                        {
                            string sVar = item.Groups[0].Value;
                            sVar = sVar.Substring(2, sVar.Length - 1);
                            ITerm v = AsSyntax.AsSyntax.ParseTerm(sVar);
                            if (v.IsVar())
                            {
                                VarTerm to = ((Structure)l).VarToReplace(v, un);
                                //matcher.appendReplacement(sb, "#{" + to.ToString() + "}");
                                if (sVar.Trim().Length > 0)
                                {
                                    userBlock.Add(sVar);
                                    //No sabemos si esto está bien: el primer Append es para copiar el trozo
                                    //no matcheado y el segundo para el trozo que sí
                                    sb.Append(st.GetString().Substring(last, item.Index - last));
                                    sb.Append(t.IsString() ? ((IStringTerm)t).GetString() : t.ToString());
                                }
                                last = item.Index + item.Length;
                            }
                        }
                        //matcher.appendTail(sb);
                        sb.Append(st.GetString().Substring(last));
                        l.SetTerm(i, new StringTermImpl(sb.ToString()));
                    }
                }
            }
            catch (ParseException pe)
            {
                Debug.Log(pe.ToString());
            }
        }
Beispiel #5
0
        public bool UnifyTerms(ITerm term, ITerm term2)
        {
            if (term.IsArithExpr())
            {
                term = term.CApply(this);
            }
            if (term2.IsArithExpr())
            {
                term2 = term2.CApply(this);
            }

            bool termIsVar  = term.IsVar();
            bool term2IsVar = term2.IsVar();

            // One of them is a variable
            if (termIsVar || term2IsVar)
            {
                VarTerm termv  = termIsVar ? (VarTerm)term : null;
                VarTerm term2v = term2IsVar ? (VarTerm)term2 : null;

                // Get their values
                ITerm termvl  = termIsVar ? Get(termv) : term;
                ITerm term2vl = term2IsVar ? Get(term2v) : term2;

                if (termvl != null && term2vl != null) // Unify the values of the two variables
                {
                    return(UnifiesNoUndo(termvl, term2vl));
                }
                else if (termvl != null) // Unify a variable with a value
                {
                    return(Bind(term2v, termvl));
                }
                else if (term2vl != null)
                {
                    return(Bind(termv, term2vl));
                }
                else // Unify two variables
                {
                    if (!UnifyTerms(termv.GetNS(), term2v.GetNS()))
                    {
                        return(false);
                    }
                    if (termv.Negated() != term2v.Negated())
                    {
                        return(false);
                    }
                    Bind(termv, term2v);
                    return(true);
                }
            }

            // Both terms are not vars
            // If any of the terms is not a literal (is a number or string), they must be equal
            // For unification, lists are literals
            if (!term.IsLiteral() && !term.IsList() || !term2.IsLiteral() && !term2.IsList())
            {
                return(term.Equals(term2));
            }

            // Case of plan body
            if (term.IsPlanBody() && term2.IsPlanBody())
            {
                IPlanBody pb1 = (IPlanBody)term;
                IPlanBody pb2 = (IPlanBody)term2;

                if (pb1.GetBodyTerm() == null && pb2.GetBodyTerm() == null)
                {
                    return(true);
                }
                if (pb1.GetBodyTerm() == null && pb2.GetBodyTerm() != null)
                {
                    return(false);
                }
                if (pb1.GetBodyTerm() != null && pb2.GetBodyTerm() == null)
                {
                    return(false);
                }

                if (pb1.GetBodyTerm().IsVar() && pb2.GetBodyTerm().IsVar())
                {
                    if (UnifiesNoUndo(pb1.GetBodyTerm(), pb2.GetBodyTerm()))
                    {
                        return(UnifiesNoUndo(pb1.GetBodyNext(), pb2.GetBodyNext()));
                    }
                    else
                    {
                        return(false);
                    }
                }

                if (pb1.GetBodyTerm().IsVar())
                {
                    if (pb1.GetBodyNext() == null)
                    {
                        return(UnifiesNoUndo(pb1.GetBodyTerm(), pb2));
                    }
                    else
                    {
                        if (pb2.GetBodyTerm() == null)
                        {
                            return(false);
                        }
                        if (UnifiesNoUndo(pb1.GetBodyTerm(), pb2.GetHead()))
                        {
                            if (pb2.GetBodyNext() == null)
                            {
                                if (pb1.GetBodyNext() != null && pb1.GetBodyNext().GetBodyTerm().IsVar() && pb1.GetBodyNext().GetBodyNext() == null)
                                {
                                    return(UnifiesNoUndo(pb1.GetBodyNext().GetBodyTerm(), new PlanBodyImpl()));
                                }
                                return(false);
                            }
                            else
                            {
                                return(UnifiesNoUndo(pb1.GetBodyNext(), pb2.GetBodyNext()));
                            }
                        }
                    }
                }
                else if (pb2.GetBodyTerm().IsVar())
                {
                    return(Unifies(pb2, pb1));
                }
            }

            // Both terms are literal
            Literal t1s = (Literal)term;
            Literal t2s = (Literal)term2;

            // Different arities
            int ts = t1s.GetArity();

            if (ts != t2s.GetArity())
            {
                return(false);
            }

            // If both are literal, they must have the same negated
            if (t1s.Negated() != t2s.Negated())
            {
                return(false);
            }

            // Different functor
            if (!t1s.GetFunctor().Equals(t2s.GetFunctor()))
            {
                return(false);
            }

            // Different namespace
            if (!UnifiesNamespace(t1s, t2s))
            {
                return(false);
            }

            // Unify inner terms
            for (int i = 0; i < ts; i++)
            {
                if (!UnifiesNoUndo(t1s.GetTerm(i), t2s.GetTerm(i)))
                {
                    return(false);
                }
            }

            // The first's annotations must be a subset of the second's annotations
            if (!t1s.HasSubsetAnnot(t2s, this))
            {
                return(false);
            }

            return(true);
        }