Esempio n. 1
0
        public async Task <string> PostCheckUser(MessageAttachments model)
        {
            foreach (var user in model.Users)
            {
                // Do what you need to with the bytes from the uploaded attachments
                var bytes = user.GetByteArray();
                AnalyzedFacialResults faceResponse = await FaceAnalyzer.DetectFaceAsync(bytes);

                if (faceResponse == null)
                {
                    return("Error");
                }

                if (faceResponse.Success)
                {
                    var identifiedUser = context.IdentifiedUsers.Where(x => x.Id == faceResponse.FaceId);

                    if (identifiedUser.Count() == 0)
                    {
                        return("User not found");
                    }
                    else if (identifiedUser.Count() >= 1)
                    {
                        return(identifiedUser.First().Email);
                    }
                }

                return(faceResponse.Error);
            }

            return("Something went wrong");
        }
Esempio n. 2
0
        public void Reply(IAgentId receiverId, MessageAction action, byte content, MessageAttachments parameter,
                          CommunicationMediums communicationMedium)
        {
            var message = new Message(AgentId, receiverId, action, content, parameter, communicationMedium);

            Reply(message);
        }
Esempio n. 3
0
 public void AddAttachments(params MessageAttachment[] messageAttachments)
 {
     foreach (var attachment in messageAttachments)
     {
         if (MessageAttachments.All(x => x.ExternalId != attachment.ExternalId))
         {
             MessageAttachments.Add(attachment);
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        ///     Agent ask an expert for the information
        /// </summary>
        public override void GetNewTasks()
        {
            var attachments = new MessageAttachments
            {
                KnowledgeId  = Knowledge.EntityId,
                KnowledgeBit = Knowledge.GetRandomBitIndex()
            };

            Send(((ExampleEnvironment)Environment).ExpertAgent.AgentId, MessageAction.Ask, SymuYellowPages.Knowledge,
                 attachments, CommunicationMediums.Email);
        }
Esempio n. 5
0
        /// <summary>
        ///     Ask Help to teammates about it belief
        ///     when task is blocked because of a lack of belief
        /// </summary>
        /// <param name="task"></param>
        /// <param name="blocker"></param>
        public virtual void TryRecoverBlockerIncompleteBelief(SymuTask task, Blocker blocker)
        {
            if (task is null)
            {
                throw new ArgumentNullException(nameof(task));
            }

            if (blocker is null)
            {
                throw new ArgumentNullException(nameof(blocker));
            }

            // Check if he has the right to receive knowledge from others agents
            if (!Cognitive.MessageContent.CanReceiveBeliefs)
            {
                // If agent has no other strategy
                // Blocker must be unblocked in a way or another
                RecoverBlockerIncompleteBeliefByGuessing(task, blocker);
                return;
            }

            var murphy       = Environment.MainOrganization.Murphies.IncompleteBelief;
            var knowledgeId  = (IAgentId)blocker.Parameter;
            var knowledgeBit = (byte)blocker.Parameter2;

            var teammates     = GetAgentIdsForInteractions(InteractionStrategy.Beliefs).ToList();
            var askInternally = murphy.AskInternally(Schedule.Step,
                                                     blocker.InitialStep);

            if (teammates.Any() && askInternally)
            {
                var attachments = new MessageAttachments();
                attachments.Add(blocker);
                attachments.Add(task);
                attachments.KnowledgeId  = knowledgeId;
                attachments.KnowledgeBit = knowledgeBit;
                var messageType =
                    murphy.AskOnWhichChannel(Cognitive.InteractionCharacteristics.PreferredCommunicationMediums);
                ImpactOfTheCommunicationMediumOnTimeSpent(messageType, true, task.KeyActivity);
                SendToMany(teammates, MessageAction.Ask, SymuYellowPages.Help, attachments, messageType);
            }
            else
            {
                if (murphy.ShouldGuess(blocker.NumberOfTries))
                {
                    // Blocker must be unblocked in a way or another
                    RecoverBlockerIncompleteBeliefByGuessing(task, blocker);
                }
                else
                {
                    TryRecoverBlockerIncompleteBeliefExternally(task, blocker);
                }
            }
        }
Esempio n. 6
0
        public void SendToMany(IEnumerable <IAgentId> receivers, MessageAction action, byte content,
                               MessageAttachments parameter, CommunicationMediums communicationMedium)
        {
            if (receivers is null)
            {
                return;
            }

            foreach (var a in receivers.Shuffle())
            {
                Send(a, action, content, parameter, communicationMedium);
            }
        }
Esempio n. 7
0
        public void AddSubscribeTest()
        {
            var attachments = new MessageAttachments();

            attachments.Add((byte)1);
            attachments.Add((byte)2);
            var message = new Message(_agent.AgentId, _agent.AgentId, MessageAction.Add, SymuYellowPages.Subscribe,
                                      attachments);

            _agent.AddSubscribe(message);
            Assert.AreEqual(1, _agent.MessageProcessor.Subscriptions.SubscribersCount(1));
            Assert.AreEqual(1, _agent.MessageProcessor.Subscriptions.SubscribersCount(2));
            Assert.AreEqual(0, _agent.MessageProcessor.Subscriptions.SubscribersCount(0));
        }
Esempio n. 8
0
        public override void TryRecoverBlockerIncompleteKnowledgeExternally(SymuTask task, Blocker blocker,
                                                                            IAgentId knowledgeId,
                                                                            byte knowledgeBit)
        {
            if (blocker == null)
            {
                throw new ArgumentNullException(nameof(blocker));
            }

            var attachments = new MessageAttachments();

            attachments.Add(blocker);
            attachments.Add(task);
            attachments.KnowledgeId  = knowledgeId;
            attachments.KnowledgeBit = knowledgeBit;
            Send(Internet.AgentId, MessageAction.Ask, SymuYellowPages.Help, attachments,
                 CommunicationMediums.ViaAPlatform);
        }
Esempio n. 9
0
        /// <summary>
        ///     Try recover blocker for an incompleteInformation blocker
        ///     Missing information come from creator => ask PO, Users, ....
        /// </summary>
        /// <param name="task"></param>
        /// <param name="blocker"></param>
        public void TryRecoverBlockerIncompleteInformation(SymuTask task, Blocker blocker)
        {
            if (task is null)
            {
                throw new ArgumentNullException(nameof(task));
            }

            if (blocker is null)
            {
                throw new ArgumentNullException(nameof(blocker));
            }

            var murphy        = Environment.MainOrganization.Murphies.IncompleteInformation;
            var askInternally = Environment.MainOrganization.Murphies.IncompleteKnowledge.AskInternally(Schedule.Step,
                                                                                                        blocker.InitialStep);

            //TODO send to creator only if he has the right to communicate to cf. Network
            if (askInternally && task.HasCreator)
            {
                var messageType = murphy.AskOnWhichChannel(Cognitive.InteractionCharacteristics
                                                           .PreferredCommunicationMediums);
                var parameter = new MessageAttachments();
                parameter.Add(blocker);
                parameter.Add(task);
                Send(task.Creator, MessageAction.Ask, SymuYellowPages.Help, parameter, messageType);
            }
            else
            {
                if (murphy.ShouldGuess(blocker.NumberOfTries))
                {
                    // Blocker must be unblocked in a way or another
                    RecoverBlockerIncompleteByGuessing(task, blocker,
                                                       Environment.MainOrganization.Murphies.IncompleteInformation, BlockerResolution.Guessing);
                }
                else
                {
                    TryRecoverBlockerIncompleteInformationExternally(task, blocker);
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// </summary>
        /// <param name="task"></param>
        /// <param name="blocker"></param>
        /// <returns></returns>
        public override void TryRecoverBlockerIncompleteBelief(SymuTask task, Blocker blocker)
        {
            if (task is null)
            {
                throw new ArgumentNullException(nameof(task));
            }

            if (blocker is null)
            {
                throw new ArgumentNullException(nameof(blocker));
            }

            // RiskAversionThreshold has been exceeded =>
            // Worker don't want to do the task, the task is blocked in base method
            // Ask advice from influencers
            var attachments = new MessageAttachments
            {
                KnowledgeId  = (IAgentId)blocker.Parameter,
                KnowledgeBit = (byte)blocker.Parameter2
            };

            SendToMany(Influencers, MessageAction.Ask, SymuYellowPages.Belief, attachments, CommunicationMediums.Email);
        }
Esempio n. 11
0
        public void Reply(IAgentId receiverId, MessageAction action, byte content, MessageAttachments parameter)
        {
            var message = new Message(AgentId, receiverId, action, content, parameter);

            Reply(message);
        }
Esempio n. 12
0
 public void SendToMany(IEnumerable <IAgentId> receivers, MessageAction action, byte content,
                        MessageAttachments parameter)
 {
     SendToMany(receivers, action, content, parameter, CommunicationMediums.System);
 }
Esempio n. 13
0
        public async Task <string> PostAddUser(MessageAttachments model)
        {
            int counter      = 0;
            var personGroups = await FaceAnalyzer.faceServiceClient.ListPersonGroupsAsync();

            var xamGroup = personGroups.Where(x => x.Name == "Xamarin").FirstOrDefault();

            foreach (var user in model.Users)
            {
                CreatePersonResult person;
                var bytes = user.GetByteArray();

                var detectedFaceResults = await FaceAnalyzer.DetectFaceAsync(bytes);

                if (detectedFaceResults.Success)
                {
                    var isUserInDatabase = context.IdentifiedUsers.Where(usr => usr.Id == detectedFaceResults.FaceId.ToString()).ToList().FirstOrDefault();

                    if (isUserInDatabase == null)
                    {
                        context.IdentifiedUsers.Add(new IdentifiedUser {
                            Id = detectedFaceResults.FaceId.ToString(), Email = user.UserEmail
                        });
                    }
                    else
                    {
                        return($"User {isUserInDatabase.Email} is already in the database");
                    }
                }
                else
                {
                    //Check if CognitiveServices XamarinGroup doesn't know of our user
                    if (detectedFaceResults.Error == "Unable to Identify User")
                    {
                        using (Stream imageFileStream = new MemoryStream(bytes))
                        {
                            person = await FaceAnalyzer.faceServiceClient.CreatePersonAsync(xamGroup.PersonGroupId, user.UserEmail);

                            await FaceAnalyzer.faceServiceClient.AddPersonFaceAsync(xamGroup.PersonGroupId, person.PersonId, imageFileStream);

                            await FaceAnalyzer.faceServiceClient.TrainPersonGroupAsync(xamGroup.PersonGroupId);

                            context.IdentifiedUsers.Add(new IdentifiedUser {
                                Id = person.PersonId.ToString(), Email = user.UserEmail
                            });
                        }
                    }
                    //If it doesn't know about our user, then there was some error i.e. multiple faces, glasses
                    else
                    {
                        return(detectedFaceResults.Error);
                    }
                }

                counter++;
            }

            await context.SaveChangesAsync();

            return($"Saved {counter} users out of {model.Users.Count} total users");
        }