/// <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); } }