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 ()) {
84 foreach (Type type
in ReflectionFu
.GetTypesFromAssemblyAttribute (assembly
, typeof (TileActivatorTypesAttribute
))) {
86 Console
.WriteLine ("* Assembly: {0}", assembly
.FullName
);
89 Console
.WriteLine (" - {0}", type
);
92 activators
.Add ((TileActivator
) Activator
.CreateInstance (type
));
93 } catch (Exception e
) {
94 Console
.WriteLine ("Caught exception while instantiating tile");
95 Console
.WriteLine (e
);
101 public static Tile
MakeTile (Beagle
.Hit hit
, Beagle
.Query query
)
103 TileActivator best
= null;
106 foreach (TileActivator activator
in activators
) {
107 if (! activator
.Validate (hit
))
115 if (activator
.Weight
> best
.Weight
)
120 return best
.BuildTile (hit
, query
);
121 } catch (Exception e
) {
122 Console
.WriteLine ("Error instantiating tile:\n{0}", e
);
129 [AttributeUsage (AttributeTargets
.Assembly
)]
130 public class TileActivatorTypesAttribute
: TypeCacheAttribute
{
131 public TileActivatorTypesAttribute (params Type
[] types
) : base (types
) { }