Exemplo n.º 1
0
        protected override AtomGroup NewAtomGroup(AtomGroup.LogicalOperator logicalOperator, object[] content)
        {
            if (equivalents.Count == 0)
            {
                return(new AtomGroup(logicalOperator, content));
            }

            // if we have equivalent atoms, try to translate content into equivalent sub-groups
            ArrayList enrichedContent = new ArrayList();

            foreach (object atomOrAtomGroup in content)
            {
                if (atomOrAtomGroup is Atom)
                {
                    Atom      atom            = (Atom)atomOrAtomGroup;
                    ArrayList atomEquivalents = RulesUtil.GetAll(equivalents, atom, new ArrayList());

                    if (atomEquivalents.Count > 1)
                    {
                        if (logicalOperator == AtomGroup.LogicalOperator.Or)
                        {
                            // in an OR block, negative atoms are surrounded by AND
                            if (atom.Negative)
                            {
                                enrichedContent.Add(new AtomGroup(AtomGroup.LogicalOperator.And, atomEquivalents.ToArray()));
                            }
                            else
                            {
                                enrichedContent.AddRange(atomEquivalents);
                            }
                        }
                        else
                        {
                            // in an AND block, positive atoms are surrounded by OR
                            if (atom.Negative)
                            {
                                enrichedContent.AddRange(atomEquivalents);
                            }
                            else
                            {
                                enrichedContent.Add(new AtomGroup(AtomGroup.LogicalOperator.Or, atomEquivalents.ToArray()));
                            }
                        }
                    }
                    else
                    {
                        // add atoms that have found no equivalents
                        enrichedContent.AddRange(atomEquivalents);
                    }
                }
                else
                {
                    // directly add atom groups
                    enrichedContent.Add(atomOrAtomGroup);
                }
            }

            return(new AtomGroup(logicalOperator, content, enrichedContent.ToArray()));
        }
Exemplo n.º 2
0
        public void FactResolve()
        {
            Fact fact2bis = new Fact("spending", new Individual("Peter Miller"), new Individual(35000));
            Fact fact2ter = RulesUtil.Resolve(fact2bis, atomF);

            Assert.IsFalse(fact2.Equals(fact2bis));
            Assert.IsTrue(fact2.Equals(fact2ter));
        }
Exemplo n.º 3
0
 /// <summary>
 /// If the passed atom matches one of the atoms in the Equivalent pair, returns the other atom, with its variable
 /// names modified to match the ones of the passed atom.
 /// </summary>
 /// <param name="atom">The atom that is potentially equivalent to one of the atoms of the Equivalent pair.</param>
 /// <returns>The atom equivalent to the passed one, or null if none of the atom pair is matching it.</returns>
 public Atom Get(Atom atom)
 {
     if (firstAtom.Matches(atom))
     {
         return(RulesUtil.TranslateVariables(atom, firstAtom, secondAtom));
     }
     return(secondAtom.Matches(atom) ? RulesUtil.TranslateVariables(atom, secondAtom, firstAtom) : null);
 }
Exemplo n.º 4
0
        public void VariableTranslation()
        {
            Atom template = new Atom("own", new Variable("person"), new Variable("object"));
            Atom source   = new Atom("own", new Variable("person"), new Variable("stuff"));
            Atom target   = new Atom("belongs", new Variable("person"), new Variable("stuff"));
            Atom expected = new Atom("belongs", new Variable("person"), new Variable("object"));

            Assert.AreEqual(expected, RulesUtil.TranslateVariables(template, source, target));
        }
Exemplo n.º 5
0
        public void AtomResolveFunctions()
        {
            Assert.AreEqual(atom3, RulesUtil.ResolveFunctions(atom3), "Atom without functions");
            Assert.IsTrue(atomF.HasFunction, "atom Hasfunction");
            Atom atomNoF = RulesUtil.ResolveFunctions(atomF);

            Assert.IsFalse(atomF.Equals(atomNoF), "Atom with functions");
            Assert.IsFalse(atomNoF.HasFunction, "atomNoF Hasfunction");
            Assert.IsTrue(fact3.Matches(atomNoF), "Fact and Atom match");
        }