Intern() private method

private Intern ( string name ) : Symbol
name string
return Symbol
Ejemplo n.º 1
0
        private void MakeKB()
        {
            var parent   = transform.parent;
            KB  parentKB = null;

            if (parent != null)
            {
                parentKB = parent.GetComponent <KB>();
            }

            kb = IsGlobal?
                 KnowledgeBase.Global
                : new KnowledgeBase(
                gameObject.name,
                gameObject,
                parentKB == null ? GameObject.Find(GlobalKBGameObjectName).KnowledgeBase() : parentKB.KnowledgeBase);

            // Add UID counter.
            ELNode.Store(kb.ELRoot / Symbol.Intern("next_uid") % 0);

            try
            {
                foreach (var file in SourceFiles)
                {
                    kb.Consult(file);
                }
            }
            catch (Exception)
            {
                Debug.Break(); // Pause the game
                throw;
            }
        }
Ejemplo n.º 2
0
 private void StartNewQuery(string command)
 {
     try
     {
         foundOneSolution = false;
         PrologContext.Reset(CurrentGameObject);
         PrologContext.Output = Output;
         PrologContext.PushGoalStack(Symbol.Intern("parse"), new object[] { command }, 0);
         object query = ISOPrologReader.ReadAndGetFreeVariables(command, freeVariablesInCurrentQuery);
         if (query == null)
         {
             throw new ArgumentNullException("command", "Prolog query may not be null.");
         }
         var goal = Term.Structurify(query, "Not a valid Prolog goal.");
         if (goal.IsFunctor(Symbol.PrologListConstructor, 2) && goal.Argument(1) == null)
         {
             goal = new Structure("reconsult", goal.Argument(0));
         }
         prologModeAnswerStream = PrologContext.ResetStackAndProve(goal).GetEnumerator();
         PrintNextQuerySolution();
     }
     catch (Exception)
     {
         StartErrorReport();
         prologModeAnswerStream = null;
         if (PrologContext.GoalStackDepth > 0)
         {
             Output.WriteLine("In goal: {0}", PrologContext.GoalStackTop);
         }
         throw;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Load assertions into the KB
        /// </summary>
        public void Consult(string path)
        {
            path = DefaultExtension(Prolog.LoadFilePath(path), ".prolog");

            using (var stream = File.OpenText(path))
            {
                string savedFileName   = Prolog.CurrentSourceFile;
                int    savedLineNumber = Prolog.CurrentSourceLineNumber;
                try
                {
                    Prolog.CurrentSourceFile       = path;
                    Prolog.CurrentSourceLineNumber = 0;
                    var textReader = new PositionTrackingTextReader(stream, path);
                    if (Path.GetExtension(path) == ".csv")
                    {
                        var functor = Symbol.Intern(Path.GetFileNameWithoutExtension(path));
                        this.IsTrue(new Structure("begin_csv_loading", functor));  // Ignore return value
                        new CSVParser(functor, ',', textReader).Read(this.LoadCSVRow);
                        this.IsTrue(new Structure("end_csv_loading", functor));    // Ignore return value
                    }
                    else
                    {
                        Consult(textReader);
                    }
                }
                finally
                {
                    Prolog.CurrentSourceFile       = savedFileName;
                    Prolog.CurrentSourceLineNumber = savedLineNumber;
                }
            }
        }
Ejemplo n.º 4
0
 public KnowledgeBase(string kbName, GameObject gameObject, KnowledgeBase parent, params KnowledgeBase[] otherImports)
 {
     if (kbName == null)
     {
         throw new ArgumentNullException("kbName");
     }
     Name            = kbName;
     this.Parent     = parent;
     this.GameObject = gameObject;
     this.ELRoot     = new ELNode(null, Symbol.Intern("root"));
     //if (parent == null) throw new ArgumentNullException("parent");
     if (otherImports == null)
     {
         throw new ArgumentNullException("otherImports");
     }
     foreach (var import in otherImports)
     {
         if (import == null)
         {
             throw new ArgumentNullException("otherImports");
         }
     }
     if (parent != null)
     {
         imports.Add(parent);
     }
     imports.AddRange(otherImports);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Adds a new user-defined binary primitive predicate given
 /// C# delegates to implement its different modes.  Assumes
 /// arguments are always atomic, and that the relation won't
 /// be called with the same unbound variable for both arguments.
 /// </summary>
 /// <param name="name">Name to give to the predicate</param>
 /// <param name="documentation">Description of the operation of the predicate</param>
 /// <param name="arg1Name">Name of the first argument (for documentation and error messages)</param>
 /// <param name="arg2Name">Name of hte second argument (for documentation and error messages)</param>
 /// <param name="filter">Implementation of predicate for case where both arguments are instantiated.</param>
 /// <param name="arg1Enumerator">For non-left-unique predicates: implementation for case where only the second argument is instantiated, so the values of the first argument must be enumerated.</param>
 /// <param name="arg1Function">For left-unique predicates: implementation for the case where only the second argument is instantiated, so need to compute the value of the first (if any).</param>
 /// <param name="arg2Enumerator">For non-right-unique predicates: implementation for case where only the first argument is instantiated, so the values of the second argument must be enumerated.</param>
 /// <param name="arg2Function">For right-unique predicates: implementation for the case where only the first argument is instantiated, so need to compute the value of the second (if any)</param>
 /// <param name="doubleEnumerator">Implementation of the case where neither argument is instantiated.</param>
 public static void Declare(
     string name,
     string documentation,
     string arg1Name,
     string arg2Name,
     Filter filter,
     Arg1Enumerator arg1Enumerator     = null,
     Arg1Function arg1Function         = null,
     Arg2Enumerator arg2Enumerator     = null,
     Arg2Function arg2Function         = null,
     DoubleEnumerator doubleEnumerator = null)
 {
     PrologPrimitives.DefinePrimitive(
         Symbol.Intern(name),
         new BinaryPrimitive <T1, T2>(
             name,
             arg1Name,
             arg2Name,
             filter,
             arg1Enumerator,
             arg1Function,
             arg2Enumerator,
             arg2Function,
             doubleEnumerator).BinaryPrimitiveImplementation,
         null,
         documentation,
         arg1Name,
         arg2Name);
 }
Ejemplo n.º 6
0
        public static Symbol GetLexicalItem(string p)
        {
            var    l = p.ToLower();
            Symbol s;

            if (LexicalItems.TryGetValue(l, out s))
            {
                return(s);
            }
            return(Symbol.Intern(l));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// For testing purposes
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static ByteCompiledRule FromCode(string code)
        {
            var       term = (Structure)(new ISOPrologReader(code).ReadTerm());
            Structure head;

            Structure[] body;
            if (term.IsFunctor(Symbol.Intern(":-"), 2))
            {
                head = Term.Structurify(term.Argument(0), "Bad rule head.");
                var bodyList = new List <Structure>();
                UnwindCommaExpression(term.Argument(1), bodyList);

                body = bodyList.ToArray();
            }
            else
            {
                head = Term.Structurify(term, "Bad rule head");
                body = new Structure[0];
            }
            return(new ByteCompiledRule(KnowledgeBase.Global.EntryForStoring(head.PredicateIndicator), head, body, "none", 1));
        }
Ejemplo n.º 8
0
 public static void DeclareIndexical(string name, Func <PrologContext, object> valueFunc)
 {
     DeclareIndexical(Symbol.Intern(name), valueFunc);
 }
Ejemplo n.º 9
0
 public Structure(string functor, params object[] args)
     : this(Symbol.Intern(functor), args)
 {
 }
 public BadProcedureException(Symbol functor, int arity)
 {
     Procedure = new Structure(Symbol.Intern("/"), functor, arity);
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Add a term (fact or rule) to the KB.
        /// </summary>
        public void Assert(Structure structure, bool atEnd, bool checkSingletons)
        {
            if (structure == null)
            {
                throw new ArgumentNullException("structure", "Term to add to KB may not be null.");
            }
            //structure = structure.Expand();

            if (structure == null)
            {
                throw new ArgumentNullException("structure");
            }

            Structure head = structure.IsFunctor(Symbol.Implication, 2)
                                 ? Term.Structurify(structure.Argument(0),
                                                    "Head of :- must be a valid proposition or predicate.")
                                 : structure;

            if (head.IsFunctor(Symbol.ColonColon, 2))
            {
                var argument = head.Argument(0);
                var kb       = argument as KnowledgeBase;
                if (kb == null)
                {
                    var o = argument as GameObject;
                    if (o != null)
                    {
                        kb = o.KnowledgeBase();
                    }
                    else
                    {
                        var c = argument as Component;
                        if (c != null)
                        {
                            kb = c.KnowledgeBase();
                        }
                        else
                        {
                            throw new ArgumentTypeException(
                                      "assert",
                                      "knowledgebase",
                                      argument,
                                      typeof(KnowledgeBase));
                        }
                    }
                }
                if (structure.IsFunctor(Symbol.Implication, 2))
                {
                    kb.Assert(
                        new Structure(Symbol.Implication, head.Argument(1), structure.Argument(1)),
                        atEnd,
                        checkSingletons);
                }
                else
                {
                    kb.Assert(structure.Argument(1), atEnd, checkSingletons);
                }
            }
            else
            {
                if (PrologPrimitives.Implementations.ContainsKey(head.Functor))
                {
                    throw new PrologException(
                              new Structure(
                                  "error",
                                  new Structure(
                                      "permission_error",
                                      Symbol.Intern("modify"),
                                      Symbol.Intern("static_procedure"),
                                      Term.PredicateIndicatorExpression(head))));
                }

                KnowledgeBaseRule assertion = KnowledgeBaseRule.FromTerm(
                    structure,
                    checkSingletons,
                    Prolog.CurrentSourceFile,
                    Prolog.CurrentSourceLineNumber);
                PredicateInfo info = EntryForStoring(head.PredicateIndicator);
                PredicateInfo parentInfo;
                if (!info.Shadow && this.Parent != null &&
                    (parentInfo = this.Parent.CheckForPredicateInfoInThisKB(head.PredicateIndicator)) != null &&
                    !parentInfo.External)
                {
                    throw new PrologException(
                              new Structure(
                                  "error",
                                  new Structure(
                                      "permission_error",
                                      Symbol.Intern("shadow"),
                                      Term.PredicateIndicatorExpression(head))));
                }

                info.Assert(assertion, atEnd);
            }
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Creates a new logic variable
 /// </summary>
 /// <param name="name">Print name for the variable</param>
 public LogicVariable(string name)
     : this(Symbol.Intern(name))
 {
 }