示例#1
0
文件: FactBase.cs 项目: vkiktev/NxBRE
        /// <summary>
        /// Modify a Fact by Retracting it and Asserting the replacement one.
        /// If the new Fact has no label (null or Empty), then the Label of the existing fact is kept.
        /// </summary>
        /// <param name="currentFact">The Fact to modify.</param>
        /// <param name="newFact">The Fact to modify to.</param>
        /// <returns>True if <term>currentFact</term> has been retracted from the FactBase, otherwise False ; this whether <term>newFact</term> already exists in the factbase, or not.</returns>
        public bool Modify(Fact currentFact, Fact newFact)
        {
            if (Retract(currentFact))
            {
                // Retraction is done, let's Assert
                try {
                    // If the new fact already exists, retract it first
                    if (Exists(newFact))
                    {
                        Retract(newFact);
                    }

                    // Preserve the label if no new one provided
                    if ((currentFact.Label != null) && (currentFact.Label != String.Empty) &&
                        ((newFact.Label == null) || (newFact.Label == String.Empty)))
                    {
                        Assert(newFact.ChangeLabel(currentFact.Label));
                    }
                    else
                    {
                        Assert(newFact);
                    }
                    return(true);
                } catch (Exception e) {
                    // Assert mysteriously failed: compensate the retraction
                    Assert(currentFact);
                    // and throw a wrapped exception
                    throw new BREException("Modify failed because Assert failed.", e);
                }
            }

            return(false);
        }
示例#2
0
        public void RenameFact()
        {
            Fact spendingPM1 = new Fact("Spending of Peter Miller",
                                        "spending",
                                        new Individual("Peter Miller"),
                                        new Individual("min(5000,EUR)"),
                                        new Individual("previous year"));

            String newLabel    = "Expenses of Peter Miller";
            Fact   spendingPM2 = spendingPM1.ChangeLabel(newLabel);

            Assert.AreEqual(spendingPM2.Label, newLabel, "Fact has been renamed");
            Assert.AreEqual(spendingPM1, spendingPM2, "Renamed facts are equals");
        }