Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
massifben committed Dec 13, 2024
1 parent 704a8d7 commit ac2c900
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.api.LnEditor;
import org.lfenergy.compas.sct.commons.domain.*;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;
import org.lfenergy.compas.sct.commons.util.ActiveStatus;

import java.util.ArrayList;
Expand Down Expand Up @@ -81,6 +82,7 @@ public Optional<ActiveStatus> getDaiModStval(TAnyLN tAnyLN) {
.map(TVal::getValue))
.map(ActiveStatus::fromValue);
}

public Stream<TAnyLN> getActiveLns(TLDevice tlDevice) {
LN0 ln0 = tlDevice.getLN0();
Stream<TLN> tlnStream = tlDevice.getLN()
Expand All @@ -99,7 +101,7 @@ public Optional<TDAI> getDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDaFilter doLi
return tAnyLN.getDOI().stream().filter(doi -> doi.getName().equals(doLinkedToDaFilter.doName()))
.findFirst()
.flatMap(doi -> {
if(structNamesList.size() > 1) {
if (structNamesList.size() > 1) {
String firstSDIName = structNamesList.removeFirst();
return this.getSdiByName(doi, firstSDIName)
.map(intermediateSdi -> findSDIByStructName(intermediateSdi, structNamesList))
Expand All @@ -116,7 +118,7 @@ public Optional<TDAI> getDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDaFilter doLi
return Optional.empty();
})
.stream().findFirst();
} else if(structNamesList.size() == 1){
} else if (structNamesList.size() == 1) {
return doi.getSDIOrDAI().stream()
.filter(unNaming -> unNaming.getClass().equals(TDAI.class))
.map(TDAI.class::cast)
Expand All @@ -132,13 +134,13 @@ public void updateOrCreateDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDa doLinkedT
createDoiSdiDaiChainIfNotExists(tAnyLN, doLinkedToDa.getDataObject(), doLinkedToDa.getDataAttribute())
.ifPresent(tdai -> {
List<DaVal> daiVals = doLinkedToDa.getDataAttribute().getDaiValues();
if(!hasSettingGroup(tdai) && daiVals.size() == 1 && daiVals.getFirst().settingGroup() == null) {
if (!hasSettingGroup(tdai) && daiVals.size() == 1 && daiVals.getFirst().settingGroup() == null) {
String value = daiVals.getFirst().val();
tdai.getVal().stream().findFirst()
.ifPresentOrElse(tVal -> tVal.setValue(value),
() -> tdai.getVal().add(newVal(value)));
} else {
for (DaVal daVal: daiVals) {
for (DaVal daVal : daiVals) {
tdai.getVal().stream()
.filter(tValElem -> tValElem.isSetSGroup() && tValElem.getSGroup() == daVal.settingGroup())
.findFirst()
Expand All @@ -152,11 +154,11 @@ public void updateOrCreateDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDa doLinkedT
public void completeFromDAInstance(TIED tied, String ldInst, TAnyLN anyLN, DoLinkedToDa doLinkedToDa) {
getDOAndDAInstances(anyLN, doLinkedToDa.toFilter())
.ifPresent(tdai -> {
if(tdai.isSetVal()) {
if (tdai.isSetVal()) {
doLinkedToDa.getDataAttribute().addDaVal(tdai.getVal());
}
if(doLinkedToDa.getDataAttribute().getFc() == TFCEnum.SG || doLinkedToDa.getDataAttribute().getFc() == TFCEnum.SE) {
if(hasSettingGroup(tdai)) {
if (doLinkedToDa.getDataAttribute().getFc() == TFCEnum.SG || doLinkedToDa.getDataAttribute().getFc() == TFCEnum.SE) {
if (hasSettingGroup(tdai)) {
boolean isIedHasConfSG = tied.isSetAccessPoint() &&
tied.getAccessPoint().stream()
.filter(tAccessPoint -> tAccessPoint.getServer() != null
Expand All @@ -170,8 +172,8 @@ public void completeFromDAInstance(TIED tied, String ldInst, TAnyLN anyLN, DoLin
} else {
log.warn(String.format("Inconsistency in the SCD file - DAI= %s with fc= %s must have a sGroup attribute", tdai.getName(), doLinkedToDa.getDataAttribute().getFc()));
doLinkedToDa.getDataAttribute().setValImport(false);
}
} else if(tdai.isSetValImport()) {
}
} else if (tdai.isSetValImport()) {
doLinkedToDa.getDataAttribute().setValImport(tdai.isValImport());
}
});
Expand All @@ -198,22 +200,22 @@ private Optional<TDAI> createDoiSdiDaiChainIfNotExists(TAnyLN tAnyLN, DataObject

TDOI doi = tAnyLN.getDOI().stream().filter(doi1 -> doi1.getName().equals(dataObject.getDoName()))
.findFirst()
.orElseGet(()-> {
.orElseGet(() -> {
TDOI newDOI = new TDOI();
newDOI.setName(dataObject.getDoName());
tAnyLN.getDOI().add(newDOI);
return newDOI;
});
if(structInstances.size() > 1){
if (structInstances.size() > 1) {
TSDI firstSDI = findOrCreateSDIFromDOI(doi, structInstances.getFirst());
TSDI lastSDI = findOrCreateSDIByStructName(firstSDI, structInstances);
if(structInstances.size() == 1){
if (structInstances.size() == 1) {
return lastSDI.getSDIOrDAI().stream()
.filter(tUnNaming -> tUnNaming.getClass().equals(TDAI.class))
.map(TDAI.class::cast)
.filter(tdai -> tdai.getName().equals(structInstances.getFirst()))
.map(tdai -> {
if(tdai.isSetValImport()) {
if (tdai.isSetValImport()) {
tdai.setValImport(dataAttribute.isValImport());
}
return tdai;
Expand All @@ -226,13 +228,13 @@ private Optional<TDAI> createDoiSdiDaiChainIfNotExists(TAnyLN tAnyLN, DataObject
return Optional.of(newDAI);
});
}
} else if(structInstances.size() == 1){
} else if (structInstances.size() == 1) {
return doi.getSDIOrDAI().stream()
.filter(tUnNaming -> tUnNaming.getClass().equals(TDAI.class))
.map(TDAI.class::cast)
.filter(tdai -> tdai.getName().equals(structInstances.getFirst()))
.map(tdai -> {
if(tdai.isSetValImport()) tdai.setValImport(dataAttribute.isValImport());
if (tdai.isSetValImport()) tdai.setValImport(dataAttribute.isValImport());
return tdai;
})
.findFirst()
Expand All @@ -247,7 +249,7 @@ private Optional<TDAI> createDoiSdiDaiChainIfNotExists(TAnyLN tAnyLN, DataObject
}

private TSDI findSDIByStructName(TSDI tsdi, List<String> sdiNames) {
if(sdiNames.isEmpty()) return tsdi;
if (sdiNames.isEmpty()) return tsdi;
return this.getSdiByName(tsdi, sdiNames.getFirst())
.map(sdi1 -> {
sdiNames.removeFirst();
Expand Down Expand Up @@ -294,15 +296,72 @@ private Optional<TSDI> getSdiByName(TSDI sdi, String sdiName) {
}

/**
*
* @param sdi TSDI
* @param sdi TSDI
* @param structName list start with sdi name
* @return already existing TSDI or newly created TSDI from given TSDI
*/
private TSDI findOrCreateSDIByStructName(TSDI sdi, List<String> structName) {
structName.removeFirst();
if(structName.isEmpty() || structName.size() == 1) return sdi;
if (structName.isEmpty() || structName.size() == 1) return sdi;
return findOrCreateSDIByStructName(findOrCreateSDIFromSDI(sdi, structName.getFirst()), structName);
}

@Override
public List<SclReportItem> updateLnModStValBasedOnLNodeStatus(SCL scl) {
scl.getSubstation().stream()
.flatMap(tSubstation -> tSubstation.getVoltageLevel().stream())
.flatMap(tVoltageLevel -> tVoltageLevel.getBay().stream())
.flatMap(tBay -> tBay.getFunction().stream())
.flatMap(tFunction -> tFunction.getLNode().stream())
.map(tlNode -> {
String lNodeLnodeStatus = extractStringPrivate(tlNode, "COMPAS-LNodeStatus")
.orElseThrow(); // FIXME
TAnyLN tAnyLN = findLn(scl, tlNode.getIedName(), tlNode.getLdInst(), tlNode.getLnClass().getFirst(), tlNode.getLnInst(), tlNode.getPrefix())
.orElseThrow();// FIXME
String lnLNodeStatus = extractStringPrivate(tAnyLN, "COMPAS-LNodeStatus")
.orElseThrow();
switch (lNodeLnodeStatus) {
case "on" -> {
if (lnLNodeStatus.contains("on")) {
// Met à jour Mod stVal s'il existe
} else {
// erreur // FIXME
}
}
case "of" -> {
if (lnLNodeStatus.contains("off")) {
// Met à jour Mod stVal s'il existe
} else {
// erreur // FIXME
}
}
default -> {
// erreur // FIXME
}
}
return null;
}
);

return List.of();
}

private static Optional<String> extractStringPrivate(TBaseElement tBaseElement, String privateType) {
return tBaseElement.getPrivate().stream()
.filter(tPrivate -> privateType.equals(tPrivate.getType()))
.flatMap(tPrivate -> tPrivate.getContent().stream())
.filter(String.class::isInstance)
.map(String.class::cast)
.filter(StringUtils::isNotBlank)
.findFirst();
}

private Optional<TAnyLN> findLn(SCL scl, String iedName, String ldInst, String lnClass, String lnInst, String prefix) {
return scl.getIED().stream()
.filter(tied -> iedName.equals(tied.getName()))
.findFirst()
.flatMap(tied -> new LdeviceService().findLdevice(tied, tlDevice -> ldInst.equals(tlDevice.getInst())))
.flatMap(tlDevice -> findAnyLn(tlDevice, tAnyLN -> matchesLn(tAnyLN, lnInst, lnClass, prefix)));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

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

import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.scl2007b4.model.TAnyLN;
import org.lfenergy.compas.scl2007b4.model.TDAI;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDaFilter;
import org.lfenergy.compas.sct.commons.dto.SclReportItem;

import java.util.List;
import java.util.Optional;

public interface LnEditor {
Expand All @@ -17,4 +20,5 @@ public interface LnEditor {

void updateOrCreateDOAndDAInstances(TAnyLN tAnyLN, DoLinkedToDa doLinkedToDa);

List<SclReportItem> updateLnModStValBasedOnLNodeStatus(SCL scl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.lfenergy.compas.sct.commons.scl.ln.AbstractLNAdapter;
import org.lfenergy.compas.sct.commons.scl.ldevice.LDeviceAdapter;
import org.lfenergy.compas.sct.commons.scl.ln.LNAdapter;
import org.lfenergy.compas.sct.commons.scl.ln.LnKey;
import org.lfenergy.compas.sct.commons.scl.ln.LnId;
import org.lfenergy.compas.sct.commons.util.Utils;

import java.util.HashSet;
Expand Down Expand Up @@ -152,14 +152,10 @@ public static <T extends TAnyLN> LNodeDTO from(AbstractLNAdapter<T> nodeAdapter,

public static LNodeDTO from(TAnyLN tAnyLN, LogicalNodeOptions options, String iedName, String ldInst, SCL scl) {
log.info(Utils.entering());
LnKey lnKey = switch (tAnyLN) {
case LN0 ln0 -> new LnKey(ln0);
case TLN tln -> new LnKey(tln);
default -> throw new IllegalStateException("Unexpected value: " + tAnyLN);
};
String inst = lnKey.getInst();
String lnClass = lnKey.getLnClass();
String prefix = lnKey.getPrefix();
LnId lnId = LnId.from(tAnyLN);
String inst = lnId.lnInst();
String lnClass = lnId.lnClass();
String prefix = lnId.prefix();
String lnType = tAnyLN.getLnType();
LNodeDTO lNodeDTO = new LNodeDTO(inst, lnClass, prefix, lnType);
if (options.isWithExtRef()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-FileCopyrightText: 2021 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.sct.commons.scl.ln;

import org.apache.commons.lang3.StringUtils;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.exception.ScdException;

import java.util.List;
import java.util.Objects;

public record LnId(String lnClass, String lnInst, String prefix) {
private static final String LN0_LNCLASS = TLLN0Enum.LLN_0.value();

/**
* Id of LN0. All LN0 have the same lnClass, lnInst and prefix in SCD
*/
public static final LnId LN0_ID = new LnId(LN0_LNCLASS, "", "");

/**
* Constructor
*
* @param lnClass always required
* @param lnInst optional. It should be empty for LN0, and should be filled for LN. No verification is done because some Lnode GAPC do not have a lninst.
* @param prefix optional. Is set to empty if null because empty is the default value on LN and the majority of the JAXB Element (LN, LNode, FCDA, ClientLN, but not on ExtRef and IEDName unfortunately)
*/
public LnId(String lnClass, String lnInst, String prefix) {
if (StringUtils.isBlank(lnClass)) {
throw new ScdException("lnClass is required");
}
this.lnClass = lnClass;
this.lnInst = Objects.requireNonNullElse(lnInst, "");
this.prefix = Objects.requireNonNullElse(prefix, "");
}

/**
* Alternative constructor with lnClass as a List instead of a String.
* JAXB classes lnClass attribute are List, even though it can only contain one element.
* This constructor makes it easier to create LnId from JAXB classes
*
* @param lnClass one element list containing the lnClass value of the LN
* @param lnInst LN lnInst
* @param prefix LN prefix
*/
public LnId(List<String> lnClass, String lnInst, String prefix) {
this(lnClass == null || lnClass.isEmpty() ? null : lnClass.getFirst(), lnInst, prefix);
}

/**
* Extract id from LN element
*
* @param tAnyLN LN element
* @return id
*/
public static LnId from(TAnyLN tAnyLN) {
if (tAnyLN instanceof TLN0) {
return LN0_ID;
} else if (tAnyLN instanceof TLN tln) {
return new LnId(tln.getLnClass(), tln.getInst(), tln.getPrefix());
} else {
throw new ScdException("Unexpected class : " + (tAnyLN != null ? tAnyLN.getClass() : null));
}
}

/**
* Extract id from LNode element
*
* @param tlNode LNode element
* @return id
*/
public static LnId from(TLNode tlNode) {
if (tlNode.getLnClass().contains(LN0_LNCLASS)) {
return LN0_ID;
} else {
return new LnId(tlNode.getLnClass(), tlNode.getLnInst(), tlNode.getPrefix());
}
}



}

This file was deleted.

Loading

0 comments on commit ac2c900

Please sign in to comment.