Пример #1
0
        /// <summary>
        /// Adds a relationship to the list
        /// </summary>
        /// <param name="r"></param>
        public void Add(Relationship r)
        {
            RelationKey key = r.GetRelationKey();

            if (knowledge.ContainsKey(key))
            {
                bool bNew = true;
                //sadly will need to do a for-loop here to prevent duplicates
                foreach (Relationship oldrelationship in knowledge[key])
                {
                    if (oldrelationship.Relation == r.Relation &&
                        oldrelationship.A == r.A &&
                        oldrelationship.B == r.B
                        )
                    {
                        bNew = false;
                    }
                }

                // If the rules already added don't do it again.
                //if (knowledge[key].Contains(r)) return; DID NOT WORK (Probably because of the weights being different?)
                if (bNew == true)
                {
                    knowledge[key].Add(r);
                }
                return;
            }
            else
            {
                List <Relationship> relationList = new List <Relationship>();
                relationList.Add(r);
                knowledge.Add(key, relationList);
            }
        }
Пример #2
0
 /// <summary>
 /// used for "what is" types of questions?
 /// i.e., What is A? Returns array of matches
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public Relationship[] MatchRelations(RelationKey key)
 {
     try
     {
         return(knowledge[key].ToArray());
     }
     catch (Exception)
     {
         // key not found; return null
         return(null);
     }
 }
Пример #3
0
 // Returns an array of relationships.
 public Relationship[] this[RelationKey key]
 {
     get
     {
         try
         {
             return(knowledge[key].ToArray());
         }
         catch (Exception)
         {
             // key not found
             return(null);
         }
     }
 }
Пример #4
0
        // Returns an array of relationships.
        public Relationship[] this[RelationKey key]
        {
            get
            {
                try
                {

                        return knowledge[key].ToArray();

                }
                catch (Exception)
                {
                    // key not found
                    return null;
                }
            }
        }
Пример #5
0
        private void DeduceRule(Relationship relationship,
                                List <Relationship> potentialNewKnowledge)
        {
            Relationship aIs = relationship;

            //Get the B's
            List <Relationship> theBs = knowledge[aIs.GetRelationKey()];

            // june 12 2009 - was having trouble with foreach working so I did a normal for


            //foreach (Relationship secondRelation in theBs)
            for (int i = 0; i < theBs.Count; i++)
            {
                Relationship secondRelation = theBs[i];
                // A is B1, B2, B3 ...
                string b = secondRelation.B;

                RelationKey bRelation = new RelationKey(b, DEDUCED_IS);

                if (!knowledge.ContainsKey(bRelation))
                {
                    // do nothing if we don't have any 'deductions' to make
                    // removed the break beacuse it was messing with looping?
                    //break;
                }
                else
                {
                    List <Relationship> theCs = knowledge[bRelation];

                    foreach (Relationship bIs in theCs)
                    {
                        double newWeight   = (aIs.Weight + bIs.Weight) / 2;
                        int    newPriority = Math.Max(aIs.Priority, bIs.Priority);
                        //Create A is C, if it doesn't exist add it.
                        Relationship newRule
                            = new Relationship(aIs.A, DEDUCED_IS, bIs.B
                                               , "", newWeight, newPriority, false);


                        potentialNewKnowledge.Add(newRule);
                    }
                }
            }
        }
Пример #6
0
        private void DeduceRule(Relationship relationship,
            List<Relationship> potentialNewKnowledge)
        {
            Relationship aIs = relationship;

            //Get the B's
            List<Relationship> theBs = knowledge[aIs.GetRelationKey()];

            // june 12 2009 - was having trouble with foreach working so I did a normal for

            //foreach (Relationship secondRelation in theBs)
            for (int i = 0 ; i < theBs.Count; i++)
            {
                Relationship secondRelation = theBs[i];
                // A is B1, B2, B3 ...
                string b = secondRelation.B;

                RelationKey bRelation = new RelationKey(b, DEDUCED_IS);

                if (!knowledge.ContainsKey(bRelation))
                {
                    // do nothing if we don't have any 'deductions' to make
                    // removed the break beacuse it was messing with looping?
                    //break;
                }
                else
                {

                    List<Relationship> theCs = knowledge[bRelation];

                    foreach (Relationship bIs in theCs)
                    {
                        double newWeight = (aIs.Weight + bIs.Weight) /2 ;
                        int newPriority = Math.Max(aIs.Priority, bIs.Priority);
                        //Create A is C, if it doesn't exist add it.
                        Relationship newRule
                            = new Relationship(aIs.A, DEDUCED_IS, bIs.B
                            , "", newWeight, newPriority, false);

                        potentialNewKnowledge.Add(newRule);
                    }
                }
            }
        }
Пример #7
0
 /// <summary>
 /// used for "what is" types of questions?
 /// i.e., What is A? Returns array of matches
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public Relationship[] MatchRelations(RelationKey key)
 {
     try
     {
         return knowledge[key].ToArray();
     }
     catch (Exception)
     {
         // key not found; return null
         return null;
     }
 }