package ie.dcu.auto.automator; import ie.dcu.auto.AutomationData; import java.lang.reflect.*; import java.util.*; /** * Creates automator instances. * * @author Kevin McGuinness */ public class AutomatorFactory { public static final String STRATEGY1 = "Strategy 1 (Deterministic)"; public static final String STRATEGY2 = "Strategy 2 (Probabilistic)"; public static final String STRATEGY3 = "Strategy 3 (Probabilistic)"; public static final String STRATEGY4 = "Strategy 4 (Probabilistic)"; private final Map> automators; private static AutomatorFactory instance; private AutomatorFactory() { automators = new HashMap>(); automators.put(STRATEGY1, DeterministicAutomator.class); automators.put(STRATEGY2, NonDeterministicAutomator1.class); automators.put(STRATEGY3, NonDeterministicAutomator2.class); automators.put(STRATEGY4, NonDeterministicAutomator3.class); } public Automator createAutomator(String strategy, AutomationData data) { try { Class clazz = automators.get(strategy); if (clazz != null) { Constructor ctor = clazz.getConstructor(AutomationData.class); return ctor.newInstance(data); } else { throw new IllegalArgumentException( "no such strategy: " + strategy); } } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } public boolean hasAutomator(String strategy) { return automators.containsKey(strategy); } public Set getAvailableStrategies() { return Collections.unmodifiableSet(automators.keySet()); } public boolean isNonDeterministic(String strategy) { if (hasAutomator(strategy)) { Class clazz = automators.get(strategy); try { Method method = clazz.getMethod("isNonDeterministic"); return (Boolean) method.invoke(null); } catch (SecurityException e) { return false; } catch (NoSuchMethodException e) { return false; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } return false; } public static AutomatorFactory getInstance() { if (instance == null) { instance = new AutomatorFactory(); } return instance; } }