/// <summary> /// Returns the possible ways that a query term can unify with a program term /// </summary> public static IBindings Unify(this ILiteral query, ILiteral program, IBindings bindings = null) { var simpleUnifier = new SimpleUnifier(); var freeVariables = new HashSet <ILiteral>(); // Run the unifier var queryFreeVars = simpleUnifier.QueryUnifier.Compile(query, bindings); if (queryFreeVars == null) { return(null); } simpleUnifier.PrepareToRunProgram(); var programFreeVars = simpleUnifier.ProgramUnifier.Compile(program, bindings); if (programFreeVars == null) { return(null); } freeVariables.UnionWith(queryFreeVars); // Retrieve the unified value for the program var result = simpleUnifier.UnifiedValue(query.UnificationKey ?? query); // If the result was valid, return as the one value from this function if (result != null) { var variableBindings = freeVariables.ToDictionary(variable => variable, variable => simpleUnifier.UnifiedValue(variable)); return(new BasicBinding(result, variableBindings)); } else { return(null); } }
public void ManualUnification() { var simpleUnifier = new SimpleUnifier(); var unifier = new TraceUnifier(simpleUnifier); var X = new[] { new Variable(), new Variable(), new Variable(), new Variable(), new Variable(), new Variable(), new Variable(), new Variable() }; var h2 = new UnboundFunctor(2); var f1 = new UnboundFunctor(1); var p3 = new UnboundFunctor(3); var a0 = new UnboundFunctor(0); unifier.BindVariable(0, X[1]); unifier.BindVariable(1, X[2]); unifier.BindVariable(2, X[3]); unifier.BindVariable(3, X[4]); unifier.BindVariable(4, X[5]); unifier.BindVariable(5, X[6]); unifier.BindVariable(6, X[7]); unifier.PutStructure(h2, 2, X[3]); unifier.SetVariable(X[2]); unifier.SetVariable(X[5]); unifier.PutStructure(f1, 1, X[4]); unifier.SetValue(X[5]); unifier.PutStructure(p3, 3, X[1]); unifier.SetValue(X[2]); unifier.SetValue(X[3]); unifier.SetValue(X[4]); unifier.GetStructure(p3, 3, X[1]); unifier.UnifyVariable(X[2]); unifier.UnifyVariable(X[3]); unifier.UnifyVariable(X[4]); unifier.GetStructure(f1, 1, X[2]); unifier.UnifyVariable(X[5]); unifier.GetStructure(h2, 2, X[3]); unifier.UnifyValue(X[4]); unifier.UnifyVariable(X[6]); unifier.GetStructure(f1, 1, X[6]); unifier.UnifyVariable(X[7]); unifier.GetStructure(a0, 0, X[7]); var result = simpleUnifier.UnifiedValue(X[1]); Assert.IsNotNull(result); }