Beispiel #1
0
 public void AddNewFacts(IEnumerable <Fact> facts)
 {
     foreach (Fact fact in facts)
     {
         Known.Add(fact);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Adds a card to a specific location in the deck.
        /// </summary>
        /// <param name="element">The element to add.</param>
        /// <param name="side">What side of the deck the item goes to.</param>
        /// <param name="location">The location to add it to.</param>
        /// <returns>This same deck (for FLUID interface reasons).</returns>
        public IDeck <TElement> Add(TElement element, DeckSide side, Location location = Location.DrawPile)
        {
            Contract.Requires(Enum.IsDefined(typeof(DeckSide), side));
            Contract.Requires(Enum.IsDefined(typeof(Location), location));
            CheckAllowAdd();

            switch (location)
            {
            case Location.DrawPile:
                ((Internal.IDrawPileInternal <TElement>)_drawPile).Add(element, side);
                break;

            case Location.DiscardPile:
                ((Internal.IDiscardPileInternal <TElement>)_discards).Add(element, side);
                break;

            case Location.Table:
                if (side != DeckSide.Default)
                {
                    throw new InvalidOperationException("Cannot add to the table on a specific side.");
                }
                else
                {
                    ((Internal.ITableInternal <TElement>)_table).Add(element);
                }
                break;

            case Location.Tableau:
                if (side != DeckSide.Default)
                {
                    throw new InvalidOperationException("Cannot add to the tableau on a specific side.");
                }
                else
                {
                    ((Internal.ITableauInternal <TElement>)_tableau).Add(element);
                }
                break;

            case Location.Hand:
                throw new InvalidOperationException("Cannot add directly to a hand.");

            default:
                throw new NotImplementedException($"Don't know about the {location} location.");
            }
            Known.Add(element);

            Events.Added(element, side, location);
            return(this);
        }
        public override Vendr Get(string id)
        {
            Known.TryGetValue(id, out var returned);

            if (returned == null)
            {
                var result = new GetVendorRequest(id).Execute(Api.Client);
                if (result.Status == 200)
                {
                    returned = new Vendr();
                    returned.UpdateFromRequest(Utils.DictionaryOfJsonFields(result.Data.Vendor));
                    Known.Add(returned.Id, returned);
                }
            }

            return(returned);
        }
Beispiel #4
0
        public bool CompleteInfer(List <Fact> inputs, List <Request> outputs)
        {
            // TODO: replace this global variable
            InferenceRule Rule = null;

            foreach (Fact input in inputs)
            {
                Known.Add(input);
            }

            bool       completeFlag = false; // duyet het luat chua
            List <int> usedIndexes  = new List <int>();

            // luu lai danh sach cac luat da di qua va duoc ap dung de tranh lap lai

            while (!completeFlag)        // neu chuat duyet het luat
            {
                bool usableFlag = false; // luat co ap dung dung khong
                int  index      = 0;     // vi tri cua luat hien tai

                // duyet tung gia thiet trong tap luat
                foreach (InferenceRule rule in RuleBase.InferenceRules)
                {
                    IEnumerable <Fact> hypotheses = rule.Hypotheses;

                    // Tim cach luu lai nhung luat ma da duyet co trong Know nhung chua co result

                    usableFlag = CheckHypothesesInKnown(hypotheses, Known); // Kiem tra luat co ap dung duoc khong
                    if (usableFlag)
                    {
                        // Kiem tra xem luat nay da ap dung chua va dua su kien vao Known chua de tranh lap lai
                        if (!usedIndexes.Contains(index))
                        {
                            // Neu chua ap dung
                            IEnumerable <Fact> conclusions = rule.Conclusions;
                            if (CheckRequestsInConclusions(outputs, conclusions))
                            // Kiem tra xem su kien ket luan co phai la ket qua khong
                            {
                                foreach (CrispFact fact in hypotheses)
                                {
                                    Console.Write(fact.ToString() + "\t");
                                }
                                Console.Write(" -> ");
                                foreach (CrispFact fact in conclusions)
                                {
                                    Console.Write(fact.ToString() + "\t");
                                }
                                Console.WriteLine();
                            }
                            else // Su kien ket luan khong la ket qua
                            {
                                foreach (Fact conclusion in conclusions)
                                {
                                    // Them tat ca su kien ket luan vao Known
                                    Known.Add(conclusion);
                                }
                            }

                            usedIndexes.Add(index); // luat o vi tri index da duyet va cap nhat vao Known
                            break;
                        }
                    }

                    index++;
                }

                // Neu duyet het luat ma chua tim duoc ket luan
                if (index == RuleBase.InferenceRules.Count)
                {
                    completeFlag = true;
                }


                foreach (RelationRule rule in RuleBase.RelationRules)
                {
                    for (int i = 0; i < Known.Count; ++i)
                    {
                        Fact fact = Known[i];

                        IndividualCrispFact individualFact = fact as IndividualCrispFact;
                        if (individualFact == null)
                        {
                            continue;
                        }

                        if (individualFact.Class == rule.Domain)
                        {
                            Tuple <IEnumerable <string>, IEnumerable <string> > result =
                                _knowledgeManager.RunQuery(rule.Query, "autogen0:" + individualFact.Individual, rule.QueryRange);
                            foreach (string rangeIndividual in result.Item2)
                            {
                                string individual = rangeIndividual.Replace("autogen0:", "");
                                Console.WriteLine(individual); // workar5ound

                                Fact rangeFact = new IndividualCrispFact
                                {
                                    Class      = rule.Range,
                                    Individual = individual
                                };

                                if (!Known.Contains(rangeFact))
                                {
                                    Known.Add(rangeFact);
                                }
                            }
                        }
                        else if (individualFact.Class == rule.Range)
                        {
                            Tuple <IEnumerable <string>, IEnumerable <string> > result =
                                _knowledgeManager.RunQuery(rule.Query, rule.QueryDomain, "autogen0:" + individualFact.Individual);
                            foreach (string domainIndividual in result.Item1)
                            {
                                string individual = domainIndividual.Replace("autogen0:", "");
                                Console.WriteLine(individual);

                                Fact domainFact = new IndividualCrispFact
                                {
                                    Class      = rule.Range,
                                    Individual = individual
                                };

                                if (!Known.Contains(domainFact))
                                {
                                    Known.Add(domainFact);
                                }
                            }
                        }
                    }
                }
            }

            // TODO:

            return(false);
        }