Ejemplo n.º 1
0
        /// @cond DOXYGEN_SHOULD_SKIP_THIS

        public override bool Equals(object obj)
        {
            Substitution s = obj as Substitution;

            if (s == null)
            {
                return(false);
            }

            return(Variable.Equals(s.Variable) && SubValue.Equals(s.SubValue));
        }
            public void AddSubstitution(Substitution s)
            {
                Constraint c;
                bool       needsBuild = !m_constraints.TryGetValue(s.Variable, out c);

                if (s.SubValue.Value.IsVariable)
                {
                    Constraint c2;
                    if (m_constraints.TryGetValue(s.SubValue.Value, out c2))
                    {
                        if (needsBuild)
                        {
                            m_constraints[s.Variable] = c2;
                            c2.EquivalentVariables.Add(s.Variable);
                        }
                        else if (!Object.ReferenceEquals(c, c2))
                        {
                            c2.UnionWith(c);
                            foreach (var v in c2.EquivalentVariables)
                            {
                                m_constraints[v] = c2;
                            }
                        }
                    }
                    else
                    {
                        if (needsBuild)
                        {
                            c = new Constraint();
                            m_constraints[s.Variable] = c;
                            c.EquivalentVariables.Add(s.Variable);
                        }

                        m_constraints[s.SubValue.Value] = c;
                        c.EquivalentVariables.Add(s.SubValue.Value);
                    }
                }
                else
                {
                    if (needsBuild)
                    {
                        c = new Constraint();
                        m_constraints[s.Variable] = c;
                        c.EquivalentVariables.Add(s.Variable);
                    }

                    c.Value = s.SubValue.Value;
                }

                m_substitutions.Add(s);
                m_hashIsDirty = true;
            }
            public bool TestConflict(Substitution subs, SubstitutionSet set, out bool canAdd)
            {
                canAdd = true;
                Constraint c;

                if (!m_constraints.TryGetValue(subs.Variable, out c))
                {
                    return(false);
                }

                Name G1 = c.Value;
                Name G2;

                if (subs.SubValue.Value.IsVariable)
                {
                    if (c.EquivalentVariables.Contains(subs.SubValue.Value))
                    {
                        canAdd = false;
                        return(false);
                    }

                    Constraint c2;
                    if (!m_constraints.TryGetValue(subs.SubValue.Value, out c2))
                    {
                        return(false);
                    }

                    if (c.Value == null || c2.Value == null)
                    {
                        return(false);
                    }

                    G2 = c2.Value;
                }
                else
                {
                    if (G1 == null)
                    {
                        return(false);
                    }

                    canAdd = false;
                    G2     = subs.SubValue.Value;
                    return(!G1.Equals(G2));                     //Conflict!!!
                }

                G1 = G1.MakeGround(set);
                G2 = G2.MakeGround(set);
                return(!G1.Equals(G2));                 //Conflict!!!
            }
Ejemplo n.º 4
0
        //return the idx jump on the valName or -1 if it can't add the substitution
        private static int FindSubsAux(Literal var, Literal val, SimpleName valName, IDictionary <string, Substitution> bindings)
        {
            //first, analyse if the value is a composed name or a single parameter
            string valDescription;
            int    valLiteralCount;

            if (val.type == LiteralType.Root)
            {
                SimpleName auxName = null;
                auxName         = SimpleWFN.BuildNameFromContainedLiteral(valName, val);
                valDescription  = auxName.ToString();
                valLiteralCount = auxName.literals.Count;
            }
            else
            {
                valDescription  = val.description;
                valLiteralCount = 1;
            }

            //check if a binding for the same variable already exists
            Substitution existingSub = null;

            bindings.TryGetValue(var.description, out existingSub);
            if (existingSub != null)
            {
                if (existingSub.SubValue.ToString().RemoveWhiteSpace().EqualsIgnoreCase(valDescription))
                {
                    return(valLiteralCount);
                }
                else
                {
                    return(-1);
                }
            }
            //if there was no existing binding to the variable
            try
            {
                bindings[var.description] = new Substitution(var.description, valDescription);
                return(valLiteralCount);
            }
            catch (BadSubstitutionException)
            {
                return(-1);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Clone Constructor
 /// </summary>
 /// <param name="substitution">The substitution to clone</param>
 private Substitution(Substitution substitution)
 {
     this.Variable = substitution.Variable;
     this.SubValue = substitution.SubValue;
 }
Ejemplo n.º 6
0
        private static bool FindSubst(Name n1, Name n2, bool allowPartialTerms, SubstitutionSet bindings)
        {
            n1 = n1.MakeGround(bindings);
            n2 = n2.MakeGround(bindings);
            var t = GetTerms(n1, n2, allowPartialTerms);

            if (t == null)
            {
                return(false);
            }

            foreach (var p in t)
            {
                Substitution candidate = null;
                bool         isVar1    = p.Item1.IsVariable;
                bool         isVar2    = p.Item2.IsVariable;

                // Case 1: x = t, where t is not a variable and x is a variable, and create substitution x/t
                if (isVar1 != isVar2)
                {
                    Name variable = (isVar1 ? p.Item1 : p.Item2);
                    Name value    = isVar1 ? p.Item2 : p.Item1;
                    if (value.ContainsVariable(variable))                               //Occurs check to prevent cyclical evaluations
                    {
                        return(false);
                    }

                    candidate = new Substitution(variable, new ComplexValue(value));
                }
                else if (isVar1)                 //isVar1 == isVar2 == true
                {
                    //Case 2: x = x, where x is a variable, ignore it. otherwise add the substitution
                    if (!(p.Item1 == p.Item2))
                    {
                        candidate = new Substitution(p.Item1, new ComplexValue(p.Item2));
                    }
                }
                else                 //isVar1 == isVar2 == false
                {
                    // Case 3: t1 = t2, where t1,t2 are not variables.
                    // If they don't contain variables, compare them to see if they are equal. If they are not equal the unification fails.
                    if (p.Item1.IsGrounded && p.Item2.IsGrounded)
                    {
                        if (p.Item1 == p.Item2)
                        {
                            continue;
                        }
                        if (p.Item1 == Name.UNIVERSAL_SYMBOL || p.Item2 == Name.UNIVERSAL_SYMBOL)
                        {
                            continue;
                        }

                        return(false);
                    }

                    //If one or both contain variables, unify the terms
                    if (!FindSubst(p.Item1, p.Item2, allowPartialTerms, bindings))
                    {
                        return(false);
                    }
                }

                if (candidate != null)
                {
                    // Step 4: check to see if the newly created substitution conflicts with any of the already created substitution.
                    // If it does, the unification fails.
                    if (!bindings.AddSubstitution(candidate))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }