Beispiel #1
0
        public static bool TryQueryStructure(
            Structure term,
            PrologContext context,
            out ELNode foundNode,
            out ELNodeEnumerator enumerator)
        {
            //
            // Dispatch based on the functor and arity.
            //

            // Handle root queries, i.e. /Key
            if (term.IsFunctor(Symbol.Slash, 1))
                return TryRootQuery(term, context, out foundNode, out enumerator);

            if (!IsELTerm(term))
                throw new Exception("Malformed EL query: " + ISOPrologWriter.WriteToString(term));

            if (term.IsFunctor(SBindNodeOperator, 2))
            {
                var variableToBind = term.Argument(1) as LogicVariable;
                if (variableToBind == null)
                    throw new ArgumentException("RHS of >> must be an uninstantiated variable: "+ ISOPrologWriter.WriteToString(term.Argument(1)));
                foundNode = null;
                return TryNodeBindingQuery(out enumerator, term.Argument(0), variableToBind, context);
            }

            return TryChildQuery(
                out foundNode,
                out enumerator,
                term.Argument(0),
                term.Argument(1),
                term.Functor == Symbol.Colon,
                context);
        }
Beispiel #2
0
 internal IEnumerable<CutState> Retract(object term)
 {
     Structure head = Term.Structurify(term, "Argument to retract must be a valid term.");
     object body = Symbol.True;
     if (head.IsFunctor(Symbol.Implication, 2))
     {
         body = head.Argument(1);
         head = Term.Structurify(head.Argument(0), "Invalid clause head.");
     }
     return Retract(head, body);
 }
Beispiel #3
0
        internal static void WriteAndPossiblyParenthesize(StringBuilder s, object o)
        {
            Structure t = o as Structure;

            if (t != null && t.IsFunctor(Symbol.Comma, 2))
            {
                s.Append('(');
                Write(s, o);
                s.Append(')');
            }
            else
            {
                Write(s, o);
            }
        }
Beispiel #4
0
        private void WritePrologList(StringBuilder s)
        {
            s.Append('[');
            bool          first   = true;
            object        current = Canonicalize(this);
            LogicVariable l       = current as LogicVariable;

            if (l != null && l.IsBound)
            {
                current = l.Value;
            }
            while (current != null)
            {
                Structure t = current as Structure;

                if (first)
                {
                    first = false;
                }
                else if (t == null)
                {
                    s.Append(" | ");
                }
                else
                {
                    s.Append(", ");
                }
                if (t == null)
                {
                    Write(s, current);
                    current = null;
                }
                else
                {
                    if (t.IsFunctor(Symbol.PrologListConstructor, 2))
                    {
                        Write(s, t.Argument(0));
                        current = t.Argument(1);
                    }
                    else
                    {
                        WriteAndPossiblyParenthesize(s, current);
                        current = null;
                    }
                }
            }
            s.Append(']');
        }
Beispiel #5
0
        /// <summary>
        /// Returns the Term whose functor is the first element of the list and whose arguments are the other elements of the list.
        /// Functional version of =..
        /// </summary>
        public static Structure FromList(Structure listExpression)
        {
            if (listExpression == null || !listExpression.IsFunctor(Symbol.PrologListConstructor, 2))
            {
                throw new ArgumentException("Argument must be a prolog list");
            }
            object functorArg = listExpression.Argument(0);

            if (functorArg == null)
            {
                throw new ArgumentException("First element of list (functor) must be a symbol.");
            }
            var functor = functorArg as Symbol;

            if (functor == null)
            {
                throw new ArgumentException("First element of list (functor) must be a symbol.");
            }
            return(new Structure(functor, Prolog.PrologListToArray(listExpression.Arguments[1])));
        }
 /// <summary>
 /// Creates a KnowledgedBaseRule given a Term object for a :- expression.
 /// </summary>
 public static KnowledgeBaseRule FromTerm(Structure structure, bool checkSingletons, string source, int line)
 {
     if (structure == null)
     {
         throw new ArgumentNullException("structure");
     }
     if (structure.IsFunctor(Symbol.Implication, 2))
     {
         var body = new List <Structure>();
         UnwindCommaExpression(structure.Argument(1), body);
         if (structure.Argument(0) == null)
         {
             throw new ArgumentException("head of rule is null");
         }
         Structure head = Term.Structurify(structure.Argument(0), "Head of :- must be a valid proposition or predicate.");
         if (head == null)
         {
             throw new ArgumentException("Head of rule is not a term.");
         }
         return(MakeRule(head, body, checkSingletons, source, line));
     }
     return(MakeRule(structure, null, checkSingletons, source, line));
 }
Beispiel #7
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);
            }
        }