/// <summary> /// Creates a deep copy of the substitution. /// </summary> /// <returns>A copy of the substitution.</returns> public ISubstitution Clone() { Substitution newSubstitution = new Substitution(); foreach (var item in this) { newSubstitution.Add(item.Key, item.Value); } return(newSubstitution); }
/// <summary> /// Creates a substitution from the given parameters and substituted constants. /// </summary> /// <param name="parameters">Parameters.</param> /// <param name="constNames">Constant names.</param> /// <returns>New substitution.</returns> public ISubstitution CreateSubstitution(Parameters parameters, params string[] constNames) { Debug.Assert(parameters.Count == constNames.Length); ISubstitution substitution = new Substitution(); for (int i = 0; i < parameters.Count; ++i) { if (!string.IsNullOrEmpty(constNames[i])) { substitution.Add(parameters[i].ParameterNameId, CreateConstant(constNames[i])); } } return(substitution); }
/// <summary> /// Gets the unification with the specified atom, i.e. calculates the variable substitution required to ground variable terms of the /// current atom with the grounded terms (i.e. constants) of the other atom. /// </summary> /// <param name="referenceAtom">Reference atom.</param> /// <returns>Unification in the form of variable substitution.</returns> public ISubstitution GetUnificationWith(IAtom referenceAtom) { Debug.Assert(GetNameId() == referenceAtom.GetNameId() && GetTerms().Count == referenceAtom.GetTerms().Count, "Unification of incompatible atoms!"); ISubstitution unification = new Substitution(); int termIndex = 0; foreach (var term in GetTerms()) { VariableTerm variableTerm = term as VariableTerm; if (variableTerm != null) { ConstantTerm valueTerm = referenceAtom.GetTerms()[termIndex] as ConstantTerm; if (valueTerm != null) { unification.Add(variableTerm.NameId, valueTerm.NameId); } } ++termIndex; } return(unification); }