Skip to content

Commit

Permalink
Add network events to IIDM API (#3203)
Browse files Browse the repository at this point in the history
* Add network events to IIDM API
* Add unit tests
* Clean all deprecated methods and remame element to property

Signed-off-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
geofjamg authored Nov 12, 2024
1 parent f42d18b commit b49d67d
Show file tree
Hide file tree
Showing 34 changed files with 625 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public void afterRemoval(String identifiable) {
// empty default implementation
}

@Override
public void onUpdate(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
// empty default implementation
}

@Override
public void onUpdate(Identifiable<?> identifiable, String attribute, String variantId, Object oldValue, Object newValue) {
// empty default implementation
Expand All @@ -44,7 +39,7 @@ public void onExtensionCreation(Extension<?> extension) {
}

@Override
public void onExtensionUpdate(Extension<?> extension, String attribute, Object oldValue, Object newValue) {
public void onExtensionUpdate(Extension<?> extension, String attribute, String variantId, Object oldValue, Object newValue) {
// empty default implementation
}

Expand All @@ -57,4 +52,34 @@ public void onExtensionBeforeRemoval(Extension<?> extension) {
public void onExtensionAfterRemoval(Identifiable<?> identifiable, String extensionName) {
// empty default implementation
}

@Override
public void onPropertyAdded(Identifiable<?> identifiable, String key, Object newValue) {
// empty default implementation
}

@Override
public void onPropertyReplaced(Identifiable<?> identifiable, String key, Object oldValue, Object newValue) {
// empty default implementation
}

@Override
public void onPropertyRemoved(Identifiable<?> identifiable, String key, Object oldValue) {
// empty default implementation
}

@Override
public void onVariantCreated(String sourceVariantId, String targetVariantId) {
// empty default implementation
}

@Override
public void onVariantOverwritten(String sourceVariantId, String targetVariantId) {
// empty default implementation
}

@Override
public void onVariantRemoved(String variantId) {
// empty default implementation
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network;

import com.powsybl.commons.extensions.Extension;
import com.powsybl.iidm.network.events.*;

import java.util.ArrayList;
import java.util.List;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public class NetworkEventRecorder implements NetworkListener {

private final List<NetworkEvent> events = new ArrayList<>();

public List<NetworkEvent> getEvents() {
return events;
}

public void reset() {
events.clear();
}

@Override
public void onCreation(Identifiable<?> identifiable) {
events.add(new CreationNetworkEvent(identifiable.getId()));
}

@Override
public void beforeRemoval(Identifiable<?> identifiable) {
events.add(new RemovalNetworkEvent(identifiable.getId(), false));
}

@Override
public void afterRemoval(String id) {
events.add(new RemovalNetworkEvent(id, true));
}

@Override
public void onUpdate(Identifiable<?> identifiable, String attribute, String variantId, Object oldValue, Object newValue) {
events.add(new UpdateNetworkEvent(identifiable.getId(), attribute, variantId, oldValue, newValue));
}

@Override
public void onExtensionCreation(Extension<?> extension) {
events.add(new ExtensionCreationNetworkEvent(((Identifiable<?>) extension.getExtendable()).getId(), extension.getName()));
}

@Override
public void onExtensionAfterRemoval(Identifiable<?> identifiable, String extensionName) {
events.add(new ExtensionRemovalNetworkEvent(identifiable.getId(), extensionName, true));
}

@Override
public void onExtensionBeforeRemoval(Extension<?> extension) {
events.add(new ExtensionRemovalNetworkEvent(((Identifiable<?>) extension.getExtendable()).getId(), extension.getName(), false));
}

@Override
public void onExtensionUpdate(Extension<?> extension, String attribute, String variantId, Object oldValue, Object newValue) {
events.add(new ExtensionUpdateNetworkEvent(((Identifiable<?>) extension.getExtendable()).getId(), extension.getName(), attribute, variantId, oldValue, newValue));
}

@Override
public void onPropertyAdded(Identifiable<?> identifiable, String key, Object newValue) {
events.add(new PropertiesUpdateNetworkEvent(identifiable.getId(), key, PropertiesUpdateNetworkEvent.PropertyUpdateType.ADDED, null, newValue));
}

@Override
public void onPropertyReplaced(Identifiable<?> identifiable, String key, Object oldValue, Object newValue) {
events.add(new PropertiesUpdateNetworkEvent(identifiable.getId(), key, PropertiesUpdateNetworkEvent.PropertyUpdateType.REPLACED, oldValue, newValue));
}

@Override
public void onPropertyRemoved(Identifiable<?> identifiable, String key, Object oldValue) {
events.add(new PropertiesUpdateNetworkEvent(identifiable.getId(), key, PropertiesUpdateNetworkEvent.PropertyUpdateType.REMOVED, oldValue, null));
}

@Override
public void onVariantCreated(String sourceVariantId, String targetVariantId) {
events.add(new VariantNetworkEvent(sourceVariantId, targetVariantId, VariantNetworkEvent.VariantEventType.CREATED));
}

@Override
public void onVariantOverwritten(String sourceVariantId, String targetVariantId) {
events.add(new VariantNetworkEvent(sourceVariantId, targetVariantId, VariantNetworkEvent.VariantEventType.OVERWRITTEN));
}

@Override
public void onVariantRemoved(String variantId) {
events.add(new VariantNetworkEvent(variantId, null, VariantNetworkEvent.VariantEventType.REMOVED));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public interface NetworkListener {

void afterRemoval(String id);

void onUpdate(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue);

void onUpdate(Identifiable<?> identifiable, String attribute, String variantId, Object oldValue, Object newValue);

void onExtensionCreation(Extension<?> extension);
Expand All @@ -30,29 +28,17 @@ public interface NetworkListener {

void onExtensionBeforeRemoval(Extension<?> extension);

void onExtensionUpdate(Extension<?> extendable, String attribute, Object oldValue, Object newValue);
void onExtensionUpdate(Extension<?> extendable, String attribute, String variantId, Object oldValue, Object newValue);

default void onElementAdded(Identifiable<?> identifiable, String attribute, Object newValue) {
// empty default implementation
}
void onPropertyAdded(Identifiable<?> identifiable, String key, Object newValue);

default void onElementReplaced(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
// empty default implementation
}
void onPropertyReplaced(Identifiable<?> identifiable, String key, Object oldValue, Object newValue);

default void onElementRemoved(Identifiable<?> identifiable, String attribute, Object oldValue) {
// empty default implementation
}
void onPropertyRemoved(Identifiable<?> identifiable, String key, Object oldValue);

default void onVariantCreated(String sourceVariantId, String targetVariantId) {
// empty default implementation
}
void onVariantCreated(String sourceVariantId, String targetVariantId);

default void onVariantOverwritten(String sourceVariantId, String targetVariantId) {
// empty default implementation
}
void onVariantOverwritten(String sourceVariantId, String targetVariantId);

default void onVariantRemoved(String variantId) {
// empty default implementation
}
void onVariantRemoved(String variantId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network.events;

import java.util.Objects;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public record CreationNetworkEvent(String id) implements NetworkEvent {
public CreationNetworkEvent {
Objects.requireNonNull(id);
}

@Override
public Type getType() {
return Type.CREATION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network.events;

import java.util.Objects;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public record ExtensionCreationNetworkEvent(String id, String extensionName) implements NetworkEvent {
public ExtensionCreationNetworkEvent {
Objects.requireNonNull(id);
Objects.requireNonNull(extensionName);
}

@Override
public Type getType() {
return Type.EXTENSION_CREATION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network.events;

import java.util.Objects;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public record ExtensionRemovalNetworkEvent(String id, String extensionName, boolean after) implements NetworkEvent {
public ExtensionRemovalNetworkEvent {
Objects.requireNonNull(id);
Objects.requireNonNull(extensionName);
}

@Override
public Type getType() {
return Type.EXTENSION_REMOVAL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network.events;

import java.util.Objects;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public record ExtensionUpdateNetworkEvent(String id, String extensionName, String attribute, String variantId, Object oldValue, Object newValue) implements NetworkEvent {
public ExtensionUpdateNetworkEvent {
Objects.requireNonNull(id);
Objects.requireNonNull(extensionName);
Objects.requireNonNull(attribute);
}

@Override
public Type getType() {
return Type.EXTENSION_UPDATE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network.events;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public interface NetworkEvent {
enum Type {
CREATION,
REMOVAL,
UPDATE,
PROPERTIES_UPDATE,
EXTENSION_CREATION,
EXTENSION_REMOVAL,
EXTENSION_UPDATE,
VARIANT,
}

Type getType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network.events;

import java.util.Objects;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public record PropertiesUpdateNetworkEvent(String id, String key, PropertyUpdateType updateType, Object oldValue, Object newValue) implements NetworkEvent {

public enum PropertyUpdateType {
ADDED,
REMOVED,
REPLACED;
}

public PropertiesUpdateNetworkEvent {
Objects.requireNonNull(id);
Objects.requireNonNull(key);
Objects.requireNonNull(updateType);
}

@Override
public Type getType() {
return Type.PROPERTIES_UPDATE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network.events;

import java.util.Objects;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public record RemovalNetworkEvent(String id, boolean after) implements NetworkEvent {
public RemovalNetworkEvent {
Objects.requireNonNull(id);
}

@Override
public Type getType() {
return Type.REMOVAL;
}
}
Loading

0 comments on commit b49d67d

Please sign in to comment.