Ejemplo n.º 1
0
        public Agent(String id, List<KVP> items, AgentDataDictionary iAgentDataDictionary,Dispatcher uiThread)
        {
            ID = id;
            _imageDictionary = iAgentDataDictionary;
            
            _name = iAgentDataDictionary.GetAgentNameByID(id);

            Items = Item.KvpToItems(items, iAgentDataDictionary, uiThread);

            int tsc = 0;
            Item tIt = null;
            foreach (var item in Items)
            {
                if (item.Key.StartsWith("account"))
                {
                    tsc = Int32.Parse(item.Amount);
                    tIt = item;
                    break;
                }
            }

            _account = tsc;

            _uiDispatcher = uiThread;
            _uiDispatcher.Invoke(() =>
            {
                _bg = new SolidColorBrush(Colors.LightYellow);
                _brd = new SolidColorBrush(ColorAndIconAssigner.GetOrAssignColorById(id));
                if (tIt!=null) Items.Remove(tIt);
            });

            st = ElementStatus.New;
        }
Ejemplo n.º 2
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;
                }
            }
        }
Ejemplo n.º 3
0
        public static ObservableCollection<Item> KvpToItems(List<KVP> src, AgentDataDictionary _agentDataDictionary, Dispatcher uiDispatcher)
        {
            List<Item> result = new List<Item>();
            if (src == null) return null;

            foreach (var tm in src)
            {
                result.Add(KvpToItem(tm, _agentDataDictionary,uiDispatcher));
            }
            return new ObservableCollection<Item>(result);
        }
Ejemplo 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;
        }
Ejemplo n.º 5
0
        public Item(String key, List<KVP> list, CfgSettings cfg, AgentDataDictionary ag, Dispatcher uiDispatcher): base(key, new List<KVP>())
        {
            cfgSettings = cfg;
            if (key.StartsWith("oblig"))
            {
               // InstanceOf = "obligation";
            }
            ImageDictionary = ag;
            UIDispatcher = uiDispatcher;
            Status = ElementStatus.Unchanged;
            
            foreach (var l in list)
            {
                if (l.Key.Equals("held_by"))
                {
                    StringAttributeList.Add("Held by", ag.GetAgentNameByID(l.Value) + " (" + l.Value + ")");
                    _held_by = l.Value;
                    ListOfItems.Remove(l);
                }
                else if (l.Key.Equals("owned_by"))
                {
                    StringAttributeList.Add("Owned by", ag.GetAgentNameByID(l.Value) + " (" + l.Value + ")");
                    _owned_by = l.Value;
                    ListOfItems.Remove(l);
                }
                else if (l.Key.Equals("instance_of"))
                {
                    StringAttributeList.Add("Instance of", l.Value);
                    InstanceOf = l.Value;
                }
                else if (l.Key.Equals("weight"))
                {
                    StringAttributeList.Add("Weight", l.Value);
                    ListOfItems.Remove(l);
                }
                else if (l.Key.Equals("status"))
                {
                    StringAttributeList.Add("Status", l.Value);
                    ListOfItems.Remove(l);
                }
                else if (l.Key.Equals("auctioneer"))
                {
                    StringAttributeList.Add("Auctioneer", ag.GetAgentNameByID(l.Value) + " (" + l.Value + ")");
                    ListOfItems.Remove(l);
                }
                else if (l.Key.Equals("highest_bidder"))
                {
                    StringAttributeList.Add("Highest bidder", ag.GetAgentNameByID(l.Value) + " (" + l.Value + ")");
                    ListOfItems.Remove(l);
                }
                else if (l.Key.Equals("last_bid_time"))
                {
                    StringAttributeList.Add("Last bid time", l.Value.Substring(0, 5));
                    ListOfItems.Remove(l);
                }
                else if (l.Key.IndexOf("_") > 0)
                {
                    String st = l.Key.Replace("_", " ");
                    st = st[0].ToString().ToUpper()[0] + st.Substring(1);
                    StringAttributeList.Add(st, l.Value);
                    ListOfItems.Remove(l);
                }
                else if (l.Key.Equals("item"))
                {
                    StringAttributeList.Add("Item", l.Value);
                    InstanceOf = l.Value;
                }
                
                else if (l.Type == ItemType.Attribute)
                {
                   // String st = l.Key[0].ToString().ToUpper()[0] + l.k.Substring(1);
                    StringAttributeList.Add(l.Key, l.Value);
                    ListOfItems.Remove(l);
                }
                else
                {
                    ListOfItems.Add(l);
                }
            }
            UIDispatcher.Invoke(() =>
            {
                BorderBrush = getBrush();
                BorderBrush2 = getHeldBrush();
            });
            

        }
Ejemplo n.º 6
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;
        }
Ejemplo n.º 7
0
        public static void UpdateState (EnvironmentState newState, EnvironmentState oldState,AgentDataDictionary _agentDataDictionary, Dispatcher uiThread)
        {
            if (oldState == null)
            {
                oldState = new EnvironmentState();
            }

            if (oldState.Clock == null)
            {
                oldState.Clock = new Clock();
            }
            if (newState.Clock == null)
            {
                newState.Clock = new Clock();
            }

            oldState.Clock.HappenedAt = newState.Clock.HappenedAt;
            oldState.Clock.ExpiredAt = newState.Clock.ExpiredAt;
            oldState.Clock.TimeStampE = newState.Clock.TimeStampE;
            oldState.Clock.TimeStampH = newState.Clock.TimeStampH;
            oldState.Clock.SetTextList();
            
            oldState.Event = newState.Event;
            UpdateAuctions(oldState.Auctions, newState.Auctions, uiThread);
            UpdateCommons(oldState.CommonRights, newState.CommonRights, uiThread);
            UpdateStep(oldState.Agents, newState.Agents, uiThread);
            //oldState.AllAgents = newState.AllAgents;
            //oldState.AllItems = newState.AllItems;
            
            //UpdateCommons(oldState.AllItems, newState.AllItems, uiThread);

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

            //        if (StateObjectMapper.ContainsAgent(oldState.Agents, ag.ID))
            //        {
            //            ag.LastStep = LogProcessor.GetAgentEndStep(ag.ID);
            //        }
                
            //}
            

            //foreach (var itm in oldState.AllItems)
            //{
            //    bool found = false;
            //    foreach (var ag in oldState.Agents)
            //    {
            //        if (StateObjectMapper.ContainsItem(ag.Items, itm.Key))
            //        {
            //            found = true;
            //            break;
            //        }
            //    }
                
            //    itm.Status = found?ElementStatus.Unchanged:ElementStatus.Deleted;

            //}
        }
Ejemplo n.º 8
0
 public void InitProcessor(CfgSettings config, Dispatcher dispatcher, RunMode runMode)
 {
     _agentDataDictionary = new AgentDataDictionary(config);
     _dispatcher = dispatcher;
     AllAgents = new ObservableCollection<Agent>();
     AllItems = new ObservableCollection<Item>();
     CurrentState = new EnvironmentState();
     CurrentState.Clock = new Clock();
     states = new StateDictionary();
     states.PropertyChanged += states_PropertyChanged;
     cfgInfo = config;
     CurrentAgent = "god";
     Index = 0;
     _runMode = runMode;
     
 }