Skip to content

Commit

Permalink
feat test getDAI v2
Browse files Browse the repository at this point in the history
Signed-off-by: Samir Romdhani <[email protected]>
  • Loading branch information
samirromdhani committed Feb 5, 2025
1 parent 19c0efb commit 8c35c14
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

package org.lfenergy.compas.sct.commons;

import org.apache.commons.lang3.StringUtils;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.api.SclElementsProvider;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDaFilter;
import org.lfenergy.compas.sct.commons.dto.*;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
Expand All @@ -15,9 +18,12 @@
import org.lfenergy.compas.sct.commons.scl.ied.*;
import org.lfenergy.compas.sct.commons.scl.ldevice.LDeviceAdapter;
import org.lfenergy.compas.sct.commons.scl.ln.AbstractLNAdapter;
import org.lfenergy.compas.sct.commons.util.Utils;

import java.util.*;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SclElementsProviderService implements SclElementsProvider {

Expand Down Expand Up @@ -91,6 +97,7 @@ public List<ControlBlock> getExtRefSourceInfo(SCL scd, ExtRefInfo extRefInfo) th
.toList();
}


@Override
public Set<DataAttributeRef> getDAI(SCL scd, String iedName, String ldInst, DataAttributeRef dataAttributeRef, boolean updatable) throws ScdException {
SclRootAdapter sclRootAdapter = new SclRootAdapter(scd);
Expand All @@ -99,6 +106,36 @@ public Set<DataAttributeRef> getDAI(SCL scd, String iedName, String ldInst, Data
return lDeviceAdapter.getDAI(dataAttributeRef, updatable);
}

@Override
public Set<DoLinkedToDa> getDAIV2(SCL scd, String iedName, String ldInst, DataAttributeRef dataAttributeRef, boolean updatable) throws ScdException {
IedService iedService = new IedService();
LnService lnService = new LnService();
LdeviceService ldeviceService = new LdeviceService(lnService);
DataTypeTemplatesService dataTypeTemplatesService = new DataTypeTemplatesService();
return iedService.findIed(scd, tied -> tied.getName().equals(iedName))
.map(tied1 -> ldeviceService.findLdevice(tied1, tlDevice -> tlDevice.getInst().equals(ldInst))
.map(tlDevice -> Stream.concat(tlDevice.getLN().stream(), Stream.of(tlDevice.getLN0()))
.filter(anyLN -> tAnyLNPredicate.test(anyLN, dataAttributeRef))
.flatMap(tAnyLN -> dataTypeTemplatesService.getFilteredDoLinkedToDa(scd.getDataTypeTemplates(), tAnyLN.getLnType(), DoLinkedToDaFilter.from(dataAttributeRef))
.map(dataAttribute -> {
lnService.completeFromDAInstance(tied1, tlDevice.getInst(), tAnyLN, dataAttribute);
return dataAttribute;
}))
.filter(dataRef -> !updatable || dataRef.isUpdatable())
.collect(Collectors.toSet()))
.orElseThrow(() -> new ScdException(String.format("LDevice.inst '%s' not found in IED '%s'", ldInst, iedName))))
.orElseThrow(() -> new ScdException(String.format("IED.name '%s' not found in SCD", iedName)));
}

private final BiPredicate<TAnyLN, DataAttributeRef> tAnyLNPredicate = (anyLN, dataRef) ->
StringUtils.isBlank(dataRef.getLnClass())
|| (anyLN instanceof TLN0
&& (dataRef.getLnClass() != null && dataRef.getLnClass().equals(TLLN0Enum.LLN_0.value())))
|| (anyLN instanceof TLN ln
&& Utils.lnClassEquals(ln.getLnClass(), dataRef.getLnClass())
&& ln.getInst().equals(dataRef.getLnInst())
&& Utils.equalsOrBothBlank(dataRef.getPrefix(), ln.getPrefix()));

@Override
public Set<EnumValDTO> getEnumTypeValues(SCL scd, String idEnum) throws ScdException {
SclRootAdapter sclRootAdapter = new SclRootAdapter(scd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package org.lfenergy.compas.sct.commons.api;

import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDaFilter;
import org.lfenergy.compas.sct.commons.dto.*;
import org.lfenergy.compas.sct.commons.exception.ScdException;

Expand Down Expand Up @@ -95,6 +97,7 @@ public interface SclElementsProvider {
* @throws ScdException SCD illegal arguments exception, missing mandatory data
*/
Set<DataAttributeRef> getDAI(SCL scd, String iedName, String ldInst, DataAttributeRef dataAttributeRef, boolean updatable) throws ScdException;
Set<DoLinkedToDa> getDAIV2(SCL scd, String iedName, String ldInst, DataAttributeRef dataAttributeRef, boolean updatable) throws ScdException;

/**
* Gets EnumTypes values of ID <em>idEnum</em> from DataTypeTemplate of SCL file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@


import org.apache.commons.lang3.StringUtils;
import org.lfenergy.compas.scl2007b4.model.TAnyLN;
import org.lfenergy.compas.sct.commons.dto.DataAttributeRef;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -36,6 +38,23 @@ public static DoLinkedToDaFilter from(String doNames, String daNames) {
return new DoLinkedToDaFilter(doName, sdoNames, daName, bdaNames);
}


public static DoLinkedToDaFilter from(DataAttributeRef dataAttributeRef) {
String doName = null;
List<String> sdoNames = null;
String daName = null;
List<String> bdaNames = null;
if (StringUtils.isNotBlank(dataAttributeRef.getDoRef())) {
doName = dataAttributeRef.getDoRef().split("\\.")[0];
sdoNames = Arrays.stream(dataAttributeRef.getDoRef().split("\\.")).skip(1).toList();
}
if (StringUtils.isNotBlank(dataAttributeRef.getDaRef())) {
daName = dataAttributeRef.getDaRef().split("\\.")[0];
bdaNames = Arrays.stream(dataAttributeRef.getDaRef().split("\\.")).skip(1).toList();
}
return new DoLinkedToDaFilter(doName, sdoNames, daName, bdaNames);
}

public String getDoRef() {
return doName + (sdoNames().isEmpty() ? StringUtils.EMPTY : "." + String.join(".", sdoNames()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;
import org.lfenergy.compas.sct.commons.dto.*;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
Expand Down Expand Up @@ -145,6 +146,34 @@ void getDAI_should_return_all_dai_when_notUpdatable() {
assertThat(result).hasSize(2433);
}

@Test
void test_dai_multiple_value_sourceDataTypeTemplateAndLN() {
// Given
SCL scd = SclTestMarshaller.getSCLFromFile("/scl-srv-import-ieds/dai_multiple_value_test.xml");
// When
Set<DataAttributeRef> resultV1 = sclElementsProviderService.getDAI(scd, "IED_NAME1", "LD_INST11", new DataAttributeRef(), false);
Set<DoLinkedToDa> resultV2 = sclElementsProviderService.getDAIV2(scd, "IED_NAME1", "LD_INST11", new DataAttributeRef(), false);
// THEN
assertThat(resultV1).hasSize(1622);
assertThat(resultV2).hasSize(1622);

Set<DataAttributeRef> resultV1_OnlyOneValue = resultV1.stream().filter(dataAttributeRef -> dataAttributeRef.getDaName().getDaiValues().size() == 1)
.collect(Collectors.toSet());
assertThat(resultV1_OnlyOneValue).hasSize(2);

Set<DataAttributeRef> resultV1_OnlyTWOValue = resultV1.stream().filter(dataAttributeRef -> dataAttributeRef.getDaName().getDaiValues().size() == 2)
.collect(Collectors.toSet());
assertThat(resultV1_OnlyTWOValue).hasSize(0);

Set<DoLinkedToDa> resultV2_OnlyOneValue = resultV2.stream().filter(doLinkedToDa -> doLinkedToDa.dataAttribute().getDaiValues().size() == 1)
.collect(Collectors.toSet());
assertThat(resultV2_OnlyOneValue).hasSize(1);// expected 2: not same as resultV1_OnlyOneValue

Set<DoLinkedToDa> resultV2_OnlyTWOValue = resultV2.stream().filter(doLinkedToDa -> doLinkedToDa.dataAttribute().getDaiValues().size() == 2)
.collect(Collectors.toSet());
assertThat(resultV2_OnlyTWOValue).hasSize(1);// expected 0: not same as resultV1_OnlyOneValue
}

@Test
void getDAI_should_return_all_dai_when_updatable() {
// given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!-- SPDX-FileCopyrightText: 2021 2025 RTE FRANCE -->
<!-- -->
<!-- SPDX-License-Identifier: Apache-2.0 -->

<SCL xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
<Header id="HeaderID" version="version" revision="Revision" toolID="toolID"/>
<IED name="IED_NAME1">
<AccessPoint name="AP_NAME">
<Server>
<Authentication/>
<LDevice inst="LD_INST11"> <!-- binder for signal from LD_INST11/LLNO -->
<LN0 lnType="LN1" lnClass="LLN0" inst=""/>
<LN lnType="LN2" lnClass="ANCR" inst="1">
<DOI name="Do21">
<DAI name="da1" valImport="true">
<Val>BBBB</Val>
</DAI>
</DOI>
</LN>
</LDevice>
</Server>
</AccessPoint>
</IED>
<DataTypeTemplates>
<LNodeType id="LN1" lnClass="LLN0">
<DO name="Do11" type="DO11" transient="true"/>
</LNodeType>
<LNodeType id="LN2" lnClass="ANCR">
<DO name="Do21" type="DO11"/>
</LNodeType>
<DOType cdc="WYE" id="DO11">
<SDO name="sdo11" type="DO12"/>
<DA name="da1" bType="Enum" fc="ST" type="RecCycModKind" valImport="true">
<Val>AAAA</Val>
</DA>
<SDO name="sdo21" type="DO12"/>
<SDO name="sdo31" type="DO12"/>
</DOType>
<DOType cdc="WYE" id="DO12">
<SDO name="sdo12" type="DO13"/>
<SDO name="sdo22" type="DO13"/>
<SDO name="sdo32" type="DO13"/>
</DOType>
<DOType cdc="WYE" id="DO13">
<DA fc="ST" name="da11" bType="Struct" type="DA11"/>
<DA fc="ST" name="da22" bType="Struct" type="DA11"/>
<DA fc="ST" name="da32" bType="Struct" type="DA11"/>
</DOType>
<DAType id="DA11">
<BDA name="bda111" bType="Struct" type="DA12"/>
<BDA name="bda221" bType="Struct" type="DA12"/>
<BDA name="bda321" bType="Struct" type="DA12"/>
</DAType>
<DAType id="DA12">
<BDA name="bdapr" bType="VisString255"/>
<BDA name="bda112" bType="Struct" type="DA13"/>
<BDA name="bda222" bType="Struct" type="DA13"/>
<BDA name="bda322" bType="Struct" type="DA13"/>
</DAType>
<DAType id="DA13">
<BDA name="bda113" bType="Enum" type="RecCycModKind" valImport="true"/>
<BDA name="bda223" bType="Enum" type="RecCycModKind"/>
<BDA name="bda323" bType="Enum" type="RecCycModKind"/>
</DAType>
<EnumType id="RecCycModKind">
<EnumVal ord="1">Completed-diff</EnumVal>
</EnumType>
</DataTypeTemplates>
</SCL>

0 comments on commit 8c35c14

Please sign in to comment.