Esempio n. 1
0
        public IRelationship CreateRelationship(IEnumerable <Guid> participants, IEnumerable <string> tags)
        {
            long hash = BaseRelationship.GenerateHash(participants);

            if (this.m_Relationships.ContainsKey(hash))
            {
                List <IRelationship> relationships = this.m_Relationships[hash];
                int           matching             = 0;
                IRelationship returnRelationship   = null;
                foreach (IRelationship relationship in relationships)
                {
                    int matches = relationship.Tags.Intersect(tags, StringComparer.OrdinalIgnoreCase).Count();
                    if (matches <= matching)
                    {
                        continue;
                    }
                    matching           = matches;
                    returnRelationship = relationship;
                }

                if (returnRelationship is null == false)
                {
                    return(returnRelationship);
                }
            }

            if (this.m_RelationshipTypes.Any(t => tags.Intersect(t.Value.Tags, StringComparer.OrdinalIgnoreCase).Any()))
            {
                int           matching = 0;
                IRelationship found    = null;
                foreach (IRelationship relationship in this.m_RelationshipTypes.Values)
                {
                    int matches = relationship.Tags.Intersect(tags, StringComparer.OrdinalIgnoreCase).Count();
                    if (matches <= matching)
                    {
                        continue;
                    }
                    matching = matches;
                    found    = relationship;
                }

                if (found is null)
                {
                    return(null);
                }

                IRelationship newRelationship = found.Create(participants);

                //newRelationship.ModifyValueOfAllParticipants(0);

                this.m_Relationships.Add(newRelationship.GenerateHashFromInstance(), newRelationship);
                return(newRelationship);
            }

            throw new InvalidOperationException("Relationship type " + tags.Print() + " not found.");
        }
Esempio n. 2
0
        public IEnumerable <IRelationship> Get(
            IEnumerable <Guid> participants,
            IEnumerable <string> tags = null,
            bool createNewIfNone      = false)
        {
            long hash = BaseRelationship.GenerateHash(participants);

            List <IRelationship> relationships = new List <IRelationship>();

            foreach (Tuple <long, IRelationship> pair in this.m_Relationships)
            {
                if (pair.Item1 != hash)
                {
                    continue;
                }

                if (tags.IsNullOrEmpty() == false && tags.Intersect(pair.Item2.Tags).Any())
                {
                    relationships.Add(pair.Item2);
                }
                else if (tags.IsNullOrEmpty())
                {
                    relationships.Add(pair.Item2);
                }
            }

            if (relationships.Count == 0 && createNewIfNone)
            {
                List <string> newTags = new List <string>(tags);
                if (newTags.IsNullOrEmpty())
                {
                    newTags.Add("friendship");
                }
                relationships.Add(this.CreateRelationship(participants, newTags));
            }

            return(relationships.ToArray());
        }