Esempio n. 1
0
        public Task <AgentContext <MemberState> > Run(object state, AgentCapability self, object message)
        {
            var agentState = state as MemberState ?? new MemberState();
            var context    = new AgentContext <MemberState>(agentState, self);

            switch (message)
            {
            case InitWetonomyAgentMessage initMsg:
                //context.SubscribeUserInput()
                var distributeCapabilityMessage = new DistributeCapabilitiesMessage
                {
                    Id = self.Issuer,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { "AddGroupMessage", context.IssueCapability(new[] { typeof(AddGroupMessage) }) },
                        { "RemoveMemberMessage", context.IssueCapability(new[] { typeof(RemoveMemberMessage) }) },
                    }
                };

                context.SendMessage(initMsg.CreatorAgentCapability, distributeCapabilityMessage, null);
                break;

            case AddGroupMessage addGroupMessage:
                context.State.Groups.Add(addGroupMessage.GroupAgentId);
                break;

            case RemoveMemberMessage removeGroupMessage:
                context.State.Groups.Remove(removeGroupMessage.GroupAgentId);
                break;
            }

            return(Task.FromResult(context));
        }
Esempio n. 2
0
        public Task <AgentContext <VotingState> > Run(object state, AgentCapability self, object message)
        {
            var context = new AgentContext <VotingState>(state as VotingState, self);

            switch (message)
            {
            case InitWetonomyAgentMessage initMsg:
                var distributeCapabilityMessage = new DistributeCapabilitiesMessage
                {
                    Id = self.Issuer,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { "AddVoteMessage", context.IssueCapability(new[] { typeof(AddVoteMessage <T>) }) },
                        { "AddDecisionMessage", context.IssueCapability(new[] { typeof(AddDecisionMessage) }) },
                        //{"ForwardMessage", context.IssueCapability(new[]{ typeof(ForwardMessage) })}
                    }
                };
                context.SendMessage(initMsg.CreatorAgentCapability, distributeCapabilityMessage, null);
                break;

            case AddVoteMessage <V> addVoteMessage:
                context.State.DecisionsVotes[addVoteMessage.DecisionId].Add(addVoteMessage.Sender, addVoteMessage.Vote);
                context.MakePublication(new NewVotePublication <V>(addVoteMessage.DecisionId, addVoteMessage.Vote));
                break;

            case AddDecisionMessage addDecisionMessage:
                var decision = new Decision <T>(
                    context.State.nonce.ToString(),
                    addDecisionMessage.Executable,
                    addDecisionMessage.ActionMessage,
                    addDecisionMessage.Start,
                    addDecisionMessage.Finale);

                context.State.Decisions.Add(context.State.nonce.ToString(), decision);
                context.State.DecisionsVotes.Add(context.State.nonce.ToString(), new Dictionary <string, V>());

                context.MakePublication(
                    new NewDecisionPublication(context.State.nonce.ToString(), addDecisionMessage.ActionMessage)
                    );

                context.AddReminder(decision.Finale - DateTime.Now, new FinalizeDecision(context.State.nonce.ToString()));
                //Just Temporary solution
                context.State.nonce++;
                break;

            case FinalizeDecision finalizeDecisionMessage:
                var             dec   = context.State.Decisions[finalizeDecisionMessage.DecisionId];
                IEnumerable <V> votes =
                    context.State.DecisionsVotes[finalizeDecisionMessage.DecisionId]
                    .Select(pair => pair.Value);

                T decisionEvaluation = context.State.VotingStategy.MakeDecision(votes);

                dec.Evaluation = decisionEvaluation;
                dec.State      = DecisionState.Finalized;


                if (dec.Executable)
                {
                    //Executes only if T is bool, not sure if we need execution when T isn't bool
                    if (decisionEvaluation is bool check && check)
                    {
                        // should discuss how we store Capabilities
                        context.ForwardMessage(null, dec.DecisionActionMessage, null);
                    }
                }
                break;
            }

            return(Task.FromResult(context));
        }
Esempio n. 3
0
        public Task <AgentContext <RecipientState> > Run(object state, AgentCapability self, object message)
        {
            var agentState = state as RecipientState ?? new RecipientState();
            var context    = new AgentContext <RecipientState>(agentState, self);

            if (message is AbstractTrigger msg)
            {
                var pair = new AgentTriggerPair(msg.Sender, message.GetType());
                if (context.State.TriggerToAction.ContainsKey(pair))
                {
                    (IList <object>, IList <object>)result = RecipientState.TriggerCheck(context.State, pair, msg);

                    foreach (var action in result.Item1)
                    {
                        context.SendMessage(context.State.TokenManagerAgent, action, null);
                    }

                    foreach (var publication in result.Item2)
                    {
                        context.MakePublication(publication);
                    }

                    return(Task.FromResult(context));
                }
            }

            switch (message)
            {
            case TokenActionAgentInitMessage initMessage:
                context.State.TokenManagerAgent = initMessage.TokenManagerAgentCapability;
                context.State.TriggerToAction   = initMessage.TriggererToAction ?? new Dictionary <AgentTriggerPair, ITriggeredAction>();
                context.State.Recipients        = initMessage.Recipients;// ?? new List<IAgentTokenKey>();
                context.State.SelfId            = self.Issuer;

                var distributeCapabilityMessage = new DistributeCapabilitiesMessage
                {
                    Id = self.Issuer,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { "AddRecipientMessage", context.IssueCapability(new[] { typeof(AddRecipientMessage) }) },
                        { "RemoveRecipientMessage", context.IssueCapability(new[] { typeof(RemoveRecipientMessage) }) },
                        { "AddTriggerToActionMessage", context.IssueCapability(new[] { typeof(AddTriggerToActionMessage) }) },
                    }
                };
                if (initMessage.Subscription != null)
                {
                    foreach (var agent in initMessage.Subscription)
                    {
                        context.AddSubscription(agent);
                    }
                }
                context.SendMessage(initMessage.CreatorAgentCapability, distributeCapabilityMessage, null);
                break;

            case AddRecipientMessage addMessage:
                if (context.State.AddRecipient(addMessage.Recipient))
                {
                    context.MakePublication(new RecipientAddedPublication(addMessage.Recipient));
                }
                break;

            case RemoveRecipientMessage removeMessage:
                if (context.State.RemoveRecipient(removeMessage.Recipient))
                {
                    context.MakePublication(new RecipientRemovedPublication(removeMessage.Recipient));
                }
                break;

            case AddTriggerToActionMessage addTriggerMessage:
                context.State.TriggerToAction.Add(addTriggerMessage.Trigger, addTriggerMessage.Action);
                break;
            }

            return(Task.FromResult(context));
        }
Esempio n. 4
0
        public Task <AgentContext <TokenBurnerState> > Run(object state, AgentCapability self, object message)
        {
            var agentState = state as TokenBurnerState ?? new TokenBurnerState();
            var context    = new AgentContext <TokenBurnerState>(agentState, self);

            if (message is AbstractTrigger msg)
            {
                var pair = new AgentTriggerPair(msg.Sender, message.GetType());
                if (context.State.TriggerToAction.ContainsKey(pair))
                {
                    (IList <object>, IList <object>)result = RecipientState.TriggerCheck(context.State, pair, msg);

                    foreach (BurnTokenMessage action in result.Item1)
                    {
                        context.SendMessage(context.State.TokenManagerAgent, action, null);
                    }

                    foreach (var publication in result.Item2)
                    {
                        context.MakePublication(publication);
                    }

                    return(Task.FromResult(context));
                }
            }

            switch (message)
            {
            case TokenActionAgentInitMessage initMessage:
                context.State.TokenManagerAgent = initMessage.TokenManagerAgentCapability;
                context.State.TriggerToAction   = initMessage.TriggererToAction ?? new Dictionary <AgentTriggerPair, ITriggeredAction>();
                context.State.SelfId            = self.Issuer;

                var distributeCapabilityMessage = new DistributeCapabilitiesMessage
                {
                    Id = self.Issuer,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { "GetTokensMessage", context.IssueCapability(new[] { typeof(GetTokensMessage) }) },
                        { "AddTriggerToActionMessage", context.IssueCapability(new[] { typeof(AddTriggerToActionMessage) }) },
                    }
                };
                if (initMessage.Subscription != null)
                {
                    foreach (var agent in initMessage.Subscription)
                    {
                        context.AddSubscription(agent);
                    }
                }
                context.SendMessage(initMessage.CreatorAgentCapability, distributeCapabilityMessage, null);
                break;

            //Tokens are minted on burner's addres so that he can burn them,
            //but they are actually for the user TokensMintedTriggerer.To
            //He is added as recipient and he can take ownership of the tokens with GetTokensMessage
            //This is done so that the system can work automaticly
            case TokensMintedTriggerer transferedMessage:
                if (context.State.AddRecipient(transferedMessage.To))
                {
                    context.State.MintedMessages.Add(transferedMessage);
                    context.MakePublication(new RecipientAddedPublication(transferedMessage.To));
                }
                break;

            case GetTokensMessage getTokensMessage:
                IAgentTokenKey agentSender;
                if (context.State.GetTokens(getTokensMessage.Recipient, getTokensMessage.Amount, out agentSender))
                {
                    var transfer = new TransferTokenMessage(getTokensMessage.Amount, agentSender, getTokensMessage.Recipient);
                    context.SendMessage(null, transfer, null);
                }
                break;

            case AddTriggerToActionMessage addTriggerMessage:
                context.State.TriggerToAction.Add(addTriggerMessage.Trigger, addTriggerMessage.Action);
                break;
            }

            return(Task.FromResult(context));
        }
Esempio n. 5
0
        public Task <AgentContext <TokenManagerState> > Run(object state, AgentCapability self, object message)
        {
            var agentState = state as TokenManagerState ?? new TokenManagerState();
            var context    = new AgentContext <TokenManagerState>(agentState, self);

            switch (message)
            {
            case InitWetonomyAgentMessage tokenManagerInitMessage:
                var distributeCapabilityMessage = new DistributeCapabilitiesMessage
                {
                    Id = self.Issuer,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { "BurnTokenMessage", context.IssueCapability(new[] { typeof(BurnTokenMessage) }) },
                        { "MintTokenMessage", context.IssueCapability(new[] { typeof(MintTokenMessage) }) },
                        { "TransferTokenMessage", context.IssueCapability(new[] { typeof(TransferTokenMessage) }) },
                    }
                };

                context.SendMessage(tokenManagerInitMessage.CreatorAgentCapability, distributeCapabilityMessage, null);
                break;

            case BurnTokenMessage burnTokenMessage:
                if (context.State.Burn(burnTokenMessage.Amount, burnTokenMessage.From))
                {
                    var notificationCapability = new AgentCapability(burnTokenMessage.From.GetAgentId(), typeof(TokensMintedNotification));

                    context.SendMessage(notificationCapability, new TokensBurnedNotification(self.Issuer, burnTokenMessage.Amount, burnTokenMessage.From), null);

                    context.MakePublication(
                        new TokenBurnPublication(burnTokenMessage.Amount, burnTokenMessage.From)
                        );
                }
                break;

            case MintTokenMessage mintTokenMessage:

                if (context.State.Mint(mintTokenMessage.Amount, mintTokenMessage.To))
                {
                    var notificationCapability = new AgentCapability(mintTokenMessage.To.GetAgentId(), typeof(TokensMintedNotification));

                    context.SendMessage(notificationCapability, new TokensMintedNotification(self.Issuer, mintTokenMessage.Amount, mintTokenMessage.To), null);

                    context.MakePublication(
                        new TokenMintPublication(mintTokenMessage.Amount, mintTokenMessage.To)
                        );
                }
                break;

            case TransferTokenMessage transferTokenMessage:
                if (context.State.Transfer(transferTokenMessage.Amount, transferTokenMessage.From, transferTokenMessage.To))
                {
                    var notificationCapabilityTo   = new AgentCapability(transferTokenMessage.To.GetAgentId(), typeof(TokensMintedNotification));
                    var notificationCapabilityFrom = new AgentCapability(transferTokenMessage.From.GetAgentId(), typeof(TokensMintedNotification));

                    context.SendMessage(notificationCapabilityTo, new TokensTransferedNotification(self.Issuer, transferTokenMessage.Amount, transferTokenMessage.From, transferTokenMessage.To), null);
                    context.SendMessage(notificationCapabilityFrom, new TokensTransferedNotification(self.Issuer, transferTokenMessage.Amount, transferTokenMessage.From, transferTokenMessage.To), null);

                    context.MakePublication(
                        new TokenTransferPublication(transferTokenMessage.Amount, transferTokenMessage.From, transferTokenMessage.To)
                        );
                }
                break;
            }
            return(Task.FromResult(context));
        }
Esempio n. 6
0
        public Task <AgentContext <FactoryState> > Run(object state, AgentCapability self, object message)
        {
            var agentState = state as FactoryState ?? new FactoryState();
            var context    = new AgentContext <FactoryState>(agentState, self);

            switch (message)
            {
            case InitWetonomyAgentMessage initMsg:
                var distributeCapabilityMessage = new DistributeCapabilitiesMessage
                {
                    Id = self.Issuer,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { "CreateGroupMessage", context.IssueCapability(new[] { typeof(CreateGroupMessage) }) },
                        { "CreateMemberMessage", context.IssueCapability(new[] { typeof(CreateMemberMessage) }) },
                        { "RemoveRefMessage", context.IssueCapability(new[] { typeof(RemoveRefMessage) }) },
                        { "GetCapabilityMessage", context.IssueCapability(new[] { typeof(GetCapabilityMessage) }) },
                        { "ForwardMessage", context.IssueCapability(new[] { typeof(ForwardMessage) }) }
                    }
                };
                context.SendMessage(initMsg.CreatorAgentCapability, distributeCapabilityMessage, null);
                break;

            case CreateGroupMessage createGroupMsg:
                // should use created one
                var distribute = context.IssueCapability(new[] { typeof(DistributeCapabilitiesMessage) });
                context.CreateAgent(createGroupMsg.Id, "GroupAgent", distribute, null);
                break;

            case CreateMemberMessage createMemberMessage:
                // should use created one
                var distribute2 = context.IssueCapability(new[] { typeof(DistributeCapabilitiesMessage) });
                context.CreateAgent(createMemberMessage.Id, "MemberAgent", distribute2, null);
                break;

            case RemoveRefMessage removeMemberMessage:
                context.State.AgentToCapabilities.Remove(removeMemberMessage.Id);
                break;

            // for forwarding
            case GetCapabilityMessage getCapabilitiesMessage:
                var capability = context.IssueCapability(new[] { getCapabilitiesMessage.CapabilityType });
                var distributeCapabilityMsg = new DistributeCapabilitiesMessage
                {
                    Id = getCapabilitiesMessage.Sender,
                    AgentCapabilities = new Dictionary <string, AgentCapability>()
                    {
                        { getCapabilitiesMessage.CapabilityType.Name, capability },
                    }
                };
                //check if Type.Name works as expected
                context.SendMessage(null, distributeCapabilityMsg, null);
                break;

            case DistributeCapabilitiesMessage distributeCapabilitiesMsg:
                context.State.AgentToCapabilities.Add(distributeCapabilitiesMsg.Id, distributeCapabilitiesMsg.AgentCapabilities);
                break;

            default:
                Task <AgentContext <BaseState> > secondaryContextTask = base.Run(agentState, self, message);
                var secondaryContext = secondaryContextTask.GetAwaiter().GetResult();
                context.MergeSecondaryContext(secondaryContext.GetCommands());
                break;
            }

            return(Task.FromResult(context));
        }