public void Agenda() { Agenda a = new Agenda(); Implication impLow = new Implication("impLow", ImplicationPriority.Minimum, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); Implication impLowMed = new Implication("impLowMed", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); Implication impMed = new Implication("impMed", ImplicationPriority.Medium, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); Implication impMedSaLo = new Implication("impMedSaLo", ImplicationPriority.Medium, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); impMedSaLo.Salience = 1; Assert.AreEqual(5101, impMedSaLo.Weight, "Weight impMedSaLo"); Implication impMedSaHi = new Implication("impMedSaHi", ImplicationPriority.Medium, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); impMedSaHi.Salience = 99; Assert.AreEqual(5199, impMedSaHi.Weight, "Weight impMedSaLo"); Implication impMedHi = new Implication("impMedHi", 75, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); Implication impHi = new Implication("impHi", ImplicationPriority.Maximum, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); // schedule in any order a.Schedule(impMedSaHi); a.Schedule(impLow); a.Schedule(impMed); a.Schedule(impMedHi); a.Schedule(impMedSaLo); a.Schedule(impLowMed); a.Schedule(impHi); // prepare for execution, which should sort this mess out a.PrepareExecution(); Implication[] expected = new Implication[] {impHi, impMedHi, impMedSaHi, impMedSaLo, impMed, impLowMed, impLow}; int i = 0; Implication implicationParser; while (a.HasMoreToExecute) { implicationParser = a.NextToExecute; Assert.IsTrue(implicationParser.Equals(expected[i]), "Agenda not ordered: " + implicationParser.Label + "::" + implicationParser.Weight + " != " + expected[i].Label + "::" + expected[i].Weight); i++; } }
protected void WriteImplication(XmlElement target, Implication implication) { XmlElement implicationElement = Document.CreateElement("Implies", DatalogNamespaceURL); // action mapping String action = String.Empty; if (implication.Action != ImplicationAction.Assert) action = implication.Action.ToString().ToLower(); ImplicationProperties ip = new ImplicationProperties(implication.Label, implication.Priority, implication.Mutex, implication.Precondition, action); WriteLabel(implicationElement, ip.ToString()); if (syntax == SaveFormatAttributes.Compact) { // in compact mode, the order is forced to body,head (equivalent to if,then) WriteAtomGroup(implicationElement, implication.AtomGroup); WriteAtom(implicationElement, implication.Deduction, false); } else { XmlElement body = Document.CreateElement("body", DatalogNamespaceURL); WriteAtomGroup(body, implication.AtomGroup); implicationElement.AppendChild(body); XmlElement head = Document.CreateElement("head", DatalogNamespaceURL); WriteAtom(head, implication.Deduction, false); implicationElement.AppendChild(head); } if (syntax == SaveFormatAttributes.Expanded) { XmlElement formula = Document.CreateElement("formula", DatalogNamespaceURL); formula.AppendChild(implicationElement); target.AppendChild(formula); } else { target.AppendChild(implicationElement); } }
private void WriteImplication(XmlElement target, Implication implication) { XmlElement eImplication = Document.CreateElement("imp", DatalogNamespaceURL); // action mapping String action = String.Empty; if (implication.Action != ImplicationAction.Assert) action = implication.Action.ToString().ToLower(); ImplicationProperties ip = new ImplicationProperties(implication.Label, implication.Priority, implication.Mutex, implication.Precondition, action); WriteLabel(eImplication, ip.ToString()); XmlElement head = Document.CreateElement("_head", DatalogNamespaceURL); WriteAtom(head, implication.Deduction, false); eImplication.AppendChild(head); XmlElement body = Document.CreateElement("_body", DatalogNamespaceURL); WriteAtomGroup(body, implication.AtomGroup); eImplication.AppendChild(body); target.AppendChild(eImplication); }
public ImmutableEventContext(IList<IList<Fact>> facts, Implication implication) { this.facts = facts; this.implication = implication; }
internal static IEventContext NewEventContext(IList<FactBase.PositiveMatchResult> facts, Implication implication) { IList<IList<Fact>> wrappedFacts = new List<IList<Fact>>(); wrappedFacts.Add(FactBase.ExtractFacts(facts)); return new ImmutableEventContext(wrappedFacts, implication); }
internal static IEventContext NewEventContext(IList<IList<FactBase.PositiveMatchResult>> facts, Implication implication) { return new ImmutableEventContext(FactBase.ExtractFacts(facts), implication); }
private int RunImplication(Implication implication) { int implicationResultsCount = 0; IList<IList<FactBase.PositiveMatchResult>> processResults = WM.FB.ProcessAtomGroup(implication.AtomGroup); if (implication.Action == ImplicationAction.Count) { if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "Counting Implication '" + implication.Label + "' counted: " + processResults.Count); bool variableFound = false; IPredicate[] members = (IPredicate[])implication.Deduction.Members.Clone(); for(int i=0; !variableFound && i<members.Length; i++) { if (members[i] is Variable) { members[i] = new Individual(processResults.Count); variableFound = true; break; } } if ((strictImplication) && (!variableFound)) throw new BREException("Strict counting implication rejected the assertion due to lack of variable predicate: " + implication.Deduction); Fact deductedFact = new Fact(implication.Deduction.Type, members); implicationResultsCount++; // counting implication factbase action bool result = WM.FB.Assert(deductedFact); if ((result) && (NewFactHandler != null)) { if (exposeEventContext) { NewFactHandler(new NewFactEventArgs(deductedFact, EventContextFactory.NewEventContext(processResults, implication))); } else { NewFactHandler(new NewFactEventArgs(deductedFact)); } } if (Logger.IsInferenceEngineVerbose) { Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, (result?"Asserted":"Ignored Assertion of ") + " Fact: " + deductedFact.ToString()); } } else if ((implication.Action == ImplicationAction.Assert) || (implication.Action == ImplicationAction.Retract)) { // loop on each result and try to build a new fact out of the predicates coming for each result foreach(IList<FactBase.PositiveMatchResult> processResult in processResults) { Fact deductedFact = BuildFact(implication.Deduction, processResult); if (deductedFact != null) { implicationResultsCount++; if (implication.Action == ImplicationAction.Retract) { // retracting implication factbase action bool result = WM.FB.Retract(deductedFact); if ((result) && (DeleteFactHandler != null)) { if (exposeEventContext) { DeleteFactHandler(new NewFactEventArgs(deductedFact, EventContextFactory.NewEventContext(processResult, implication))); } else { DeleteFactHandler(new NewFactEventArgs(deductedFact)); } } if (Logger.IsInferenceEngineVerbose) { Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, (result?"Retracted":"Ignored Retraction of ") + " Fact: " + deductedFact.ToString()); } } else { // asserting implication factbase action bool result = WM.FB.Assert(deductedFact); if ((result) && (NewFactHandler != null)) { if (exposeEventContext) { NewFactHandler(new NewFactEventArgs(deductedFact, EventContextFactory.NewEventContext(processResult, implication))); } else { NewFactHandler(new NewFactEventArgs(deductedFact)); } } if (Logger.IsInferenceEngineVerbose) { Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, (result?"Asserted":"Ignored Assertion of ") + " Fact: " + deductedFact.ToString()); } } } } } else if (implication.Action == ImplicationAction.Modify) { foreach(IList<FactBase.PositiveMatchResult> processResult in processResults) { // look for facts to modify by: // - resolving variable predicates of the deduction // - replacing formulas with variables // and performing a search in the fact base Atom modificationTargetLookup = FactBase.BuildQueryFromDeduction(implication.Deduction, processResult); if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "Modifying Implication '" + implication.Label + "' will target matches of: " + modificationTargetLookup); foreach(Fact factToModify in FactBase.ExtractAllFacts(WM.FB.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, modificationTargetLookup)))) { if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "-> found target: " + factToModify); // for each fact, perform the modification Fact deductedFact = BuildFact(implication.Deduction, FactBase.EnrichResults(processResult, modificationTargetLookup, factToModify)); if (Logger.IsInferenceEngineVerbose) Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, "-> modified target: " + deductedFact); if ((deductedFact != null) && (!factToModify.Equals(deductedFact))) { implicationResultsCount++; bool result = WM.FB.Modify(factToModify, deductedFact); if ((result) && (ModifyFactHandler != null)) { if (exposeEventContext) { ModifyFactHandler(new NewFactEventArgs(factToModify, deductedFact, EventContextFactory.NewEventContext(processResult, implication))); } else { ModifyFactHandler(new NewFactEventArgs(factToModify, deductedFact)); } } if (Logger.IsInferenceEngineVerbose) { Logger.InferenceEngineSource.TraceEvent(TraceEventType.Verbose, 0, (result?"Modified":"Ignored Modification of ") + " Fact: " + factToModify.ToString()); } } } } } else { throw new BREException("Implication action not supported: " + implication.Action); } return implicationResultsCount; }
public void WrongImplicationLowSalience() { Implication i = new Implication(null, ImplicationPriority.Medium, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); i.Salience = -1; Assert.Fail("Should never reach me!"); }
public void QueryIsNotImplication() { Query query = new Query("get spending", new AtomGroup(AtomGroup.LogicalOperator.And, new Atom("spending", new Variable("customer"), new Individual("min(5000,EUR)"), new Individual("previous year")))); Implication implication = new Implication("get spending", ImplicationPriority.Medium, String.Empty, String.Empty, atom2_2, query.AtomGroup); Assert.IsFalse(query.Equals(implication), "QueryIsNotImplication"); }
public void PreconditionMissing() { ImplicationBase ib = new ImplicationBase(); Implication impMedLow = new Implication("impMedLow", 25, String.Empty, "missing", imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); ib.Add(impMedLow); ib.Add(new Implication("impMedHi", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3))); new PreconditionManager(ib).AnalyzeImplications(); Assert.Fail("Should never reach me!"); }
public void ImplicationBase() { ImplicationBase ib = new ImplicationBase(); Implication imp = new Implication("impMedLow", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); ib.Add(imp); ib.Add(new Implication("impMedHi", 75, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3))); Assert.AreEqual(imp, ib.Get("impMedLow"), "Get"); Assert.IsNull(ib.Get("missing bit"), "Failed Get"); }
public void GetPreconditionChildren() { ImplicationBase ib = new ImplicationBase(); Implication impMedLow = new Implication("impMedLow", 25, String.Empty, String.Empty, imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); ib.Add(impMedLow); Implication impMedHi = new Implication("impMedHi", 25, String.Empty, "impMedLow", imp2, new AtomGroup(AtomGroup.LogicalOperator.And, atom2_2, atom3)); ib.Add(impMedHi); new PreconditionManager(ib).AnalyzeImplications(); IList<Implication> preconditionChildren = ib.GetPreconditionChildren(impMedLow); Assert.AreEqual(1, preconditionChildren.Count, "preconditionChildren size"); Assert.IsTrue(preconditionChildren.Contains(impMedHi), "preconditionChildren content"); }