Exemplo n.º 1
0
        public PrologResult Query(string query)
        {
            query = ":-" + query; //Use the query form for Prolog.NET
            CodeSentence sentence = Parser.Parse(query)[0];
            Query q = new Prolog.Query(sentence);
            PrologMachine machine = PrologMachine.Create(program, q);

            PrologResult result = new PrologResult(machine.RunToSuccess());//First time run marks TRUE/FALSE result.
            ExecutionResults resultStatus = result.Status;

            while ( resultStatus == ExecutionResults.Success)
            {
                PrologVariableList variables = machine.QueryResults.Variables;
                Dictionary<string, string> vars = new Dictionary<string, string>();
                foreach (PrologVariable v in variables)
                {
                    vars.Add(v.Name, v.Text);
                }
                if(vars.Count != 0)
                    result.Vars.Add(vars);
                resultStatus = machine.RunToSuccess();
            }

            return result;
        }
Exemplo n.º 2
0
        public static PrologMachine Create(Program program, Query query)
        {
            if (program == null)
            {
                throw new ArgumentNullException("program");
            }
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            return new PrologMachine(program, query);
        }
Exemplo n.º 3
0
 public static WamMachine Create(Program program, Query query)
 {
     if (program == null)
     {
         throw new ArgumentNullException("program");
     }
     if (query == null)
     {
         throw new ArgumentNullException("query");
     }
     var wamMachine = new WamMachine(program, query);
     wamMachine.Initialize();
     return wamMachine;
 }
Exemplo n.º 4
0
 PrologMachine Execute(Prolog.Program program, Query query)
 {
     try
     {
         var machine = PrologMachine.Create(program, query);
         machine.ExecutionComplete += CodeExecuted;
         var result = machine.RunToSuccess();
         Console.WriteLine(Enum.GetName(typeof(ExecutionResults), result));
         return machine;
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error, got exception: {0}", ex.Message);
         return null;
     }
 }
Exemplo n.º 5
0
        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;
        }
Exemplo n.º 6
0
 PrologMachine Execute(PrologMachine machine, Query query)
 {
     try
     {
         machine.ExecutionComplete += CodeExecuted;
         machine.RunToBacktrack();
         machine.Restart();
         var result = machine.RunToSuccess();
         Console.WriteLine(Enum.GetName(typeof(ExecutionResults), result));
         return machine;
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error, got exception: {0}", ex.Message);
         return null;
     }
 }
Exemplo n.º 7
0
        WamMachine(Program program, Query query)
        {
            if (program == null)
            {
                throw new ArgumentNullException("program");
            }
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            Program = program;
            Query = query;

            _contextStack = new Stack<WamContext>();
            CurrentContext = null;

            _performanceStatistics = new PerformanceStatistics();
        }
Exemplo n.º 8
0
        private PrologMachine(Program program, Query query)
        {
            if (program == null)
            {
                throw new ArgumentNullException("program");
            }
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            m_wamMachine = WamMachine.Create(program, query);

            m_stackFrames = new PrologStackFrameList(this);
            m_arguments = new PrologVariableList(this);
            m_temporaryVariables = new PrologVariableList(this);

            Synchronize();

            m_queryResults = null;
        }
Exemplo n.º 9
0
        void ProcessInput(bool executeQuery)
        {
            var input = CurrentInput;
            if (input == null) return;

            input = input.Trim();
            if (string.IsNullOrEmpty(input)) return;

            TranscriptProvider.Transcript.AddTranscriptEntry(TranscriptEntryTypes.Request, input);

            var selectedClause = CurrentClauseProvider.SelectedClause;

            var codeSentences = Parser.Parse(input);
            if (codeSentences == null || codeSentences.Length == 0)
            {
                TranscriptProvider.Transcript.AddTranscriptEntry(TranscriptEntryTypes.Response, Resources.Strings.MessageUnrecognizedInput);
                return;
            }

            foreach (var codeSentence in codeSentences)
            {
                if (codeSentence.Head == null) // query
                {
                    var query = new Query(codeSentence);
                    MachineProvider.Machine = PrologMachine.Create(ProgramProvider.Program, query);
                    MachineProvider.Machine.ExecutionComplete += OnMachineExecutionComplete;
                    if (executeQuery)
                    {
                        MachineProvider.Machine.RunToSuccess();
                    }
                }
                else // fact or rule
                {
                    if (selectedClause != null && selectedClause.Container.Procedure.Functor == Functor.Create(codeSentence.Head.Functor))
                    {
                        selectedClause.CodeSentence = codeSentence;
                        TranscriptProvider.Transcript.AddTranscriptEntry(TranscriptEntryTypes.Response, Resources.Strings.ResponseSuccess);
                    }
                    else
                    {
                        if (ProgramProvider.Program.Contains(codeSentence))
                        {
                            TranscriptProvider.Transcript.AddTranscriptEntry(TranscriptEntryTypes.Response, Resources.Strings.MessageDuplicateClause);
                        }
                        else
                        {
                            ProgramProvider.Program.Add(codeSentence);
                            TranscriptProvider.Transcript.AddTranscriptEntry(TranscriptEntryTypes.Response, Resources.Strings.ResponseSuccess);
                        }
                    }
                }
            }
        }