2 using System
.Collections
;
3 using System
.Reflection
;
8 namespace Search
.Tiles
{
10 // FIXME: Should we call this something else?
11 public abstract class TileActivator
: IComparable
{
13 private ArrayList flavors
;
15 protected TileActivator ()
17 this.flavors
= new ArrayList ();
20 public abstract Tile
BuildTile (Beagle
.Hit hit
, Beagle
.Query query
);
22 public int Weight
= 0;
24 protected void AddSupportedFlavor (HitFlavor flavor
)
29 public virtual bool Validate (Beagle
.Hit hit
)
31 if (flavors
.Count
< 1)
36 HitFlavor best
= null;
38 foreach (HitFlavor flavor
in flavors
) {
39 if (! flavor
.IsMatch (hit
))
47 if (flavor
.Weight
> best
.Weight
) {
53 Weight
+= best
.Weight
;
60 public int CompareTo (object o
)
62 TileActivator other
= (TileActivator
)o
;
67 return (this.Weight
- other
.Weight
);
71 // FIXME: Rename this and move out of this file
72 public static class TileActivatorOrg
{
74 private static ArrayList activators
;
76 static TileActivatorOrg ()
78 activators
= new ArrayList ();
80 foreach (Assembly assembly
in AppDomain
.CurrentDomain
.GetAssemblies ()) {
82 foreach (Type type
in ReflectionFu
.GetTypesFromAssemblyAttribute (assembly
, typeof (TileActivatorTypesAttribute
))) {
84 activators
.Add ((TileActivator
) Activator
.CreateInstance (type
));
85 } catch (Exception e
) {
86 Console
.WriteLine ("Caught exception while instantiating tile");
87 Console
.WriteLine (e
);
93 public static Tile
MakeTile (Beagle
.Hit hit
, Beagle
.Query query
)
95 TileActivator best
= null;
98 foreach (TileActivator activator
in activators
) {
99 if (! activator
.Validate (hit
))
107 if (activator
.Weight
> best
.Weight
)
112 return best
.BuildTile (hit
, query
);
113 } catch (Exception e
) {
114 Console
.WriteLine ("Error instantiating tile:\n{0}", e
);
121 [AttributeUsage (AttributeTargets
.Assembly
)]
122 public class TileActivatorTypesAttribute
: TypeCacheAttribute
{
123 public TileActivatorTypesAttribute (params Type
[] types
) : base (types
) { }