Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        private void PriorityUnion(FeatureStruct other, VariableBindings varBindings, IDictionary <FeatureValue, FeatureValue> copies)
        {
            other = Dereference(other);

            copies[other] = this;

            foreach (KeyValuePair <Feature, FeatureValue> featVal in _definite)
            {
                FeatureValue thisValue = Dereference(featVal.Value);
                FeatureValue otherValue;
                if (other._definite.TryGetValue(featVal.Key, out otherValue))
                {
                    otherValue = Dereference(otherValue);
                    var otherFS = otherValue as FeatureStruct;
                    if (otherFS != null && !copies.ContainsKey(otherFS))
                    {
                        var thisFS = thisValue as FeatureStruct;
                        if (thisFS != null)
                        {
                            thisFS.PriorityUnion(otherFS, varBindings, copies);
                        }
                    }
                }
            }

            foreach (KeyValuePair <Feature, FeatureValue> featVal in other._definite)
            {
                FeatureValue otherValue = Dereference(featVal.Value);
                FeatureValue thisValue;
                if (_definite.TryGetValue(featVal.Key, out thisValue))
                {
                    otherValue = Dereference(otherValue);
                    var otherFS = otherValue as FeatureStruct;
                    if (otherFS != null)
                    {
                        if (thisValue is FeatureStruct)
                        {
                            FeatureValue reentrant;
                            if (copies.TryGetValue(otherFS, out reentrant))
                            {
                                _definite[featVal.Key] = reentrant;
                            }
                        }
                        else
                        {
                            _definite[featVal.Key] = otherFS.DeepCloneImpl(copies);
                        }
                    }
                    else
                    {
                        var otherSfv = (SimpleFeatureValue)otherValue;
                        SimpleFeatureValue binding;
                        if (otherSfv.IsVariable && varBindings.TryGetValue(otherSfv.VariableName, out binding))
                        {
                            _definite[featVal.Key] = binding.GetVariableValue(otherSfv.Agree);
                        }
                        else
                        {
                            _definite[featVal.Key] = otherSfv.DeepCloneImpl(copies);
                        }
                    }
                }
                else
                {
                    _definite[featVal.Key] = otherValue.DeepCloneImpl(copies);
                }
            }
        }