private void lookForUnitRefutation(OTTERAnswerHandler ansHandler,
                                           IndexedClauses idxdClauses, Clause clause, List <Clause> sos,
                                           List <Clause> usable)
        {
            List <Clause> toCheck = new List <Clause>();

            if (ansHandler.isCheckForUnitRefutation(clause))
            {
                foreach (Clause s in sos)
                {
                    if (s.isUnitClause())
                    {
                        toCheck.Add(s);
                    }
                }
                foreach (Clause u in usable)
                {
                    if (u.isUnitClause())
                    {
                        toCheck.Add(u);
                    }
                }
            }

            if (toCheck.Count > 0)
            {
                toCheck = infer(clause, toCheck);
                foreach (Clause t in toCheck)
                {
                    // * clause <- SIMPLIFY(clause)
                    Clause t2 = getClauseSimplifier().simplify(t);

                    // * discard clause if it is a tautology
                    if (t2.isTautology())
                    {
                        continue;
                    }

                    // * if clause has no literals then a refutation has been found
                    // or if it just contains the answer literal.
                    if (!ansHandler.isAnswer(t2))
                    {
                        // * sos <- [clause | sos]
                        // This check ensure duplicate clauses are not
                        // introduced which will cause the
                        // LightestClauseHeuristic to loop continuously
                        // on the same pair of objects.
                        if (!sos.Contains(t2) && !usable.Contains(t2))
                        {
                            idxdClauses.addClause(t2, sos, usable);
                        }
                    }

                    if (ansHandler.isComplete())
                    {
                        break;
                    }
                }
            }
        }
        // END-InferenceProcedure

        /**
         * <pre>
         * procedure OTTER(sos, usable)
         *   inputs: sos, a set of support-clauses defining the problem (a global variable)
         *   usable, background knowledge potentially relevant to the problem
         * </pre>
         */
        private InferenceResult otter(OTTERAnswerHandler ansHandler,
                                      IndexedClauses idxdClauses, List <Clause> sos, List <Clause> usable)
        {
            getLightestClauseHeuristic().initialSOS(sos);

            // * repeat
            do
            {
                // * clause <- the lightest member of sos
                Clause clause = getLightestClauseHeuristic().getLightestClause();
                if (null != clause)
                {
                    // * move clause from sos to usable
                    sos.Remove(clause);
                    getLightestClauseHeuristic().removedClauseFromSOS(clause);
                    usable.Add(clause);
                    // * PROCESS(INFER(clause, usable), sos)
                    process(ansHandler, idxdClauses, infer(clause, usable), sos,
                            usable);
                }
                // * until sos = [] or a refutation has been found
            } while (sos.Count != 0 && !ansHandler.isComplete());

            return(ansHandler);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///<code>
        /// procedure OTTER(sos, usable) 
        ///  inputs: sos, a set of support-clauses defining the problem (a global variable) 
        ///  usable, background knowledge potentially relevant to the problem
        ///</code>
        /// </summary>
        /// <param name="ansHandler"></param>
        /// <param name="idxdClauses"></param>
        /// <param name="sos"></param>
        /// <param name="usable"></param>
        /// <returns></returns>
        private IInferenceResult Otter(OTTERAnswerHandler ansHandler,
                IndexedClauses idxdClauses, ISet<Clause> sos, ISet<Clause> usable) 
        {

            LightestClauseHeuristic.InitialSOS(sos);

            // * repeat
            do {
                // * clause <- the lightest member of sos
                Clause clause = LightestClauseHeuristic.GetLightestClause();
                if (null != clause) 
                {
                    // * move clause from sos to usable
                    sos.Remove(clause);
                    LightestClauseHeuristic.RemovedClauseFromSOS(clause);
                    usable.Add(clause);
                    // * PROCESS(INFER(clause, usable), sos)
                    this.Process(ansHandler, idxdClauses, this.Infer(clause, usable), sos, usable);
                }

                // * until sos = [] or a refutation has been found
            } 
            while (sos.Count != 0 && !ansHandler.IsComplete());

            return ansHandler;
        }
        // procedure PROCESS(clauses, sos)
        private void process(OTTERAnswerHandler ansHandler,
                             IndexedClauses idxdClauses, List <Clause> clauses, List <Clause> sos,
                             List <Clause> usable)
        {
            // * for each clause in clauses do
            foreach (Clause clause in clauses)
            {
                // * clause <- SIMPLIFY(clause)
                Clause clause2 = getClauseSimplifier().simplify(clause);

                // * merge identical literals
                // Note: Not required as handled by Clause Implementation
                // which keeps literals within a Set, so no duplicates
                // will exist.

                // * discard clause if it is a tautology
                if (clause2.isTautology())
                {
                    continue;
                }

                // * if clause has no literals then a refutation has been found
                // or if it just contains the answer literal.
                if (!ansHandler.isAnswer(clause2))
                {
                    // * sos <- [clause | sos]
                    // This check ensure duplicate clauses are not
                    // introduced which will cause the
                    // LightestClauseHeuristic to loop continuously
                    // on the same pair of objects.
                    if (!sos.Contains(clause2) && !usable.Contains(clause2))
                    {
                        foreach (Clause ac in clause2.getFactors())
                        {
                            if (!sos.Contains(ac) && !usable.Contains(ac))
                            {
                                idxdClauses.addClause(ac, sos, usable);

                                // * if clause has one literal then look for unit
                                // refutation
                                lookForUnitRefutation(ansHandler, idxdClauses, ac,
                                                      sos, usable);
                            }
                        }
                    }
                }

                if (ansHandler.isComplete())
                {
                    break;
                }
            }
        }
        // START-InferenceProcedure
        public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha)
        {
            List <Clause> sos    = new List <Clause>();
            List <Clause> usable = new List <Clause>();

            // Usable set will be the set of clauses in the KB,
            // are assuming this is satisfiable as using the
            // Set of Support strategy.
            foreach (Clause c in KB.getAllClauses())
            {
                Clause c2 = KB.standardizeApart(c);
                c2.setStandardizedApartCheckNotRequired();
                usable.AddRange(c2.getFactors());
            }

            // Ensure reflexivity axiom is added to usable if using paramodulation.
            if (isUseParamodulation())
            {
                // Reflexivity Axiom: x = x
                TermEquality reflexivityAxiom = new TermEquality(new Variable("x"),
                                                                 new Variable("x"));
                Clause reflexivityClause = new Clause();
                reflexivityClause.addLiteral(new Literal(reflexivityAxiom));
                reflexivityClause = KB.standardizeApart(reflexivityClause);
                reflexivityClause.setStandardizedApartCheckNotRequired();
                usable.Add(reflexivityClause);
            }

            Sentence notAlpha = new NotSentence(alpha);
            // Want to use an answer literal to pull
            // query variables where necessary
            Literal         answerLiteral          = KB.createAnswerLiteral(notAlpha);
            List <Variable> answerLiteralVariables = KB
                                                     .collectAllVariables(answerLiteral.getAtomicSentence());
            Clause answerClause = new Clause();

            if (answerLiteralVariables.Count > 0)
            {
                Sentence notAlphaWithAnswer = new ConnectedSentence(Connectors.OR,
                                                                    notAlpha, answerLiteral.getAtomicSentence());
                foreach (Clause c in KB.convertToClauses(notAlphaWithAnswer))
                {
                    Clause c2 = KB.standardizeApart(c);
                    c2.setProofStep(new ProofStepGoal(c2));
                    c2.setStandardizedApartCheckNotRequired();
                    sos.AddRange(c2.getFactors());
                }

                answerClause.addLiteral(answerLiteral);
            }
            else
            {
                foreach (Clause c in KB.convertToClauses(notAlpha))
                {
                    Clause c2 = KB.standardizeApart(c);
                    c2.setProofStep(new ProofStepGoal(c2));
                    c2.setStandardizedApartCheckNotRequired();
                    sos.AddRange(c2.getFactors());
                }
            }

            // Ensure all subsumed clauses are removed
            foreach (Clause c in SubsumptionElimination.findSubsumedClauses(usable))
            {
                usable.Remove(c);
            }
            foreach (Clause c in SubsumptionElimination.findSubsumedClauses(sos))
            {
                sos.Remove(c);
            }

            OTTERAnswerHandler ansHandler = new OTTERAnswerHandler(answerLiteral,
                                                                   answerLiteralVariables, answerClause, maxQueryTime);

            IndexedClauses idxdClauses = new IndexedClauses(
                getLightestClauseHeuristic(), sos, usable);

            return(otter(ansHandler, idxdClauses, sos, usable));
        }
Ejemplo n.º 6
0
        public IInferenceResult Ask(FOLKnowledgeBase KB, ISentence alpha) 
        {
            ISet<Clause> sos = new HashedSet<Clause>();
            ISet<Clause> usable = new HashedSet<Clause>();

            // Usable set will be the set of clauses in the KB,
            // are assuming this is satisfiable as using the
            // ISet of Support strategy.
            //foreach (var standardizedC in KB.GetAllClauses().Select(c => KB.StandardizeApart(c)))
            foreach (var standardizedC in KB.GetAllClauses())
            {
                standardizedC.SetStandardizedApartCheckNotRequired();
                usable.UnionWith(standardizedC.GetFactors());
            }

            // Ensure reflexivity axiom is added to usable if using paramodulation.
            if (this.UseParamodulation) 
            {
                // Reflexivity Axiom: x = x
                var reflexivityAxiom = new TermEquality(new Variable("x"), new Variable("x"));
                var reflexivityClause = new Clause();
                reflexivityClause.AddLiteral(new Literal(reflexivityAxiom));
                reflexivityClause = KB.StandardizeApart(reflexivityClause);
                reflexivityClause.SetStandardizedApartCheckNotRequired();
                usable.Add(reflexivityClause);
            }

            ISentence notAlpha = new NotSentence(alpha);
            // Want to use an answer literal to pull
            // query variables where necessary
            var answerLiteral = KB.CreateAnswerLiteral(notAlpha);
            var answerLiteralVariables = KB
                    .CollectAllVariables(answerLiteral.AtomicSentence);
            var answerClause = new Clause();

            if (answerLiteralVariables.Count > 0) {
                ISentence notAlphaWithAnswer = new ConnectedSentence(Connectors.Or,
                        notAlpha, answerLiteral.AtomicSentence);
               // foreach (var standardizedC in
              //      KB.ConvertToClauses(notAlphaWithAnswer).Select(KB.StandardizeApart))
                  foreach (var standardizedC in
                      KB.ConvertToClauses(notAlphaWithAnswer))
                   {
                       Clause c = KB.StandardizeApart(standardizedC);
                       c.SetProofStep(new ProofStepGoal(c));
                       c.SetStandardizedApartCheckNotRequired();
                       sos.UnionWith(c.GetFactors());
                   }

                answerClause.AddLiteral(answerLiteral);
            } 
            else 
            {
                //foreach (var standardizedC in
                //    KB.ConvertToClauses(notAlpha).Select(KB.StandardizeApart)) 
                foreach (var standardizedC in
                    KB.ConvertToClauses(notAlpha))
                {
                    Clause c = KB.StandardizeApart(standardizedC);
                    c.SetProofStep(new ProofStepGoal(c));
                    c.SetStandardizedApartCheckNotRequired();
                    sos.UnionWith(standardizedC.GetFactors());
                }
            }

            // Ensure all subsumed clauses are removed
            usable.ExceptWith(SubsumptionElimination.FindSubsumedClauses(usable));
            sos.ExceptWith(SubsumptionElimination.FindSubsumedClauses(sos));

            var ansHandler = new OTTERAnswerHandler(answerLiteral,
                    answerLiteralVariables, answerClause, this.MaxQueryTime);

            var idxdClauses = new IndexedClauses(LightestClauseHeuristic, sos, usable);

            return this.Otter(ansHandler, idxdClauses, sos, usable);
        }
        private void lookForUnitRefutation(OTTERAnswerHandler ansHandler,
                IndexedClauses idxdClauses, Clause clause, List<Clause> sos,
                List<Clause> usable)
        {

            List<Clause> toCheck = new List<Clause>();

            if (ansHandler.isCheckForUnitRefutation(clause))
            {
                foreach (Clause s in sos)
                {
                    if (s.isUnitClause())
                    {
                        toCheck.Add(s);
                    }
                }
                foreach (Clause u in usable)
                {
                    if (u.isUnitClause())
                    {
                        toCheck.Add(u);
                    }
                }
            }

            if (toCheck.Count > 0)
            {
                toCheck = infer(clause, toCheck);
                foreach (Clause t in toCheck)
                {
                    // * clause <- SIMPLIFY(clause)
                    Clause t2 = getClauseSimplifier().simplify(t);

                    // * discard clause if it is a tautology
                    if (t2.isTautology())
                    {
                        continue;
                    }

                    // * if clause has no literals then a refutation has been found
                    // or if it just contains the answer literal.
                    if (!ansHandler.isAnswer(t2))
                    {
                        // * sos <- [clause | sos]
                        // This check ensure duplicate clauses are not
                        // introduced which will cause the
                        // LightestClauseHeuristic to loop continuously
                        // on the same pair of objects.
                        if (!sos.Contains(t2) && !usable.Contains(t2))
                        {
                            idxdClauses.addClause(t2, sos, usable);
                        }
                    }

                    if (ansHandler.isComplete())
                    {
                        break;
                    }
                }
            }
        }
        // procedure PROCESS(clauses, sos)
        private void process(OTTERAnswerHandler ansHandler,
                IndexedClauses idxdClauses, List<Clause> clauses, List<Clause> sos,
                List<Clause> usable)
        {

            // * for each clause in clauses do
            foreach (Clause clause in clauses)
            {
                // * clause <- SIMPLIFY(clause)
                Clause clause2 = getClauseSimplifier().simplify(clause);

                // * merge identical literals
                // Note: Not required as handled by Clause Implementation
                // which keeps literals within a Set, so no duplicates
                // will exist.

                // * discard clause if it is a tautology
                if (clause2.isTautology())
                {
                    continue;
                }

                // * if clause has no literals then a refutation has been found
                // or if it just contains the answer literal.
                if (!ansHandler.isAnswer(clause2))
                {
                    // * sos <- [clause | sos]
                    // This check ensure duplicate clauses are not
                    // introduced which will cause the
                    // LightestClauseHeuristic to loop continuously
                    // on the same pair of objects.
                    if (!sos.Contains(clause2) && !usable.Contains(clause2))
                    {
                        foreach (Clause ac in clause2.getFactors())
                        {
                            if (!sos.Contains(ac) && !usable.Contains(ac))
                            {
                                idxdClauses.addClause(ac, sos, usable);

                                // * if clause has one literal then look for unit
                                // refutation
                                lookForUnitRefutation(ansHandler, idxdClauses, ac,
                                        sos, usable);
                            }
                        }
                    }
                }

                if (ansHandler.isComplete())
                {
                    break;
                }
            }
        }
        // END-InferenceProcedure
        //

        /**
         * <pre>
         * procedure OTTER(sos, usable) 
         *   inputs: sos, a set of support-clauses defining the problem (a global variable) 
         *   usable, background knowledge potentially relevant to the problem
         * </pre>
         */
        private InferenceResult otter(OTTERAnswerHandler ansHandler,
                IndexedClauses idxdClauses, List<Clause> sos, List<Clause> usable)
        {

            getLightestClauseHeuristic().initialSOS(sos);

            // * repeat
            do
            {
                // * clause <- the lightest member of sos
                Clause clause = getLightestClauseHeuristic().getLightestClause();
                if (null != clause)
                {
                    // * move clause from sos to usable
                    sos.Remove(clause);
                    getLightestClauseHeuristic().removedClauseFromSOS(clause);
                    usable.Add(clause);
                    // * PROCESS(INFER(clause, usable), sos)
                    process(ansHandler, idxdClauses, infer(clause, usable), sos,
                            usable);
                }

                // * until sos = [] or a refutation has been found
            } while (sos.Count != 0 && !ansHandler.isComplete());

            return ansHandler;
        }
        //
        // START-InferenceProcedure
        public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha) {
		List<Clause> sos = new List<Clause>();
		List<Clause> usable = new List<Clause>();

		// Usable set will be the set of clauses in the KB,
		// are assuming this is satisfiable as using the
		// Set of Support strategy.
		foreach (Clause c in KB.getAllClauses()) {
			Clause c2 = KB.standardizeApart(c);
			c2.setStandardizedApartCheckNotRequired();
			usable.AddRange(c2.getFactors());
		}

		// Ensure reflexivity axiom is added to usable if using paramodulation.
		if (isUseParamodulation()) {
			// Reflexivity Axiom: x = x
			TermEquality reflexivityAxiom = new TermEquality(new Variable("x"),
					new Variable("x"));
			Clause reflexivityClause = new Clause();
			reflexivityClause.addLiteral(new Literal(reflexivityAxiom));
			reflexivityClause = KB.standardizeApart(reflexivityClause);
			reflexivityClause.setStandardizedApartCheckNotRequired();
			usable.Add(reflexivityClause);
		}

		Sentence notAlpha = new NotSentence(alpha);
		// Want to use an answer literal to pull
		// query variables where necessary
		Literal answerLiteral = KB.createAnswerLiteral(notAlpha);
		List<Variable> answerLiteralVariables = KB
				.collectAllVariables(answerLiteral.getAtomicSentence());
		Clause answerClause = new Clause();

		if (answerLiteralVariables.Count > 0) {
			Sentence notAlphaWithAnswer = new ConnectedSentence(Connectors.OR,
					notAlpha, answerLiteral.getAtomicSentence());
			foreach (Clause c in KB.convertToClauses(notAlphaWithAnswer)) {
			    Clause c2 = KB.standardizeApart(c);
				c2.setProofStep(new ProofStepGoal(c2));
				c2.setStandardizedApartCheckNotRequired();
				sos.AddRange(c2.getFactors());
			}

			answerClause.addLiteral(answerLiteral);
		} else {
			foreach (Clause c in KB.convertToClauses(notAlpha)) {
				Clause c2 = KB.standardizeApart(c);
				c2.setProofStep(new ProofStepGoal(c2));
				c2.setStandardizedApartCheckNotRequired();
				sos.AddRange(c2.getFactors());
			}
		}

		// Ensure all subsumed clauses are removed
        foreach (Clause c in SubsumptionElimination.findSubsumedClauses(usable))
        {
            usable.Remove(c);
        }
        foreach (Clause c in SubsumptionElimination.findSubsumedClauses(sos))
        {
            sos.Remove(c);
        }

		OTTERAnswerHandler ansHandler = new OTTERAnswerHandler(answerLiteral,
				answerLiteralVariables, answerClause, maxQueryTime);

		IndexedClauses idxdClauses = new IndexedClauses(
				getLightestClauseHeuristic(), sos, usable);

		return otter(ansHandler, idxdClauses, sos, usable);
	}