Commit 2dca1b4f authored by Dominique Marcadet's avatar Dominique Marcadet

add BasicType (partial) and Enumeration validation

parent 168656c2
......@@ -25,7 +25,9 @@ import java.util.stream.Stream;
import org.eclipse.emf.common.util.DiagnosticChain;
import fr.centralesupelec.edf.riseclipse.iec61850.nsd.CDC;
import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DataAttribute;
import fr.centralesupelec.edf.riseclipse.iec61850.nsd.SubDataObject;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.DA;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.DOType;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.SDO;
import fr.centralesupelec.edf.riseclipse.util.AbstractRiseClipseConsole;
......@@ -45,6 +47,7 @@ public class CDCValidator {
private DataAttributePresenceConditionValidator dataAttributePresenceConditionValidator;
private SubDataObjectPresenceConditionValidator subDataObjectPresenceConditionValidator;
private HashMap< String, TypeValidator > dataAttributeValidatorMap = new HashMap<>();
private HashMap< String, CDCValidator > subDataObjectValidatorMap = new HashMap<>();
private HashSet< DOType > validatedDOType = new HashSet<>();
......@@ -52,6 +55,16 @@ public class CDCValidator {
dataAttributePresenceConditionValidator = DataAttributePresenceConditionValidator.get( cdc );
subDataObjectPresenceConditionValidator = SubDataObjectPresenceConditionValidator.get( cdc );
for( DataAttribute da : cdc.getDataAttribute() ) {
TypeValidator validator = TypeValidator.get( da.getType() );
if( validator != null ) {
dataAttributeValidatorMap.put( da.getName(), validator );
}
else {
AbstractRiseClipseConsole.getConsole().warning( "[NSD setup] Type not found for DataAttribute " + da.getName() );
}
}
for( SubDataObject sdo : cdc.getSubDataObject() ) {
CDCValidator validator = CDCValidator.get( sdo.getType() );
if( validator != null ) {
......@@ -84,6 +97,16 @@ public class CDCValidator {
.forEach( d -> subDataObjectPresenceConditionValidator.addSDO( d, diagnostics ));
res = subDataObjectPresenceConditionValidator.validate( doType, diagnostics ) && res;
for( DA da : doType.getDA() ) {
TypeValidator validator = dataAttributeValidatorMap.get( da.getName() );
if( validator != null ) {
validator.validateDA( da, diagnostics );
}
else {
AbstractRiseClipseConsole.getConsole().warning( "[NSD validation] while validating DOType (line " + doType.getLineNumber() + "): validator for DA " + da.getName() + " not found" );
}
}
for( SDO sdo : doType.getSDO() ) {
CDCValidator validator = subDataObjectValidatorMap.get( sdo.getName() );
......
/**
* Copyright (c) 2019 CentraleSupélec & EDF.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* This file is part of the RiseClipse tool
*
* Contributors:
* Computer Science Department, CentraleSupélec
* EDF R&D
* Contacts:
* dominique.marcadet@centralesupelec.fr
* aurelie.dehouck-neveu@edf.fr
* Web site:
* http://wdi.supelec.fr/software/RiseClipse/
*/
package fr.centralesupelec.edf.riseclipse.iec61850.scl.validator.nsd;
import java.util.HashMap;
import java.util.HashSet;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import fr.centralesupelec.edf.riseclipse.iec61850.nsd.Enumeration;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.DA;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.EnumType;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.EnumVal;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.Val;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.validator.RiseClipseValidatorSCL;
import fr.centralesupelec.edf.riseclipse.util.AbstractRiseClipseConsole;
public class EnumerationValidator extends TypeValidator {
private HashMap< String, Integer > literals = new HashMap<>();
private String name;
private String inheritedFromName;
private EnumerationValidator inheritedFrom;
private HashSet< EnumType > validatedEnumType = new HashSet<>();
public EnumerationValidator( Enumeration enumeration ) {
this.name = enumeration.getName();
this.inheritedFromName = enumeration.getInheritedFrom();
enumeration
.getLiteral()
.stream()
.forEach( e -> literals.put( e.getName(), e.getLiteralVal() ));
}
public String getName() {
return name;
}
@Override
public boolean validateDA( DA da, DiagnosticChain diagnostics ) {
AbstractRiseClipseConsole.getConsole().verbose( "[NSD validation] EnumerationValidator.validateDA( " + da.getName() + " ) at line " + da.getLineNumber() );
if(( inheritedFromName != null ) && ( inheritedFrom == null )) {
TypeValidator inheritedValidator = TypeValidator.get( inheritedFromName );
if(( inheritedValidator != null ) && ( inheritedValidator instanceof EnumerationValidator )) {
inheritedFrom = ( EnumerationValidator ) inheritedValidator;
}
else {
diagnostics.add( new BasicDiagnostic(
Diagnostic.WARNING,
RiseClipseValidatorSCL.DIAGNOSTIC_SOURCE,
0,
"[NSD validation] validator for inherited enumeration " + inheritedFromName + " not found",
new Object[] { da } ));
// Avoid checking again
inheritedFromName = null;
}
}
boolean res = true;
if( ! "Enum".equals( da.getBType() )) {
diagnostics.add( new BasicDiagnostic(
Diagnostic.ERROR,
RiseClipseValidatorSCL.DIAGNOSTIC_SOURCE,
0,
"[NSD validation] bType of DA " + da.getName() + " in DOType (id = " + da.getParentDOType().getId()
+ ", line = " + da.getParentDOType().getLineNumber() + ") is not Enum",
new Object[] { da } ));
res = false;
}
if( ! getName().equals( da.getType() )) {
diagnostics.add( new BasicDiagnostic(
Diagnostic.ERROR,
RiseClipseValidatorSCL.DIAGNOSTIC_SOURCE,
0,
"[NSD validation] type of DA " + da.getName() + " in DOType (id = " + da.getParentDOType().getId()
+ ", line = " + da.getParentDOType().getLineNumber() + ") is not " + getName(),
new Object[] { da } ));
res = false;
}
for( Val val : da.getVal() ) {
res = validateValue( da, val.getValue(), diagnostics ) && res;
}
if( da.getRefersToEnumType() != null ) {
res = validateEnumType( da.getRefersToEnumType(), diagnostics ) && res;
}
return res;
}
protected boolean validateValue( DA da, String value, DiagnosticChain diagnostics ) {
boolean res = true;
if( ! literals.containsKey( value )) {
if( inheritedFrom != null ) {
res = inheritedFrom.validateValue( da, value, diagnostics ) && res;
}
else {
diagnostics.add( new BasicDiagnostic(
Diagnostic.ERROR,
RiseClipseValidatorSCL.DIAGNOSTIC_SOURCE,
0,
"[NSD validation] value of DA " + da.getName() + " in DOType (id = " + da.getParentDOType().getId()
+ ", line = " + da.getParentDOType().getLineNumber() + ") is not valid",
new Object[] { da } ));
res = false;
}
}
return res;
}
public boolean validateEnumType( EnumType enumType, DiagnosticChain diagnostics ) {
if( validatedEnumType.contains( enumType )) return true;
AbstractRiseClipseConsole.getConsole().verbose( "[NSD validation] EnumerationValidator.validateEnumType( " + enumType.getId() + " ) at line " + enumType.getLineNumber() );
validatedEnumType.add( enumType );
boolean res = true;
// enumType.getId().equals( getName() ) already tested because enumType.getId().equals( da.getType() )
for( EnumVal enumVal : enumType.getEnumVal() ) {
if( ! literals.containsKey( enumVal.getValue() )) {
if( inheritedFrom != null ) {
res = inheritedFrom.validateEnumType( enumType, diagnostics ) && res;
}
else {
diagnostics.add( new BasicDiagnostic(
Diagnostic.ERROR,
RiseClipseValidatorSCL.DIAGNOSTIC_SOURCE,
0,
"[NSD validation] EnumVal " + enumVal.getValue() + " in EnumType (id = " + enumType.getId()
+ " at line " + enumVal.getLineNumber() + " is unknown",
new Object[] { enumVal } ));
res = false;
}
}
else if( literals.get( enumVal.getValue() ).equals( enumVal.getOrd() )) {
diagnostics.add( new BasicDiagnostic(
Diagnostic.ERROR,
RiseClipseValidatorSCL.DIAGNOSTIC_SOURCE,
0,
"[NSD validation] EnumVal " + enumVal.getValue() + " in EnumType (id = " + enumType.getId()
+ " at line " + enumVal.getLineNumber() + " has incorrect ord (" + enumVal.getOrd()
+ " instead of " + literals.get( enumVal.getValue() ),
new Object[] { enumVal } ));
res = false;
}
}
// TODO: do we have to check that all literals in Enumeration are present as EnumVal ?
return res;
}
}
......@@ -41,6 +41,7 @@ public class NsdEObjectValidator implements EValidator {
public NsdEObjectValidator( NsdResourceSetImpl nsdResourceSet ) {
// Order is important !
TypeValidator.buildValidators( nsdResourceSet.getBasicTypeStream(), nsdResourceSet.getEnumerationStream(), nsdResourceSet.getConstructedAttributeStream() );
CDCValidator.buildValidators( nsdResourceSet.getCDCStream() );
LNClassValidator.buildValidators( nsdResourceSet.getLNClassStream() );
}
......
/**
* Copyright (c) 2019 CentraleSupélec & EDF.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* This file is part of the RiseClipse tool
*
* Contributors:
* Computer Science Department, CentraleSupélec
* EDF R&D
* Contacts:
* dominique.marcadet@centralesupelec.fr
* aurelie.dehouck-neveu@edf.fr
* Web site:
* http://wdi.supelec.fr/software/RiseClipse/
*/
package fr.centralesupelec.edf.riseclipse.iec61850.scl.validator.nsd;
import java.util.HashMap;
import java.util.stream.Stream;
import org.eclipse.emf.common.util.DiagnosticChain;
import fr.centralesupelec.edf.riseclipse.iec61850.nsd.BasicType;
import fr.centralesupelec.edf.riseclipse.iec61850.nsd.ConstructedAttribute;
import fr.centralesupelec.edf.riseclipse.iec61850.nsd.Enumeration;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.DA;
public abstract class TypeValidator {
private static HashMap< String, TypeValidator > validators = new HashMap<>();
public static TypeValidator get( String name ) {
return validators.get( name );
}
public static void buildValidators( Stream< BasicType > basicTypeStream, Stream< Enumeration > enumerationStream, Stream< ConstructedAttribute > constructedAttributeStream ) {
basicTypeStream
.forEach( basicType -> validators.put( basicType.getName(), BasicTypeValidator.get( basicType )));
enumerationStream
.forEach( enumeration -> validators.put( enumeration.getName(), new EnumerationValidator( enumeration )));
}
public abstract boolean validateDA( DA da, DiagnosticChain diagnostics );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment