コード例 #1
0
            public AnswerHandler(FOLKnowledgeBase kb, Sentence query, long maxQueryTime, FOLModelElimination fOLModelElimination)
            {
                finishTime = CommonFactory.Now().AddMilliseconds(maxQueryTime);

                Sentence refutationQuery = new NotSentence(query);

                // Want to use an answer literal to pull
                // query variables where necessary
                Literal answerLiteral = kb.createAnswerLiteral(refutationQuery);

                answerLiteralVariables = kb.collectAllVariables(answerLiteral.getAtomicSentence());

                // Create the Set of Support based on the Query.
                if (answerLiteralVariables.Size() > 0)
                {
                    Sentence refutationQueryWithAnswer = new ConnectedSentence(
                        Connectors.OR, refutationQuery, answerLiteral
                        .getAtomicSentence().copy());

                    sos = fOLModelElimination.createChainsFromClauses(kb.convertToClauses(refutationQueryWithAnswer));

                    answerChain.addLiteral(answerLiteral);
                }
                else
                {
                    sos = fOLModelElimination.createChainsFromClauses(kb.convertToClauses(refutationQuery));
                }

                foreach (Chain s in sos)
                {
                    s.setProofStep(new ProofStepGoal(s));
                }
            }
            public AnswerHandler(FOLKnowledgeBase kb, Sentence aQuery,
                                 long maxQueryTime, FOLModelElimination folModelElimination)
            {
                finishTime = System.DateTime.UtcNow.Ticks + maxQueryTime;

                Sentence refutationQuery = new NotSentence(aQuery);

                // Want to use an answer literal to pull
                // query variables where necessary
                Literal answerLiteral = kb.createAnswerLiteral(refutationQuery);

                answerLiteralVariables = kb.collectAllVariables(answerLiteral
                                                                .getAtomicSentence());

                // Create the Set of Support based on the Query.
                if (answerLiteralVariables.Count > 0)
                {
                    Sentence refutationQueryWithAnswer = new ConnectedSentence(
                        Connectors.OR, refutationQuery, (Sentence)answerLiteral
                        .getAtomicSentence().copy());

                    sos = folModelElimination.createChainsFromClauses(kb
                                                                      .convertToClauses(refutationQueryWithAnswer));

                    answerChain.addLiteral(answerLiteral);
                }
                else
                {
                    sos = folModelElimination.createChainsFromClauses(kb
                                                                      .convertToClauses(refutationQuery));
                }

                foreach (Chain s in sos)
                {
                    s.setProofStep(new ProofStepGoal(s));
                }
            }
        // 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));
        }
コード例 #4
0
        //
        // START-InferenceProcedure
        public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha)
        {
            // clauses <- the set of clauses in CNF representation of KB ^ ~alpha
            ISet <Clause> clauses = CollectionFactory.CreateSet <Clause>();

            foreach (Clause cIter in KB.getAllClauses())
            {
                Clause c = cIter;
                c = KB.standardizeApart(c);
                c.setStandardizedApartCheckNotRequired();
                clauses.AddAll(c.getFactors());
            }
            Sentence notAlpha = new NotSentence(alpha);
            // Want to use an answer literal to pull
            // query variables where necessary
            Literal         answerLiteral          = KB.createAnswerLiteral(notAlpha);
            ISet <Variable> answerLiteralVariables = KB.collectAllVariables(answerLiteral.getAtomicSentence());
            Clause          answerClause           = new Clause();

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

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

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

            // new <- {}
            ISet <Clause> newClauses = CollectionFactory.CreateSet <Clause>();
            ISet <Clause> toAdd      = CollectionFactory.CreateSet <Clause>();
            // loop do
            int noOfPrevClauses = clauses.Size();

            do
            {
                if (null != tracer)
                {
                    tracer.stepStartWhile(clauses, clauses.Size(), newClauses.Size());
                }

                newClauses.Clear();

                // for each Ci, Cj in clauses do
                Clause[] clausesA = clauses.ToArray();

                // Basically, using the simple T)wo F)inger M)ethod here.
                for (int i = 0; i < clausesA.Length; i++)
                {
                    Clause cI = clausesA[i];
                    if (null != tracer)
                    {
                        tracer.stepOuterFor(cI);
                    }
                    for (int j = i; j < clausesA.Length; j++)
                    {
                        Clause cJ = clausesA[j];

                        if (null != tracer)
                        {
                            tracer.stepInnerFor(cI, cJ);
                        }

                        // resolvent <- FOL-RESOLVE(Ci, Cj)
                        ISet <Clause> resolvents = cI.binaryResolvents(cJ);

                        if (resolvents.Size() > 0)
                        {
                            toAdd.Clear();
                            // new <- new <UNION> resolvent
                            foreach (Clause rc in resolvents)
                            {
                                toAdd.AddAll(rc.getFactors());
                            }

                            if (null != tracer)
                            {
                                tracer.stepResolved(cI, cJ, toAdd);
                            }

                            ansHandler.checkForPossibleAnswers(toAdd);

                            if (ansHandler.isComplete())
                            {
                                break;
                            }

                            newClauses.AddAll(toAdd);
                        }

                        if (ansHandler.isComplete())
                        {
                            break;
                        }
                    }
                    if (ansHandler.isComplete())
                    {
                        break;
                    }
                }

                noOfPrevClauses = clauses.Size();

                // clauses <- clauses <UNION> new
                clauses.AddAll(newClauses);

                if (ansHandler.isComplete())
                {
                    break;
                }

                // if new is a <SUBSET> of clauses then finished
                // searching for an answer
                // (i.e. when they were added the # clauses
                // did not increase).
            } while (noOfPrevClauses < clauses.Size());

            if (null != tracer)
            {
                tracer.stepFinished(clauses, ansHandler);
            }

            return(ansHandler);
        }