File size: 2,755 Bytes
90ca39a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace AIMA.Agent.Object
{
    public abstract class ObjectWithDynamicAttributesBase : IObjectWithDynamicAttributes
    {
        private Dictionary<object, object> Attributes { get; init; } = new();
        public IReadOnlyDictionary<object, object> ReadOnlyAttributes => Attributes;
        public virtual string DescribeType => GetType().Name;
        public virtual string DescribeAttributes()
        {
            var stringBuildr = new StringBuilder('[');
            bool first = true;
            foreach (object key in Attributes.Keys)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    stringBuildr.Append(", ");
                }
                stringBuildr.Append(key);
                stringBuildr.Append("==");
                stringBuildr.Append(Attributes[key]);
            }
            stringBuildr.Append(']');
            return stringBuildr.ToString();
        }
        public virtual HashSet<object> GetKeySet() => new(Attributes.Keys);
        public virtual void SetAttribute(object key, object value) => Attributes[key] = value;
        public virtual object GetAttribute(object key) => Attributes[key];
        public virtual object RemoveAttribute(object key) => Attributes.Remove(key);
        public virtual IObjectWithDynamicAttributes Copy()
        {
            ObjectWithDynamicAttributesBase copy = null;
            try
            {
                copy = (ObjectWithDynamicAttributesBase)GetType().GetConstructor(Type.EmptyTypes).Invoke(null);
                foreach (object value in Attributes)
                {
                    copy.Attributes.Add(value, Attributes[value]);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            return copy;
        }

        public override bool Equals(object o)
        {
            if (o == null || GetType() != o.GetType())
            {
                return base.Equals(o);
            }
            return Attributes.Equals(((ObjectWithDynamicAttributesBase)o).Attributes);
        }

        public override int GetHashCode()
        {
            return Attributes.GetHashCode();
        }

        public override string ToString()
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(DescribeType);
            stringBuilder.Append(DescribeAttributes());
            return stringBuilder.ToString();
        }
    }
}