public ArrayList GetPreconditionChildren(Implication parentImplication) { ArrayList result = new ArrayList(); foreach(Implication implication in implications) if (implication.PreconditionImplication == parentImplication) result.Add(implication); return result; }
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++; } }
public void Add(Implication implication) { if (implications.Contains(implication)) throw new BREException("The knowledge base already contains an implication labeled: " + implication.Label); implications.Add(implication); ArrayList typeListeners; foreach(Atom atom in implication.AtomGroup.AllAtoms) { if (!implicationsMap.ContainsKey(atom.Type)) { typeListeners = new ArrayList(); implicationsMap.Add(atom.Type, typeListeners); } else typeListeners = (ArrayList) implicationsMap[atom.Type]; if (!typeListeners.Contains(implication)) typeListeners.Add(implication); } }
public void Add(Implication implication) { int indexOfDuplicate = implications.IndexOf(implication); if (indexOfDuplicate >= 0) throw new BREException("When adding: " + implication + " the knowledge base detected a duplicated with: " + implications[indexOfDuplicate]); implications.Add(implication); ArrayList typeListeners; foreach(Atom atom in implication.AtomGroup.AllAtoms) { if (!implicationsMap.ContainsKey(atom.Type)) { typeListeners = new ArrayList(); implicationsMap.Add(atom.Type, typeListeners); } else typeListeners = (ArrayList) implicationsMap[atom.Type]; if (!typeListeners.Contains(implication)) typeListeners.Add(implication); } }
private int RunImplication(Implication implication) { int implicationResultsCount = 0; FactBase.ProcessResultSet processResults = WM.FB.ProcessAtomGroup(implication.AtomGroup); if (implication.Action == ImplicationAction.Count) { if (HasLogListener) ForceDispatchLog("Counting Implication '" + implication.Label + "' counted: " + processResults.Count, LogEventImpl.DEBUG); 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 ((IEImpl.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)) NewFactHandler(new NewFactEventArgs(deductedFact)); if (HasLogListener) ForceDispatchLog((result?"Asserted":"Ignored Assertion of ") + " Fact: " + deductedFact.ToString(), LogEventImpl.DEBUG); } 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(ArrayList 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)) DeleteFactHandler(new NewFactEventArgs(deductedFact)); if (HasLogListener) ForceDispatchLog((result?"Retracted":"Ignored Retraction of ") + " Fact: " + deductedFact.ToString(), LogEventImpl.DEBUG); } else { // asserting implication factbase action bool result = WM.FB.Assert(deductedFact); if ((result) && (NewFactHandler != null)) NewFactHandler(new NewFactEventArgs(deductedFact)); if (HasLogListener) ForceDispatchLog((result?"Asserted":"Ignored Assertion of ") + " Fact: " + deductedFact.ToString(), LogEventImpl.DEBUG); } } } } else if (implication.Action == ImplicationAction.Modify) { foreach(ArrayList 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 (HasLogListener) ForceDispatchLog("Modifying Implication '" + implication.Label + "' will target matches of: " + modificationTargetLookup, LogEventImpl.DEBUG); foreach(Fact factToModify in FactBase.ExtractFacts(WM.FB.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, modificationTargetLookup)))) { if (HasLogListener) ForceDispatchLog("-> found target: " + factToModify, LogEventImpl.DEBUG); // for each fact, perform the modification Fact deductedFact = BuildFact(implication.Deduction, FactBase.EnrichResults(processResult, modificationTargetLookup, factToModify)); if (HasLogListener) ForceDispatchLog("-> modified target: " + deductedFact, LogEventImpl.DEBUG); if ((deductedFact != null) && (!factToModify.Equals(deductedFact))) { implicationResultsCount++; bool result = WM.FB.Modify(factToModify, deductedFact); if ((result) && (ModifyFactHandler != null))ModifyFactHandler(new NewFactEventArgs(factToModify, deductedFact)); if (HasLogListener) ForceDispatchLog((result?"Modified":"Ignored Modification of ") + " Fact: " + factToModify.ToString(), LogEventImpl.DEBUG); } } } } else throw new BREException("Implication action not supported: " + implication.Action); return implicationResultsCount; }
private 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); } }
/// <summary> /// Schedule a single implication. /// </summary> /// <remarks> /// This method is public just for unit test purposes. /// </remarks> /// <param name="implication">The implication to schedule.</param> public void Schedule(Implication implication) { if (!agenda.Contains(implication)) agenda.Add(implication); }
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 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(); ArrayList preconditionChildren = ib.GetPreconditionChildren(impMedLow); Assert.AreEqual(1, preconditionChildren.Count, "preconditionChildren size"); Assert.IsTrue(preconditionChildren.Contains(impMedHi), "preconditionChildren content"); }