Exemplo n.º 1
0
        public static Clause ApplySubstitution(Clause X, Substitution T)
        {
            for (int i = 0; i < X.literal.Count; i++)
            {
                X.literal[i] = Literal.ApplySubstitution(X.literal[i], T);
            }

            return(X);
        }
Exemplo n.º 2
0
        public static Literal ApplySubstitution(Literal A, Substitution T)
        {
            String oldLiteral = A.Compose();

            for (int i = 0; i < T.replacementList.Count; i++)
            {
                Replacement R       = T.replacementList[i];
                String      varName = R.X.name;

                oldLiteral = oldLiteral.Replace(varName, R.t.Compose());
            }

            return(Literal.Decompose(oldLiteral));
        }
Exemplo n.º 3
0
 public SolutionStep(Goal g, Clause c, Substitution theta)
 {
     this.goal         = g.Compose();
     this.clause       = c.Compose();
     this.substitution = theta.Compose();
 }
Exemplo n.º 4
0
        public static void Solve(Goal goal)
        {
            // End condition
            if (_LOOP_IDENTIFIER_ <= 0)
            {
                //solutionFound = true;
                //solution = new List<SolutionStep>();
                //solution.Add(new SolutionStep());
                return;
            }
            else
            {
                if (goal.IsEmpty() && !solutionFound)
                {
                    solutionFound = true;
                    solution.Add(new SolutionStep());
                }

                // Find solution using back-tracking

                for (int i = 0; i < goal.literal.Count && !solutionFound; i++)
                {
                    Literal Ai = goal.literal[i];
                    for (int j = 0; j < logicProgram.Count && !solutionFound; j++)
                    {
                        Literal Bj = logicProgram[j].literal[0];

                        Substitution thetai = Solver.Unify(Ai, Bj);

                        if (thetai != null && thetai.isValid())
                        {
                            // Log solution step
                            solution.Add(new SolutionStep(goal, logicProgram[j], thetai));

                            // Backup current goal
                            //Literal backup = goal.literal[i];
                            Goal backupGoal = Lib.DeepClone <Goal>(goal);

                            // Change goal
                            goal.literal.RemoveAt(i);
                            goal.literal.InsertRange(i, logicProgram[j].literal.GetRange(1, logicProgram[j].literal.Count - 1));

                            goal = Goal.ApplySubstitution(goal, thetai);

                            _LOOP_IDENTIFIER_--;
                            // ====================== Recursive call
                            Solve(goal);
                            // ====================== Recursive call
                            _LOOP_IDENTIFIER_++;

                            if (!solutionFound)
                            {
                                // Restore goal
                                //goal.literal.RemoveRange(i, logicProgram[j].literal.Count - 1);
                                //goal.literal.Insert(i, backup);
                                goal = backupGoal;

                                // Remove solution step
                                solution.RemoveAt(solution.Count - 1);
                            }
                        }
                    }
                }
            }
        }