GeneratorUtil { public static void connectGenerator(Generator g) { Terminal t = g.getTerminal(); t.connect(); if (g.isVoltageRegulatorOn()) { Bus bus = t.getBusView().getBus(); if (bus != null) { double targetV = bus.getGeneratorStream() .filter(g2 -> !g2.getId().equals(g.getId())) .findFirst().map(Generator::getTargetV).orElse(Double.NaN); if (!Double.isNaN(targetV)) { g.setTargetV(targetV); } else if (!Double.isNaN(bus.getV())) { g.setTargetV(bus.getV()); } } } } private  GeneratorUtil(); static void connectGenerator(Generator g);  }
@Test public void testConnectVoltageRegulatorOff() { g2.setVoltageRegulatorOn(false); GeneratorUtil.connectGenerator(g2); assertTrue(g2.getTerminal().isConnected()); assertEquals(22., g2.getTargetV(), 0.01); }
@Test public void testConnectVoltageRegulatorOnWithAlreadyConnectedGenerators() { g2.setVoltageRegulatorOn(true); GeneratorUtil.connectGenerator(g2); assertTrue(g2.getTerminal().isConnected()); assertEquals(33., g2.getTargetV(), 0.01); }
@Test public void testConnectVoltageRegulatorOnWithoutAlreadyConnectedGenerators() { g3.getTerminal().disconnect(); g2.setVoltageRegulatorOn(true); GeneratorUtil.connectGenerator(g2); assertTrue(g2.getTerminal().isConnected()); assertEquals(99., g2.getTargetV(), 0.01); }