public void NewSessionAndUpdatePhase()
        {
            using (var unitOfWork = UnitOfWorkFactory.Create()) {
                var sessionOfExperts = new SessionOfExperts("BaseNotion");
                GetRepository <SessionOfExperts>().AddOrUpdate(sessionOfExperts);

                unitOfWork.Commit();
            }

            using (var unitOfWork = UnitOfWorkFactory.Create()) {
                var session = LinqProvider.Query <SessionOfExperts>().Single();
                session.NextPhaseOrFinish();
                GetRepository <SessionOfExperts>().AddOrUpdate(session);

                unitOfWork.Commit();
            }

            using (UnitOfWorkFactory.Create()) {
                var session = LinqProvider.Query <SessionOfExperts>().Single();

                session.CurrentPhase.Should().Be(SessionPhase.SpecifyingAssociationsTypes);
                session.StartTime.Should().BeNow();
                session.BaseNotion.Should().Be("BaseNotion");
            }
        }
Example #2
0
        public void ReplaceAssociation_NotEmptyCollection()
        {
            using (var unitOfWork = UnitOfWorkFactory.Create()) {
                var sessionOfExperts = new SessionOfExperts("baseNotion");
                GetRepository <SessionOfExperts>().AddOrUpdate(sessionOfExperts);

                var expert = new Expert("expertName", sessionOfExperts);
                expert.ReplaceAllAssociations(new[] { "notion1", "notion2" });
                GetRepository <Expert>().AddOrUpdate(expert);

                unitOfWork.Commit();
            }

            using (var unitOfWork = UnitOfWorkFactory.Create()) {
                var expert = LinqProvider.Query <Expert>().Single();
                expert.ReplaceAllAssociations(new[] { "notion3", "notion4" });
                GetRepository <Expert>().AddOrUpdate(expert);

                unitOfWork.Commit();
            }

            using (UnitOfWorkFactory.Create()) {
                var expert = LinqProvider.Query <Expert>().Single();

                expert.Associations.Should().BeEquivalentTo(
                    new[]
                {
                    new { Expert = expert, Notion = "notion3" },
                    new { Expert = expert, Notion = "notion4" }
                },
                    opt => opt.ExcludingMissingMembers());
            }
        }
Example #3
0
        public virtual void CreateSemanticNetworkFromNodeCandidates(
            [NotNull] IReadOnlyCollection <NodeCandidate> nodeCandidates,
            [NotNull] SessionOfExperts sessionOfExperts)
        {
            if (nodeCandidates == null)
            {
                throw new ArgumentNullException(nameof(nodeCandidates));
            }
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }

            var generalVergeType = _relationTypeRepository.GetGeneralType();
            var generalNodeType  = _notionTypeRepository.GetGeneralType();

            var root = GetOrCreateNode(sessionOfExperts.BaseNotion, generalNodeType, sessionOfExperts);

            _nodeRepository.AddOrUpdate(root);

            foreach (var nodeCandidate in nodeCandidates.Where(x => x.IsSaveAsNode))
            {
                var node = GetOrCreateNode(
                    nodeCandidate.Notion,
                    _notionTypeRepository.GetById(nodeCandidate.TypeId),
                    sessionOfExperts);
                _nodeRepository.AddOrUpdate(node);

                var straightVerge = UpdateOrCreateVerge(root, node, generalVergeType, nodeCandidate.ExpertPercent, sessionOfExperts);
                var reverseVerge  = UpdateOrCreateVerge(node, root, generalVergeType, nodeCandidate.ExpertPercent, sessionOfExperts);

                _vergeRepository.AddOrUpdate(straightVerge);
                _vergeRepository.AddOrUpdate(reverseVerge);
            }
        }
Example #4
0
        /// <summary>
        /// Generate new relations for each expert of the session.
        /// </summary>
        /// <param name="sessionOfExperts">Session of experts.</param>
        /// <param name="nodesOfSession">Nodes of the session.</param>
        public virtual void CreateRelations(SessionOfExperts sessionOfExperts, IReadOnlyCollection <Node> nodesOfSession)
        {
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }
            if (nodesOfSession == null)
            {
                throw new ArgumentNullException(nameof(nodesOfSession));
            }

            var experts =
                _expertRepository.GetExpertsBySession(
                    new GetExpertsBySessionSpecification(sessionOfExperts, ExpertFetch.Relations));
            var nodes = nodesOfSession.Where(n => n.Notion != sessionOfExperts.BaseNotion).ToArray();

            if (experts != null)
            {
                foreach (var expert in experts)
                {
                    expert.GenerateRelations(nodes);
                    _expertRepository.AddOrUpdate(expert);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Sets types for associations of expert.
        /// </summary>
        /// <param name="associations">List of plain objects, that contains id of association and its type.</param>
        /// <param name="expertName">Expert name.</param>
        /// <param name="sessionOfExperts">Session of experts.</param>
        public virtual void AssociationsTypes(
            [NotNull] IReadOnlyCollection <AssociationDto> associations,
            [NotNull] string expertName,
            [NotNull] SessionOfExperts sessionOfExperts)
        {
            if (associations == null)
            {
                throw new ArgumentNullException(nameof(associations));
            }
            if (expertName == null)
            {
                throw new ArgumentNullException(nameof(expertName));
            }
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }

            var expert = GetExpertByNameAndSession(expertName, sessionOfExperts, ExpertFetch.Associations);

            IfExpertDoesNotExistThrow(expert, expertName, sessionOfExperts);

            foreach (var association in associations)
            {
                var typeNode = _notionTypeRepository.GetById(association.TypeId);

                // ReSharper disable once PossibleNullReferenceException
                expert.SetTypeForAssociation(association.Id, typeNode, association.OfferType);
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            _expertRepository.AddOrUpdate(expert);
        }
Example #6
0
        public virtual void SaveRelationsAsVergesOfSemanticNetwork(
            [NotNull] IReadOnlyCollection <GroupedRelation> groupedRelations,
            [NotNull] SessionOfExperts session)
        {
            if (groupedRelations == null)
            {
                throw new ArgumentNullException(nameof(groupedRelations));
            }
            if (session == null)
            {
                throw new ArgumentNullException(nameof(session));
            }

            foreach (var groupedRelation in groupedRelations)
            {
                var verge = UpdateOrCreateVerge(
                    groupedRelation.Source,
                    groupedRelation.Destination,
                    groupedRelation.Type,
                    groupedRelation.Percent,
                    session);

                _vergeRepository.AddOrUpdate(verge);
            }
        }
Example #7
0
        public void UpdateLastCompletedPhase()
        {
            using (var unitOfWork = UnitOfWorkFactory.Create()) {
                var sessionOfExperts = new SessionOfExperts("baseNotion");
                GetRepository <SessionOfExperts>().AddOrUpdate(sessionOfExperts);

                var expert = new Expert("expertName", sessionOfExperts);
                GetRepository <Expert>().AddOrUpdate(expert);

                unitOfWork.Commit();
            }

            using (var unitOfWork = UnitOfWorkFactory.Create()) {
                var expert = LinqProvider.Query <Expert>().Single();
                expert.LastCompletedPhase = SessionPhase.SpecifyingAssociationsTypes;

                unitOfWork.Commit();
            }

            using (UnitOfWorkFactory.Create()) {
                var expert = LinqProvider.Query <Expert>().Single();

                expert.LastCompletedPhase.Should().Be(SessionPhase.SpecifyingAssociationsTypes);
            }
        }
Example #8
0
        /// <summary>
        /// Replaces associations of the expert with new.
        /// </summary>
        /// <param name="notions">Notions of associations.</param>
        /// <param name="expertName">Expert name.</param>
        /// <param name="sessionOfExperts">Session of experts.</param>
        public virtual void Associations(
            [NotNull] IReadOnlyCollection <string> notions,
            [NotNull] string expertName,
            [NotNull] SessionOfExperts sessionOfExperts)
        {
            if (notions == null)
            {
                throw new ArgumentNullException(nameof(notions));
            }
            if (expertName == null)
            {
                throw new ArgumentNullException(nameof(expertName));
            }
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }

            var expert = GetExpertByNameAndSession(expertName, sessionOfExperts, ExpertFetch.Associations);

            IfExpertDoesNotExistThrow(expert, expertName, sessionOfExperts);

            // ReSharper disable once PossibleNullReferenceException
            expert.ReplaceAllAssociations(notions);

            _expertRepository.AddOrUpdate(expert);
        }
Example #9
0
        public void TestAddSession()
        {
            using (var unitOfWork = UnitOfWorkFactory.Create()) {
                var notionType = new NotionType("type");
                GetRepository <NotionType>().AddOrUpdate(notionType);

                var node = new Node("notion", notionType);
                GetRepository <Node>().AddOrUpdate(node);

                unitOfWork.Commit();
            }

            using (var unitOfWork = UnitOfWorkFactory.Create()) {
                var session = new SessionOfExperts("baseNotion");
                GetRepository <SessionOfExperts>().AddOrUpdate(session);

                var node = LinqProvider.Query <Node>().Single();
                node.AddSessionOfExperts(session);

                unitOfWork.Commit();
            }

            using (UnitOfWorkFactory.Create()) {
                var node = LinqProvider.Query <Node>().Single();

                node.SessionsOfExperts.Count.Should().Be(1);
            }
        }
        public void Ctor_StartedIsNow_CurrenSessionIsMakingAssociations()
        {
            var session = new SessionOfExperts("baseNotion");

            session.CurrentPhase.Should().Be(SessionPhase.MakingAssociations);
            session.StartTime.Should().BeNow();
            session.BaseNotion.Should().Be("baseNotion");
        }
Example #11
0
        /// <summary>
        /// Sets types for relations of expert.
        /// </summary>
        /// <param name="relationTuple">Plain object, that contains id of relation and flags of existence any relations.</param>
        /// <param name="expertName">Expert name.</param>
        /// <param name="sessionOfExperts">Session of experts.</param>
        public virtual void RelationTypes(
            [NotNull] RelationTupleDto relationTuple,
            [NotNull] string expertName,
            [NotNull] SessionOfExperts sessionOfExperts)
        {
            if (relationTuple == null)
            {
                throw new ArgumentNullException(nameof(relationTuple));
            }
            if (expertName == null)
            {
                throw new ArgumentNullException(nameof(expertName));
            }
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }

            var expert = GetExpertByNameAndSession(expertName, sessionOfExperts, ExpertFetch.Relations);

            IfExpertDoesNotExistThrow(expert, expertName, sessionOfExperts);

            var straightRelationTypes = new List <RelationType>();
            var reverseRelationTypes  = new List <RelationType>();

            if (relationTuple.DoesRelationExist)
            {
                var generalType  = _relationTypeRepository.GetGeneralType();
                var taxonomyType = _relationTypeRepository.GetTaxonomyType();
                var meronomyType = _relationTypeRepository.GetMeronomyType();

                straightRelationTypes.Add(generalType);
                reverseRelationTypes.Add(generalType);

                if (relationTuple.IsStraightTaxonym)
                {
                    straightRelationTypes.Add(taxonomyType);
                }
                if (relationTuple.IsStraightMeronym)
                {
                    straightRelationTypes.Add(meronomyType);
                }
                if (relationTuple.IsReverseTaxonym)
                {
                    reverseRelationTypes.Add(taxonomyType);
                }
                if (relationTuple.IsReverseMeronym)
                {
                    reverseRelationTypes.Add(meronomyType);
                }
            }

            // ReSharper disable once PossibleNullReferenceException
            expert.SetTypesForRelation(relationTuple.StraightRelationId, straightRelationTypes, null);
            expert.SetTypesForRelation(relationTuple.ReverseRelationId, reverseRelationTypes, null);
            _expertRepository.AddOrUpdate(expert);
        }
Example #12
0
        public void Equals_DifferentTypes_ReturnFalsse()
        {
            var node            = new Node("notion", Substitute.For <NotionType>());
            var sessionOfExpert = new SessionOfExperts("notion");

            var result = node.Equals(sessionOfExpert);

            result.Should().BeFalse();
        }
        public GetExpertCountSpecification([NotNull] SessionOfExperts sessionOfExperts)
        {
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }

            SessionOfExperts = sessionOfExperts;
        }
Example #14
0
        public IReadOnlyCollection <Node> GetBySession([NotNull] SessionOfExperts session)
        {
            if (session == null)
            {
                throw new ArgumentNullException(nameof(session));
            }

            return(_linqProvider.Query <Node>().Where(x => x.SessionsOfExperts.Contains(session)).ToList());
        }
Example #15
0
        public SemanticNetworkReadModel GetSemanticNetworkBySession([NotNull] SessionOfExperts session)
        {
            if (session == null)
            {
                throw new ArgumentNullException(nameof(session));
            }

            Func <VergeOfSession, VergeReadModel> vergeProjection = sessionVerge =>
                                                                    new VergeReadModel(
                sessionVerge.Verge.SourceNode.Notion,
                sessionVerge.Verge.SourceNode.Type.Name,
                sessionVerge.Verge.DestinationNode.Notion,
                sessionVerge.Verge.DestinationNode.Type.Name,
                sessionVerge.Verge.Type.Name,
                sessionVerge.Weight);

            _linqProvider.Query <Verge>()
            .Where(
                x => x.SourceNode.SessionsOfExperts.Contains(session) ||
                x.SourceNode.SessionsOfExperts.Contains(session))
            .FetchMany(x => x.SessionWeightSlices).ThenFetch(x => x.SessionOfExperts)
            .Fetch(x => x.SourceNode)
            .Fetch(x => x.DestinationNode)
            .Fetch(x => x.Type)
            .ToFuture();

            _linqProvider.Query <Node>()
            .Where(x => x.SessionsOfExperts.Contains(session))
            .Fetch(x => x.Type)
            .FetchMany(x => x.IngoingVerges)
            .ToFuture();

            var nodes = _linqProvider.Query <Node>()
                        .Where(x => x.SessionsOfExperts.Contains(session))
                        .FetchMany(x => x.OutgoingVerges)
                        .ToFuture()
                        .ToList();

            var concepts = nodes
                           .Select(
                x => new ConceptReadModel(
                    x.Notion,
                    x.Type.Name,
                    x.IngoingVerges
                    .SelectMany(v => v.SessionWeightSlices)
                    .Where(sv => sv.SessionOfExperts == session)
                    .Select(vergeProjection)
                    .ToList(),
                    x.OutgoingVerges
                    .SelectMany(v => v.SessionWeightSlices)
                    .Where(sv => sv.SessionOfExperts == session)
                    .Select(vergeProjection).ToList()))
                           .ToList();

            return(new SemanticNetworkReadModel(concepts));
        }
Example #16
0
 public ExpertWithCollections(
     string name,
     SessionOfExperts session,
     IEnumerable <Association> associations,
     IEnumerable <Relation> relations)
     : base(name, session)
 {
     _associations.AddRange(associations);
     _relations.AddRange(relations);
 }
Example #17
0
        public virtual void FinishCurrentPhase([NotNull] string expertName, [NotNull] SessionOfExperts sessionOfExperts)
        {
            var expert = GetExpertByNameAndSession(expertName, sessionOfExperts, ExpertFetch.None);

            IfExpertDoesNotExistThrow(expert, expertName, sessionOfExperts);

            // ReSharper disable once PossibleNullReferenceException
            expert.FinishCurrentPhase();
            _expertRepository.AddOrUpdate(expert);
        }
Example #18
0
        private Expert GetExpertByNameAndSession(
            [NotNull] string expertName,
            [NotNull] SessionOfExperts sessionOfExperts,
            ExpertFetch expertFetch)
        {
            var expert = _expertRepository.GetExpertByNameAndSession(
                new GetExpertByNameAndSessionSpecification(expertName, sessionOfExperts, expertFetch));

            return(expert);
        }
Example #19
0
        public virtual IReadOnlyCollection <NodeCandidate> GetNodeCandidatesBySession(
            [NotNull] SessionOfExperts sessionOfExperts)
        {
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }

            return(_associationRepository.GetNodeCandidatesBySession(sessionOfExperts));
        }
Example #20
0
 private void IfExpertDoesNotExistThrow(
     [CanBeNull] Expert expert,
     [NotNull] string name,
     [NotNull] SessionOfExperts sessionOfExperts)
 {
     if (expert == null)
     {
         throw new InvalidOperationException($"Expert with name {name} doesn't join session {sessionOfExperts}");
     }
 }
Example #21
0
        private Node GetOrCreateNode(
            [NotNull] string notion,
            [NotNull] NotionType type,
            [NotNull] SessionOfExperts sessionOfExperts)
        {
            var node = _nodeRepository.GetByNotionAndType(notion, type) ?? new Node(notion, type);

            node.AddSessionOfExperts(sessionOfExperts);

            return(node);
        }
Example #22
0
        private Expert FromExpertRepositoryReturnFakeExpert(string expertName, SessionOfExperts session)
        {
            var expert = Substitute.For <Expert>();

            FakeExpertRepository.GetExpertByNameAndSession(
                Arg.Is <GetExpertByNameAndSessionSpecification>(
                    x => x.ExpertName == expertName && x.SessionOfExperts == session))
            .Returns(expert);

            return(expert);
        }
        /// <summary>
        /// Starts new current session.
        /// </summary>
        /// <param name="baseNotion">Notion for that experts suggests associations.</param>
        public void StartNewSession([NotNull] string baseNotion)
        {
            if (baseNotion == null)
            {
                throw new ArgumentNullException(nameof(baseNotion));
            }
            IfCurrentSessionExistsThrow();

            var session = new SessionOfExperts(baseNotion);

            _sessionOfExpertsRepository.AddOrUpdate(session);
        }
        public void NextPhase_SessionExistsAndIsInSelectingNodePhase_CreateRelationBlanks()
        {
            var serviceUnderTest = CreateServiceUnderTest();
            var session          = new SessionOfExperts("baseNotion");

            session.SetProperty(nameof(session.CurrentPhase), SessionPhase.SelectingNodes);
            _fakeSessionOfExpertsRepository.GetCurrent().Returns(session);

            serviceUnderTest.NextPhase();

            _fakeExpertService.Received(1).CreateRelations(
                Arg.Is(serviceUnderTest.CurrentSession),
                Arg.Any <IReadOnlyCollection <Node> >());
        }
Example #25
0
        private Verge UpdateOrCreateVerge(
            [NotNull] Node sourceNode,
            [NotNull] Node destinationNode,
            [NotNull] RelationType type,
            double percent,
            [NotNull] SessionOfExperts sessionOfExperts)
        {
            var weight = PercentToWeight(percent);
            var verge  = _vergeRepository.GetByNodesAndTypes(sourceNode, destinationNode, type) ??
                         new Verge(sourceNode, destinationNode, type, weight);

            verge.UpdateWeightFromSession(weight, sessionOfExperts);

            return(verge);
        }
Example #26
0
        /// <summary>
        /// Checks that expert has joined the session.
        /// </summary>
        /// <param name="expertName">Expert name.</param>
        /// <param name="sessionOfExperts">Session of Experts.</param>
        /// <remarks>If current session does no exist returns false.</remarks>
        public virtual void JoinSession([NotNull] string expertName, [NotNull] SessionOfExperts sessionOfExperts)
        {
            if (expertName == null)
            {
                throw new ArgumentNullException(nameof(expertName));
            }
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }

            var expert = new Expert(expertName, sessionOfExperts);

            _expertRepository.AddOrUpdate(expert);
        }
Example #27
0
        private void Seed()
        {
            using (var unitOfWork = UnitOfWorkFactory.Create())
            {
                _session1 = new SessionOfExperts("baseNotion");
                _session2 = new SessionOfExperts("otherNotion");

                var sessionRepo = GetRepository <SessionOfExperts>();
                sessionRepo.AddOrUpdate(_session1);
                sessionRepo.AddOrUpdate(_session2);

                _expert1 = new Expert("name1", _session1);
                _expert2 = new Expert("name2", _session2);
                _expert3 = new Expert("name3", _session1);
                var expertRepo = GetRepository <Expert>();
                expertRepo.AddOrUpdate(_expert1);
                expertRepo.AddOrUpdate(_expert2);
                expertRepo.AddOrUpdate(_expert3);

                var notionType = new NotionType("type");
                GetRepository <NotionType>().AddOrUpdate(notionType);

                var nodeRepo = GetRepository <Node>();
                var node1    = new Node("notion1", notionType);
                node1.AddSessionOfExperts(_session1);
                nodeRepo.AddOrUpdate(node1);

                var node2 = new Node("notion2", notionType);
                node2.AddSessionOfExperts(_session1);
                nodeRepo.AddOrUpdate(node2);

                var relationType = new RelationType("type");
                GetRepository <RelationType>().AddOrUpdate(relationType);

                var vergeRepo = GetRepository <Verge>();
                var verge1    = new Verge(node1, node2, relationType, 20);
                verge1.UpdateWeightFromSession(20, _session1);
                vergeRepo.AddOrUpdate(verge1);

                var verge2 = new Verge(node2, node1, relationType, 20);
                verge2.UpdateWeightFromSession(20, _session1);
                vergeRepo.AddOrUpdate(verge2);

                unitOfWork.Commit();
            }
        }
Example #28
0
        /// <summary>
        /// Checks that expert has joined the session.
        /// </summary>
        /// <param name="expertName">Expert name.</param>
        /// <param name="sessionOfExperts">Session of experts.</param>
        /// <returns>If expert join the current session returns true else returns false.</returns>
        /// <remarks>If current session does no exist returns false.</remarks>
        public virtual bool DoesExpertJoinSession(
            [NotNull] string expertName,
            [NotNull] SessionOfExperts sessionOfExperts)
        {
            if (expertName == null)
            {
                throw new ArgumentNullException(nameof(expertName));
            }
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }

            var expert = GetExpertByNameAndSession(expertName, sessionOfExperts, ExpertFetch.None);

            return(expert != null);
        }
Example #29
0
        /// <summary>
        /// Check that expert completed current phase.
        /// </summary>
        /// <param name="expertName">Expert name.</param>
        /// <param name="sessionOfExperts">Session of experts.</param>
        /// <returns>if expert completed current phase returns true else returns false.</returns>
        public virtual bool DoesExpertCompleteCurrentPhase(
            [NotNull] string expertName,
            [NotNull] SessionOfExperts sessionOfExperts)
        {
            if (expertName == null)
            {
                throw new ArgumentNullException(nameof(expertName));
            }
            if (sessionOfExperts == null)
            {
                throw new ArgumentNullException(nameof(sessionOfExperts));
            }

            var expert = GetExpertByNameAndSession(expertName, sessionOfExperts, ExpertFetch.None);

            return(expert != null && expert.IsPhaseCompleted);
        }
Example #30
0
        public IReadOnlyCollection <NodeCandidate> GetNodeCandidatesBySession(SessionOfExperts session)
        {
            var totalExpectCount = new GetExpertCountQuery(_linqProvider).Execute(
                new GetExpertCountSpecification(session));

            return(_linqProvider.Query <Association>()
                   .Where(x => x.Expert.SessionOfExperts == session)
                   .GroupBy(x => new { x.Notion, TypeName = x.Type.Name, TypeId = x.Type.Id })
                   .Select(
                       gr => new NodeCandidate
            {
                Notion = gr.Key.Notion,
                TypeId = gr.Key.TypeId,
                TypeName = gr.Key.TypeName,
                ExpertCount = gr.Count(),
                TotalExpert = totalExpectCount
            })
                   .ToList());
        }