@Test public void testParseIf() throws Exception { Function f = FunctionFactory.parse(new ConfigThingy("IF", "\"true\" THEN \"then\" ELSE \"else\""), funcLib, dialogLib, context); assertTrue(f instanceof IfFunction); f = FunctionFactory.parse(new ConfigThingy("IF", "THEN \"then\" ELSE \"else\" \"true\""), funcLib, dialogLib, context); assertTrue(f instanceof IfFunction); assertThrows(ConfigurationErrorException.class, () -> FunctionFactory .parse(new ConfigThingy("IF", "\"false\" THEN \"then\" THEN \"then\""), funcLib, dialogLib, context)); assertThrows(ConfigurationErrorException.class, () -> FunctionFactory .parse(new ConfigThingy("IF", "\"true\" ELSE \"else\" ELSE \"else\""), funcLib, dialogLib, context)); assertThrows(ConfigurationErrorException.class, () -> FunctionFactory .parse(new ConfigThingy("IF", "THEN \"else\" ELSE \"else\""), funcLib, dialogLib, context)); }
private static Function parseIF(ConfigThingy conf, FunctionLibrary funcLib, DialogLibrary dialogLib, Map<Object, Object> context) { ConfigThingy thenConf = conf.query("THEN"); ConfigThingy elseConf = conf.query("ELSE"); if (thenConf.count() > 1 || elseConf.count() > 1) throw new ConfigurationErrorException( L.m("In IF darf maximal ein THEN und ein ELSE vorhanden sein")); if (conf.count() - thenConf.count() - elseConf.count() != 1) throw new ConfigurationErrorException( L.m("IF muss genau eine Bedingung enthalten.")); if (thenConf.count() == 0) { thenConf = new ConfigThingy("dummy"); thenConf.add("THEN").add(""); } if (elseConf.count() == 0) { elseConf = new ConfigThingy("dummy"); elseConf.add("ELSE").add(""); } Iterator<ConfigThingy> iter = conf.iterator(); ConfigThingy condition; do { condition = iter.next(); } while (condition.getName().equals("THEN") || condition.getName().equals("ELSE")); Function ifFun = parse(condition, funcLib, dialogLib, context); Function thenFun = parseChildren(thenConf, funcLib, dialogLib, context); Function elseFun = parseChildren(elseConf, funcLib, dialogLib, context); return new IfFunction(ifFun, thenFun, elseFun); }
FunctionFactory { private static Function parseIF(ConfigThingy conf, FunctionLibrary funcLib, DialogLibrary dialogLib, Map<Object, Object> context) { ConfigThingy thenConf = conf.query("THEN"); ConfigThingy elseConf = conf.query("ELSE"); if (thenConf.count() > 1 || elseConf.count() > 1) throw new ConfigurationErrorException( L.m("In IF darf maximal ein THEN und ein ELSE vorhanden sein")); if (conf.count() - thenConf.count() - elseConf.count() != 1) throw new ConfigurationErrorException( L.m("IF muss genau eine Bedingung enthalten.")); if (thenConf.count() == 0) { thenConf = new ConfigThingy("dummy"); thenConf.add("THEN").add(""); } if (elseConf.count() == 0) { elseConf = new ConfigThingy("dummy"); elseConf.add("ELSE").add(""); } Iterator<ConfigThingy> iter = conf.iterator(); ConfigThingy condition; do { condition = iter.next(); } while (condition.getName().equals("THEN") || condition.getName().equals("ELSE")); Function ifFun = parse(condition, funcLib, dialogLib, context); Function thenFun = parseChildren(thenConf, funcLib, dialogLib, context); Function elseFun = parseChildren(elseConf, funcLib, dialogLib, context); return new IfFunction(ifFun, thenFun, elseFun); } }
FunctionFactory { private static Function parseIF(ConfigThingy conf, FunctionLibrary funcLib, DialogLibrary dialogLib, Map<Object, Object> context) { ConfigThingy thenConf = conf.query("THEN"); ConfigThingy elseConf = conf.query("ELSE"); if (thenConf.count() > 1 || elseConf.count() > 1) throw new ConfigurationErrorException( L.m("In IF darf maximal ein THEN und ein ELSE vorhanden sein")); if (conf.count() - thenConf.count() - elseConf.count() != 1) throw new ConfigurationErrorException( L.m("IF muss genau eine Bedingung enthalten.")); if (thenConf.count() == 0) { thenConf = new ConfigThingy("dummy"); thenConf.add("THEN").add(""); } if (elseConf.count() == 0) { elseConf = new ConfigThingy("dummy"); elseConf.add("ELSE").add(""); } Iterator<ConfigThingy> iter = conf.iterator(); ConfigThingy condition; do { condition = iter.next(); } while (condition.getName().equals("THEN") || condition.getName().equals("ELSE")); Function ifFun = parse(condition, funcLib, dialogLib, context); Function thenFun = parseChildren(thenConf, funcLib, dialogLib, context); Function elseFun = parseChildren(elseConf, funcLib, dialogLib, context); return new IfFunction(ifFun, thenFun, elseFun); } private  FunctionFactory(); }
FunctionFactory { private static Function parseIF(ConfigThingy conf, FunctionLibrary funcLib, DialogLibrary dialogLib, Map<Object, Object> context) { ConfigThingy thenConf = conf.query("THEN"); ConfigThingy elseConf = conf.query("ELSE"); if (thenConf.count() > 1 || elseConf.count() > 1) throw new ConfigurationErrorException( L.m("In IF darf maximal ein THEN und ein ELSE vorhanden sein")); if (conf.count() - thenConf.count() - elseConf.count() != 1) throw new ConfigurationErrorException( L.m("IF muss genau eine Bedingung enthalten.")); if (thenConf.count() == 0) { thenConf = new ConfigThingy("dummy"); thenConf.add("THEN").add(""); } if (elseConf.count() == 0) { elseConf = new ConfigThingy("dummy"); elseConf.add("ELSE").add(""); } Iterator<ConfigThingy> iter = conf.iterator(); ConfigThingy condition; do { condition = iter.next(); } while (condition.getName().equals("THEN") || condition.getName().equals("ELSE")); Function ifFun = parse(condition, funcLib, dialogLib, context); Function thenFun = parseChildren(thenConf, funcLib, dialogLib, context); Function elseFun = parseChildren(elseConf, funcLib, dialogLib, context); return new IfFunction(ifFun, thenFun, elseFun); } private  FunctionFactory(); static Function alwaysTrueFunction(); static Function parseGrandchildren(ConfigThingy conf,
      FunctionLibrary funcLib, DialogLibrary dialogLib, Map<Object, Object> context); static Function parseChildren(ConfigThingy conf, FunctionLibrary funcLib,
      DialogLibrary dialogLib, Map<Object, Object> context); static Function parse(ConfigThingy conf, FunctionLibrary funcLib,
      DialogLibrary dialogLib, Map<Object, Object> context); static FunctionLibrary parseFunctions(ConfigThingy conf,
      DialogLibrary dialogLib, Map<Object, Object> context, FunctionLibrary baselib); static FunctionLibrary parseFunctions(FunctionLibrary funcs,
      ConfigThingy conf, String section, DialogLibrary dialogLib,
      Map<Object, Object> context); static Map<String, Function> parseTrafos(ConfigThingy trafoConf, String nodeName, FunctionLibrary funcLib, DialogLibrary dialogLib,
      Map<Object, Object> context); }
FunctionFactory { private static Function parseIF(ConfigThingy conf, FunctionLibrary funcLib, DialogLibrary dialogLib, Map<Object, Object> context) { ConfigThingy thenConf = conf.query("THEN"); ConfigThingy elseConf = conf.query("ELSE"); if (thenConf.count() > 1 || elseConf.count() > 1) throw new ConfigurationErrorException( L.m("In IF darf maximal ein THEN und ein ELSE vorhanden sein")); if (conf.count() - thenConf.count() - elseConf.count() != 1) throw new ConfigurationErrorException( L.m("IF muss genau eine Bedingung enthalten.")); if (thenConf.count() == 0) { thenConf = new ConfigThingy("dummy"); thenConf.add("THEN").add(""); } if (elseConf.count() == 0) { elseConf = new ConfigThingy("dummy"); elseConf.add("ELSE").add(""); } Iterator<ConfigThingy> iter = conf.iterator(); ConfigThingy condition; do { condition = iter.next(); } while (condition.getName().equals("THEN") || condition.getName().equals("ELSE")); Function ifFun = parse(condition, funcLib, dialogLib, context); Function thenFun = parseChildren(thenConf, funcLib, dialogLib, context); Function elseFun = parseChildren(elseConf, funcLib, dialogLib, context); return new IfFunction(ifFun, thenFun, elseFun); } private  FunctionFactory(); static Function alwaysTrueFunction(); static Function parseGrandchildren(ConfigThingy conf,
      FunctionLibrary funcLib, DialogLibrary dialogLib, Map<Object, Object> context); static Function parseChildren(ConfigThingy conf, FunctionLibrary funcLib,
      DialogLibrary dialogLib, Map<Object, Object> context); static Function parse(ConfigThingy conf, FunctionLibrary funcLib,
      DialogLibrary dialogLib, Map<Object, Object> context); static FunctionLibrary parseFunctions(ConfigThingy conf,
      DialogLibrary dialogLib, Map<Object, Object> context, FunctionLibrary baselib); static FunctionLibrary parseFunctions(FunctionLibrary funcs,
      ConfigThingy conf, String section, DialogLibrary dialogLib,
      Map<Object, Object> context); static Map<String, Function> parseTrafos(ConfigThingy trafoConf, String nodeName, FunctionLibrary funcLib, DialogLibrary dialogLib,
      Map<Object, Object> context);  }