Skip to content

Commit

Permalink
feat : Follow up - deconstructing compas-flow
Browse files Browse the repository at this point in the history
Signed-off-by: massifben <[email protected]>
  • Loading branch information
massifben committed Dec 13, 2024
1 parent e411dcb commit 704a8d7
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

package org.lfenergy.compas.sct.commons.scl;

import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.util.PrivateUtils;
import org.lfenergy.compas.scl2007b4.model.TExtRef;
import org.lfenergy.compas.scl2007b4.model.TInputs;
import org.lfenergy.compas.scl2007b4.model.TLDevice;
import org.lfenergy.compas.scl2007b4.model.TLLN0Enum;
import org.lfenergy.compas.sct.commons.util.Utils;

import java.util.ArrayList;
Expand All @@ -30,28 +32,6 @@ public Stream<TExtRef> getExtRefs(TLDevice tlDevice) {
.flatMap(tInputs -> tInputs.getExtRef().stream());
}

/**
* List all CompasFlows in this LDevice
*
* @return list of ExtRefs. List is modifiable.
*/
public Stream<TCompasFlow> getCompasFlows(TLDevice tlDevice) {
return getInputs(tlDevice).stream()
.flatMap(tInputs -> PrivateUtils.extractCompasPrivates(tlDevice.getLN0().getInputs(), TCompasFlow.class));
}

/**
* Find CompasFlows that match given ExtRef
*
* @param inputs inputs containing Privates CompasFlow and TExtRefs
* @param tExtRef corresponding to CompasFlow we are searching
* @return list of matching CompasFlows
*/
public Stream<TCompasFlow> getMatchingCompasFlows(TInputs inputs, TExtRef tExtRef) {
return PrivateUtils.extractCompasPrivates(inputs, TCompasFlow.class)
.filter(compasFlow -> isMatchingExtRef(compasFlow, tExtRef));
}

/**
* Debind ExtRef
*
Expand All @@ -73,25 +53,6 @@ public void clearExtRefBinding(TExtRef extRef) {
extRef.unsetSrcLNClass();
}


/**
* Check if extRef matches CompasFlow
*
* @param compasFlow compasFlow
* @param extRef extRef
* @return true if all required attributes matches. Note that empty string, whitespaces only string and null values are considered as matching
* (missing attributes matches attribute with empty string value or whitespaces only). Return false otherwise.
*/
private boolean isMatchingExtRef(TCompasFlow compasFlow, TExtRef extRef) {
String extRefLnClass = extRef.isSetLnClass() ? extRef.getLnClass().get(0) : null;
return Utils.equalsOrBothBlank(compasFlow.getDataStreamKey(), extRef.getDesc())
&& Utils.equalsOrBothBlank(compasFlow.getExtRefiedName(), extRef.getIedName())
&& Utils.equalsOrBothBlank(compasFlow.getExtRefldinst(), extRef.getLdInst())
&& Utils.equalsOrBothBlank(compasFlow.getExtRefprefix(), extRef.getPrefix())
&& Utils.equalsOrBothBlank(compasFlow.getExtReflnClass(), extRefLnClass)
&& Utils.equalsOrBothBlank(compasFlow.getExtReflnInst(), extRef.getLnInst());
}

/**
* Checks if two ExtRefs fed by same Control Block
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public enum PrivateEnum {

COMPAS_BAY("COMPAS-Bay", TCompasBay.class),
COMPAS_CRITERIA("COMPAS-Criteria", TCompasCriteria.class),
COMPAS_FLOW("COMPAS-Flow", TCompasFlow.class),
COMPAS_FUNCTION("COMPAS-Function", TCompasFunction.class),
COMPAS_ICDHEADER("COMPAS-ICDHeader", TCompasICDHeader.class),
COMPAS_LDEVICE("COMPAS-LDevice", TCompasLDevice.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

package org.lfenergy.compas.sct.commons.scl;

import org.assertj.core.groups.Tuple;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.util.PrivateEnum;

import java.util.List;
import java.util.stream.Stream;
Expand Down Expand Up @@ -68,40 +66,6 @@ private static Stream<Arguments> provideLDevices() {
);
}

@Test
void getCompasFlows_should_return_compasFlow() {
// Given
TLDevice tlDevice = new TLDevice();
tlDevice.setLN0(new LN0());
TInputs tInputs = new TInputs();
tlDevice.getLN0().setInputs(tInputs);
TPrivate tPrivate = new TPrivate();
tPrivate.setType(PrivateEnum.COMPAS_FLOW.getPrivateType());
tPrivate.getContent().add(new ObjectFactory().createFlow(new TCompasFlow()));
tInputs.getPrivate().add(tPrivate);
// When
Stream<TCompasFlow> result = extRefService.getCompasFlows(tlDevice);
// Then
assertThat(result).hasSize(1);
}

@Test
void getMatchingCompasFlows_should_succeed() {
//Given
TInputs tInputs = new TInputs();
TExtRef tExtRef1 = createExtRef("Desc_1", "IED_Name_1", "LD_INST_1");
tInputs.getExtRef().add(tExtRef1);
TCompasFlow tCompasFlow1 = createCompasFlow("Desc_1", "IED_Name_1", "LD_INST_1");
TCompasFlow tCompasFlow2 = createCompasFlow("Desc_2", "IED_Name_2", "LD_INST_2");
tInputs.getPrivate().add(createPrivateCompasFlow(List.of(tCompasFlow1, tCompasFlow2)));
//When
Stream<TCompasFlow> matchingCompasFlows = extRefService.getMatchingCompasFlows(tInputs, tExtRef1);
//Then
assertThat(matchingCompasFlows).hasSize(1)
.map(TCompasFlow::getDataStreamKey, TCompasFlow::getExtRefiedName)
.containsExactly(Tuple.tuple("Desc_1", "IED_Name_1"));
}

@Test
void clearExtRefBinding_should_remove_binding() {
//Given
Expand Down Expand Up @@ -211,13 +175,6 @@ void filterDuplicatedExtRefs_should_not_remove_not_duplicated_extrefs() {
.hasSize(6);
}

private static TPrivate createPrivateCompasFlow(List<TCompasFlow> compasFlows) {
TPrivate tPrivate = new TPrivate();
tPrivate.setType(PrivateEnum.COMPAS_FLOW.getPrivateType());
tPrivate.getContent().addAll(compasFlows.stream().map(value -> new ObjectFactory().createFlow(value)).toList());
return tPrivate;
}

private TExtRef createExtRef(String desc, String iedName, String ldInst) {
TExtRef tExtRef1 = new TExtRef();
tExtRef1.setDesc(desc);
Expand All @@ -228,14 +185,4 @@ private TExtRef createExtRef(String desc, String iedName, String ldInst) {
return tExtRef1;
}

private TCompasFlow createCompasFlow(String dataStreamKey, String extRefIedName, String extRefLdInst) {
TCompasFlow tCompasFlow = new TCompasFlow();
tCompasFlow.setDataStreamKey(dataStreamKey);
tCompasFlow.setExtRefiedName(extRefIedName);
tCompasFlow.setExtRefldinst(extRefLdInst);
tCompasFlow.setExtReflnClass("LN");
tCompasFlow.setExtReflnInst("1");
return tCompasFlow;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.lfenergy.compas.sct.commons.scl.ln.LN0Adapter;
import org.lfenergy.compas.sct.commons.scl.ln.LNAdapter;
import org.lfenergy.compas.sct.commons.util.ControlBlockEnum;
import org.lfenergy.compas.sct.commons.util.PrivateUtils;
import org.lfenergy.compas.sct.commons.util.Utils;
import org.opentest4j.AssertionFailedError;

Expand Down Expand Up @@ -74,13 +73,6 @@ public static TExtRef findExtRef(SCL scl, String iedName, String ldInst, String
.orElseThrow(() -> new AssertionFailedError(String.format("ExtRef.desc=%s not found in IED.name=%s,LDevice.inst=%s", extRefDesc, iedName, ldInst)));
}

public static TCompasFlow findCompasFlow(SCL scl, String iedName, String ldInst, String compasFlowDataStreamKey) {
return PrivateUtils.extractCompasPrivates(findInputs(scl, iedName, ldInst).getCurrentElem(), TCompasFlow.class)
.filter(compasFlow -> compasFlowDataStreamKey.equals(compasFlow.getDataStreamKey()))
.findFirst()
.orElseThrow(() -> new AssertionFailedError(String.format("CompasFlow.dataStreamKey=%s not found in IED.name=%s,LDevice.inst=%s", compasFlowDataStreamKey, iedName, ldInst)));
}

public static LN0Adapter findLn0(SCL scl, String iedName, String ldInst) {
LDeviceAdapter lDevice = findLDevice(scl, iedName, ldInst);
if (!lDevice.hasLN0()) {
Expand Down Expand Up @@ -170,14 +162,6 @@ public static DataSetAdapter findDataSet(SCL scl, String iedName, String ldInst,
dataSetName, iedName, ldInst)));
}

public static ControlBlockAdapter findControlBlock(SCL scl, String iedName, String ldInst, String cbName,
ControlBlockEnum controlBlockEnum) {
LN0Adapter ln0 = findLn0(scl, iedName, ldInst);
return ln0.findControlBlock(cbName, controlBlockEnum)
.orElseThrow(() -> new AssertionFailedError(String.format("%s name=%s not found in IED.name=%s,LDevice.inst=%s,LN0",
controlBlockEnum.getControlBlockClass().getSimpleName(), cbName, iedName, ldInst)));
}

public static <T extends TControl> T findControlBlock(SCL scl, String iedName, String ldInst, String cbName, Class<T> controlBlockClass) {
LN0Adapter ln0 = findLn0(scl, iedName, ldInst);
return ln0.getTControlsByType(controlBlockClass).stream()
Expand Down Expand Up @@ -213,12 +197,6 @@ public static Stream<TDataSet> streamAllDataSets(SCL scl) {
.flatMap(List::stream);
}

public static <T extends TControl> Stream<T> streamAllControlBlocks(SCL scl, Class<T> controlBlockClass) {
return streamAllLn0Adapters(scl)
.map(ln0Adapter -> ln0Adapter.getTControlsByType(controlBlockClass))
.flatMap(List::stream);
}

public static Stream<LN0Adapter> streamAllLn0Adapters(SCL scl) {
return new SclRootAdapter(scl)
.streamIEDAdapters()
Expand Down Expand Up @@ -253,16 +231,6 @@ public static Stream<String> streamAllConnectedApGseP(SCL scd, String pType) {
.map(TP::getValue);
}

public static TConnectedAP addConnectedAp(SCL scd, String subNetworkName, String apName, String iedName) {
scd.setCommunication(new TCommunication());
TSubNetwork subNetwork = new TSubNetwork();
subNetwork.setName(subNetworkName);
scd.getCommunication().getSubNetwork().add(subNetwork);
TConnectedAP connectedAP = newConnectedAp(iedName, apName);
subNetwork.getConnectedAP().add(connectedAP);
return connectedAP;
}

public static SclRootAdapter createSclRootAdapterWithIed(String iedName) {
SCL scl = new SCL();
scl.setHeader(new THeader());
Expand Down

0 comments on commit 704a8d7

Please sign in to comment.