public IList <LogicResult> GetQuerySolutions(string query)
        {
            query         = query.Trim();
            query         = query.AddEndDot();
            fEngine.Query = query;

            var retVal = new List <LogicResult>();

            foreach (var solution in fEngine.SolutionIterator)
            {
                var returnedSolution = new LogicResult();
                returnedSolution.IsLast   = solution.IsLast;
                returnedSolution.Solution = solution.ToString();
                returnedSolution.Solved   = solution.Solved;

                returnedSolution.Variables = new List <Variable>();
                foreach (var variableBinding in solution.VarValuesIterator)
                {
                    returnedSolution.Variables.Add(new Variable()
                    {
                        Name      = variableBinding.Name,
                        TextValue = variableBinding.Value.ToString(),
                        DataType  = variableBinding.DataType
                    });
                }

                retVal.Add(returnedSolution);
            }

            return(retVal);
        }
        public void Test_AM()
        {
            var service = new LogicService();

            service.AddFact("fish(\"Poecilia reticulata\")");
            service.AddFact("fish(\"Xiphophorus hellerii\")");

            IList <LogicResult> queryResult = service.GetQuerySolutions("fish(X)");

            Assert.AreEqual(2, queryResult.Count);

            LogicResult solution = queryResult[0];

            Assert.AreEqual(1, solution.Variables.Count);
            var varSubst = solution.Variables[0];

            Assert.AreEqual("X", varSubst.Name);
            Assert.AreEqual("\"Poecilia reticulata\"", varSubst.TextValue);

            solution = queryResult[1];
            Assert.AreEqual(1, solution.Variables.Count);
            varSubst = solution.Variables[0];
            Assert.AreEqual("X", varSubst.Name);
            Assert.AreEqual("\"Xiphophorus hellerii\"", varSubst.TextValue);
        }
        public void Test_EnumerateVariables()
        {
            var service = new LogicService();

            var addFactResult = service.AddFact("dog(ripley)");

            AssertResponseOk(addFactResult);

            addFactResult = service.AddFact("dog(charlie)");
            AssertResponseOk(addFactResult);

            addFactResult = service.AddFact("mammal(X):-dog(X)");
            AssertResponseOk(addFactResult);

            IList <LogicResult> queryResult = service.GetQuerySolutions("mammal(X)");

            Assert.AreEqual(2, queryResult.Count);

            LogicResult solution = queryResult[0];

            Assert.AreEqual(1, solution.Variables.Count);
            Assert.AreEqual("X", solution.Variables[0].Name);
            Assert.AreEqual("ripley", solution.Variables[0].TextValue);

            solution = queryResult[1];
            Assert.AreEqual(1, solution.Variables.Count);
            Assert.AreEqual("X", solution.Variables[0].Name);
            Assert.AreEqual("charlie", solution.Variables[0].TextValue);
        }
 private void AssertResponseOk(LogicResult result)
 {
     Assert.IsTrue(result.Solved);
     Assert.IsTrue(string.IsNullOrWhiteSpace(result.Message));
 }