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); }
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); }
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); }
public void Replace(VariableBindings varBindings) { foreach (KeyValuePair <string, SimpleFeatureValue> varBinding in varBindings) { _varBindings[varBinding.Key] = varBinding.Value; } }
protected VariableBindings(VariableBindings varBindings) : this() { foreach (KeyValuePair <string, SimpleFeatureValue> kvp in varBindings._varBindings) { _varBindings[kvp.Key] = kvp.Value.DeepClone(); } }
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 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 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); }
public void PriorityUnion(FeatureStruct other, VariableBindings varBindings) { if (other == null) { throw new ArgumentNullException("other"); } CheckFrozen(); PriorityUnion(other, varBindings, new Dictionary <FeatureValue, FeatureValue>()); }
public void Subtract(FeatureStruct other, VariableBindings varBindings) { if (other == null) { throw new ArgumentNullException("other"); } CheckFrozen(); SubtractImpl(other, varBindings, new Dictionary <FeatureStruct, ISet <FeatureStruct> >()); }
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); }
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); }
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); }
/// <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); }
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); }
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); }
/// <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); }
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; } }
internal abstract bool IsUnifiableImpl(FeatureValue other, bool useDefaults, VariableBindings varBindings);
public bool Subsumes(FeatureStruct other, VariableBindings varBindings) { return(Subsumes(other, false, varBindings)); }
public bool Unify(FeatureStruct other, VariableBindings varBindings, out FeatureStruct output) { return(Unify(other, false, varBindings, out output)); }
internal override bool AddImpl(FeatureValue other, VariableBindings varBindings, IDictionary <FeatureStruct, ISet <FeatureStruct> > visited) { return(UnionImpl(other, varBindings, visited)); }
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 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 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); }
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)); }
protected abstract bool NondestructiveUnify(FeatureValue other, bool useDefaults, IDictionary <FeatureValue, FeatureValue> copies, VariableBindings varBindings, out FeatureValue output);
internal abstract bool DestructiveUnify(FeatureValue other, bool useDefaults, bool preserveInput, IDictionary <FeatureValue, FeatureValue> copies, VariableBindings varBindings);
internal abstract bool SubsumesImpl(FeatureValue other, bool useDefaults, VariableBindings varBindings);
internal abstract bool SubtractImpl(FeatureValue other, VariableBindings varBindings, IDictionary <FeatureStruct, ISet <FeatureStruct> > visited);