コード例 #1
0
ファイル: KnowledgeBase.cs プロジェクト: rzubek/UnityProlog
 /// <summary>
 /// Add a term (fact or rule) to the KB.
 /// </summary>
 public void Assert(object term, bool atEnd, bool checkSingletons)
 {
     if (term == null)
         throw new ArgumentNullException("term", "Term to assert in KB cannot be null.");
     if (ELProlog.IsELTerm(term))
         ELProlog.Update(term, this);
     else
     {
         Assert(
             Term.Structurify(term, "Assertion is not a valid proposition or predicate."),
             atEnd,
             checkSingletons);
     }
 }
コード例 #2
0
ファイル: KnowledgeBase.cs プロジェクト: mantoun/MKULTRA
        public void Consult(TextReader inStream)
        {
            sourceFiles.NoteFile(Prolog.CurrentSourceFile);
            var reader = new ISOPrologReader(inStream);

            reader.SkipLayout();
            int lastLine = reader.LineNumber;

            using (var context = PrologContext.Allocate(this, this))
            {
                try
                {
                    object unexpanded;
                    Prolog.CurrentSourceLineNumber = lastLine;
                    while ((unexpanded = reader.ReadTerm()) != Symbol.EndOfFile)
                    {
                        // Perform user-level macroexpansion.
                        object assertion = TermExpansion(unexpanded);
                        if (ELProlog.IsELTerm(assertion))
                        {
                            // It's an EL term.
                            ELProlog.Update(assertion, this);
                        }
                        else
                        {
                            // It's a normal Prolog term
                            var t = Term.Structurify(
                                assertion,
                                "Assertions in prolog files must be valid propositions or predicates.");

                            // Perform built-in macroexpansion.
                            t = t.Expand();

                            if (t.IsFunctor(Symbol.Implication, 1))
                            {
                                context.Reset();
                                var goal = Term.Structurify(
                                    t.Argument(0),
                                    "Argument to a :- directive must be an atom or structure.");
                                // Run t once, but don't backtrack for a second solution (since it's presumably an imperative anyway).
                                Prove(goal.Functor, goal.Arguments, context, 0).GetEnumerator().MoveNext();
                            }
                            else
                            {
                                Assert(t, true, true);
                            }
                        }
                        reader.SkipLayout();
                        lastLine = reader.LineNumber;
                        Prolog.CurrentSourceLineNumber = lastLine;
                    }
                }
                catch (InferenceStepsExceededException e)
                {
                    Repl.RecordExceptionSourceLocation(e, lastLine);
                    throw;
                }
                catch (Exception e)
                {
                    UnityEngine.Debug.LogException(e);
                    Repl.RecordExceptionSourceLocation(e, lastLine);
                    throw new PrologError(
                              e,
                              context.StackTrace(Prolog.CurrentSourceFile, Prolog.CurrentSourceLineNumber, "consult/1", false));
                }
            }
        }