Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#239): Exclude DOI Mod-stVal in DAI updatability check #240

Merged
merged 3 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
package org.lfenergy.compas.sct.commons.scl.ied;

import org.lfenergy.compas.scl2007b4.model.TDAI;
import org.lfenergy.compas.scl2007b4.model.TDOI;
import org.lfenergy.compas.scl2007b4.model.TPrivate;
import org.lfenergy.compas.scl2007b4.model.TVal;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.scl.SclElementAdapter;
import org.lfenergy.compas.sct.commons.util.CommonConstants;

import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

/**
* A representation of the model object
Expand All @@ -35,9 +35,6 @@
* <li>{@link AbstractDAIAdapter#addPrivate(TPrivate) <em>Add <b>TPrivate </b> under this object</em>}</li>
* </ul>
* <li>Checklist functions</li>
* <ul>
* <li>{@link AbstractDAIAdapter#isValImport <em>Check value Of <b>valImport </b> attribute</em>}</li>
* </ul>
* </ol>
*
* @see org.lfenergy.compas.scl2007b4.model.TDAI
Expand Down Expand Up @@ -88,13 +85,11 @@ public void setValImport(boolean b) {
currentElem.setValImport(b);
}

/**
* Cheks <em>ValImport</em> boolean value
*
* @return <em>Boolean</em> value of <em>ValImport</em> if define or <em>null</em>
*/
public Boolean isValImport() {
return currentElem.isSetValImport() ? currentElem.isValImport() : null;
private boolean isDOModDAstVal() {
SaintierFr marked this conversation as resolved.
Show resolved Hide resolved
if (parentAdapter.getCurrentElem() instanceof final TDOI tdoi) {
return currentElem.getName().equals(CommonConstants.STVAL) && tdoi.getName().equals(CommonConstants.MOD_DO_NAME);
}
return false;
}

public AbstractDAIAdapter<? extends SclElementAdapter> update(Map<Long, String> daiValues) throws ScdException {
Expand All @@ -116,30 +111,33 @@ public AbstractDAIAdapter<? extends SclElementAdapter> update(Map<Long, String>
* @throws ScdException throws when DAI for which SGroup should be updated is not updatable
*/
public void update(Long sGroup, String val) throws ScdException {
if (currentElem.isSetValImport() && !currentElem.isValImport()) {
if (!isDOModDAstVal() && currentElem.isSetValImport() && !currentElem.isValImport()) {
SaintierFr marked this conversation as resolved.
Show resolved Hide resolved
String msg = String.format(
"DAI(%s) cannot be updated : valImport(false)", currentElem.getName()
"DAI(%s) cannot be updated : valImport(false) %s", currentElem.getName(), getXPath()
);
throw new ScdException(msg);
}
Stream<TVal> tValStream = currentElem.getVal().stream();
if (sGroup != null && sGroup != 0) {
Optional<TVal> tVal = tValStream.filter(tValElem -> tValElem.isSetSGroup() &&
sGroup.equals(tValElem.getSGroup()))
.findFirst();
if (tVal.isPresent()) {
tVal.get().setValue(val);
} else {
TVal newTVal = new TVal();
newTVal.setValue(val);
newTVal.setSGroup(sGroup);
currentElem.getVal().add(newTVal);
}
updateSGroupVal(sGroup, val);
} else {
updateVal(val);
}
}

private void updateSGroupVal(Long sGroup, String val) {
SaintierFr marked this conversation as resolved.
Show resolved Hide resolved
currentElem.getVal().stream()
.filter(tValElem -> tValElem.isSetSGroup() && sGroup.equals(tValElem.getSGroup()))
.findFirst()
.orElseGet(
() -> {
TVal newTVal = new TVal();
newTVal.setSGroup(sGroup);
currentElem.getVal().add(newTVal);
return newTVal;
})
.setValue(val);
}

public void updateVal(String s) {
currentElem.getVal().stream().findFirst()
.orElseGet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,46 +705,40 @@ public void updateDAI(@NonNull ResumedDataTemplate rDtt) throws ScdException {
DAITracker.MatchResult matchResult = daiTracker.search();
AbstractDAIAdapter<?> daiAdapter = null;
IDataParentAdapter doiOrSdoiAdapter;
if (matchResult == DAITracker.MatchResult.FULL_MATCH) {
// update
daiAdapter = (AbstractDAIAdapter) daiTracker.getBdaiOrDaiAdapter();
if ((daiAdapter.isValImport() != null && daiAdapter.isValImport()) ||
(daiAdapter.isValImport() == null && rDtt.isUpdatable())) {
daiAdapter.update(daTypeName.getDaiValues());
return;
} else {
throw new ScdException(String.format("DAI (%s -%s) cannot be updated", doTypeName, daTypeName));
}
}

if (rDtt.isUpdatable()) {
if (!rDtt.isUpdatable())
return;

if (rDtt.isUpdatable() && matchResult == DAITracker.MatchResult.FULL_MATCH) {
daiAdapter = (AbstractDAIAdapter) daiTracker.getBdaiOrDaiAdapter();
} else {
doiOrSdoiAdapter = daiTracker.getDoiOrSdoiAdapter();
int idx = daiTracker.getIndexDoType();
int indexDoType = daiTracker.getIndexDoType();
int doSz = doTypeName.getStructNames().size();
if (matchResult == DAITracker.MatchResult.FAILED) {
doiOrSdoiAdapter = addDOI(doTypeName.getName());
idx = 0;
} else if (idx == -1) {
idx = 0;
} else if (idx == doSz - 1) {
idx = doSz;
indexDoType = 0;
} else if (indexDoType == -1) {
indexDoType = 0;
} else if (indexDoType == doSz - 1) {
indexDoType = doSz;
}
for (int i = idx; i < doSz; ++i) {
for (int i = indexDoType; i < doSz; ++i) {
String sdoName = doTypeName.getStructNames().get(i);
doiOrSdoiAdapter = doiOrSdoiAdapter.addSDOI(sdoName);
}

IDataParentAdapter daiOrBdaiAdapter = daiTracker.getDoiOrSdoiAdapter();
idx = daiTracker.getIndexDaType();
int indexDaType = daiTracker.getIndexDaType();
int daSz = daTypeName.getStructNames().size();
if (idx <= -1) {
idx = 0;
} else if (idx == daSz - 1) {
idx = daSz;
if (indexDaType <= -1) {
indexDaType = 0;
} else if (indexDaType == daSz - 1) {
indexDaType = daSz;
}
for (int i = idx; i < daSz; ++i) {
for (int i = indexDaType; i < daSz; ++i) {
String bdaName = daTypeName.getStructNames().get(i);
if (idx == 0) {
if (indexDaType == 0) {
daiOrBdaiAdapter = doiOrSdoiAdapter.addSDOI(daTypeName.getName());
} else if (i == daSz - 1) {
daiAdapter = daiOrBdaiAdapter.addDAI(bdaName, rDtt.isUpdatable());
Expand All @@ -755,9 +749,8 @@ public void updateDAI(@NonNull ResumedDataTemplate rDtt) throws ScdException {
if (daiAdapter == null) {
daiAdapter = doiOrSdoiAdapter.addDAI(daTypeName.getName(), rDtt.isUpdatable());
}

daiAdapter.update(daTypeName.getDaiValues());
}
daiAdapter.update(daTypeName.getDaiValues());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,52 @@ void updateLDeviceStatus_shouldReturnUpdatedFile() {
assertEquals("off", getLDeviceStatusValue(sclReport.getSclRootAdapter().getCurrentElem(), "IedName3", "LDSUIED").get().getValue());
}

@Test
void updateLDeviceStatus_shouldReturnUpdatedFile_when_DAI_Mod_DO_stVal_whatever_it_is_updatable_or_not() {
// Given
SCL givenScl = SclTestMarshaller.getSCLFromFile("/scd-refresh-lnode/issue_165_enhance_68_Test_Dai_Updatable.scd");
assertThat(getLDeviceStatusValue(givenScl, "IedName1", "LDSUIED"))
.map(TVal::getValue)
.hasValue("off");
assertThat(getLDeviceStatusValue(givenScl, "IedName2", "LDSUIED"))
.map(TVal::getValue)
.hasValue("on");
assertThat(getLDeviceStatusValue(givenScl, "IedName3", "LDSUIED"))
.map(TVal::getValue)
.isNotPresent();
assertThat(getLDeviceStatusValue(givenScl, "IedName4", "LDSUIED"))
.map(TVal::getValue)
.hasValue("on");
assertThat(getLDeviceStatusValue(givenScl, "IedName5", "LDSUIED"))
.map(TVal::getValue)
.hasValue("on");

// When
SclReport sclReport = SclService.updateLDeviceStatus(givenScl);

// Then
assertThat(sclReport.isSuccess()).isTrue();
assertThat(getLDeviceStatusValue(sclReport.getSclRootAdapter().getCurrentElem(), "IedName1", "LDSUIED"))
.map(TVal::getValue)
.hasValue("on");

assertThat(getLDeviceStatusValue(sclReport.getSclRootAdapter().getCurrentElem(), "IedName2", "LDSUIED"))
.map(TVal::getValue)
.hasValue("off");

assertThat(getLDeviceStatusValue(sclReport.getSclRootAdapter().getCurrentElem(), "IedName3", "LDSUIED"))
.map(TVal::getValue)
.hasValue("off");

assertThat(getLDeviceStatusValue(sclReport.getSclRootAdapter().getCurrentElem(), "IedName4", "LDSUIED"))
.map(TVal::getValue)
.hasValue("off");

assertThat(getLDeviceStatusValue(sclReport.getSclRootAdapter().getCurrentElem(), "IedName5", "LDSUIED"))
.map(TVal::getValue)
.hasValue("off");
}

private Optional<TVal> getLDeviceStatusValue(SCL scl, String iedName, String ldInst) {
return getValFromDaiName(scl, iedName, ldInst, "Mod", "stVal");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void testInnerDAIAdapter() {
DOIAdapter.DAIAdapter daiAdapter = initInnerDAIAdapter("Do", "angRef");

// Then
assertNull(daiAdapter.isValImport());
assertThat(daiAdapter.getCurrentElem().isSetValImport()).isFalse();
daiAdapter.setValImport(true);
assertTrue(daiAdapter.isValImport());
assertThat(daiAdapter.getCurrentElem().isSetValImport()).isTrue();

// test tree map
assertThatThrownBy(() -> daiAdapter.getDataAdapterByName(TOTO)).isInstanceOf(UnsupportedOperationException.class);
Expand Down
Loading