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); }
private void RemoveVariables(ISet <FeatureStruct> visited) { if (visited.Contains(this)) { return; } visited.Add(this); foreach (KeyValuePair <Feature, FeatureValue> featVal in _definite.ToArray()) { FeatureValue value = Dereference(featVal.Value); var sfv = value as SimpleFeatureValue; if (sfv != null) { if (sfv.IsVariable) { _definite.Remove(featVal.Key); } } else { var fs = (FeatureStruct)value; fs.RemoveVariables(visited); if (fs.IsEmpty) { _definite.Remove(featVal.Key); } } } }
internal override bool ValueEqualsImpl(FeatureValue other, ISet <FeatureValue> visitedSelf, ISet <FeatureValue> visitedOther, IDictionary <FeatureValue, FeatureValue> visitedPairs) { if (other == null) { return(false); } SimpleFeatureValue otherSfv; if (!Dereference(other, out otherSfv)) { return(false); } if (this == otherSfv) { return(true); } if (visitedSelf.Contains(this) || visitedOther.Contains(otherSfv)) { FeatureValue fv; if (visitedPairs.TryGetValue(this, out fv)) { return(fv == otherSfv); } return(false); } visitedSelf.Add(this); visitedOther.Add(otherSfv); visitedPairs[this] = otherSfv; return(ValueEquals(otherSfv)); }
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); }
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); }
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); }
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); }
internal override bool ValueEqualsImpl(FeatureValue other, ISet <FeatureValue> visitedSelf, ISet <FeatureValue> visitedOther, IDictionary <FeatureValue, FeatureValue> visitedPairs) { if (other == null) { return(false); } FeatureStruct otherFS; if (!Dereference(other, out otherFS)) { return(false); } if (this == otherFS) { return(true); } if (visitedSelf.Contains(this) || visitedOther.Contains(otherFS)) { FeatureValue fv; if (visitedPairs.TryGetValue(this, out fv)) { return(fv == otherFS); } return(false); } visitedSelf.Add(this); visitedOther.Add(otherFS); visitedPairs[this] = otherFS; if (_definite.Count != otherFS._definite.Count) { return(false); } foreach (KeyValuePair <Feature, FeatureValue> kvp in _definite) { FeatureValue thisValue = Dereference(kvp.Value); FeatureValue otherValue; if (!otherFS._definite.TryGetValue(kvp.Key, out otherValue)) { return(false); } otherValue = Dereference(otherValue); if (!thisValue.ValueEqualsImpl(otherValue, visitedSelf, visitedOther, visitedPairs)) { return(false); } } return(true); }
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); }
public void AddValue(IEnumerable <Feature> path, FeatureValue value) { CheckFrozen(); Feature lastFeature; FeatureStruct lastFS; if (FollowPath(path, out lastFeature, out lastFS)) { lastFS._definite[lastFeature] = value; } throw new ArgumentException("The feature path is invalid.", "path"); }
/// <summary> /// Adds the specified feature-value pair. /// </summary> /// <param name="feature">The feature.</param> /// <param name="value">The value.</param> public void AddValue(Feature feature, FeatureValue value) { if (feature == null) { throw new ArgumentNullException("feature"); } if (value == null) { throw new ArgumentNullException("value"); } CheckFrozen(); _definite[feature] = value; }
internal override string ToStringImpl(ISet <FeatureValue> visited, IDictionary <FeatureValue, int> reentranceIds) { if (visited.Contains(this)) { return(string.Format("<{0}>", reentranceIds[this])); } visited.Add(this); var sb = new StringBuilder(); int id; if (reentranceIds.TryGetValue(this, out id)) { sb.Append(id); sb.Append("="); } if (_definite.Count > 0) { bool firstFeature = true; if (_definite.Count > 0) { sb.Append("["); } foreach (KeyValuePair <Feature, FeatureValue> kvp in _definite) { FeatureValue value = Dereference(kvp.Value); if (!firstFeature) { sb.Append(", "); } sb.Append(kvp.Key.Description); sb.Append(":"); sb.Append(value.ToStringImpl(visited, reentranceIds)); firstFeature = false; } if (_definite.Count > 0) { sb.Append("]"); } } else { sb.Append("ANY"); } return(sb.ToString()); }
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); }
internal override void FindReentrances(IDictionary <FeatureValue, bool> reentrances) { if (reentrances.ContainsKey(this)) { reentrances[this] = true; } else { reentrances[this] = false; foreach (FeatureValue value in _definite.Values) { FeatureValue v = Dereference(value); v.FindReentrances(reentrances); } } }
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); }
internal override int FreezeImpl(ISet <FeatureValue> visited) { if (visited.Contains(this)) { return(1); } visited.Add(this); IsFrozen = true; int code = 23; foreach (KeyValuePair <Feature, FeatureValue> kvp in _definite.OrderBy(kvp => kvp.Key.ID)) { code = code * 31 + kvp.Key.GetHashCode(); FeatureValue value = Dereference(kvp.Value); code = code * 31 + value.FreezeImpl(visited); } return(code); }
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; } }
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); } } }
public override bool ValueEquals(FeatureValue other) { var otherSfv = other as SimpleFeatureValue; return(otherSfv != null && ValueEquals(otherSfv)); }
public override bool ValueEquals(FeatureValue other) { var otherFS = other as FeatureStruct; return(otherFS != null && ValueEquals(otherFS)); }
/// <summary> /// Adds the specified feature-value pair. /// </summary> /// <param name="feature">The feature.</param> /// <param name="value">The value.</param> public void AddValue(Feature feature, FeatureValue value) { CheckFrozen(); _definite[feature] = value; }
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); }
internal override bool AddImpl(FeatureValue other, VariableBindings varBindings, IDictionary <FeatureStruct, ISet <FeatureStruct> > visited) { return(UnionImpl(other, varBindings, visited)); }
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); }