Exemplo n.º 1
0
 public static KVP DecipherLine(String msg)
 {
     try
     {
         String line = msg.Replace(" ", "");
         line = line.Replace("\r", "");
         line = line.Replace("\n", "");
         StringBuilder ss = new StringBuilder(line); //proverit', ne odli li tam chasy
         ss.Remove(0, 6); // length of "state(["
         ss.Remove(ss.Length - 2, 2); //length of ");"
         KVP root = new KVP("state", IsolateList(ss.ToString()));
         return root;
     }
     catch (Exception)
     {
         return null;
     }
 }
Exemplo n.º 2
0
        public Clock(KVP kvp)
        {
            foreach (var n in kvp.ListOfItems)
            {
                if (n.Key.Equals("expired_at"))
                {
                    ExpiredAt = (n.Value.Contains('.')) ?  n.Value.Substring(0, n.Value.LastIndexOf('.') + 2):n.Value ;
                    ExpiredAt = ToTime(ExpiredAt);
                    TimeStampE = n.Value;
                }
                if (n.Key.Equals("happened_at"))
                {
                    HappenedAt = (n.Value.Contains('.')) ? n.Value.Substring(0, n.Value.LastIndexOf('.') + 2): n.Value ;
                    HappenedAt = ToTime(HappenedAt);
                    TimeStampH = n.Value;
                }
            }
            //TextList = new List<CfgStr>() { new CfgStr("Happened at: \t" + HappenedAt), new CfgStr("Expired at: \t" + ExpiredAt), new CfgStr("Step number: \t" + "0") };

            //TextList = new List<CfgStr>() { new CfgStr(HappenedAt), new CfgStr(ExpiredAt), new CfgStr("0") };
            TextList = new List<CfgStr>() { new CfgStr(HappenedAt), new CfgStr(ExpiredAt) };
            //TextListNames = new List<CfgStr>() { new CfgStr("Happened at:"), new CfgStr("Expired at:"), new CfgStr("Step number:") };
            TextListNames = new List<CfgStr>() { new CfgStr("Happened at:"), new CfgStr("Expired at:")};
        }
Exemplo n.º 3
0
        public SystemEvent(KVP kvp, AgentDataDictionary ag) 
        {
            foreach (var k in kvp.ListOfItems)
            {
                if (k.Key == "description")
                {
                    String ts = k.Value;
                    
                    ts = ts.Remove(0, k.Value.IndexOf("(") + 1);
                    ts = ts.Remove(ts.Length - 1, 1);

                    String subject = ts.Substring(0, ts.IndexOf(","));
                    ts = ts.Remove(0, ts.IndexOf(",") + 1);

                    String action = ts.Substring(0, ts.LastIndexOf(","));
                    ts = ts.Remove(0, ts.LastIndexOf(",") + 1);

                    String successfully = ts;

                    String obj = "";

                    if (action.Contains("("))
                    {
                        obj = action.Substring(action.IndexOf("(")+1, action.LastIndexOf(")")-action.IndexOf("(")-1);
                        action = action.Remove(action.IndexOf("("), action.LastIndexOf(")") - action.IndexOf("(")+1);
                    }

                    if (action.StartsWith("initializ"))
                    {
                        _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " +
                                   "has successfully initialized the simulation.";
                    }
                    else if (action.StartsWith("admit"))
                    {
                        _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " +
                                   "has admitted " + (!obj.Equals("") ? ag.GetAgentNameByID(obj)+ "(" + obj + ")": "" + ".");
                    }
                    else if (action.StartsWith("dismiss"))
                    {
                        _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " +
                                  "has dismissed " + (!obj.Equals("") ? ag.GetAgentNameByID(obj) + "(" + obj + ")" : "" + ".");
                    }
                    else if (action.StartsWith("dismiss"))
                    {
                        _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " +
                                  "has enforced the following punishments: " + obj + ".";
                    }
                    else if (successfully.Equals("successfully"))
                    {
                        if (action.StartsWith("open_auction"))
                        {
                            String _objAuction = obj.Substring(0, obj.IndexOf(','));
                            String obj1 = (obj.Remove(0, obj.IndexOf(',') + 1)).Replace(" ", "");
                            String _objItem = obj1.Substring(0, obj1.IndexOf(","));
                            String _objPrice = (obj1.Remove(0, obj1.IndexOf(',') + 1));

                            _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " + "has created the auction \"" + _objAuction + "\" for " +
                                       _objItem + " with opening price of " + _objPrice;
                        }
                        else if (action.StartsWith("close_auction"))
                        {
                            _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " + "has closed the auction \"" +
                                       obj +"\"";
                        }
                        else if (action.StartsWith("place_bid"))
                        {
                            String _objAuction = obj.Substring(0, obj.IndexOf(','));
                            String _objPrice = (obj.Remove(0, obj.IndexOf(',') + 1)).Replace(" ", "");
                            _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " + "has placed the bid of " + _objPrice +
                                       " in auction \"" + _objAuction +"\".";
                        }
                        else if (action.StartsWith("sell"))
                        {
                            String _objItem = obj.Substring(0, obj.IndexOf(','));
                            String obj1 = (obj.Remove(0, obj.IndexOf(',') + 1)).Replace(" ", "");
                            String _objPrice = obj1.Substring(0, obj1.IndexOf(","));
                            String _objAgent = (obj1.Remove(0, obj1.IndexOf(',') + 1));
                            _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " + "has sold the item " + _objItem +
                                       " for " +_objPrice +" to " + ag.GetAgentNameByID(_objAgent) + "(" + _objAgent + ")";
                        }
                        else if (action.StartsWith("flip_coin"))
                        {
                            _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " + "has flipped the coin.";
                        }
                        else
                        {
                            _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " + successfully +
                                       " performed the following action: " +
                                       action + (!obj.Equals("") ? "(" + obj + ")" : "") + ".";
                        }
                    }
                    else
                    {
                        _message = "Agent " + ag.GetAgentNameByID(subject) + "(" + subject + ") " + successfully +
                                   " tried to perform the following action: " +
                                   action + (!obj.Equals("") ? "(" + obj + ")" : "") + ".";
                    }


                }
                if (k.Key == "happened_at")
                {
                    _timestamp = (k.Value.Contains('.')) ? k.Value.Substring(0, k.Value.LastIndexOf('.') + 2) : k.Value;
                }
            }
        }
Exemplo n.º 4
0
        public static Item KvpToItem(KVP src, AgentDataDictionary adata, Dispatcher uiDispatcher)
        {
            Item result;

            if (src.Value != "\0")
            {
                result = new Item(src.Key, src.Value, uiDispatcher);
                result.Type = ItemType.Attribute;
            }
            else if (src.ListOfItems!=null)
            {
                result = new Item(src.Key, src.ListOfItems, cfgSettings, adata, uiDispatcher);
                if (src.Key.StartsWith("right("))
                {
                    result.Type = ItemType.Right;
                }
                else if (src.Key.StartsWith("obligation("))
                {
                    result.Type = ItemType.Obligation;
                }
                else
                {
                    result.Type = ItemType.Asset;
                }
            }
            else throw new Exception("Empty attribute value");

            return result;
        }
Exemplo n.º 5
0
        public static EnvironmentState MapState(KVP root, AgentDataDictionary _agentDataDictionary, Dispatcher uiThread)
        {
            ObservableCollection<Agent> agList = new ObservableCollection <Agent>();
            ObservableCollection<Item> aucList = new ObservableCollection<Item>();
            ObservableCollection<Item> comList = new ObservableCollection<Item>();

            Dictionary<String, List<KVP>> agts = new Dictionary<string, List<KVP>>();

            EnvironmentState state = new EnvironmentState();
            foreach (var kvp in root.ListOfItems)
            {
                if (kvp.Key.Equals("clock"))
                {
                    state.Clock = new Clock(kvp);
                }

                Item tItem = Item.KvpToItem(kvp, _agentDataDictionary, uiThread);
                if (AgentDataDictionary.IsSpecialItem(tItem.InstanceOf))
                {
                    aucList.Add(tItem);
                }

                if (kvp.Key.StartsWith("event"))
                {
                    //VIK - for future use
                    //Vik -- the future has come. 2014-08-25
                    state.Event = new SystemEvent(kvp, _agentDataDictionary);
                }
                else
                {
                    foreach (var elt in kvp.ListOfItems)
                    {
                        if (elt.Key.Equals("held_by"))
                        {
                            if (!agts.ContainsKey(elt.Value))
                            {
                                agts.Add(elt.Value, new List<KVP> {kvp});
                            }
                            else
                            {
                                List<KVP> tl;
                                if (agts.TryGetValue(elt.Value, out tl))
                                {
                                    tl.Add(kvp);
                                }
                            }
                        }
                        if (elt.Key.Equals("owned_by"))
                        {
                            if (!agts.ContainsKey(elt.Value))
                            {
                                agts.Add(elt.Value, new List<KVP> {});
                            }
                        }
                    }
                }

            }
            foreach (var agt in agts)
            {
                if (!agt.Key.StartsWith("_"))
                {
                    Agent malagent = new Agent(agt.Key, agt.Value, _agentDataDictionary, uiThread);
                    agList.Add(new Agent(agt.Key, agt.Value, _agentDataDictionary, uiThread));
                }
                else
                {
                    ObservableCollection<Item> tRights = Item.KvpToItems(agt.Value, _agentDataDictionary, uiThread);
                    foreach (var tRight in tRights)
                    {
                        comList.Add(tRight);
                    }
                    
                }
            }
            if (!agts.ContainsKey("god"))
            {
                agList.Add(new Agent("god", new List<KVP>(), _agentDataDictionary, uiThread));
            }
            state.Agents = agList;

            //foreach (var ag in state.AllAgents)
            //{
            //    if (!StateObjectMapper.ContainsAgent(state.Agents, ag.ID))
            //    {
            //        ag.Status = ElementStatus.Deleted;
            //    }
            //    else
            //    {
            //        ag.Status = ElementStatus.Unchanged;
            //    }
            //}

            state.Auctions = aucList;
            state.CommonRights = comList;
            return state;
        }