예제 #1
0
        public void ExcludeFactsFromSelection()
        {
            FactBase fb = new FactBase();

            Assert.IsTrue(fb.Assert(new Fact("spending",
                                             new Individual("Peter Miller"),
                                             new Individual(5000),
                                             new Individual("previous year"))), "Assert 'P.Miller Spending'");

            Fact jqdSpending = new Fact("JQD Spending",
                                        "spending",
                                        new Individual("John Q.Clone Doe"),
                                        new Individual(7000),
                                        new Individual("previous year"));

            Assert.IsTrue(fb.Assert(jqdSpending), "Assert 'JQD Spending'");

            Atom filter = new Atom("spending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThanEqualTo", "5000"),
                                   new Individual("previous year"));

            Assert.AreEqual(2, CountEnumerator(fb.Select(filter, null)), "Query with NxBRE function and no fact exclusion");

            IList <Fact> excludedFacts = new List <Fact>();

            excludedFacts.Add(jqdSpending);

            Assert.AreEqual(1, CountEnumerator(fb.Select(filter, excludedFacts)), "Query with NxBRE function with fact exclusion");
        }
예제 #2
0
        public void ProcessOr()
        {
            FactBase fb = new FactBase();

            PopulateFactBase(fb);

            Atom atom1 = new Atom("spending",
                                  new Variable("name"),
                                  new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "1000"),
                                  new Individual("previous year"));

            Atom atom2 = new Atom("lending",
                                  new Variable("name"),
                                  new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                  new Individual("previous year"));

            IList <IList <FactBase.PositiveMatchResult> > result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.Or, atom1, atom2));

            Assert.AreEqual(3, result.Count, "2 positive atoms");

            atom1 = new Atom("spending",
                             new Variable("name"),
                             new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "9999"),
                             new Individual("previous year"));

            result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.Or, atom1, atom2));
            Assert.AreEqual(1, result.Count, "1 positive atom");
        }
예제 #3
0
        public void FactBaseCloning()
        {
            FactBase fb = new FactBase();

            PopulateFactBase(fb);

            FactBase clone = (FactBase)fb.Clone();

            Assert.AreEqual(fb.Count, clone.Count, "Same number of facts");

            Fact jqdSpending = clone.GetFact("JQD Spending");

            Assert.AreEqual("JQD Spending", jqdSpending.Label, "Correct fact label");

            int countBefore = clone.Count;

            Assert.IsTrue(clone.Retract(jqdSpending), "Retracted 'JQD Spending'");
            Assert.AreEqual(countBefore - 1, clone.Count, "Count after retraction");

            Assert.IsFalse(clone.Exists(jqdSpending), "'JQD Spending' really retracted");
            Assert.IsTrue(fb.Exists(jqdSpending), "'JQD Spending' still in original");

            Atom filter = new Atom("spending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                   new Individual("previous year"));

            Assert.AreEqual(1, CountEnumerator(fb.Select(filter, null)), "Query on the original");
            Assert.AreEqual(0, CountEnumerator(clone.Select(filter, null)), "Query on the clone");
        }
예제 #4
0
        public void FactBaseAssertRetract()
        {
            FactBase fb = new FactBase();

            Assert.IsTrue(fb.Assert(new Fact("spending",
                                             new Individual("Peter Miller"),
                                             new Individual("min(5000,EUR)"),
                                             new Individual("previous year"))), "Assert 'P.Miller Spending'");

            Assert.IsTrue(fb.Assert(new Fact("JQD Spending",
                                             "spending",
                                             new Individual("John Q.Clone Doe"),
                                             new Individual("min(7000,EUR)"),
                                             new Individual("previous year"))), "Assert 'JQD Spending'");

            Assert.IsNotNull(fb.GetFact("JQD Spending"), "Exist 'JQD Spending'");

            Assert.IsFalse(fb.Assert(fact3bis), "Assert fact3bis");
            Assert.IsTrue(fb.Assert(factX), "Assert factX");

            Fact jqdSpending = fb.GetFact("JQD Spending");

            Assert.IsTrue(fb.Exists(jqdSpending));
            Assert.IsTrue(fb.Retract(jqdSpending));
            Assert.IsFalse(fb.Exists(jqdSpending));

            Assert.IsNull(fb.GetFact("JQD Spending"), "Get Retracted 'JQD Spending'");
        }
예제 #5
0
        public static FactBase GetFactBase(string item)
        {
            var fb = new FactBase();

            fb.SetFromString(item);
            return(fb);
        }
예제 #6
0
        /// <summary>
        /// Assert a fact to the FactBase.
        /// </summary>
        private static void CoreAssert(string rel, object e1, object e2, object e3, Tvar val)
        {
            // Don't assert a fact that's already been asserted
            if (!HasBeenAsserted(rel, e1, e2, e3))
            {
                // Assert the fact
                Fact f = new Fact(rel, e1, e2, e3, val);
                FactBase.Add(f);

                // TODO: This breaks when the objects are not Things (hence the try-catch)
                try
                {
                    // Add Things to the ThingBase
                    AddThing((Thing)e1);
                    AddThing((Thing)e2);
                    AddThing((Thing)e3);

                    // Look for additional inferences that can be drawn, based on assumptions.
                    Assumptions.TriggerInferences(rel, (Thing)e1, (Thing)e2, (Thing)e3, val);
                }
                catch
                {
                }
            }
        }
예제 #7
0
        public static string GetFactString(Concept concept, List <Dimension> dimensions)
        {
            var fb = new FactBase();

            fb.Concept    = concept;
            fb.Dimensions = dimensions;
            return(fb.GetFactString());
        }
예제 #8
0
        public string GetFactString(List <Dimension> dimensions)
        {
            var typedfact = new FactBase();

            typedfact.Dimensions = dimensions.ToList();
            var typedfactstring = typedfact.FactString.Trim();

            return(typedfactstring);
        }
예제 #9
0
        public void FactBaseAssertModify()
        {
            FactBase fb = new FactBase();

            Fact spendingPM1 = new Fact("Spending of Peter Miller",
                                        "spending",
                                        new Individual("Peter Miller"),
                                        new Individual("min(5000,EUR)"),
                                        new Individual("previous year"));

            Assert.IsTrue(fb.Assert(spendingPM1), "Assert 'P.Miller Spending' v1");
            Assert.IsFalse(fb.Assert(spendingPM1), "Can not assert 'P.Miller Spending' v1 twice!");
            Assert.IsTrue(fb.Exists(spendingPM1), "Exists 'P.Miller Spending' v1");

            Fact spendingJQD = new Fact("Spending of John Q. Doe",
                                        "spending",
                                        new Individual("John Q.Clone Doe"),
                                        new Individual("min(7000,EUR)"),
                                        new Individual("previous year"));

            Assert.IsTrue(fb.Assert(spendingJQD), "Assert 'JQD Spending'");

            Fact spendingPM2 = new Fact("spending",
                                        new Individual("Peter Miller"),
                                        new Individual("min(9999,EUR)"),
                                        new Individual("previous year"));

            Assert.IsTrue(fb.Modify(spendingPM1, spendingPM2), "Modify 'P.Miller Spending' v1 to v2");
            Assert.IsNotNull(fb.GetFact(spendingPM1.Label), "(1) Label has been maintained");
            Assert.AreEqual(2, fb.Count, "(1) FactBase size");
            Assert.IsFalse(fb.Exists(spendingPM1), "(1) Not Exists 'P.Miller Spending' v1");
            Assert.IsTrue(fb.Exists(spendingPM2), "(1) Exists 'P.Miller Spending' v2");
            Assert.IsTrue(fb.Exists(spendingJQD), "(1) Exists 'JQD Spending'");

            Assert.IsFalse(fb.Modify(spendingPM1, spendingPM2), "Can not modify an inexistant fact");
            Assert.AreEqual(2, fb.Count, "(2) FactBase size");
            Assert.IsFalse(fb.Exists(spendingPM1), "(2) Not Exists 'P.Miller Spending' v1");
            Assert.IsTrue(fb.Exists(spendingPM2), "(2) Exists 'P.Miller Spending' v2");
            Assert.IsTrue(fb.Exists(spendingJQD), "(2) Exists 'JQD Spending'");

            Assert.IsTrue(fb.Modify(spendingPM2, spendingPM2), "Can modify a fact to itself");
            Assert.AreEqual(2, fb.Count, "(3) FactBase size");
            Assert.IsFalse(fb.Exists(spendingPM1), "(3) Not Exists 'P.Miller Spending' v1");
            Assert.IsTrue(fb.Exists(spendingPM2), "(3) Exists 'P.Miller Spending' v2");
            Assert.IsTrue(fb.Exists(spendingJQD), "(3) Exists 'JQD Spending'");

            Assert.IsTrue(fb.Modify(spendingJQD, spendingPM2), "Modify 'JQD Spending' to 'P.Miller Spending' v2");
            Assert.IsNotNull(fb.GetFact(spendingJQD.Label), "(4) Label has been maintained");
            Assert.AreEqual(1, fb.Count, "(4) FactBase size");
            Assert.IsFalse(fb.Exists(spendingPM1), "(4) Not Exists 'P.Miller Spending' v1");
            Assert.IsTrue(fb.Exists(spendingPM2), "(4) Exists 'P.Miller Spending' v2");
            Assert.IsFalse(fb.Exists(spendingJQD), "(4) Not Exists 'JQD Spending'");
        }
예제 #10
0
        public void PredicateNxBREFunction()
        {
            FactBase fb = new FactBase();

            PopulateFactBase(fb);

            Atom filter = new Atom("spending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                   new Individual("previous year"));

            Assert.AreEqual(1, CountEnumerator(fb.Select(filter, null)), "Query with NxBRE function");
        }
예제 #11
0
        public string GetDynamicCellID(string cellID, FactBase fact)
        {
            var intkeys = this.GetFactIntKey(fact.FactString);

            if (this.FactDictionary.FactsByInstanceKey.ContainsKey(intkeys))
            {
                var ix       = this.FactDictionary.FactsByInstanceKey[intkeys];
                var instfact = this.FactDictionary.FactsByIndex[ix];
                return(GetDynamicCellID(cellID, instfact));
            }
            else
            {
            }
            return(cellID);
        }
예제 #12
0
        private Fact BuildFact(Atom targetAtom, ArrayList processResult)
        {
            Atom factBuilder = FactBase.Populate(targetAtom, processResult, true);

            if (!factBuilder.IsFact)
            {
                if (IEImpl.StrictImplication)
                {
                    throw new BREException("Strict implication rejected the assertion of incompletely resolved fact: " + factBuilder);
                }
            }
            else
            {
                return(new Fact(factBuilder));
            }

            return(null);
        }
예제 #13
0
        protected InstanceFact GetFact(string factstring, Instance instance)
        {
            var facts = new List <InstanceFact>();

            if (instance.FactDictionary.ContainsKey(factstring))
            {
                facts = instance.FactDictionary.GetFacts(factstring);
                if (facts.Count == 1)
                {
                    return(facts[0]);
                }
            }
            var shouldprocess = false;

            foreach (var td in Taxonomy.TypedDimensions)
            {
                if (factstring.Contains(td.Key))
                {
                    shouldprocess = true;
                    break;
                }
            }
            if (shouldprocess)
            {
                var factbase = new FactBase();
                factbase.SetFromString(factstring);
                var factkey = factbase.GetFactKey();
                if (instance.FactDictionary.ContainsKey(factkey))
                {
                    facts = instance.FactDictionary.GetFacts(factkey);
                    foreach (var fact in facts)
                    {
                        if (fact.FactString == factstring)
                        {
                            return(fact);
                        }
                    }
                    facts.Clear();
                }
            }
            return(facts.FirstOrDefault());
        }
예제 #14
0
        private int RunStrictTypingFactBaseTest(bool strictTyping)
        {
            FactBase fb = new FactBase();

            fb.strictTyping = strictTyping;

            Assert.IsTrue(fb.Assert(new Fact("spending",
                                             new Individual("foo"),
                                             new Individual(7000))), "Assert 'foo Spending'");

            Assert.IsTrue(fb.Assert(new Fact("spending",
                                             new Individual("bar"),
                                             new Individual("7000"))), "Assert 'bar Spending'");

            Atom filter = new Atom("spending",
                                   new Variable("name"),
                                   new Individual("7000"));

            return(CountEnumerator(fb.Select(filter, null)));
        }
예제 #15
0
        private void PopulateFactBase(FactBase fb)
        {
            Assert.IsTrue(fb.Assert(new Fact("spending",
                                             new Individual("foo"),
                                             new Individual(7000))), "Assert 'foo Spending'");

            Assert.IsTrue(fb.Assert(new Fact("spending",
                                             new Individual("Peter Miller"),
                                             new Individual(5000),
                                             new Individual("previous year"))), "Assert 'P.Miller Spending'");

            Assert.IsTrue(fb.Assert(new Fact("JQD Spending",
                                             "spending",
                                             new Individual("John Q.Clone Doe"),
                                             new Individual(7000),
                                             new Individual("previous year"))), "Assert 'JQD Spending'");

            Assert.IsTrue(fb.Assert(new Fact("lending",
                                             new Individual("John Q.Clone Doe"),
                                             new Individual(7000),
                                             new Individual("previous year"))), "Assert 'JQD Lending'");
        }
예제 #16
0
        public void ProcessAnd()
        {
            FactBase fb = new FactBase();

            PopulateFactBase(fb);

            Atom atom1 = new Atom("spending",
                                  new Variable("name"),
                                  new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                  new Individual("previous year"));

            Atom atom2 = new Atom("lending",
                                  new Variable("name"),
                                  new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                  new Individual("previous year"));

            IList <IList <FactBase.PositiveMatchResult> > result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, atom1, atom2));

            Assert.AreEqual(1, result.Count, "2 positive atoms");

            atom2 = new Atom("lending",
                             new Variable("name"),
                             new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "15000"),
                             new Individual("previous year"));

            result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, atom1, atom2));
            Assert.AreEqual(0, result.Count, "1 positive and 1 negative atoms");

            atom2 = new Atom("spending", new Variable("otherName"), new Variable("whateverAmount"), new Individual("previous year"));

            result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, atom1, atom2));
            Assert.AreEqual(2, result.Count, "2 positive combinatorial atoms");

            atom2  = new Atom("spending", new Variable("otherName"), new Variable("whateverAmount"), new Individual("unknown year"));
            result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, atom1, atom2));
            Assert.AreEqual(0, result.Count, "1 positive and 1 negative combinatorial atoms");
        }
예제 #17
0
        public static LayoutItem Copy(LayoutItem li_original)
        {
            var li_new = new LayoutItem();

            li_new._Axis      = li_original._Axis;
            li_new._Category  = li_original._Category;
            li_new.FactString = li_original.FactString;

            var fact = new FactBase();

            fact.SetFromString(li_new.FactString);

            li_new.Dimensions = fact.Dimensions;
            li_new.Concept    = fact.Concept;

            li_new._ID           = li_original._ID;
            li_new._IsAbstract   = li_original._IsAbstract;
            li_new.Label         = li_original.Label;
            li_new._LabelCode    = li_original._LabelCode;
            li_new._LabelContent = li_original._LabelContent;
            li_new._LabelID      = li_original._LabelID;
            li_new._Role         = li_original._Role;
            return(li_new);
        }
예제 #18
0
 public FactBase GetFactBaseByIndexString(string factstring)
 {
     if (factstring.StartsWith("I:"))
     {
         var id = Utilities.Converters.FastParse(factstring.Substring(2));
         if (id > -1 && id < this.Facts.Count)
         {
             return(this.Facts[id]);
         }
     }
     if (factstring.StartsWith("T:"))
     {
         var id = Utilities.Converters.FastParse(factstring.Substring(2));
         if (Taxonomy.FactsManager.Count > id)
         {
             var fact      = new FactBase();
             var key       = Taxonomy.FactsManager.GetFactKey(id);
             var stringkey = Taxonomy.GetFactStringKey(key);
             fact.SetFromString(stringkey);
             return(fact);
         }
     }
     return(null);
 }
예제 #19
0
파일: TestCore.cs 프로젝트: Ghasan/NxBRE
        public void ProcessAnd()
        {
            FactBase fb = new FactBase();
            PopulateFactBase(fb);

            Atom atom1 = new Atom("spending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                   new Individual("previous year"));

            Atom atom2 = new Atom("lending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                   new Individual("previous year"));

            IList<IList<FactBase.PositiveMatchResult>> result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, atom1, atom2));
            Assert.AreEqual(1, result.Count, "2 positive atoms");

            atom2 = new Atom("lending",
                       new Variable("name"),
                       new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "15000"),
                       new Individual("previous year"));

            result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, atom1, atom2));
            Assert.AreEqual(0, result.Count, "1 positive and 1 negative atoms");

            atom2 = new Atom("spending", new Variable("otherName"), new Variable("whateverAmount"), new Individual("previous year"));

            result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, atom1, atom2));
            Assert.AreEqual(2, result.Count, "2 positive combinatorial atoms");

            atom2 = new Atom("spending", new Variable("otherName"), new Variable("whateverAmount"), new Individual("unknown year"));
            result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.And, atom1, atom2));
            Assert.AreEqual(0, result.Count, "1 positive and 1 negative combinatorial atoms");
        }
예제 #20
0
파일: TestCore.cs 프로젝트: Ghasan/NxBRE
        public void ExcludeFactsFromSelection()
        {
            FactBase fb = new FactBase();

            Assert.IsTrue(fb.Assert(new Fact("spending",
                                                        new Individual("Peter Miller"),
                                                        new Individual(5000),
                                                        new Individual("previous year"))), "Assert 'P.Miller Spending'");

            Fact jqdSpending = new Fact("JQD Spending",
                                                   "spending",
                                                new Individual("John Q.Clone Doe"),
                                                new Individual(7000),
                                                new Individual("previous year"));

            Assert.IsTrue(fb.Assert(jqdSpending), "Assert 'JQD Spending'");

            Atom filter = new Atom("spending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThanEqualTo", "5000"),
                                   new Individual("previous year"));

            Assert.AreEqual(2, CountEnumerator(fb.Select(filter, null)), "Query with NxBRE function and no fact exclusion");

            IList<Fact> excludedFacts = new List<Fact>();
            excludedFacts.Add(jqdSpending);

            Assert.AreEqual(1, CountEnumerator(fb.Select(filter, excludedFacts)), "Query with NxBRE function with fact exclusion");
        }
예제 #21
0
파일: TestCore.cs 프로젝트: Ghasan/NxBRE
        public void FactBaseAssertModify()
        {
            FactBase fb = new FactBase();

            Fact spendingPM1 = new Fact("Spending of Peter Miller",
                                        "spending",
                                               new Individual("Peter Miller"),
                                               new Individual("min(5000,EUR)"),
                                               new Individual("previous year"));

            Assert.IsTrue(fb.Assert(spendingPM1), "Assert 'P.Miller Spending' v1");
            Assert.IsFalse(fb.Assert(spendingPM1), "Can not assert 'P.Miller Spending' v1 twice!");
            Assert.IsTrue(fb.Exists(spendingPM1), "Exists 'P.Miller Spending' v1");

            Fact spendingJQD = new Fact("Spending of John Q. Doe",
                                        "spending",
                                            new Individual("John Q.Clone Doe"),
                                            new Individual("min(7000,EUR)"),
                                            new Individual("previous year"));

            Assert.IsTrue(fb.Assert(spendingJQD), "Assert 'JQD Spending'");

            Fact spendingPM2 = new Fact("spending",
                                               new Individual("Peter Miller"),
                                               new Individual("min(9999,EUR)"),
                                               new Individual("previous year"));

            Assert.IsTrue(fb.Modify(spendingPM1, spendingPM2), "Modify 'P.Miller Spending' v1 to v2");
            Assert.IsNotNull(fb.GetFact(spendingPM1.Label), "(1) Label has been maintained");
            Assert.AreEqual(2, fb.Count, "(1) FactBase size");
            Assert.IsFalse(fb.Exists(spendingPM1), "(1) Not Exists 'P.Miller Spending' v1");
            Assert.IsTrue(fb.Exists(spendingPM2), "(1) Exists 'P.Miller Spending' v2");
            Assert.IsTrue(fb.Exists(spendingJQD), "(1) Exists 'JQD Spending'");

            Assert.IsFalse(fb.Modify(spendingPM1, spendingPM2), "Can not modify an inexistant fact");
            Assert.AreEqual(2, fb.Count, "(2) FactBase size");
            Assert.IsFalse(fb.Exists(spendingPM1), "(2) Not Exists 'P.Miller Spending' v1");
            Assert.IsTrue(fb.Exists(spendingPM2), "(2) Exists 'P.Miller Spending' v2");
            Assert.IsTrue(fb.Exists(spendingJQD), "(2) Exists 'JQD Spending'");

            Assert.IsTrue(fb.Modify(spendingPM2, spendingPM2), "Can modify a fact to itself");
            Assert.AreEqual(2, fb.Count, "(3) FactBase size");
            Assert.IsFalse(fb.Exists(spendingPM1), "(3) Not Exists 'P.Miller Spending' v1");
            Assert.IsTrue(fb.Exists(spendingPM2), "(3) Exists 'P.Miller Spending' v2");
            Assert.IsTrue(fb.Exists(spendingJQD), "(3) Exists 'JQD Spending'");

            Assert.IsTrue(fb.Modify(spendingJQD, spendingPM2), "Modify 'JQD Spending' to 'P.Miller Spending' v2");
            Assert.IsNotNull(fb.GetFact(spendingJQD.Label), "(4) Label has been maintained");
            Assert.AreEqual(1, fb.Count, "(4) FactBase size");
            Assert.IsFalse(fb.Exists(spendingPM1), "(4) Not Exists 'P.Miller Spending' v1");
            Assert.IsTrue(fb.Exists(spendingPM2), "(4) Exists 'P.Miller Spending' v2");
            Assert.IsFalse(fb.Exists(spendingJQD), "(4) Not Exists 'JQD Spending'");
        }
예제 #22
0
파일: TestCore.cs 프로젝트: Ghasan/NxBRE
        public void FactBaseAssertRetract()
        {
            FactBase fb = new FactBase();

            Assert.IsTrue(fb.Assert(new Fact("spending",
                            new Individual("Peter Miller"),
                            new Individual("min(5000,EUR)"),
                            new Individual("previous year"))), "Assert 'P.Miller Spending'");

            Assert.IsTrue(fb.Assert(new Fact("JQD Spending",
                               "spending",
                            new Individual("John Q.Clone Doe"),
                            new Individual("min(7000,EUR)"),
                            new Individual("previous year"))), "Assert 'JQD Spending'");

            Assert.IsNotNull(fb.GetFact("JQD Spending"), "Exist 'JQD Spending'");

            Assert.IsFalse(fb.Assert(fact3bis), "Assert fact3bis");
            Assert.IsTrue(fb.Assert(factX), "Assert factX");

            Fact jqdSpending = fb.GetFact("JQD Spending");
            Assert.IsTrue(fb.Exists(jqdSpending));
            Assert.IsTrue(fb.Retract(jqdSpending));
            Assert.IsFalse(fb.Exists(jqdSpending));

            Assert.IsNull(fb.GetFact("JQD Spending"), "Get Retracted 'JQD Spending'");
        }
예제 #23
0
파일: TestCore.cs 프로젝트: Ghasan/NxBRE
        public void FactBaseCloning()
        {
            FactBase fb = new FactBase();
            PopulateFactBase(fb);

            FactBase clone = (FactBase)fb.Clone();
            Assert.AreEqual(fb.Count, clone.Count, "Same number of facts");

            Fact jqdSpending = clone.GetFact("JQD Spending");
            Assert.AreEqual("JQD Spending", jqdSpending.Label, "Correct fact label");

            int countBefore = clone.Count;
            Assert.IsTrue(clone.Retract(jqdSpending), "Retracted 'JQD Spending'");
            Assert.AreEqual(countBefore-1, clone.Count, "Count after retraction");

            Assert.IsFalse(clone.Exists(jqdSpending), "'JQD Spending' really retracted");
            Assert.IsTrue(fb.Exists(jqdSpending), "'JQD Spending' still in original");

            Atom filter = new Atom("spending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                   new Individual("previous year"));

            Assert.AreEqual(1, CountEnumerator(fb.Select(filter, null)), "Query on the original");
            Assert.AreEqual(0, CountEnumerator(clone.Select(filter, null)), "Query on the clone");
        }
예제 #24
0
파일: TestCore.cs 프로젝트: Ghasan/NxBRE
        private int RunStrictTypingFactBaseTest(bool strictTyping)
        {
            FactBase fb = new FactBase();
            fb.strictTyping = strictTyping;

            Assert.IsTrue(fb.Assert(new Fact("spending",
                                                        new Individual("foo"),
                                                        new Individual(7000))), "Assert 'foo Spending'");

            Assert.IsTrue(fb.Assert(new Fact("spending",
                                                        new Individual("bar"),
                                                        new Individual("7000"))), "Assert 'bar Spending'");

            Atom filter = new Atom("spending",
                                   new Variable("name"),
                                   new Individual("7000"));

            return CountEnumerator(fb.Select(filter, null));
        }
예제 #25
0
파일: TestCore.cs 프로젝트: Ghasan/NxBRE
        public void PredicateNxBREFunction()
        {
            FactBase fb = new FactBase();
            PopulateFactBase(fb);

            Atom filter = new Atom("spending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                   new Individual("previous year"));

            Assert.AreEqual(1, CountEnumerator(fb.Select(filter, null)), "Query with NxBRE function");
        }
예제 #26
0
        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);
        }
예제 #27
0
        public override void PrepareInitialization()
        {
            lockStrategy.AcquireUniqueLock(lockTimeOut);

            WorkingType = WorkingMemoryTypes.Global;
            globalFactBase = new FactBase();
        }
예제 #28
0
 public override void PrepareInitialization()
 {
     WorkingType = WorkingMemoryTypes.Global;
     globalFactBase = new FactBase();
     WorkingFactBase = globalFactBase;
 }
예제 #29
0
파일: TestCore.cs 프로젝트: Ghasan/NxBRE
        public void ProcessOr()
        {
            FactBase fb = new FactBase();
            PopulateFactBase(fb);

            Atom atom1 = new Atom("spending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "1000"),
                                   new Individual("previous year"));

            Atom atom2 = new Atom("lending",
                                   new Variable("name"),
                                   new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "5000"),
                                   new Individual("previous year"));

            IList<IList<FactBase.PositiveMatchResult>> result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.Or, atom1, atom2));
            Assert.AreEqual(3, result.Count, "2 positive atoms");

            atom1 = new Atom("spending",
                       new Variable("name"),
                       new Function(Function.FunctionResolutionType.NxBRE, "foo", null, "GreaterThan", "9999"),
                       new Individual("previous year"));

            result = fb.ProcessAtomGroup(new AtomGroup(AtomGroup.LogicalOperator.Or, atom1, atom2));
            Assert.AreEqual(1, result.Count, "1 positive atom");
        }
예제 #30
0
파일: TestCore.cs 프로젝트: Ghasan/NxBRE
        private void PopulateFactBase(FactBase fb)
        {
            Assert.IsTrue(fb.Assert(new Fact("spending",
                                                        new Individual("foo"),
                                                        new Individual(7000))), "Assert 'foo Spending'");

            Assert.IsTrue(fb.Assert(new Fact("spending",
                                                        new Individual("Peter Miller"),
                                                        new Individual(5000),
                                                        new Individual("previous year"))), "Assert 'P.Miller Spending'");

            Assert.IsTrue(fb.Assert(new Fact("JQD Spending",
                                                           "spending",
                                                        new Individual("John Q.Clone Doe"),
                                                        new Individual(7000),
                                                        new Individual("previous year"))), "Assert 'JQD Spending'");

            Assert.IsTrue(fb.Assert(new Fact("lending",
                                                        new Individual("John Q.Clone Doe"),
                                                        new Individual(7000),
                                                        new Individual("previous year"))), "Assert 'JQD Lending'");
        }
예제 #31
0
        public Hierarchy <LayoutItem> GetInstanceExtensions(Table table)
        {
            if (table.ID.Contains("07"))
            {
            }
            if (ExtDictionary.Count == 0)
            {
                return(table.Extensions);
            }
            else
            {
                if (this.Extensions == null)
                {
                    var hlroot = new Hierarchy <LayoutItem>(table.Extensions.Item);
                    var ix     = 1;
                    foreach (var typedext in ExtDictionary)
                    {
                        var factstring = typedext.Key;
                        var typedfact  = new FactBase();
                        typedfact.SetFromString(factstring);
                        typedfact.SetTyped();

                        foreach (var ext in table.Extensions.Children)
                        {
                            var li_original = ext.Item;
                            var li_new      = LayoutItem.Copy(li_original);
                            foreach (var dim in li_new.Dimensions)
                            {
                                var instancedim = typedfact.Dimensions.FirstOrDefault(i => i.DimensionItem == dim.DimensionItem && i.Domain == dim.Domain);
                                if (instancedim != null)
                                {
                                    dim.DomainMember = instancedim.DomainMember;
                                }
                            }
                            var fact = new FactBase();
                            fact.Dimensions = li_new.Dimensions;
                            fact.Concept    = li_new.Concept;
                            fact.SetFactString();
                            li_new.FactString = fact.FactString;

                            Label extensionlabel = new Label();
                            var   code           = String.Format(Table.LabelCodeFormat, typedext.Value);
                            var   content        = String.Format(Table.ExtensionLableContentFormat, code);

                            extensionlabel.LabelID = code;
                            extensionlabel.Code    = code;
                            extensionlabel.Content = content;
                            if (fact.Dimensions.Count == 1)
                            {
                                extensionlabel      = table.Taxonomy.GetLabelForDimensionDomainMember(fact.Dimensions.FirstOrDefault());
                                extensionlabel.Code = code;
                            }

                            if (fact.Dimensions.Count > 1)
                            {
                                extensionlabel.Content = "";
                                extensionlabel.Code    = "";
                                foreach (var dim in fact.Dimensions)
                                {
                                    extensionlabel.Code    += dim.DomainMemberFullName + ",";
                                    extensionlabel.Content += table.Taxonomy.GetLabelForDimensionDomainMember(dim) + ",";
                                }
                            }
                            li_new.ID           = extensionlabel.LabelID;
                            li_new.LabelCode    = extensionlabel.Code;
                            li_new.LabelContent = extensionlabel.Content;

                            var hli = new Hierarchy <LayoutItem>(li_new);
                            hlroot.Children.Add(hli);
                            hli.Parent = hlroot;

                            ix++;
                        }
                    }
                    Extensions = hlroot;
                }
                return(Extensions);
            }
        }
예제 #32
0
        public Cell AddCells_Old_2(Cell cell, InstanceFact fact, Table table)
        {
            var dynamiccell = new Cell();

            dynamiccell.Report    = cell.Report;
            dynamiccell.Extension = cell.Extension;
            dynamiccell.Row       = cell.Row;
            dynamiccell.Column    = cell.Column;

            if (fact.Dimensions.Count == 0)
            {
                var factstring = Instance.GetFactStringKey(fact.InstanceKey);
                var tmpfact    = new FactBase();
                tmpfact.SetFromString(factstring);
                fact.Dimensions.AddRange(tmpfact.Dimensions);
            }

            if (cell.Extension == Literals.DynamicCode)
            {
                var ext            = table.Extensions.Children.FirstOrDefault();
                var opendimensions = ext.Item.Dimensions.Where(i => String.IsNullOrEmpty(i.DomainMember)).ToList();

                //var typeddimensions = ext.Item.Dimensions.Where(i => i.IsTyped).ToList();
                //var axistypeddimensions = Dimension.GetDimensions(fact.Dimensions, typeddimensions);

                var instanceopendimensions = Dimension.GetDimensions(fact.Dimensions, opendimensions);
                var openfactstring         = GetFactString(instanceopendimensions);

                if (!ExtDictionary.ContainsKey(openfactstring))
                {
                    var extnr = String.Format("{0}", ExtDictionary.Count + 1);
                    ExtDictionary.Add(openfactstring, extnr);
                }
                dynamiccell.Extension = ExtDictionary[openfactstring];
            }
            if (cell.Row == Literals.DynamicCode)
            {
                // var cell = table.Rows

                var row                 = table.Rows.FirstOrDefault(i => i.Item.LabelCode == cell.Row);
                var typeddimensions     = row.Item.Dimensions.Where(i => i.IsTyped).ToList();
                var axistypeddimensions = Dimension.GetDimensions(fact.Dimensions, typeddimensions);
                var typedfactstring     = GetTypedFactString(axistypeddimensions);

                if (!RowDictionary.ContainsKey(typedfactstring))
                {
                    var rownr = String.Format("{0}", RowDictionary.Count + 1);
                    RowDictionary.Add(typedfactstring, rownr);
                }
                dynamiccell.Row = RowDictionary[typedfactstring];
            }
            if (cell.Column == Literals.DynamicCode)
            {
                // var cell = table.Rows

                var col                 = table.Columns.FirstOrDefault(i => i.Item.LabelCode == cell.Row);
                var typeddimensions     = col.Item.Dimensions.Where(i => i.IsTyped).ToList();
                var axistypeddimensions = Dimension.GetDimensions(fact.Dimensions, typeddimensions);
                var typedfactstring     = GetTypedFactString(axistypeddimensions);

                if (!ColDictionary.ContainsKey(typedfactstring))
                {
                    var rownr = String.Format("{0}", RowDictionary.Count + 1);
                    ColDictionary.Add(typedfactstring, rownr);
                }
                dynamiccell.Column = ColDictionary[typedfactstring];
            }
            //if (dynamiccell.CellID != cell.CellID)
            //{
            //    //var cellfactstring = fact.FactString;
            //    //if (CellOfFact.ContainsKey(fact.FactString))
            //    //if (1 == 2)
            //    //{
            //    //    //var existing = CellOfFact[fact.FactString];

            //    //    var existingfacts = TaxonomyEngine.CurrentEngine.CurrentInstance.FactDictionary.FactsByTaxonomyKey[fact.InstanceKey];
            //    //    //var existingfact = existingfacts.FirstOrDefault(i => i.FactString == fact.FactString);
            //    //    var ctid = typeof(InstanceFact).IsAssignableFrom(fact.GetType()) ? ((InstanceFact)fact).ContextID : "";
            //    //    var msg = String.Format("Fact {0} already exist >> {1}!", fact, ctid);
            //    //    Utilities.Logger.WriteLine(msg);
            //    //}
            //    //else
            //    //{
            //    //    //var item = this.Instance.FactDictionary[fact.FactIntkeys];
            //    //    //CellOfFact.Add(fact.FactString, dynamiccell.CellID);
            //    //}
            //}
            //else
            //{

            //}
            fact.Cells.Add(dynamiccell.CellID);

            return(dynamiccell);
        }
예제 #33
0
        public string CheckCells(LogicalModel.Validation.ValidationParameter parameter)
        {
            var cellfound = false;
            var sb        = new StringBuilder();
            var sequence  = parameter.BindAsSequence ? "Sequence" : "";

            sb.AppendLine("parameter: " + parameter.Name + " " + sequence);
            var c_sb = new StringBuilder();
            var log  = false;

            foreach (var factgroup in parameter.TaxFacts)
            {
                foreach (var factid in factgroup.AsEnumerable())
                {
                    var cellslist = new List <List <String> >();
                    var factkey   = Taxonomy.FactsManager.GetFactKey(factid);
                    var fact      = FactBase.GetFactFrom(Taxonomy.GetFactStringKey(factkey));
                    if (Taxonomy.HasFact(factkey))
                    {
                        var cells = Taxonomy.GetCellsOfFact(factkey);
                        if (parameter.RuleID.Contains("0602"))
                        {
                            cellslist.Add(cells.Select(i => i + " {" + factkey + "} ").ToList());
                        }
                        else
                        {
                            cellslist.Add(cells);
                        }
                        if (cells.Count == 0)
                        {
                            if (log)
                            {
                                c_sb.AppendLine(parameter.RuleID + " fact found but no cells! " + factkey);
                            }
                        }
                    }
                    else
                    {
                        var s_facts = Taxonomy.FactKeysAsEnumerable().Select(i => i).AsEnumerable();
                        if (fact.Concept != null)
                        {
                            //s_facts = s_facts.Where(i => i.StartsWith(fact.Concept.Content));
                            //if (Taxonomy.FactsOfDimensions.ContainsKey(fact.Concept.Content))
                            var ix = Taxonomy.FactParts[fact.Concept.Content];
                            if (Taxonomy.FactsOfParts.ContainsKey(ix))
                            {
                                s_facts = Taxonomy.FactsOfParts[ix].Select(
                                    i => Taxonomy.FactsManager.GetFactKey(i));
                            }
                            else
                            {
                                s_facts = new List <int[]>();
                            }
                        }
                        foreach (var dimension in fact.Dimensions)
                        {
                            s_facts = s_facts.Where(i => Taxonomy.GetFactStringKey(i).Contains(dimension.DomainMemberFullName));
                        }
                        // var s_factlist = s_facts.ToList();
                        var hasanyfact = false;

                        foreach (var s_fact in s_facts)
                        {
                            hasanyfact = true;
                            var cells = Taxonomy.GetCellsOfFact(s_fact);
                            if (cells.Count == 0)
                            {
                                if (log)
                                {
                                    c_sb.AppendLine(parameter.RuleID + " for parameter " + parameter.Name + " no cells were found! " + s_fact);
                                }
                            }
                            if (parameter.RuleID.Contains("0602"))
                            {
                                cellslist.Add(cells.Select(i => i + " {" + factkey + "} ").ToList());
                            }
                            else
                            {
                                cellslist.Add(cells);
                            }
                        }

                        if (!hasanyfact)
                        {
                            if (log)
                            {
                                c_sb.AppendLine(parameter.RuleID + " fact for parameter " + parameter.Name + " not found! " + factkey);
                            }
                        }
                    }
                    if (cellslist.Count == 0)
                    {
                    }
                    else
                    {
                        cellfound = true;
                    }
                    //sb.AppendLine(fact.GetFactKey());
                    foreach (var cells in cellslist)
                    {
                        foreach (var cell in cells)
                        {
                            sb.Append(cell + ", ");
                        }
                        sb.AppendLine();
                    }
                }
            }
            if (!cellfound)
            {
                if (log)
                {
                    c_sb.AppendLine("None of the Fact Groups can be found for " + parameter.RuleID + " - " + parameter.Name);
                }
            }
            if (c_sb.Length > 0)
            {
                Logger.WriteLine(c_sb.ToString());
            }
            return(sb.ToString());
        }