Ejemplo n.º 1
0
        public bool Unify(FeatureStruct other, bool useDefaults, VariableBindings varBindings, out FeatureStruct output)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            other = Dereference(other);

            VariableBindings tempVarBindings = varBindings == null ? null : varBindings.DeepClone();
            FeatureValue     newFV;

            if (!UnifyImpl(other, useDefaults, tempVarBindings, out newFV))
            {
                output = null;
                return(false);
            }

            if (varBindings != null)
            {
                varBindings.Replace(tempVarBindings);
            }
            output = (FeatureStruct)newFV;
            return(true);
        }
Ejemplo n.º 2
0
        internal override bool SubtractImpl(FeatureValue other, VariableBindings varBindings, IDictionary <FeatureStruct, ISet <FeatureStruct> > visited)
        {
            FeatureStruct otherFS;

            if (Dereference(other, out otherFS))
            {
                ISet <FeatureStruct> visitedOthers = visited.GetValue(this, () => new HashSet <FeatureStruct>());
                if (!visitedOthers.Contains(otherFS))
                {
                    visitedOthers.Add(otherFS);

                    foreach (KeyValuePair <Feature, FeatureValue> featVal in otherFS._definite)
                    {
                        FeatureValue otherValue = Dereference(featVal.Value);
                        FeatureValue thisValue;
                        if (_definite.TryGetValue(featVal.Key, out thisValue))
                        {
                            thisValue = Dereference(thisValue);
                            if (!thisValue.SubtractImpl(otherValue, varBindings, visited))
                            {
                                _definite.Remove(featVal.Key);
                            }
                        }
                    }
                }
            }
            return(_definite.Count > 0);
        }
Ejemplo n.º 3
0
        internal override bool SubsumesImpl(FeatureValue other, bool useDefaults, VariableBindings varBindings)
        {
            FeatureStruct otherFS;

            if (!Dereference(other, out otherFS))
            {
                return(false);
            }

            foreach (KeyValuePair <Feature, FeatureValue> featVal in _definite)
            {
                FeatureValue thisValue = Dereference(featVal.Value);
                FeatureValue otherValue;
                if (otherFS._definite.TryGetValue(featVal.Key, out otherValue))
                {
                    otherValue = Dereference(otherValue);
                    if (!thisValue.SubsumesImpl(otherValue, useDefaults, varBindings))
                    {
                        return(false);
                    }
                }
                else if (useDefaults && featVal.Key.DefaultValue != null)
                {
                    if (!thisValue.SubsumesImpl(featVal.Key.DefaultValue, true, varBindings))
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 4
0
 public void Replace(VariableBindings varBindings)
 {
     foreach (KeyValuePair <string, SimpleFeatureValue> varBinding in varBindings)
     {
         _varBindings[varBinding.Key] = varBinding.Value;
     }
 }
Ejemplo n.º 5
0
 protected VariableBindings(VariableBindings varBindings)
     : this()
 {
     foreach (KeyValuePair <string, SimpleFeatureValue> kvp in varBindings._varBindings)
     {
         _varBindings[kvp.Key] = kvp.Value.DeepClone();
     }
 }
Ejemplo n.º 6
0
        protected override bool NondestructiveUnify(FeatureValue other, bool useDefaults, IDictionary <FeatureValue, FeatureValue> copies,
                                                    VariableBindings varBindings, out FeatureValue output)
        {
            FeatureStruct otherFS;

            if (!Dereference(other, out otherFS))
            {
                output = null;
                return(false);
            }

            var copy = new FeatureStruct();

            copies[this]  = copy;
            copies[other] = copy;
            foreach (KeyValuePair <Feature, FeatureValue> featVal in otherFS._definite)
            {
                FeatureValue otherValue = Dereference(featVal.Value);
                FeatureValue thisValue;
                if (_definite.TryGetValue(featVal.Key, out thisValue))
                {
                    thisValue = Dereference(thisValue);
                    FeatureValue newValue;
                    if (!thisValue.UnifyImpl(otherValue, useDefaults, copies, varBindings, out newValue))
                    {
                        output = null;
                        return(false);
                    }
                    copy.AddValue(featVal.Key, newValue);
                }
                else if (useDefaults && featVal.Key.DefaultValue != null)
                {
                    thisValue = featVal.Key.DefaultValue.DeepCloneImpl(null);
                    FeatureValue newValue;
                    if (!thisValue.UnifyImpl(otherValue, true, copies, varBindings, out newValue))
                    {
                        output = null;
                        return(false);
                    }
                    copy._definite[featVal.Key] = newValue;
                }
                else
                {
                    copy._definite[featVal.Key] = otherValue.DeepCloneImpl(copies);
                }
            }

            foreach (KeyValuePair <Feature, FeatureValue> featVal in _definite)
            {
                if (!otherFS._definite.ContainsKey(featVal.Key))
                {
                    copy._definite[featVal.Key] = Dereference(featVal.Value).DeepCloneImpl(copies);
                }
            }

            output = copy;
            return(true);
        }
Ejemplo n.º 7
0
        internal override bool IsUnifiableImpl(FeatureValue other, bool useDefaults, VariableBindings varBindings)
        {
            SimpleFeatureValue otherSfv;

            if (!Dereference(other, out otherSfv))
            {
                return(false);
            }

            if (!IsVariable && !otherSfv.IsVariable)
            {
                if (!Overlaps(false, otherSfv, false))
                {
                    return(false);
                }
            }
            else if (IsVariable && !otherSfv.IsVariable)
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(VariableName, out binding))
                {
                    if (!binding.Overlaps(!Agree, otherSfv, false))
                    {
                        return(false);
                    }
                }
                else
                {
                    varBindings[VariableName] = otherSfv.GetVariableValue(Agree);
                }
            }
            else if (!IsVariable && otherSfv.IsVariable)
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(otherSfv.VariableName, out binding))
                {
                    if (!Overlaps(false, binding, !otherSfv.Agree))
                    {
                        return(false);
                    }
                }
                else
                {
                    varBindings[otherSfv.VariableName] = GetVariableValue(otherSfv.Agree);
                }
            }
            else
            {
                if (VariableName != otherSfv.VariableName || Agree != otherSfv.Agree)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 8
0
        internal bool UnifyImpl(FeatureValue other, bool useDefaults, IDictionary <FeatureValue, FeatureValue> copies,
                                VariableBindings varBindings, out FeatureValue output)
        {
            other = Dereference(other);

            FeatureValue fv1;

            if (!copies.TryGetValue(this, out fv1))
            {
                fv1 = null;
            }
            FeatureValue fv2;

            if (!copies.TryGetValue(other, out fv2))
            {
                fv2 = null;
            }

            if (fv1 == null && fv2 == null)
            {
                if (!NondestructiveUnify(other, useDefaults, copies, varBindings, out output))
                {
                    output = null;
                    return(false);
                }
            }
            else if (fv1 != null && fv2 != null)
            {
                if (!fv1.DestructiveUnify(fv2, useDefaults, false, copies, varBindings))
                {
                    output = null;
                    return(false);
                }
                output = fv1;
            }
            else if (fv1 != null)
            {
                if (!fv1.DestructiveUnify(other, useDefaults, true, copies, varBindings))
                {
                    output = null;
                    return(false);
                }
                output = fv1;
            }
            else
            {
                if (!fv2.DestructiveUnify(this, useDefaults, true, copies, varBindings))
                {
                    output = null;
                    return(false);
                }
                output = fv2;
            }

            return(true);
        }
Ejemplo n.º 9
0
        public void PriorityUnion(FeatureStruct other, VariableBindings varBindings)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            CheckFrozen();
            PriorityUnion(other, varBindings, new Dictionary <FeatureValue, FeatureValue>());
        }
Ejemplo n.º 10
0
        public void Subtract(FeatureStruct other, VariableBindings varBindings)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            CheckFrozen();
            SubtractImpl(other, varBindings, new Dictionary <FeatureStruct, ISet <FeatureStruct> >());
        }
Ejemplo n.º 11
0
        internal override bool SubtractImpl(FeatureValue other, VariableBindings varBindings, IDictionary <FeatureStruct, ISet <FeatureStruct> > visited)
        {
            SimpleFeatureValue otherSfv;

            if (!Dereference(other, out otherSfv))
            {
                return(true);
            }

            if (!IsVariable && !otherSfv.IsVariable)
            {
                ExceptWith(false, otherSfv, false);
            }
            else if (IsVariable && !otherSfv.IsVariable)
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(VariableName, out binding))
                {
                    UnionWith(false, binding, !Agree);
                    ExceptWith(false, otherSfv, false);
                    VariableName = null;
                }
            }
            else if (!IsVariable && otherSfv.IsVariable)
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(otherSfv.VariableName, out binding))
                {
                    ExceptWith(false, binding, !otherSfv.Agree);
                }
            }
            else
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(VariableName, out binding))
                {
                    UnionWith(false, binding, !Agree);
                    SimpleFeatureValue otherBinding;
                    if (varBindings.TryGetValue(otherSfv.VariableName, out otherBinding))
                    {
                        ExceptWith(false, otherSfv, false);
                    }
                    VariableName = null;
                }
                else if (!varBindings.ContainsVariable(otherSfv.VariableName))
                {
                    return(VariableName != otherSfv.VariableName || Agree != otherSfv.Agree);
                }
            }

            return(IsSatisfiable);
        }
Ejemplo n.º 12
0
        protected override bool NondestructiveUnify(FeatureValue other, bool useDefaults, IDictionary <FeatureValue, FeatureValue> copies,
                                                    VariableBindings varBindings, out FeatureValue output)
        {
            FeatureValue copy = DeepClone();

            copies[this]  = copy;
            copies[other] = copy;
            if (!copy.DestructiveUnify(other, useDefaults, true, copies, varBindings))
            {
                output = null;
                return(false);
            }
            output = copy;
            return(true);
        }
Ejemplo n.º 13
0
        public bool Subsumes(FeatureStruct other, bool useDefaults, VariableBindings varBindings)
        {
            other = Dereference(other);

            VariableBindings tempVarBindings = varBindings == null ? new VariableBindings() : varBindings.DeepClone();

            if (SubsumesImpl(other, useDefaults, tempVarBindings))
            {
                if (varBindings != null)
                {
                    varBindings.Replace(tempVarBindings);
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Determines whether the specified set of feature values is compatible with this
        /// set of feature values. It is much like <c>Matches</c> except that if a the
        /// specified set does not contain a feature in this set, it is still a match.
        /// It basically checks to make sure that there is no contradictory features.
        /// </summary>
        /// <param name="other">The feature value.</param>
        /// <param name="useDefaults"></param>
        /// <param name="varBindings"></param>
        /// <returns>
        ///     <c>true</c> the sets are compatible, otherwise <c>false</c>.
        /// </returns>
        public bool IsUnifiable(FeatureStruct other, bool useDefaults, VariableBindings varBindings)
        {
            other = Dereference(other);

            VariableBindings definiteVarBindings = varBindings == null ? new VariableBindings() : varBindings.DeepClone();

            if (IsUnifiableImpl(other, useDefaults, definiteVarBindings))
            {
                if (varBindings != null)
                {
                    varBindings.Replace(definiteVarBindings);
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 15
0
        internal override bool AddImpl(FeatureValue other, VariableBindings varBindings, IDictionary <FeatureStruct, ISet <FeatureStruct> > visited)
        {
            FeatureStruct otherFS;

            if (Dereference(other, out otherFS))
            {
                ISet <FeatureStruct> visitedOthers = visited.GetOrCreate(this, () => new HashSet <FeatureStruct>());
                if (!visitedOthers.Contains(otherFS))
                {
                    visitedOthers.Add(otherFS);

                    foreach (KeyValuePair <Feature, FeatureValue> featVal in otherFS._definite)
                    {
                        FeatureValue otherValue = Dereference(featVal.Value);
                        FeatureValue thisValue;
                        if (_definite.TryGetValue(featVal.Key, out thisValue))
                        {
                            thisValue = Dereference(thisValue);
                        }
                        else
                        {
                            if (otherValue is FeatureStruct)
                            {
                                thisValue = new FeatureStruct();
                            }
                            else if (otherValue is StringFeatureValue)
                            {
                                thisValue = new StringFeatureValue();
                            }
                            else
                            {
                                thisValue = new SymbolicFeatureValue((SymbolicFeature)featVal.Key);
                            }
                            _definite[featVal.Key] = thisValue;
                        }
                        if (!thisValue.AddImpl(otherValue, varBindings, visited))
                        {
                            _definite.Remove(featVal.Key);
                        }
                    }
                }
            }
            return(_definite.Count > 0);
        }
Ejemplo n.º 16
0
        public bool Subsumes(FeatureStruct other, bool useDefaults, VariableBindings varBindings)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            other = Dereference(other);

            VariableBindings tempVarBindings = varBindings == null ? null : varBindings.DeepClone();

            if (SubsumesImpl(other, useDefaults, tempVarBindings))
            {
                if (varBindings != null)
                {
                    varBindings.Replace(tempVarBindings);
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Determines whether the specified set of feature values is compatible with this
        /// set of feature values. It is much like <c>Matches</c> except that if a the
        /// specified set does not contain a feature in this set, it is still a match.
        /// It basically checks to make sure that there is no contradictory features.
        /// </summary>
        /// <param name="other">The feature value.</param>
        /// <param name="useDefaults"></param>
        /// <param name="varBindings"></param>
        /// <returns>
        ///     <c>true</c> the sets are compatible, otherwise <c>false</c>.
        /// </returns>
        public bool IsUnifiable(FeatureStruct other, bool useDefaults, VariableBindings varBindings)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            other = Dereference(other);

            VariableBindings definiteVarBindings = varBindings == null ? null : varBindings.DeepClone();

            if (IsUnifiableImpl(other, useDefaults, definiteVarBindings))
            {
                if (varBindings != null)
                {
                    varBindings.Replace(definiteVarBindings);
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 18
0
        private void ReplaceVariables(VariableBindings varBindings, ISet <FeatureStruct> visited)
        {
            if (visited.Contains(this))
            {
                return;
            }

            visited.Add(this);

            var replacements = new Dictionary <Feature, FeatureValue>();

            foreach (KeyValuePair <Feature, FeatureValue> featVal in _definite)
            {
                FeatureValue value = Dereference(featVal.Value);
                var          sfv   = value as SimpleFeatureValue;
                if (sfv != null)
                {
                    if (sfv.IsVariable)
                    {
                        SimpleFeatureValue binding;
                        if (varBindings.TryGetValue(sfv.VariableName, out binding))
                        {
                            replacements[featVal.Key] = binding.GetVariableValue(sfv.Agree);
                        }
                    }
                }
                else
                {
                    var fs = (FeatureStruct)value;
                    fs.ReplaceVariables(varBindings, visited);
                }
            }

            foreach (KeyValuePair <Feature, FeatureValue> replacement in replacements)
            {
                _definite[replacement.Key] = replacement.Value;
            }
        }
Ejemplo n.º 19
0
 internal abstract bool IsUnifiableImpl(FeatureValue other, bool useDefaults, VariableBindings varBindings);
Ejemplo n.º 20
0
 public bool Subsumes(FeatureStruct other, VariableBindings varBindings)
 {
     return(Subsumes(other, false, varBindings));
 }
Ejemplo n.º 21
0
 public bool Unify(FeatureStruct other, VariableBindings varBindings, out FeatureStruct output)
 {
     return(Unify(other, false, varBindings, out output));
 }
Ejemplo n.º 22
0
 internal override bool AddImpl(FeatureValue other, VariableBindings varBindings, IDictionary <FeatureStruct, ISet <FeatureStruct> > visited)
 {
     return(UnionImpl(other, varBindings, visited));
 }
Ejemplo n.º 23
0
        internal override bool UnionImpl(FeatureValue other, VariableBindings varBindings, IDictionary <FeatureStruct, ISet <FeatureStruct> > visited)
        {
            SimpleFeatureValue otherSfv;

            if (!Dereference(other, out otherSfv))
            {
                return(true);
            }

            if (!IsVariable && !otherSfv.IsVariable)
            {
                UnionWith(false, otherSfv, false);
            }
            else if (IsVariable && !otherSfv.IsVariable)
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(VariableName, out binding))
                {
                    UnionWith(false, binding, !Agree);
                    UnionWith(false, otherSfv, false);
                }
                else
                {
                    UnionWith(false, otherSfv, false);
                    varBindings[VariableName] = otherSfv.GetVariableValue(Agree);
                }
                VariableName = null;
            }
            else if (!IsVariable && otherSfv.IsVariable)
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(otherSfv.VariableName, out binding))
                {
                    UnionWith(false, binding, !otherSfv.Agree);
                }
                else
                {
                    varBindings[otherSfv.VariableName] = GetVariableValue(otherSfv.Agree);
                }
            }
            else
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(VariableName, out binding))
                {
                    UnionWith(false, binding, !Agree);
                    SimpleFeatureValue otherBinding;
                    if (varBindings.TryGetValue(otherSfv.VariableName, out otherBinding))
                    {
                        UnionWith(false, otherBinding, !otherSfv.Agree);
                    }
                    VariableName = null;
                }
                else
                {
                    SimpleFeatureValue otherBinding;
                    if (varBindings.TryGetValue(otherSfv.VariableName, out otherBinding))
                    {
                        UnionWith(false, otherBinding, !otherSfv.Agree);
                        VariableName = null;
                    }
                    else
                    {
                        return(VariableName == otherSfv.VariableName && Agree == otherSfv.Agree);
                    }
                }
            }

            return(!IsUninstantiated);
        }
Ejemplo n.º 24
0
        internal override bool DestructiveUnify(FeatureValue other, bool useDefaults, bool preserveInput,
                                                IDictionary <FeatureValue, FeatureValue> copies, VariableBindings varBindings)
        {
            FeatureStruct otherFS;

            if (!Dereference(other, out otherFS))
            {
                return(false);
            }

            if (this == otherFS)
            {
                return(true);
            }

            if (preserveInput)
            {
                if (copies != null)
                {
                    copies[otherFS] = this;
                }
            }
            else
            {
                otherFS.Forward = this;
            }

            foreach (KeyValuePair <Feature, FeatureValue> featVal in otherFS._definite)
            {
                FeatureValue otherValue = Dereference(featVal.Value);
                FeatureValue thisValue;
                if (_definite.TryGetValue(featVal.Key, out thisValue))
                {
                    thisValue = Dereference(thisValue);
                    if (!thisValue.DestructiveUnify(otherValue, useDefaults, preserveInput, copies, varBindings))
                    {
                        return(false);
                    }
                }
                else if (useDefaults && featVal.Key.DefaultValue != null)
                {
                    thisValue = featVal.Key.DefaultValue.DeepCloneImpl(null);
                    _definite[featVal.Key] = thisValue;
                    if (!thisValue.DestructiveUnify(otherValue, true, preserveInput, copies, varBindings))
                    {
                        return(false);
                    }
                }
                else
                {
                    _definite[featVal.Key] = preserveInput ? otherValue.DeepCloneImpl(copies) : otherValue;
                }
            }

            return(true);
        }
Ejemplo n.º 25
0
        internal override bool DestructiveUnify(FeatureValue other, bool useDefaults, bool preserveInput, IDictionary <FeatureValue, FeatureValue> copies, VariableBindings varBindings)
        {
            SimpleFeatureValue otherSfv;

            if (!Dereference(other, out otherSfv))
            {
                return(false);
            }

            if (this == otherSfv)
            {
                return(true);
            }

            if (!IsVariable && !otherSfv.IsVariable)
            {
                if (!Overlaps(false, otherSfv, false))
                {
                    return(false);
                }
                IntersectWith(false, otherSfv, false);
            }
            else if (IsVariable && !otherSfv.IsVariable)
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(VariableName, out binding))
                {
                    if (!binding.Overlaps(!Agree, otherSfv, false))
                    {
                        return(false);
                    }
                    UnionWith(false, binding, !Agree);
                    IntersectWith(false, otherSfv, false);
                }
                else
                {
                    UnionWith(false, otherSfv, false);
                    varBindings[VariableName] = otherSfv.GetVariableValue(Agree);
                }
                VariableName = null;
            }
            else if (!IsVariable && otherSfv.IsVariable)
            {
                SimpleFeatureValue binding;
                if (varBindings.TryGetValue(otherSfv.VariableName, out binding))
                {
                    if (!Overlaps(false, binding, !otherSfv.Agree))
                    {
                        return(false);
                    }
                    IntersectWith(false, binding, !otherSfv.Agree);
                }
                else
                {
                    varBindings[otherSfv.VariableName] = GetVariableValue(otherSfv.Agree);
                }
            }
            else
            {
                if (VariableName != otherSfv.VariableName || Agree != otherSfv.Agree)
                {
                    return(false);
                }
            }

            if (preserveInput)
            {
                if (copies != null)
                {
                    copies[otherSfv] = this;
                }
            }
            else
            {
                otherSfv.Forward = this;
            }

            return(true);
        }
Ejemplo n.º 26
0
        internal bool UnifyImpl(FeatureValue other, bool useDefaults, VariableBindings varBindings, out FeatureValue output)
        {
            var copies = new Dictionary <FeatureValue, FeatureValue>();

            return(UnifyImpl(other, useDefaults, copies, varBindings, out output));
        }
Ejemplo n.º 27
0
 protected abstract bool NondestructiveUnify(FeatureValue other, bool useDefaults, IDictionary <FeatureValue, FeatureValue> copies,
                                             VariableBindings varBindings, out FeatureValue output);
Ejemplo n.º 28
0
 internal abstract bool DestructiveUnify(FeatureValue other, bool useDefaults, bool preserveInput,
                                         IDictionary <FeatureValue, FeatureValue> copies, VariableBindings varBindings);
Ejemplo n.º 29
0
 internal abstract bool SubsumesImpl(FeatureValue other, bool useDefaults, VariableBindings varBindings);
Ejemplo n.º 30
0
 internal abstract bool SubtractImpl(FeatureValue other, VariableBindings varBindings, IDictionary <FeatureStruct, ISet <FeatureStruct> > visited);