Ejemplo n.º 1
0
        //public Fact processFact(Fact fact)
        //{
        //    if (fact is VariableFact)
        //        return processFact(fact as VariableFact);
        //    return processFact(fact as GroundFact);
        //}
        public Fact ProcessBaseFact(Fact fact)
        {
            if (fact is VariableFact)
                return ProcessFact(fact as VariableFact);

            return ProcessFact(fact as GroundFact);
        }
Ejemplo n.º 2
0
        public static Substitution Unify(Fact x, Fact y)
        {
            Substitution theta = new Substitution();
            bool isGood = UnifyTerm(x.ToTerm(), y.ToTerm(), theta);

            return isGood ? theta : null;
        }
Ejemplo n.º 3
0
        private static void EnterUnificationLevel(Fact f1, Fact f2)
        {
            if (!Logger.IsDebugEnabled)
                return;

            Logger.Debug(string.Format("{0}Unifier: attempting to unify {1} and {2}", Util.MakeIndent(_unificationLevel), f1.ToString(GameContainer.SymbolTable), f2.ToString(GameContainer.SymbolTable)));
            _unificationLevel++;
        }
Ejemplo n.º 4
0
        protected ReasoningEntity(Parser parser)
        {
            Parser = parser;
            SymbolTable = Parser.SymbolTable;

            QueryTerminal = MakeQuery("terminal");
            QueryNext = MakeQuery("next", "?x");

            Random = new Random();
        }
Ejemplo n.º 5
0
        private static Fact CleanParentheses(Fact sentence)
        {
            if (sentence.Arity == 0)
                return sentence;
            Term[] cleanedBody = sentence.GetTerms().Select(CleanParentheses).ToArray();
            if (!cleanedBody.Any())
                return new GroundFact(sentence.RelationName);

            return new VariableFact(true, sentence.RelationName, cleanedBody);
        }
Ejemplo n.º 6
0
        private Fact RenameDistinct(Fact distinct, Dictionary<TermVariable, TermVariable> renamings)
        {
            if (distinct is GroundFact)
                return distinct;

            Term arg1 = RenameTerm(distinct.GetTerm(0), renamings);
            Term arg2 = RenameTerm(distinct.GetTerm(1), renamings);

            return new VariableFact(false, distinct.RelationName, arg1, arg2);
        }
Ejemplo n.º 7
0
        private static Fact SubstituteDistinct(Fact distinct, Substitution theta)
        {
            if (distinct is GroundFact)
                return distinct;

            Term arg1 = SubstituteTerm(distinct.GetTerm(0), theta);
            Term arg2 = SubstituteTerm(distinct.GetTerm(1), theta);

            return arg1.HasVariables || arg2.HasVariables
                ? new VariableFact(false, distinct.RelationName, arg1, arg2)
                : (Fact) new GroundFact(distinct.RelationName, arg1, arg2);
        }
Ejemplo n.º 8
0
        private HashSet<Fact> Ask(Fact query, HashSet<Fact> context, bool askOne)
        {
            LinkedList<Expression> goals = new LinkedList<Expression>();
            goals.AddFirst(query);

            var answers = new HashSet<Substitution>();
            HashSet<Fact> alreadyAsking = new HashSet<Fact>();
            Ask(goals, new KnowledgeBase(context), new Substitution(), new ProverCache(), new VariableRenamer(), askOne, answers, alreadyAsking);

            var results = new HashSet<Fact>();
            foreach (Substitution theta in answers)
                results.Add(Substituter.Substitute(query, theta));

            return results;
        }
Ejemplo n.º 9
0
        public static Substitution Mgu(Fact f1, Fact f2)
        {
            // Make sure this is even worth our time to check
            if (f1.RelationName != f2.RelationName)
                return null;
            if (f1.Arity != f2.Arity)
                return null;

            EnterUnificationLevel(f1, f2);

            var subs = new Substitution();

            if (Mgu(f1, f2, subs))
            {
                ExitUnificationLevel(subs);
                return subs;
            }
            ExitUnificationLevel(null);
            return null;
        }
Ejemplo n.º 10
0
        public static AssignmentsImpl GetAssignmentsWithRecursiveInput(Implication rule,
                                                                       ISentenceDomainModel model, ISentenceForm form, Fact input,
                                                                       Dictionary<ISentenceForm, FunctionInfo> functionInfoMap,
                                                                       Dictionary<ISentenceForm, ICollection<Fact>> completedSentenceFormValues)
        {
            //Look for the literal(s) in the rule with the sentence form of the
            //recursive input. This can be tricky if there are multiple matching
            //literals.
            var matchingLiterals = rule.Antecedents.Conjuncts.OfType<Fact>().Where(form.Matches).ToList();

            var assignmentsList = new List<AssignmentsImpl>();
            foreach (Fact matchingLiteral in matchingLiterals)
            {
                var preassignment = new TermObjectSubstitution();
                preassignment.Add(matchingLiteral.Unify(input));
                if (preassignment.NumMappings() > 0)
                {
                    var assignments = new AssignmentsImpl(
                        preassignment,
                        rule,
                        //TODO: This one getVarDomains call is why a lot of
                        //SentenceModel/DomainModel stuff is required. Can
                        //this be better factored somehow?
                        SentenceDomainModels.GetVarDomains(rule, model, SentenceDomainModels.VarDomainOpts.IncludeHead),
                        functionInfoMap,
                        completedSentenceFormValues);
                    assignmentsList.Add(assignments);
                }
            }

            if (assignmentsList.Count == 0)
                return new AssignmentsImpl();
            if (assignmentsList.Count == 1)
                return assignmentsList[0];
            throw new Exception("Not yet implemented: assignments for recursive functions with multiple recursive conjuncts");
            //TODO: Plan to implement by subclassing TermObjectSubstitution into something
            //that contains and iterates over multiple TermObjectSubstitution
        }
Ejemplo n.º 11
0
 public ISentenceForm GetSentenceForm(Fact sentence)
 {
     return _formModel.GetSentenceForm(sentence);
 }
Ejemplo n.º 12
0
 public ISentenceForm GetSentenceForm(Fact sentence)
 {
     return new SimpleSentenceForm(sentence);
 }
Ejemplo n.º 13
0
        /**
         * Given a sentence of the constant form's sentence form, finds all
         * the variables in the sentence that can be produced functionally.
         *
         * Note the corner casein If a variable appears twice in a sentence,
         * it CANNOT be produced in this way.
         */
        public ISet<TermVariable> GetProducibleVars(Fact sentence)
        {
            if (!_form.Matches(sentence))
                throw new Exception("Sentence " + sentence + " does not match constant form");
            List<Term> tuple = sentence.NestedTerms.ToList();

            ISet<TermVariable> candidateVars = new HashSet<TermVariable>();
            //Variables that appear multiple times go into multipleVars
            ISet<TermVariable> multipleVars = new HashSet<TermVariable>();
            //...which, of course, means we have to spot non-candidate vars
            ISet<TermVariable> nonCandidateVars = new HashSet<TermVariable>();

            for (int i = 0; i < tuple.Count; i++)
            {
                Term term = tuple[i];
                var termVariable = term as TermVariable;
                if (termVariable != null && !multipleVars.Contains(term))
                {
                    var var = termVariable;
                    if (candidateVars.Contains(var) || nonCandidateVars.Contains(var))
                    {
                        multipleVars.Add(var);
                        candidateVars.Remove(var);
                    }
                    else if (_dependentSlots[i])
                        candidateVars.Add(var);
                    else
                        nonCandidateVars.Add(var);
                }
            }

            return candidateVars;
        }
Ejemplo n.º 14
0
 public Fact AskOne(Fact query, HashSet<Fact> context)
 {
     return Ask(query, context, true).FirstOrDefault();
 }
Ejemplo n.º 15
0
 public bool Prove(Fact query, HashSet<Fact> context)
 {
     return AskOne(query, context) != null;
 }
Ejemplo n.º 16
0
 private static void VisitRelation(Fact relation, GdlVisitor visitor)
 {
     visitor.VisitRelation(relation);
     VisitTerms(relation.GetTerms(), visitor);
 }
Ejemplo n.º 17
0
 public static Fact Substitute(Fact sentence, Substitution theta)
 {
     return SubstituteSentence(sentence, theta);
 }
Ejemplo n.º 18
0
 public Fact Rename(Fact sentence)
 {
     return RenameSentence(sentence, new Dictionary<TermVariable, TermVariable>());
 }
Ejemplo n.º 19
0
        private Fact RenameSentence(Fact sentence, Dictionary<TermVariable, TermVariable> renamings)
        {
            if (sentence is GroundFact)
                return sentence;

            var body = new List<Term>();
            for (int i = 0; i < sentence.Arity; i++)
                body.Add(RenameTerm(sentence.GetTerm(i), renamings));

            return new VariableFact(false, sentence.RelationName, body.ToArray());
        }
Ejemplo n.º 20
0
        private static bool Mgu(Fact f1, Fact f2, Substitution subsSoFar)
        {
            // Make sure this is even worth our time to check
            if (f1.RelationName != f2.RelationName)
                return false;
            if (f1.Arity != f2.Arity)
                return false;

            // Find the mgu for each column of the facts
            for (int i = 0; i < f1.Arity; i++)
            {
                // If there is no mgu, just die
                if (Mgu(f1.GetTerm(i), f2.GetTerm(i), subsSoFar) == false)
                    return false;
            }

            return true;
        }
Ejemplo n.º 21
0
 private static void VisitDistinct(Fact distinct, GdlVisitor visitor)
 {
     visitor.VisitDistinct(distinct);
     VisitTerm(distinct.GetTerm(0), visitor);
     VisitTerm(distinct.GetTerm(1), visitor);
 }
Ejemplo n.º 22
0
 public bool IsTrueConstant(Fact sentence)
 {
     ////TODO: This could be even more efficient; we don't need to bucket by form
     //ISentenceForm form = _sentenceModel.GetSentenceForm(sentence);
     //return _sentencesByForm[form].Contains(sentence);
     return _allSentences.Contains(sentence);
 }
Ejemplo n.º 23
0
        private static Fact SubstituteSentence(Fact sentence, Substitution theta)
        {
            if (sentence is GroundFact)
                return sentence;

            var body = new List<Term>();
            for (int i = 0; i < sentence.Arity; i++)
                body.Add(SubstituteTerm(sentence.GetTerm(i), theta));

            var variableFact = new VariableFact(false, sentence.RelationName, body.ToArray());
            return variableFact.VariablesOrEmpty.Any()
                ? variableFact
                : (Fact) new GroundFact(sentence.RelationName, body.ToArray());
        }
Ejemplo n.º 24
0
 private void AskDistinct(Fact distinct, LinkedList<Expression> goals, KnowledgeBase context, Substitution theta, 
     ProverCache cache, VariableRenamer renamer, bool askOne, HashSet<Substitution> results, HashSet<Fact> alreadyAsking)
 {
     if (!distinct.GetTerm(0).Equals(distinct.GetTerm(1)))
         Ask(goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
 }
Ejemplo n.º 25
0
 private static void VisitSentence(Fact sentence, GdlVisitor visitor)
 {
     visitor.VisitSentence(sentence);
     VisitRelation(sentence, visitor);
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Returns true iff the given sentence is of this sentence form.
        /// </summary>
        public override bool Matches(Fact sentence)
        {
            if (sentence.RelationName != Name || sentence.Arity != _arity)
                return false;

            for (int i = 0; i < sentence.Arity; i++)
            {
                var term = sentence.GetTerm(i) as TermFunction;
                if (term == null && _functions.ContainsKey(i))
                    return false;

                if (term != null)
                {
                    if (!_functions.ContainsKey(i) || !_functions[i].Matches(term))
                        return false;
                }
            }
            return true;
        }
Ejemplo n.º 27
0
 public bool HasConstantForm(Fact sentence)
 {
     return ConstantSentenceForms.Any(form => form.Matches(sentence));
 }
Ejemplo n.º 28
0
 public SimpleSentenceForm(Fact sentence)
     : this(sentence.RelationName, sentence.Arity, sentence.GetTerms())
 {
 }
Ejemplo n.º 29
0
 public List<Implication> Fetch(Fact sentence)
 {
     return _contents.ContainsKey(sentence.RelationName)
         ? _contents[sentence.RelationName]
         : new List<Implication>();
 }
Ejemplo n.º 30
0
 internal static VariableFact CloneWithEmptyTerms(Fact fact)
 {
     var terms = new Term[fact.Arity];
     for (int i = 0; i < fact.Arity; i++)
     {
         var termFunction = terms[i] as TermFunction;
         if (termFunction != null)
             terms[i] = termFunction.CloneWithEmptyTerms();
         else
             terms[i] = TermVariable.MakeTermVariable();
     }
     return new VariableFact(false, fact.RelationName, terms);
 }