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

            if (message is AgentRootInitMessage rootInitMessage)
            {
                for (int i = 0; i < 20; i++)
                {
                    context.CreateAgent("Agent" + i, "SingleAgent", new NumberMessage()
                    {
                        Creator = self, Number = i
                    }, null);
                }
            }
            else if (message is NotifyNumber msg)
            {
                Console.WriteLine("{0}", msg.Number);
            }
            return(Task.FromResult(context));
        }
Esempio n. 2
0
        public Task <AgentContext <object> > Run(object state, AgentCapability self, object message)
        {
            var context = new AgentContext <object>(state, self);

            if (message is AgentRootInitMessage rootInitMessage)
            {
                var cap = context.IssueCapability(new[] { typeof(PingPongMessage) });
                context.CreateAgent("AgentTwoId", "AgentTwo", new PingPongMessage {
                    AgentOne = cap
                }, null);
            }
            else if (message is PingPongMessage pingPongMessage)
            {
                context.SendMessage(pingPongMessage.AgentTwo, new PingPongMessage
                {
                    AgentOne = pingPongMessage.AgentOne,
                    AgentTwo = pingPongMessage.AgentTwo,
                    Content  = "Ping"
                }, null);
            }
            return(Task.FromResult(context));
        }
Esempio n. 3
0
        public Task <AgentContext <OrganizationState> > Run(object state, AgentCapability self, object message)
        {
            var organizationState    = state as OrganizationState ?? new OrganizationState();
            var context              = new AgentContext <OrganizationState>(organizationState, self);
            var distributeCapability = context.IssueCapability(new[] { typeof(DistributeCapabilitiesMessage) });

            switch (message)
            {
            case AgentRootInitMessage _:

                var memberFactoryInitMessage = new InitWetonomyAgentMessage(distributeCapability);
                context.CreateAgent("memberAndGroupFactory", "MemberAndGroupFactoryAgent", memberFactoryInitMessage, null);
                break;

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

                switch (DistributeCapabilitiesMessage.Id)
                {
                // -------- Members, Groups & Voting --------
                case "memberAndGroupFactory":
                    //var createFirstMemberMsg = new CreateGroupMessage("FirstGroup");
                    //var creatGroupMessage = DistributeCapabilitiesMessage.AgentCapabilities["MintTokenMessage"];
                    //context.SendMessage(creatGroupMessage, createFirstMemberMsg, null);

                    var creatMemberCap = DistributeCapabilitiesMessage.AgentCapabilities["CreateMemberMessage"];
                    for (int i = 0; i < 5; i++)
                    {
                        var createMemberMsg = new CreateMemberMessage("MemberDebt" + i);
                        context.SendMessage(creatMemberCap, createMemberMsg, null);
                    }


                    var votingInitMessage = new InitWetonomyAgentMessage(distributeCapability);
                    context.CreateAgent("centralVoting", "VotingAgent", votingInitMessage, null);
                    break;

                case "centralVoting":
                    //var votingInitMessage = new InitWetonomyAgentMessage(distributeCapability);
                    //context.CreateAgent("centralVoting", "VotingAgent", votingInitMessage, null);
                    var moneyTokenManagerInitMessage = new InitWetonomyAgentMessage(distributeCapability);
                    context.CreateAgent("moneyTokenManager", "TokenManagerAgent", moneyTokenManagerInitMessage, null);
                    break;

                // -------- Token Flow --------

                case "moneyTokenManager":
                    var mintCapability   = DistributeCapabilitiesMessage.AgentCapabilities["MintTokenMessage"];
                    var moneyTokenMinter = new TokenActionAgentInitMessage(
                        mintCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>(),
                        null,
                        new HashSet <string>()
                    {
                        "debtTokenBurner", "allowanceTokenBurner"
                    });
                    context.CreateAgent("moneyTokenMinter", "TokenMinterAgent", moneyTokenMinter, null);
                    break;

                case "moneyTokenMinter":

                    var cashTokenManagerInitMessage      = new InitWetonomyAgentMessage(distributeCapability);
                    var debtTokenManagerInitMessage      = new InitWetonomyAgentMessage(distributeCapability);
                    var allowanceTokenManagerInitMessage = new InitWetonomyAgentMessage(distributeCapability);

                    context.CreateAgent("cashTokenManager", "TokenManagerAgent", cashTokenManagerInitMessage, null);
                    context.CreateAgent("debtTokenManager", "TokenManagerAgent", debtTokenManagerInitMessage, null);
                    context.CreateAgent("allowanceTokenManager", "TokenManagerAgent", allowanceTokenManagerInitMessage, null);
                    break;

                case "cashTokenManager":
                    var splitCapability    = DistributeCapabilitiesMessage.AgentCapabilities["TransferTokenMessage"];
                    var tokenSplitterAgent = new TokenActionAgentInitMessage(
                        splitCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("cashTokenManager", typeof(TokensTransferedNotification)), new UniformSplitterStrategy() }
                    },
                        new List <IAgentTokenKey>()
                    {
                        new SingleAngentTokenKey("cashTokenBurnerForDebt"), new SingleAngentTokenKey("cashTokenBurnerForAllowance")
                    });
                    context.CreateAgent("cashTokenSplitter", "TokenSplitterAgent", tokenSplitterAgent, null);


                    var burnCapability         = DistributeCapabilitiesMessage.AgentCapabilities["BurnTokenMessage"];
                    var cashTokenBurnerForDebt = new TokenActionAgentInitMessage(
                        burnCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("cashTokenManager", typeof(TokensTransferedNotification)), new SelfBurnStrategy() }
                    });
                    context.CreateAgent("cashTokenBurnerForDebt", "TokenBurnerAgent", cashTokenBurnerForDebt, null);


                    var cashTokenBurnerForAllowance = new TokenActionAgentInitMessage(
                        burnCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("cashTokenManager", typeof(TokensTransferedNotification)), new SelfBurnStrategy() }
                    });
                    context.CreateAgent("cashTokenBurnerForAllowance", "TokenBurnerAgent", cashTokenBurnerForAllowance, null);
                    break;

                case "debtTokenManager":
                    var debtBurnCapability = DistributeCapabilitiesMessage.AgentCapabilities["BurnTokenMessage"];
                    var debtTokenBurner    = new TokenActionAgentInitMessage(
                        debtBurnCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("cashTokenBurnerForDebt", typeof(TokensBurnedTriggerer)), new SequentialBurnStrategy() }
                    },
                        null,
                        new HashSet <string>()
                    {
                        "cashTokenBurnerForDebt", "debtTokenMinter"
                    });
                    context.CreateAgent("debtTokenBurner", "TokenBurnerAgent", debtTokenBurner, null);

                    var debtMintCapability = DistributeCapabilitiesMessage.AgentCapabilities["MintTokenMessage"];
                    var debtTokenMinter    = new TokenActionAgentInitMessage(
                        debtMintCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        { new AgentTriggerPair("AgentRoot", typeof(MockMessageTrigger)), new MintForAgentOnBurnerStrategy() }
                    });
                    context.CreateAgent("debtTokenMinter", "TokenMinterAgent", debtTokenMinter, null);
                    break;

                case "allowanceTokenManager":

                    //Should add minter
                    var allowanceBurnCapability = DistributeCapabilitiesMessage.AgentCapabilities["BurnTokenMessage"];
                    var allowanceTokenBurner    = new TokenActionAgentInitMessage(
                        allowanceBurnCapability,
                        distributeCapability,
                        new Dictionary <AgentTriggerPair, ITriggeredAction>()
                    {
                        //{ new AgentTriggerPair("cashTokenBurnerForAllowance" ,typeof(TokensBurnedTriggerer)), new SequentialBurnStrategy()}
                    },
                        null,
                        new HashSet <string>()
                    {
                        "cashTokenBurnerForAllowance"
                    });
                    context.CreateAgent("allowanceTokenBurner", "TokenBurnerAgent", allowanceTokenBurner, null);
                    break;

                case "debtTokenMinter":
                    var debtTokenMinterCapability = new AgentCapability("debtTokenMinter", typeof(MockMessageTrigger));

                    for (int i = 0; i < 5; i++)
                    {
                        var mintTokenMessage = new MockMessageTrigger("AgentRoot", 100, new SingleAngentTokenKey("MemberDebt" + i));
                        context.SendMessage(debtTokenMinterCapability, mintTokenMessage, null);
                    }
                    break;

                case "debtTokenBurner":

                    var cap1            = context.State.AgentToCapabilities["moneyTokenMinter"]["AddTriggerToActionMessage"];
                    var debtBurnTrigger = new AddTriggerToActionMessage(new AgentTriggerPair("debtTokenBurner", typeof(TokensBurnedTriggerer)), new SingleMintAfterBurnStrategy());
                    context.SendMessage(cap1, debtBurnTrigger, null);
                    break;

                case "allowanceTokenBurner":

                    var cap2 = context.State.AgentToCapabilities["moneyTokenMinter"]["AddTriggerToActionMessage"];
                    var allowanceBurnTrigger = new AddTriggerToActionMessage(new AgentTriggerPair("allowanceTokenBurner", typeof(TokensBurnedTriggerer)), new SingleMintAfterBurnStrategy());
                    context.SendMessage(cap2, allowanceBurnTrigger, null);

                    // Imitate user action
                    var mintSingleKey = new SingleAngentTokenKey(self.Issuer);
                    context.AddReminder(TimeSpan.FromSeconds(5), new MintTokenMessage(100, mintSingleKey));
                    break;
                }
                break;

            // Imitate user action
            case MintTokenMessage mintCashToken:
                var cashTokenManager = context.State.AgentToCapabilities["cashTokenManager"]["MintTokenMessage"];
                context.SendMessage(cashTokenManager, mintCashToken, null);
                break;

            case TokensMintedNotification tokensMintedNotification:
                var transferCashTokenManager = context.State.AgentToCapabilities["cashTokenManager"]["TransferTokenMessage"];
                context.SendMessage(transferCashTokenManager, new TransferTokenMessage(100, new SingleAngentTokenKey(self.Issuer), new SingleAngentTokenKey("cashTokenSplitter")), null);
                break;
            }
            return(Task.FromResult(context));
        }
Esempio n. 4
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));
        }