Esempio n. 1
0
        internal DbConstraint Raw(string variableName)
        {
            var variable             = "$" + variableName;
            var variableSubstitution = _evaluatedElement.GetSubstitution(variable);
            var result = DbConstraint.Entity(variableSubstitution.Token);

            return(result);
        }
Esempio n. 2
0
        internal static void AdviceTests()
        {
            var mind = new MindSet();

            mind
            .AddPattern("$something", "can", "have", "$something2")
            .HowToEvaluate(c =>
                           c["something"].ExtendByAnswer("what does @ have?", c["something2"])
                           )

            .AddPattern("every", "$something")
            .HowToEvaluate(c =>
                           DbConstraint.Entity(null).ExtendByAnswer("what @ is?", c["something"])
                           )

            .AddPattern("blue", "$something")

            ;

            mind.AddFact("dog", "what color @ has?", "black");
            mind.AddFact("whale", "what color @ has?", "blue");
            mind.AddFact("whale", "where @ lives?", "sea");
            mind.AddFact("dog", "what @ is?", "mammal");
            mind.AddFact("whale", "what @ is?", "mammal");

            var evaluation = mind.Evaluator.Evaluate("every blue mammal", Evaluator.HowToEvaluateQ);

            wl(evaluation.ToString());

            foreach (var value in mind.Database.Query(evaluation.Constraint))
            {
                Console.WriteLine("Result: " + value.Substitution);
            }

            //what is sky?
            //the stuff above
            //what is a blue color?
            //its an example of a color

            wl("S: What is The Great Britain?");
            wl("U: It is a country.");

            wl("S: What is a country?");
            wl("U: Its a location with a culture, people and so on.");
            wl("U: USA is a country.");
            wl("U: Country is a word.");
            wl("....");
            //writes
            //  - how is defined? A location
            //  - which extra props. it has? culture
            //  - which extra props. it has? people

            wl("S: What culture a country has?");
            wl("U: It depends on a concrete country.");

            wl("S: So do you know some country?");
            wl("U: Sure, e.g. germany with their strict culture.");
        }
Esempio n. 3
0
        internal Body(MindSet mind)
        {
            Mind = mind;

            //common abilities
            AddNativeAction(Print, "say", "$what");
            AddNativeAction(FormulateTopicQuestion, "formulate", "a", "topic", "question");
            AddNativeAction(AddSensorAction, RawEvaluator, ValueEvaluator, "add", "$action", "to", "$sensor");

            //topic handling
            AddNativeAction(WriteTopic, "write", "topic", "$topic");
            AddNativeAction(SetPointerToEmptyEntity, "set", "topic", "pointer", "to", "a", "missing", "entity");

            //search handling
            AddNativeAction(Search, "search", "$something");

            mind
            .AddPattern("input", "from", "user")
            .HowToEvaluate(c => DbConstraint.Entity(_inputHistory.Last()))

            .AddPattern("topic")
            .HowToEvaluate(c => _currentTopic)

            .AddPattern("a", "database", "question")
            .HowToEvaluate(c => databaseQuestion())

            .AddPattern("yes")
            .IsTrue(c => DbConstraint.Entity("yes"))

            .AddPattern("no")
            .IsTrue(c => DbConstraint.Entity("no"))

            .AddPattern("first", "$action1", "second", "$action2", "third", "$action3")
            .HowToDo(c =>
            {
                var action1 = c.Evaluate("action1", Evaluator.HowToDoQ);
                var action2 = c.Evaluate("action2", Evaluator.HowToDoQ);
                var action3 = c.Evaluate("action3", Evaluator.HowToDoQ);

                return(DbConstraint.Entity(NativeMutliCallHandler).ExtendByAnswer("1?", action1).ExtendByAnswer("2?", action2).ExtendByAnswer("3?", action3));
            })

            .AddPattern("first", "$action1", "second", "$action2")
            .HowToDo(c =>
            {
                var action1 = c.Evaluate("action1", Evaluator.HowToDoQ);
                var action2 = c.Evaluate("action2", Evaluator.HowToDoQ);

                return(DbConstraint.Entity(NativeMutliCallHandler).ExtendByAnswer("1?", action1).ExtendByAnswer("2?", action2));
            })
            ;

            _nativeCallWrappers.Add(NativeMutliCallHandler, MultiCall);
        }
Esempio n. 4
0
        protected string[] AnswerWithMany(string subject, string question)
        {
            var result = new List <string>();

            foreach (var row in Query(new ConstraintEntry(DbConstraint.Entity(subject), question, null)))
            {
                result.Add(row.Substitution);
            }

            return(result.ToArray());
        }
Esempio n. 5
0
        private DbConstraint findUserCriterions(DbConstraint constraint)
        {
            var answers = Mind.Database.GetAnswers("user", "What @ wants?").ToArray();

            var result = new DbConstraint();

            foreach (var answer in answers)
            {
                result = result.ExtendByAnswer("What @ wants?", DbConstraint.Entity(answer));
            }

            return(result);
        }
Esempio n. 6
0
        internal EmptyAgent()
        {
            Body = new Body(Mind);

            Mind
            .AddPattern("when", "$something", "then", "$action")
            .HowToDo("add $action to $something")

            .AddPattern("say", "$something")
            .HowToDo("print $something")

            .AddPattern("do", "$something")
            .HowToDo(c => Mind.Evaluator.Evaluate(c["something"].PhraseConstraint, Evaluator.HowToDoQ).Constraint)

            .AddPattern("user", "input")
            .HowToEvaluate("input from user")

            .AddPattern("$something", "is", "defined")
            .HowToEvaluate(c => isDefined(c["something"]))
            .IsTrue(c => isDefined(c["something"]))

            .AddPattern("action", "for", "$something")
            .HowToEvaluate(c => DbConstraint.Entity(null).ExtendBySubject(c["something"], Evaluator.HowToDoQ))

            .AddPattern("if", "$something", "then", "$something2")
            .HowToDo(c =>
            {
                var isConditionTrue = c.IsTrue("something");
                if (isConditionTrue)
                {
                    var result = c.Evaluate("something2", Evaluator.HowToDoQ);
                    return(result);
                }

                return(DbConstraint.Entity("nothing"));
            })

            .AddPattern("ask", "$something")
            .HowToDo("first write topic $something second set topic pointer to a missing entity third formulate a topic question")

            ;


            Body
            .SensorAction("before input processing",
                          "if action for user input is defined then do user input" //without this, nothing gets executed
                          )

            .Policy("when missing output then ask a database question")
            ;
        }
Esempio n. 7
0
        protected DbConstraint MultiCall(DbConstraint call)
        {
            var arguments = call.SubjectConstraints.OrderBy(c => c.Question.Length).OrderBy(c => c.Question).ToArray();

            foreach (var argument in arguments)
            {
                var callResult = executeCall(argument.Answer);
                if (!callResult)
                {
                    return(DbConstraint.Entity("fail"));
                }
            }

            return(DbConstraint.Entity("success"));
        }
Esempio n. 8
0
        internal EvaluationResult Evaluate(MatchElement element, string question, EvaluationContext parentContext)
        {
            var context = new EvaluationContext(this, element, parentContext);

            var elementRepresentation = element.Pattern.Representation;
            var evaluationDescription = getEvaluationDescription(elementRepresentation, question, context);

            if (evaluationDescription == null)
            {
                //we can't do better than create explicit entity constraint and generate HowToEvaluateQ.
                return(new EvaluationResult(DbConstraint.Entity(element.Token), new DbConstraint(new ConstraintEntry(DbConstraint.Entity(element.Token), question, null))));
            }

            return(Evaluate(evaluationDescription, question, context));
        }
Esempio n. 9
0
        private NativePhraseEvaluator CreateActionSemantic(Action action, string actionName)
        {
            var nativeCallWrapperId             = "%" + actionName;
            NativeCallWrapper nativeCallWrapper = i =>
            {
                action();
                return(null);
            };

            _nativeCallWrappers.Add(nativeCallWrapperId, nativeCallWrapper);

            return(c =>
            {
                var callEntity = DbConstraint.Entity(nativeCallWrapperId);
                return callEntity;
            });
        }
Esempio n. 10
0
        internal IEnumerable <string> GetAnswers(string subject, string question)
        {
            var isSuccess = false;

            foreach (var entry in _data)
            {
                if (entry.Subject == subject && entry.Question == question)
                {
                    isSuccess = true;
                    yield return(entry.Answer);
                }
            }

            if (!isSuccess)
            {
                _failingConstraints.Add(new DbConstraint(new ConstraintEntry(DbConstraint.Entity(subject), question, null)));
            }
        }
Esempio n. 11
0
        private NativePhraseEvaluator CreateActionSemantic(Action <DbConstraint> action, string actionName, string parameterName1)
        {
            var nativeCallWrapperId             = "%" + actionName;
            NativeCallWrapper nativeCallWrapper = i =>
            {
                var argumentEntity1 = i.GetSubjectConstraint(parameterName1);
                action(argumentEntity1);
                return(null);
            };

            _nativeCallWrappers.Add(nativeCallWrapperId, nativeCallWrapper);

            return(c =>
            {
                var callEntity = DbConstraint.Entity(nativeCallWrapperId);
                return callEntity
                .ExtendByAnswer(parameterName1, c[parameterName1])
                ;
            });
        }
Esempio n. 12
0
        private NativePhraseEvaluator CreateActionSemantic(Action <string, string> action, ParameterEvaluator param1, ParameterEvaluator param2, string actionName, string parameterName1, string parameterName2)
        {
            var nativeCallWrapperId             = "%" + actionName;
            NativeCallWrapper nativeCallWrapper = i =>
            {
                var argumentEntity1 = i.GetSubjectConstraint(parameterName1);
                var argumentEntity2 = i.GetSubjectConstraint(parameterName2);
                action(argumentEntity1.PhraseConstraint, argumentEntity2.PhraseConstraint);
                return(null);
            };

            _nativeCallWrappers.Add(nativeCallWrapperId, nativeCallWrapper);

            return(c =>
            {
                var callEntity = DbConstraint.Entity(nativeCallWrapperId);
                return callEntity
                .ExtendByAnswer(parameterName1, param1(parameterName1, c))
                .ExtendByAnswer(parameterName2, param2(parameterName2, c))
                ;
            });
        }
Esempio n. 13
0
 internal DbConstraint IfDbContains(DbConstraint constraint)
 {
     return(Mind.Database.Query(constraint).Any() ? DbConstraint.Entity("true") : DbConstraint.Entity("false"));
 }
Esempio n. 14
0
        internal RestaurantAgent()
        {
            Mind
            .AddPattern("a", "$something")
            .HowToEvaluate(c => c["something"])

            .AddPattern("an", "$something")
            .HowToEvaluate(c => c["something"])

            .AddPattern("if", "$something", "then", "$something2")
            .HowToDo(c =>
            {
                var isConditionTrue = c.IsTrue("something");
                if (isConditionTrue)
                {
                    return(c["something2"]);
                }

                return(DbConstraint.Entity("nothing"));
            })

            .AddPattern("$someone", "said", "$something")
            .HowToEvaluate(c => c["someone"].ExtendByAnswer("What @ said?", c["something"]))

            .AddPattern("user", "specified", "$something")
            .IsTrue("criterions on $something exists")

            .AddPattern("$something", "exists")
            .IsTrue(c => Body.IfDbContains(c["something"]))

            .AddPattern("criterions", "on", "$something")
            .HowToEvaluate(c => findUserCriterions(c["something"]))

            .AddPattern("you", "know", "$something")
            .IsTrue(c => Body.IfDbContains(c["something"]))

            .AddPattern("which", "$something")
            .HowToEvaluate(c =>
                           new DbConstraint().ExtendByAnswer("What is @?", c["something"])
                           )

            .AddPattern("$something", "user", "wants")
            .HowToEvaluate(c =>
                           c["something"].ExtendBySubject(DbConstraint.Entity("user"), "What @ wants?")
                           )


            .AddPattern("remember", "$something")
            .HowToDo(c =>
            {
                var fact = c["something"];
                if (fact.PhraseConstraint != "nothing")
                {
                    var answerEntry = fact.SubjectConstraints.First();
                    Mind.Database.AddFact(fact.PhraseConstraint, answerEntry.Question, answerEntry.Answer.PhraseConstraint);
                }

                return(DbConstraint.Entity("nothing"));
            })

            .AddPattern("I", "want", "$something")
            .HowToEvaluate(c =>
                           DbConstraint.Entity("user").ExtendByAnswer("What @ wants?", c["something"]))

            .AddPattern("fact", "from", "$someone")
            .HowToEvaluate(c =>
            {
                var input          = Mind.Database.GetAnswers("user", "What @ said?").First();
                var evaluation     = Mind.Evaluator.Evaluate(input, Evaluator.HowToEvaluateQ, c);
                var factConstraint = evaluation.Constraint;
                if (factConstraint.PhraseConstraint != null && (factConstraint.SubjectConstraints.Any() || factConstraint.AnswerConstraints.Any()))
                {
                    return(evaluation.Constraint);
                }
                else
                {
                    return(DbConstraint.Entity("nothing"));
                }
            })

            ;

            /*AddPolicyFact("remember fact from user");
             * AddPolicyFact("if user said hello then say hi");
             * AddPolicyFact("if you know which restaurant user wants then offer it");*/
        }