NamedRuleUnitConfig { public static List<NamedRuleUnitConfig> fromContext(GeneratorContext context) { HashSet<String> canonicalNames = new HashSet<>(); for (String k : context.getApplicationProperties()) { if (k.startsWith(CONFIG_PREFIX)) { String rest = k.substring(CONFIG_PREFIX.length()); Optional<String> unitCanonicalName = parseQuotedIdentifier(rest); unitCanonicalName.ifPresent(canonicalNames::add); } } ArrayList<NamedRuleUnitConfig> configs = new ArrayList<>(); for (String canonicalName : canonicalNames) { EventProcessingType eventProcessingType = context.getApplicationProperty( String.format(CONFIG_EVENT_PROCESSING_TYPE, canonicalName)) .map(String::toUpperCase) .map(EventProcessingType::valueOf) .orElse(null); ClockType clockType = context.getApplicationProperty( String.format(CONFIG_CLOCK_TYPE, canonicalName)) .map(String::toUpperCase) .map(ClockType::valueOf) .orElse(null); Optional<String> sp = context.getApplicationProperty( String.format(CONFIG_SESSIONS_POOL, canonicalName)); Integer sessionPool = sp.map(Integer::parseInt).orElse(null); configs.add(new NamedRuleUnitConfig( canonicalName, new RuleUnitConfig( eventProcessingType, clockType, sessionPool))); } return configs; } NamedRuleUnitConfig(String canonicalName, RuleUnitConfig config); static List<NamedRuleUnitConfig> fromContext(GeneratorContext context); String getCanonicalName(); RuleUnitConfig getConfig();  }
@Test public void multiUnit() { Properties properties = new Properties(); properties.put("kogito.rules.some.other.config", "ignore me"); properties.put("kogito.rules.\"my.rule.Unit\".event-processing-type", EventProcessingType.CLOUD.name()); properties.put("kogito.rules.\"my.rule.Unit\".clock-type", ClockType.PSEUDO.name()); properties.put("kogito.rules.\"my.rule.Unit\".sessions-pool", "10"); properties.put("kogito.rules.\"my.rule.Unit2\".event-processing-type", EventProcessingType.STREAM.name()); GeneratorContext context = GeneratorContext.ofProperties(properties); List<NamedRuleUnitConfig> namedRuleUnitConfigs = NamedRuleUnitConfig.fromContext(context); assertThat(namedRuleUnitConfigs).hasSize(2); Map<String, RuleUnitConfig> map = namedRuleUnitConfigs.stream() .collect(toMap(NamedRuleUnitConfig::getCanonicalName, NamedRuleUnitConfig::getConfig)); RuleUnitConfig myRuleUnitConfig = map.get("my.rule.Unit"); assertThat(myRuleUnitConfig).isNotNull(); assertThat(myRuleUnitConfig.getDefaultedEventProcessingType()).isEqualTo(EventProcessingType.CLOUD); assertThat(myRuleUnitConfig.getDefaultedClockType()).isEqualTo(ClockType.PSEUDO); assertThat(myRuleUnitConfig.getSessionPool().getAsInt()).isEqualTo(10); RuleUnitConfig myRuleUnit2Config = map.get("my.rule.Unit2"); assertThat(myRuleUnit2Config).isNotNull(); assertThat(myRuleUnit2Config.getDefaultedEventProcessingType()).isEqualTo(EventProcessingType.STREAM); assertThat(myRuleUnit2Config.getDefaultedClockType()).isEqualTo(ClockType.REALTIME); assertThat(myRuleUnit2Config.getSessionPool()).isEmpty(); }
@Test public void unbalancedParentheses() { Properties properties = new Properties(); properties.put("kogito.rules.some.other.config", "ignore me"); properties.put("kogito.rules.\"my.rule.Unit", EventProcessingType.CLOUD.name()); GeneratorContext context = GeneratorContext.ofProperties(properties); List<NamedRuleUnitConfig> namedRuleUnitConfigs = NamedRuleUnitConfig.fromContext(context); assertThat(namedRuleUnitConfigs).isEmpty(); }