Example #1
0
        void ExecuteCode()
        {
            var codeSentences = GetCodeSentences();
            if (codeSentences == null)
                return;

            var query = GetQuery();
            if (query == null)
                return;

            var program = new Prolog.Program();
            foreach (var sentence in codeSentences)
                program.Add(sentence);

            textResult.Text = "";
            try
            {
                var machine = PrologMachine.Create(program, query);
                machine.ExecutionComplete += CodeExecuted;
                var result = machine.RunToSuccess();
                textStatus.Text = Enum.GetName(typeof(ExecutionResults), result);
            }
            catch (Exception ex)
            {
                textStatus.Text = "Error";
                textResult.Text += string.Format("Got exception: {0}", ex.Message);
                return;
            }
        }
Example #2
0
        public void Run()
        {
            var program = new Prolog.Program();
            var content = File.ReadAllText(_fileName);
            var codeSentences = GetCodeSentences(content);
            if (codeSentences == null) return;

            foreach (var sentence in codeSentences)
                program.Add(sentence);

            PrologMachine lastMachine = null;
            while (true)
            {
                Console.WriteLine("Hint.: "+_queryHint);
                Console.Write("Query: ");
                var input = Console.ReadLine();
                if (string.IsNullOrEmpty(input)) return;

                var query = GetQuery(input);
                if (query == null) continue;

                if (input.ToLower() == ";" && lastMachine != null)
                {
                    lastMachine = Execute(lastMachine, query);
                    continue;
                }

                lastMachine = Execute(program, query);
            }
        }
        public string Prolog(string program, string query)
        {
            string ret = "";
            Query pro_query;
            Program pro_program = new Program();
            CodeSentence[] pro_pro_sent;
            CodeSentence[] pro_query_sent;

            try
            {
                pro_pro_sent = Parser.Parse(program);
            }
            catch (Exception e)
            {
                throw new Exception("PROGRAM ERROR : " + e.ToString());
            }
            try
            {
                pro_query_sent = Parser.Parse(query);
            }
            catch (Exception e)
            {
                throw new Exception("QUERY ERROR : " + e.ToString());
            }
            if (pro_pro_sent.Length == 0)
                throw new Exception("VALID PROGRAM CODE NOT AVAILABLE");
            if (pro_query_sent.Length == 0)
                throw new Exception("VALID QUERY CODE NOT AVAILABLE");

            foreach (CodeSentence s in pro_pro_sent)
            {
                pro_program.Add(s);
            }
            pro_query = new Query(pro_query_sent[0]);

            PrologMachine m = PrologMachine.Create(pro_program, pro_query);
            m.RunToSuccess();

            ret = "";
            foreach (var v in m.QueryResults.Variables)
            {
                ret += v.Text + "\n";
            }
            ret = ret.Substring(0, ret.Length - 1);

            return ret;
        }
Example #4
0
        public void Run()
        {
            var program       = new Prolog.Program();
            var content       = File.ReadAllText(_fileName);
            var codeSentences = GetCodeSentences(content);

            if (codeSentences == null)
            {
                return;
            }

            foreach (var sentence in codeSentences)
            {
                program.Add(sentence);
            }

            PrologMachine lastMachine = null;

            while (true)
            {
                Console.WriteLine("Hint.: " + _queryHint);
                Console.Write("Query: ");
                var input = Console.ReadLine();
                if (string.IsNullOrEmpty(input))
                {
                    return;
                }

                var query = GetQuery(input);
                if (query == null)
                {
                    continue;
                }

                if (input.ToLower() == ";" && lastMachine != null)
                {
                    lastMachine = Execute(lastMachine, query);
                    continue;
                }

                lastMachine = Execute(program, query);
            }
        }
Example #5
0
        private void ExecutePrologProgram(string programStr)
        {
            var lines         = programStr.Split(Environment.NewLine.ToCharArray());
            var codeSentences = new List <CodeSentence>();

            foreach (var line in lines)
            {
                codeSentences.AddRange(Prolog.Parser.Parse(line));
            }
            Prolog.Program program = new Prolog.Program();
            foreach (var codeSentence in codeSentences)
            {
                program.Add(codeSentence);
            }
            var           qw      = Parser.Parse($":-{QueryTextBox.Text}");
            Query         query   = new Query(qw[0]);
            PrologMachine machine = PrologMachine.Create(program, query);
            StringBuilder sb      = new StringBuilder();

            sb.AppendLine(machine.RunToSuccess().ToString());
            if (machine.QueryResults != null)
            {
                foreach (var v in machine.QueryResults.Variables)
                {
                    sb.AppendLine($"{v.Name} = {v.Text}");
                }
            }

            //if (machine.StackFrames != null)
            //{
            //    foreach (var v in machine.StackFrames)
            //    {
            //        sb.AppendLine(v.ToString());
            //    }
            //}
            ResultTextBox.Text = sb.ToString();
        }