Skip to content

Commit

Permalink
Merge pull request #393 from com-pas/develop
Browse files Browse the repository at this point in the history
Release 0.2.22
  • Loading branch information
gleizesDor authored Apr 29, 2024
2 parents 905f002 + 3c1533e commit ff181df
Show file tree
Hide file tree
Showing 31 changed files with 2,206 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2024 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.compas.sct.commons;

import org.lfenergy.compas.scl2007b4.model.TBDA;
import org.lfenergy.compas.scl2007b4.model.TDAType;

import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class BDAService {

public Stream<TBDA> getBDAs(TDAType tdaType) {
return tdaType.getBDA().stream();
}

public Stream<TBDA> getFilteredBDAs(TDAType tdaType, Predicate<TBDA> tTBDAPredicate) {
return getBDAs(tdaType).filter(tTBDAPredicate);
}

public Optional<TBDA> findBDA(TDAType tdaType, Predicate<TBDA> tBDAPredicate) {
return getFilteredBDAs(tdaType, tBDAPredicate).findFirst();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2024 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.compas.sct.commons;

import org.lfenergy.compas.scl2007b4.model.TDAType;
import org.lfenergy.compas.scl2007b4.model.TDataTypeTemplates;

import java.util.Optional;

public class DaTypeService {

public Optional<TDAType> findDaType(TDataTypeTemplates tDataTypeTemplates, String daTypeId) {
return tDataTypeTemplates.getDAType().stream()
.filter(tdaType -> tdaType.isSetId() && tdaType.getId().equals(daTypeId)).findFirst();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2024 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.compas.sct.commons;

import org.lfenergy.compas.scl2007b4.model.*;

import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class DaiService {

public Stream<TDAI> getDais(TDOI tdoi) {
return tdoi.getSDIOrDAI()
.stream()
.filter(dai -> dai.getClass().equals(TDAI.class))
.map(TDAI.class::cast);
}

public Stream<TDAI> getFilteredDais(TDOI tdoi, Predicate<TDAI> tdaiPredicate) {
return getDais(tdoi).filter(tdaiPredicate);
}

public Optional<TDAI> findDai(TDOI tdoi, Predicate<TDAI> tdaiPredicate) {
return getFilteredDais(tdoi, tdaiPredicate).findFirst();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@

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.DataTypeTemplateReader;
import org.lfenergy.compas.sct.commons.domain.DataAttribute;
import org.lfenergy.compas.sct.commons.domain.DataObject;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDaFilter;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static org.lfenergy.compas.sct.commons.util.CommonConstants.MOD_DO_NAME;
import static org.lfenergy.compas.sct.commons.util.CommonConstants.STVAL_DA_NAME;

public class DataTypeTemplatesService {
public class DataTypeTemplatesService implements DataTypeTemplateReader {

final LnodeTypeService lnodeTypeService = new LnodeTypeService();
final DoTypeService doTypeService = new DoTypeService();
final DaTypeService daTypeService = new DaTypeService();
final DoService doService = new DoService();
final SDOOrDAService sdoOrDAService = new SDOOrDAService();
final BDAService bdaService = new BDAService();

/**
* verify if DO(name=Mod)/DA(name=stVal) exists in DataTypeTemplate
Expand All @@ -33,14 +46,116 @@ public class DataTypeTemplatesService {
*/
public boolean isDoModAndDaStValExist(TDataTypeTemplates dtt, String lNodeTypeId) {
return lnodeTypeService.findLnodeType(dtt, lNodeType -> lNodeTypeId.equals(lNodeType.getId()))
.map(lNodeType -> doService.findDo(lNodeType, tdo -> MOD_DO_NAME.equals(tdo.getName()))
.map(tdo -> doTypeService.findDoType(dtt, doType -> tdo.getType().equals(doType.getId()))
.map(doType -> doType.getSDOOrDA().stream()
.filter(sdoOrDa -> sdoOrDa.getClass().equals(TDA.class))
.map(TDA.class::cast)
.anyMatch(tda -> STVAL_DA_NAME.equals(tda.getName())))
.orElse(false))
.orElse(false))
.orElse(false);
.flatMap(lNodeType -> doService.findDo(lNodeType, tdo -> MOD_DO_NAME.equals(tdo.getName()))
.flatMap(tdo -> doTypeService.findDoType(dtt, doType -> tdo.getType().equals(doType.getId()))
.map(doType -> sdoOrDAService.findDA(doType, tda -> STVAL_DA_NAME.equals(tda.getName())).isPresent())))
.orElse(false);
}

@Override
public Stream<DoLinkedToDa> getAllDoLinkedToDa(TDataTypeTemplates dtt) {
return lnodeTypeService.getLnodeTypes(dtt)
.flatMap(tlNodeType -> {
DoLinkedToDa doLinkedToDa = new DoLinkedToDa();
doLinkedToDa.setDataObject(new DataObject());
doLinkedToDa.setDataAttribute(new DataAttribute());
return tlNodeType.getDO()
.stream()
.map(tdo -> doTypeService.findDoType(dtt, tdoType -> tdoType.getId().equals(tdo.getType()))
.map(doType -> {
doLinkedToDa.getDataObject().setDoName(tdo.getName());
return doTypeService.getAllSDOLinkedToDa(dtt, doType, doLinkedToDa).stream();
}))
.filter(Optional::isPresent)
.flatMap(Optional::orElseThrow);
});
}

@Override
public Stream<DoLinkedToDa> getFilteredDoLinkedToDa(TDataTypeTemplates dtt, String lNodeTypeId, DoLinkedToDaFilter doLinkedToDaFilter) {
return lnodeTypeService.findLnodeType(dtt, tlNodeType -> tlNodeType.getId().equals(lNodeTypeId))
.stream()
.flatMap(tlNodeType -> doService.getFilteredDos(tlNodeType, tdo -> StringUtils.isBlank(doLinkedToDaFilter.getDoName())
|| (StringUtils.isNotBlank(doLinkedToDaFilter.getDoName()) && doLinkedToDaFilter.getDoName().equals(tdo.getName())))
.flatMap(tdo -> {
DoLinkedToDa doLinkedToDa = new DoLinkedToDa();
DataObject dataObject = new DataObject();
dataObject.setDoName(tdo.getName());
doLinkedToDa.setDataObject(dataObject);
doLinkedToDa.setDataAttribute(new DataAttribute());
return doTypeService.findDoType(dtt, tdoType -> tdoType.getId().equals(tdo.getType()))
.stream()
.flatMap(tdoType -> {
doLinkedToDa.getDataObject().setCdc(tdoType.getCdc());
return doTypeService.getAllSDOLinkedToDa(dtt, tdoType, doLinkedToDa).stream()
.filter(doLinkedToDa1 -> StringUtils.isBlank(doLinkedToDaFilter.getDoName()) || doLinkedToDa1.getDoRef().startsWith(doLinkedToDaFilter.getDoRef())
&& StringUtils.isBlank(doLinkedToDaFilter.getDaName()) || doLinkedToDa1.getDaRef().startsWith(doLinkedToDaFilter.getDaRef()));
});
}));
}

@Override
public Optional<DoLinkedToDa> findDoLinkedToDa(TDataTypeTemplates dtt, String lNodeTypeId, DoLinkedToDa doLinkedToDa) {
List<String> dataRefList = new ArrayList<>(doLinkedToDa.getDataObject().getSdoNames());
dataRefList.addAll(doLinkedToDa.getDataAttribute().getBdaNames());

return lnodeTypeService.findLnodeType(dtt, lNodeType -> lNodeTypeId.equals(lNodeType.getId()))
.flatMap(lNodeType -> doService.findDo(lNodeType, tdo -> tdo.getName().equals(doLinkedToDa.getDataObject().getDoName()))
// Search DoType for each DO
.flatMap(tdo -> doTypeService.findDoType(dtt, doType -> doType.getId().equals(tdo.getType()))
.flatMap(tdoType -> {
// Search last DoType from DOType (SDO) > DOType (SDO)
TDOType lastDoType = findDOTypeBySdoName(dtt, tdoType, dataRefList);
// Search first DA from last DoType
return sdoOrDAService.findDA(lastDoType, tda1 -> tda1.getName().equals(doLinkedToDa.getDataAttribute().getDaName()))
.flatMap(tda -> {
// Check if first DA is STRUCT or not
if(!tda.getBType().equals(TPredefinedBasicTypeEnum.STRUCT)) {
return Optional.of(doLinkedToDa);
}
// Search first DaType from DOType (from last DOType where DA is STRUCT)
return getDATypeByDaName(dtt, lastDoType, tda.getName())
.flatMap(tdaType -> {
// Search last DAType from first DAType
TDAType lastDAType = findDATypeByBdaName(dtt, tdaType, tbda -> tbda.isSetBType()
&& tbda.getBType().equals(TPredefinedBasicTypeEnum.STRUCT), dataRefList);

// last DAType should contain BDA not STRUCT
if(dataRefList.size() != 1) return Optional.empty();
String lastBdaName = dataRefList.getFirst();
return bdaService.findBDA(lastDAType, tbda -> tbda.getName().equals(lastBdaName)
&& !tbda.getBType().equals(TPredefinedBasicTypeEnum.STRUCT))
.flatMap(tbda -> Optional.of(doLinkedToDa));
});
});
})
));
}

private Optional<TDAType> getDATypeByDaName(TDataTypeTemplates dtt, TDOType tdoType, String daName) {
return sdoOrDAService.findDA(tdoType, tda -> tda.getName().equals(daName))
.flatMap(tda -> daTypeService.findDaType(dtt, tda.getType()));
}

private TDOType findDOTypeBySdoName(TDataTypeTemplates dtt, TDOType tdoType, List<String> sdoNames) {
if(sdoNames.isEmpty()) return tdoType;
return sdoOrDAService.findSDO(tdoType, tsdo -> tsdo.getName().equals(sdoNames.getFirst()))
.flatMap(tsdo -> doTypeService.findDoType(dtt, tdoType2 -> tdoType2.getId().equals(tsdo.getType())))
.map(tdoType2 -> {
sdoNames.removeFirst();
return findDOTypeBySdoName(dtt, tdoType2, sdoNames);
}).orElse(tdoType);
}

private TDAType findDATypeByBdaName(TDataTypeTemplates dtt, TDAType tdaType, Predicate<TBDA> tbdaPredicate, List<String> bdaNames) {
if(bdaNames.isEmpty()) return tdaType;
return bdaService.getFilteredBDAs(tdaType, tbdaPredicate)
.findFirst()
.flatMap(tbda -> daTypeService.findDaType(dtt, tbda.getType()))
.map(tdaType2 -> {
bdaNames.removeFirst();
return findDATypeByBdaName(dtt, tdaType2, tbdaPredicate, bdaNames);
}).orElse(tdaType);
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
// SPDX-FileCopyrightText: 2023 RTE FRANCE
// SPDX-FileCopyrightText: 2023 2024 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.compas.sct.commons;

import org.lfenergy.compas.scl2007b4.model.TDataTypeTemplates;
import org.lfenergy.compas.scl2007b4.model.TDOType;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.domain.DataAttribute;
import org.lfenergy.compas.sct.commons.domain.DoLinkedToDa;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class DoTypeService {

final DaTypeService daTypeService = new DaTypeService();
final SDOOrDAService sdoOrDAService = new SDOOrDAService();
final BDAService bdaService = new BDAService();

public Stream<TDOType> getDoTypes(TDataTypeTemplates tDataTypeTemplates) {
return tDataTypeTemplates.getDOType().stream();
}
Expand All @@ -25,4 +32,66 @@ public Optional<TDOType> findDoType(TDataTypeTemplates tDataTypeTemplates, Predi
return getFilteredDoTypes(tDataTypeTemplates, tdoTypePredicate).findFirst();
}

}
public List<DoLinkedToDa> getAllSDOLinkedToDa(TDataTypeTemplates dtt, TDOType tdoType, DoLinkedToDa doLinkedToDaTemplate) {
List<DoLinkedToDa> result = new ArrayList<>();
// DA -> BDA -> BDA..
sdoOrDAService.getDAs(tdoType).forEach(tda -> {
DoLinkedToDa doLinkedToDa = DoLinkedToDa.copyFrom(doLinkedToDaTemplate);
doLinkedToDa.getDataAttribute().setDaName(tda.getName());
if(tda.isSetFc()) {
doLinkedToDa.getDataAttribute().setFc(tda.getFc());
}

// STRUCT type (BType=STRUCT) refer to BDA, otherwise it is DA
if(TPredefinedBasicTypeEnum.STRUCT.equals(tda.getBType())) {
daTypeService.findDaType(dtt, tda.getType())
.ifPresent(nextDaType -> result.addAll(getDaLinkedToBDA(dtt, nextDaType, doLinkedToDa).toList()));
} else {
doLinkedToDa.setDataAttribute(updateDataAttributeFromDaOrBda(tda, doLinkedToDa.getDataAttribute()));
result.add(doLinkedToDa);
}
});
// SDO -> SDO -> SDO..
sdoOrDAService.getSDOs(tdoType)
.forEach(tsdo -> {
if(tsdo.isSetType()){
findDoType(dtt, tdoType1 -> tdoType1.getId().equals(tsdo.getType()))
.ifPresent(nextDoType -> {
DoLinkedToDa newDoLinkedToDa = DoLinkedToDa.copyFrom(doLinkedToDaTemplate);
newDoLinkedToDa.getDataObject().getSdoNames().add(tsdo.getName());
if(nextDoType.isSetCdc()) {
newDoLinkedToDa.getDataObject().setCdc(nextDoType.getCdc());
}
result.addAll(getAllSDOLinkedToDa(dtt, nextDoType, newDoLinkedToDa));
});
}
});
return result;
}
private Stream<DoLinkedToDa> getDaLinkedToBDA(TDataTypeTemplates dtt, TDAType tdaType1, DoLinkedToDa doLinkedToDaTemplate) {
// BDA -> BDA -> BDA..
return bdaService.getBDAs(tdaType1)
.flatMap(tbda -> {
DoLinkedToDa newDoLinkedToDa = DoLinkedToDa.copyFrom(doLinkedToDaTemplate);
newDoLinkedToDa.getDataAttribute().getBdaNames().add(tbda.getName());

// STRUCT type (BType=STRUCT) refer to complex BDA object, otherwise it is kind of DA object
if(TPredefinedBasicTypeEnum.STRUCT.equals(tbda.getBType())){
return daTypeService.findDaType(dtt, tbda.getType())
.stream().flatMap(nextDaType -> getDaLinkedToBDA(dtt, nextDaType, newDoLinkedToDa));
} else {
newDoLinkedToDa.setDataAttribute(updateDataAttributeFromDaOrBda(tbda, newDoLinkedToDa.getDataAttribute()));
return Stream.of(newDoLinkedToDa);
}
});
}

private DataAttribute updateDataAttributeFromDaOrBda(TAbstractDataAttribute daOrBda, DataAttribute dataAttribute) {
if (daOrBda.isSetType()) dataAttribute.setType(daOrBda.getType());
if (daOrBda.isSetBType()) dataAttribute.setBType(daOrBda.getBType());
if (daOrBda.isSetValImport()) dataAttribute.setValImport(daOrBda.isValImport());
if (daOrBda.isSetVal()) dataAttribute.addDaVal(daOrBda.getVal());
return dataAttribute;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2024 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.compas.sct.commons;

import org.lfenergy.compas.scl2007b4.model.TAnyLN;
import org.lfenergy.compas.scl2007b4.model.TDOI;

import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class DoiService {

public Stream<TDOI> getDois(TAnyLN tAnyLN) {
return tAnyLN.getDOI().stream();
}

public Stream<TDOI> getFilteredDois(TAnyLN tAnyLN, Predicate<TDOI> tdoiPredicate) {
return getDois(tAnyLN).filter(tdoiPredicate);
}

public Optional<TDOI> findDoi(TAnyLN tAnyLN, Predicate<TDOI> tdoiPredicate) {
return getFilteredDois(tAnyLN, tdoiPredicate).findFirst();
}

}
Loading

0 comments on commit ff181df

Please sign in to comment.