From 3aa5358b1a0e58bbf6e2371ca5f4075124bd3ac8 Mon Sep 17 00:00:00 2001 From: Dominique Marcadet <Dominique.Marcadet@centralesupelec.fr> Date: Fri, 29 Mar 2019 16:45:03 +0100 Subject: [PATCH] use RiseClipse formatter --- .../scl/validator/AnyLNValidator.java | 258 ++++---- .../iec61850/scl/validator/DOIValidator.java | 549 +++++++++--------- .../scl/validator/NsdEObjectValidator.java | 246 ++++---- .../scl/validator/NsdModelLoader.java | 34 +- .../iec61850/scl/validator/NsdValidator.java | 27 +- .../scl/validator/RiseClipseValidatorSCL.java | 66 +-- .../scl/validator/SCLModelLoader.java | 13 +- 7 files changed, 565 insertions(+), 628 deletions(-) diff --git a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/AnyLNValidator.java b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/AnyLNValidator.java index b20d109..f1abde7 100644 --- a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/AnyLNValidator.java +++ b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/AnyLNValidator.java @@ -2,158 +2,134 @@ package fr.centralesupelec.edf.riseclipse.iec61850.scl.validator; import java.util.HashMap; import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.eclipse.emf.common.util.DiagnosticChain; -import org.eclipse.emf.common.util.EList; -import org.eclipse.emf.ecore.EClass; -import org.eclipse.emf.ecore.EDataType; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.EValidator; -import org.eclipse.emf.ecore.resource.Resource; - -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DocumentRoot; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.FunctionalConstraint; import fr.centralesupelec.edf.riseclipse.iec61850.nsd.AbstractLNClass; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.BasicType; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.CDC; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DataAttribute; import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DataObject; import fr.centralesupelec.edf.riseclipse.iec61850.nsd.LNClass; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.NS; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.util.NsdResourceSetImpl; import fr.centralesupelec.edf.riseclipse.iec61850.scl.AnyLN; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.DA; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.DAI; import fr.centralesupelec.edf.riseclipse.iec61850.scl.DOI; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.DOType; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.LNode; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.LNodeType; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.Val; import fr.centralesupelec.edf.riseclipse.util.AbstractRiseClipseConsole; public class AnyLNValidator { - - private String lnClass; - private HashMap<String, DataObject> doMap; - private HashMap<String, DOIValidator> cdcMap; - - - public AnyLNValidator(LNClass lnClass) { - this.lnClass = lnClass.getName(); - this.doMap = new HashMap<>(); // link between DOI (name) and its respective DataObject - this.cdcMap = new HashMap<>(); // link between CDC (name) and its respective DOIValidator - generateValidators(doMap, cdcMap, lnClass); - - // LNClass hierarchy taken into account - AbstractLNClass parent = lnClass.getRefersToAbstractLNClass(); - while(parent != null) { - generateValidators(doMap, cdcMap, parent); - parent = parent.getRefersToAbstractLNClass(); - } - - } - - public void generateValidators(HashMap<String, DataObject> doMap, HashMap<String, DOIValidator> cdcMap, AbstractLNClass lnClass) { - for(DataObject dObj : lnClass.getDataObject()) { - this.doMap.put(dObj.getName(), dObj); - if (dObj.getRefersToCDC() != null) { - if(!cdcMap.containsKey(dObj.getRefersToCDC().getName())) { - this.cdcMap.put(dObj.getRefersToCDC().getName(), new DOIValidator(dObj.getRefersToCDC())); - } - } - } - } - - public void generateValidators(HashMap<String, DataObject> doMap, HashMap<String, DOIValidator> cdcMap, LNClass lnClass) { - for(DataObject dObj : lnClass.getDataObject()) { - this.doMap.put(dObj.getName(), dObj); - if (dObj.getRefersToCDC() != null) { - if(!cdcMap.containsKey(dObj.getRefersToCDC().getName())) { - this.cdcMap.put(dObj.getRefersToCDC().getName(), new DOIValidator(dObj.getRefersToCDC())); - } - } - } - } - - - public boolean validateLN(AnyLN ln) { - HashSet<String> checkedDO = new HashSet<>(); - - for( DOI doi : ln.getDOI() ) { - AbstractRiseClipseConsole.getConsole().info( "validateDOI( " + doi.getName()+ " )" ); - + + private String lnClass; + private HashMap< String, DataObject > doMap; + private HashMap< String, DOIValidator > cdcMap; + + public AnyLNValidator( LNClass lnClass ) { + this.lnClass = lnClass.getName(); + this.doMap = new HashMap<>(); // link between DOI (name) and its respective DataObject + this.cdcMap = new HashMap<>(); // link between CDC (name) and its respective DOIValidator + generateValidators( doMap, cdcMap, lnClass ); + + // LNClass hierarchy taken into account + AbstractLNClass parent = lnClass.getRefersToAbstractLNClass(); + while( parent != null ) { + generateValidators( doMap, cdcMap, parent ); + parent = parent.getRefersToAbstractLNClass(); + } + + } + + public void generateValidators( HashMap< String, DataObject > doMap, HashMap< String, DOIValidator > cdcMap, + AbstractLNClass lnClass ) { + for( DataObject dObj : lnClass.getDataObject() ) { + this.doMap.put( dObj.getName(), dObj ); + if( dObj.getRefersToCDC() != null ) { + if( !cdcMap.containsKey( dObj.getRefersToCDC().getName() ) ) { + this.cdcMap.put( dObj.getRefersToCDC().getName(), new DOIValidator( dObj.getRefersToCDC() ) ); + } + } + } + } + + public void generateValidators( HashMap< String, DataObject > doMap, HashMap< String, DOIValidator > cdcMap, + LNClass lnClass ) { + for( DataObject dObj : lnClass.getDataObject() ) { + this.doMap.put( dObj.getName(), dObj ); + if( dObj.getRefersToCDC() != null ) { + if( !cdcMap.containsKey( dObj.getRefersToCDC().getName() ) ) { + this.cdcMap.put( dObj.getRefersToCDC().getName(), new DOIValidator( dObj.getRefersToCDC() ) ); + } + } + } + } + + public boolean validateLN( AnyLN ln ) { + HashSet< String > checkedDO = new HashSet<>(); + + for( DOI doi : ln.getDOI() ) { + AbstractRiseClipseConsole.getConsole().info( "validateDOI( " + doi.getName() + " )" ); + // Test if DOI is a possible DOI in this LN - if(!this.doMap.containsKey(doi.getName())) { - AbstractRiseClipseConsole.getConsole().error( "DO " + doi.getName() + " not found in LNClass " + ln.getLnClass()); - return false; - } - - // Control of DOI presence in LN - String presCond = this.doMap.get(doi.getName()).getPresCond(); - this.updateCompulsory(doi.getName(), presCond, checkedDO); - - // Validation of DOI content - if( ! validateDOI(doi) ) { - return false; - } - - } - - // Verify all necessary DOI were present - if(!this.doMap.entrySet().stream().map(x -> checkCompulsory(x.getKey(), x.getValue().getPresCond(), checkedDO)).reduce((a, b) -> a && b).get()) { - AbstractRiseClipseConsole.getConsole().error( "LN does not contain all mandatory DO from class " + ln.getLnClass()); + if( !this.doMap.containsKey( doi.getName() ) ) { + AbstractRiseClipseConsole.getConsole() + .error( "DO " + doi.getName() + " not found in LNClass " + ln.getLnClass() ); + return false; + } + + // Control of DOI presence in LN + String presCond = this.doMap.get( doi.getName() ).getPresCond(); + this.updateCompulsory( doi.getName(), presCond, checkedDO ); + + // Validation of DOI content + if( !validateDOI( doi ) ) { + return false; + } + + } + + // Verify all necessary DOI were present + if( !this.doMap.entrySet().stream() + .map( x -> checkCompulsory( x.getKey(), x.getValue().getPresCond(), checkedDO ) ) + .reduce( ( a, b ) -> a && b ).get() ) { + AbstractRiseClipseConsole.getConsole() + .error( "LN does not contain all mandatory DO from class " + ln.getLnClass() ); + return false; + } + return true; + } + + public boolean checkCompulsory( String name, String presCond, HashSet< String > checked ) { + switch( presCond ) { + case "M": + if( !checked.contains( name ) ) { + AbstractRiseClipseConsole.getConsole().error( "DO " + name + " is missing" ); + return false; + } + } + return true; + } + + public boolean updateCompulsory( String name, String presCond, HashSet< String > checked ) { + switch( presCond ) { + case "M": + case "O": + if( checked.contains( name ) ) { + AbstractRiseClipseConsole.getConsole().error( "DO " + name + " cannot appear more than once" ); + return false; + } + else { + checked.add( name ); + break; + } + case "F": + AbstractRiseClipseConsole.getConsole().error( "DO " + name + " is forbidden" ); return false; - } - return true; - } - - public boolean checkCompulsory(String name, String presCond, HashSet<String> checked) { - switch(presCond) { - case "M": - if(!checked.contains(name)) { - AbstractRiseClipseConsole.getConsole().error( "DO "+ name + " is missing"); - return false; - } - } - return true; + } + return true; } - - public boolean updateCompulsory(String name, String presCond, HashSet<String> checked) { - switch(presCond) { - case "M": - case "O": - if(checked.contains(name)) { - AbstractRiseClipseConsole.getConsole().error("DO " + name + " cannot appear more than once"); - return false; - } else { - checked.add(name); - break; - } - case "F": - AbstractRiseClipseConsole.getConsole().error("DO " + name + " is forbidden"); - return false; - } - return true; - } - - - public boolean validateDOI(DOI doi) { - - AbstractRiseClipseConsole.getConsole().info( "found DO " + doi.getName() + " in LNClass " + this.lnClass); - + + public boolean validateDOI( DOI doi ) { + + AbstractRiseClipseConsole.getConsole().info( "found DO " + doi.getName() + " in LNClass " + this.lnClass ); + // DOIValidator validates DOI content - String cdc = this.doMap.get(doi.getName()).getRefersToCDC().getName(); - return cdcMap.get(cdc).validateDOI(doi); - } - - - public void log(String message) { - AbstractRiseClipseConsole.getConsole().info(message); + String cdc = this.doMap.get( doi.getName() ).getRefersToCDC().getName(); + return cdcMap.get( cdc ).validateDOI( doi ); } - + + public void log( String message ) { + AbstractRiseClipseConsole.getConsole().info( message ); + } + } diff --git a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/DOIValidator.java b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/DOIValidator.java index c6039a9..60826d7 100644 --- a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/DOIValidator.java +++ b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/DOIValidator.java @@ -2,313 +2,298 @@ package fr.centralesupelec.edf.riseclipse.iec61850.scl.validator; import java.util.HashMap; import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.eclipse.emf.common.util.DiagnosticChain; -import org.eclipse.emf.common.util.EList; -import org.eclipse.emf.ecore.EClass; -import org.eclipse.emf.ecore.EDataType; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.EValidator; -import org.eclipse.emf.ecore.resource.Resource; - -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DocumentRoot; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.FunctionalConstraint; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.BasicType; import fr.centralesupelec.edf.riseclipse.iec61850.nsd.CDC; import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DataAttribute; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DataObject; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.LNClass; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.NS; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.util.NsdResourceSetImpl; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.AnyLN; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.DA; import fr.centralesupelec.edf.riseclipse.iec61850.scl.DAI; import fr.centralesupelec.edf.riseclipse.iec61850.scl.DOI; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.DOType; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.LNode; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.LNodeType; import fr.centralesupelec.edf.riseclipse.iec61850.scl.Val; import fr.centralesupelec.edf.riseclipse.util.AbstractRiseClipseConsole; public class DOIValidator { - private String cdc; - private HashMap<String, DataAttribute> daMap; - //private static Boolean test = false; - - public DOIValidator(CDC cdc) { - this.cdc = cdc.getName(); - this.daMap = new HashMap<>(); // link between DAI (name) and its respective DataAttribute - for(DataAttribute da : cdc.getDataAttribute()){ - this.daMap.put(da.getName(), da); - } - /*if(!test) { - testValidateVal(); - } - test = true;*/ - } + private String cdc; + private HashMap< String, DataAttribute > daMap; + //private static Boolean test = false; + + public DOIValidator( CDC cdc ) { + this.cdc = cdc.getName(); + this.daMap = new HashMap<>(); // link between DAI (name) and its respective DataAttribute + for( DataAttribute da : cdc.getDataAttribute() ) { + this.daMap.put( da.getName(), da ); + } + /*if(!test) { + testValidateVal(); + } + test = true;*/ + } + + public boolean validateDOI( DOI doi ) { + HashSet< String > checkedDA = new HashSet<>(); - - public boolean validateDOI(DOI doi) { - HashSet<String> checkedDA = new HashSet<>(); + for( DAI dai : doi.getDAI() ) { + AbstractRiseClipseConsole.getConsole().info( "validateDAI( " + dai.getName() + " )" ); - for( DAI dai : doi.getDAI() ) { - AbstractRiseClipseConsole.getConsole().info( "validateDAI( " + dai.getName()+ " )" ); - // Test if DAI is a possible DAI in this DOI - if(!this.daMap.containsKey(dai.getName())) { - AbstractRiseClipseConsole.getConsole().error( "DA " + dai.getName() + " not found in CDC " + this.cdc); - return false; - } - - // Control of DAI presence in DOI - String presCond = this.daMap.get(dai.getName()).getPresCond(); - this.updateCompulsory(dai.getName(), presCond, checkedDA); - - // Validation of DAI content - if( ! validateDAI(dai) ){ - return false; - } - - } + if( !this.daMap.containsKey( dai.getName() ) ) { + AbstractRiseClipseConsole.getConsole().error( "DA " + dai.getName() + " not found in CDC " + this.cdc ); + return false; + } + + // Control of DAI presence in DOI + String presCond = this.daMap.get( dai.getName() ).getPresCond(); + this.updateCompulsory( dai.getName(), presCond, checkedDA ); - // Verify all necessary DAI were present - if(!this.daMap.entrySet().stream().map(x -> checkCompulsory(x.getKey(), x.getValue().getPresCond(), checkedDA)).reduce((a, b) -> a && b).get()) { - AbstractRiseClipseConsole.getConsole().error( "DO does not contain all mandatory DA from CDC " + this.cdc); + // Validation of DAI content + if( !validateDAI( dai ) ) { + return false; + } + + } + + // Verify all necessary DAI were present + if( !this.daMap.entrySet().stream() + .map( x -> checkCompulsory( x.getKey(), x.getValue().getPresCond(), checkedDA ) ) + .reduce( ( a, b ) -> a && b ).get() ) { + AbstractRiseClipseConsole.getConsole().error( "DO does not contain all mandatory DA from CDC " + this.cdc ); return false; - } - return true; - } - - public boolean checkCompulsory(String name, String presCond, HashSet<String> checked) { - switch(presCond) { - case "M": - if(!checked.contains(name)) { - AbstractRiseClipseConsole.getConsole().error( "DA "+ name + " is missing"); - return false; - } - } - return true; + } + return true; } - - public boolean updateCompulsory(String name, String presCond, HashSet<String> checked) { - switch(presCond) { - case "M": - case "O": - if(checked.contains(name)) { - AbstractRiseClipseConsole.getConsole().error("DA " + name + " cannot appear more than once"); - return false; - } else { - checked.add(name); - break; - } - case "F": - AbstractRiseClipseConsole.getConsole().error("DA " + name + " is forbidden"); - return false; - } - return true; - } - - public boolean validateDAI(DAI dai) { + public boolean checkCompulsory( String name, String presCond, HashSet< String > checked ) { + switch( presCond ) { + case "M": + if( !checked.contains( name ) ) { + AbstractRiseClipseConsole.getConsole().error( "DA " + name + " is missing" ); + return false; + } + } + return true; + } + + public boolean updateCompulsory( String name, String presCond, HashSet< String > checked ) { + switch( presCond ) { + case "M": + case "O": + if( checked.contains( name ) ) { + AbstractRiseClipseConsole.getConsole().error( "DA " + name + " cannot appear more than once" ); + return false; + } + else { + checked.add( name ); + break; + } + case "F": + AbstractRiseClipseConsole.getConsole().error( "DA " + name + " is forbidden" ); + return false; + } + return true; + } + + public boolean validateDAI( DAI dai ) { + + AbstractRiseClipseConsole.getConsole().info( "found DA " + dai.getName() + " in CDC " + this.cdc ); - AbstractRiseClipseConsole.getConsole().info( "found DA " + dai.getName() + " in CDC " + this.cdc); - // DataAttributes that are BASIC have a BasicType which describes allowed Val of DA - DataAttribute da = this.daMap.get(dai.getName()); - if(da.getTypeKind().getName().equals("BASIC")) { - for(Val val : dai.getVal()) { - if( ! validateVal(val.getValue(), da.getType()) ) { - AbstractRiseClipseConsole.getConsole().error( "Val " + val.getValue() + " of DA " + dai.getName() + - " is not of type " + da.getType());; - return false; - } - AbstractRiseClipseConsole.getConsole().info( "Val " + val.getValue() + " of DA " + dai.getName() + - " is of type " + da.getType()); - } + DataAttribute da = this.daMap.get( dai.getName() ); + if( da.getTypeKind().getName().equals( "BASIC" ) ) { + for( Val val : dai.getVal() ) { + if( !validateVal( val.getValue(), da.getType() ) ) { + AbstractRiseClipseConsole.getConsole().error( "Val " + val.getValue() + " of DA " + dai.getName() + + " is not of type " + da.getType() ); + ; + return false; + } + AbstractRiseClipseConsole.getConsole().info( "Val " + val.getValue() + " of DA " + dai.getName() + + " is of type " + da.getType() ); + } } - + return true; - } - - public boolean validateVal(String val, String type) { - int v; - long l; - float f; - switch(type) { - case "BOOLEAN": - return (val.equals("0") || val.equals("1") || val.equals("false") || val.equals("true")); - case "INT8": - try { - v = Integer.parseInt(val); - } catch(Exception e) { - return false; - } - return v >= -128 && v <= 127; - case "INT16": - try { - v = Integer.parseInt(val); - } catch(Exception e) { - return false; - } - return v >= -32768 && v <= 32767; - case "INT32": - try { - v = Integer.parseInt(val); - } catch(Exception e) { - return false; - } - return v >= Integer.MIN_VALUE && v <= Integer.MAX_VALUE; - case "INT64": - try { - l = Long.parseLong(val); - } catch(Exception e) { - return false; - } - return l >= Long.MIN_VALUE && l <= Long.MAX_VALUE; - case "INT8U": - try { - v = Integer.parseInt(val); - } catch(Exception e) { - return false; - } - return v >= 0 && v <= 255; - case "INT16U": - try { - v = Integer.parseInt(val); - } catch(Exception e) { - return false; - } - return v >= 0 && v <= 65535; - case "INT32U": - try { - l = Long.parseLong(val); - } catch(Exception e) { - return false; - } + } + + public boolean validateVal( String val, String type ) { + int v; + long l; + float f; + switch( type ) { + case "BOOLEAN": + return( val.equals( "0" ) || val.equals( "1" ) || val.equals( "false" ) || val.equals( "true" ) ); + case "INT8": + try { + v = Integer.parseInt( val ); + } + catch( Exception e ) { + return false; + } + return v >= -128 && v <= 127; + case "INT16": + try { + v = Integer.parseInt( val ); + } + catch( Exception e ) { + return false; + } + return v >= -32768 && v <= 32767; + case "INT32": + try { + v = Integer.parseInt( val ); + } + catch( Exception e ) { + return false; + } + return v >= Integer.MIN_VALUE && v <= Integer.MAX_VALUE; + case "INT64": + try { + l = Long.parseLong( val ); + } + catch( Exception e ) { + return false; + } + return l >= Long.MIN_VALUE && l <= Long.MAX_VALUE; + case "INT8U": + try { + v = Integer.parseInt( val ); + } + catch( Exception e ) { + return false; + } + return v >= 0 && v <= 255; + case "INT16U": + try { + v = Integer.parseInt( val ); + } + catch( Exception e ) { + return false; + } + return v >= 0 && v <= 65535; + case "INT32U": + try { + l = Long.parseLong( val ); + } + catch( Exception e ) { + return false; + } String max = "4294967295"; - return l >= 0 && l <= Long.parseLong(max); - case "FLOAT32": - try { - f = Float.parseFloat(val); - } catch(Exception e) { - return false; - } - return f >= -Float.MAX_VALUE && f <= Float.MAX_VALUE; - case "Octet64": - byte[] bytes = val.getBytes(); - return bytes.length <= 64; - case "VisString64": - return val.length() <= 64; - case "VisString129": - return val.length() <= 129; - case "Unicode255": - case "VisString255": - return val.length() <= 255; - default: - return true; - } + return l >= 0 && l <= Long.parseLong( max ); + case "FLOAT32": + try { + f = Float.parseFloat( val ); + } + catch( Exception e ) { + return false; + } + return f >= -Float.MAX_VALUE && f <= Float.MAX_VALUE; + case "Octet64": + byte[] bytes = val.getBytes(); + return bytes.length <= 64; + case "VisString64": + return val.length() <= 64; + case "VisString129": + return val.length() <= 129; + case "Unicode255": + case "VisString255": + return val.length() <= 255; + default: + return true; + } } - - + public void testValidateVal() { - log("\n--\tSTART TEST\t--\n"); - assertTrue(validateVal("0", "BOOLEAN")); - assertTrue(validateVal("1", "BOOLEAN")); - assertTrue(validateVal("true", "BOOLEAN")); - assertTrue(validateVal("false", "BOOLEAN")); - assertTrue(!validateVal("2", "BOOLEAN")); - assertTrue(!validateVal("-1", "BOOLEAN")); - assertTrue(!validateVal("string", "BOOLEAN")); - log(""); - assertTrue(validateVal("1", "INT8")); - assertTrue(validateVal("0", "INT8")); - assertTrue(validateVal("-1", "INT8")); - assertTrue(validateVal("127", "INT8")); - assertTrue(validateVal("-128", "INT8")); - assertTrue(!validateVal("128", "INT8")); - assertTrue(!validateVal("-129", "INT8")); - assertTrue(!validateVal("string", "INT8")); - assertTrue(!validateVal("22.2", "INT8")); - log(""); - assertTrue(validateVal("32767", "INT16")); - assertTrue(validateVal("-32768", "INT16")); - assertTrue(!validateVal("32768", "INT16")); - assertTrue(!validateVal("-32769", "INT16")); - log(""); - assertTrue(validateVal(Integer.toString(Integer.MAX_VALUE), "INT32")); - assertTrue(validateVal(Integer.toString(Integer.MIN_VALUE), "INT32")); - assertTrue(!validateVal("2147483648", "INT32")); - assertTrue(!validateVal("-2147483649", "INT32")); - log(""); - assertTrue(validateVal(Long.toString(Long.MAX_VALUE), "INT64")); - assertTrue(validateVal(Long.toString(Long.MIN_VALUE), "INT64")); - assertTrue(!validateVal("9223372036854775808", "INT64")); - assertTrue(!validateVal("-9223372036854775809", "INT64")); - log(""); - assertTrue(validateVal("0", "INT8U")); - assertTrue(validateVal("255", "INT8U")); - assertTrue(!validateVal("256", "INT8U")); - assertTrue(!validateVal("-1", "INT8U")); - assertTrue(!validateVal("-2", "INT8U")); - log(""); - assertTrue(validateVal("0", "INT16U")); - assertTrue(validateVal("65535", "INT16U")); - assertTrue(!validateVal("65536", "INT16U")); - assertTrue(!validateVal("-1", "INT16U")); - assertTrue(!validateVal("-2", "INT16U")); - log(""); - assertTrue(validateVal("0", "INT32U")); - assertTrue(validateVal("4294967295", "INT32U")); - assertTrue(!validateVal("4294967296", "INT32U")); - assertTrue(!validateVal("-1", "INT32U")); - assertTrue(!validateVal("-2", "INT32U")); - log(""); - assertTrue(validateVal("0.0", "FLOAT32")); - assertTrue(validateVal("1.2345", "FLOAT32")); - assertTrue(validateVal("-1.2345", "FLOAT32")); - assertTrue(validateVal("100", "FLOAT32")); - assertTrue(validateVal(Float.toString(Float.MAX_VALUE), "FLOAT32")); - assertTrue(validateVal(Float.toString(-Float.MAX_VALUE), "FLOAT32")); - assertTrue(!validateVal("3.4028236E38", "FLOAT32")); - assertTrue(!validateVal("-3.4028236E38", "FLOAT32")); - assertTrue(!validateVal("string", "FLOAT32")); - log(""); - assertTrue(validateVal("1234567890123456789012345678901234567890123456789012345678901234", "Octet64")); - assertTrue(!validateVal("12345678901234567890123456789012345678901234567890123456789012345", "Octet64")); - log(""); - assertTrue(validateVal("1234567890123456789012345678901234567890123456789012345678901234", "VisString64")); - assertTrue(!validateVal("12345678901234567890123456789012345678901234567890123456789012345", "VisString64")); - log(""); - assertTrue(validateVal("1234567890123456789012345678901234567890123456789012345678901234" - + "12345678901234567890123456789012345678901234567890123456789012345", "VisString129")); - assertTrue(!validateVal("1234567890123456789012345678901234567890123456789012345678901234" - + "123456789012345678901234567890123456789012345678901234567890123456", "VisString129")); - log(""); - assertTrue(validateVal("1234567890123456789012345678901234567890123456789012345678901234" - + "1234567890123456789012345678901234567890123456789012345678901234" - + "1234567890123456789012345678901234567890123456789012345678901234" - + "123456789012345678901234567890123456789012345678901234567890123", "VisString255")); - assertTrue(!validateVal("1234567890123456789012345678901234567890123456789012345678901234" - + "1234567890123456789012345678901234567890123456789012345678901234" - + "1234567890123456789012345678901234567890123456789012345678901234" - + "1234567890123456789012345678901234567890123456789012345678901234", "VisString255")); + log( "\n--\tSTART TEST\t--\n" ); + assertTrue( validateVal( "0", "BOOLEAN" ) ); + assertTrue( validateVal( "1", "BOOLEAN" ) ); + assertTrue( validateVal( "true", "BOOLEAN" ) ); + assertTrue( validateVal( "false", "BOOLEAN" ) ); + assertTrue( !validateVal( "2", "BOOLEAN" ) ); + assertTrue( !validateVal( "-1", "BOOLEAN" ) ); + assertTrue( !validateVal( "string", "BOOLEAN" ) ); + log( "" ); + assertTrue( validateVal( "1", "INT8" ) ); + assertTrue( validateVal( "0", "INT8" ) ); + assertTrue( validateVal( "-1", "INT8" ) ); + assertTrue( validateVal( "127", "INT8" ) ); + assertTrue( validateVal( "-128", "INT8" ) ); + assertTrue( !validateVal( "128", "INT8" ) ); + assertTrue( !validateVal( "-129", "INT8" ) ); + assertTrue( !validateVal( "string", "INT8" ) ); + assertTrue( !validateVal( "22.2", "INT8" ) ); + log( "" ); + assertTrue( validateVal( "32767", "INT16" ) ); + assertTrue( validateVal( "-32768", "INT16" ) ); + assertTrue( !validateVal( "32768", "INT16" ) ); + assertTrue( !validateVal( "-32769", "INT16" ) ); + log( "" ); + assertTrue( validateVal( Integer.toString( Integer.MAX_VALUE ), "INT32" ) ); + assertTrue( validateVal( Integer.toString( Integer.MIN_VALUE ), "INT32" ) ); + assertTrue( !validateVal( "2147483648", "INT32" ) ); + assertTrue( !validateVal( "-2147483649", "INT32" ) ); + log( "" ); + assertTrue( validateVal( Long.toString( Long.MAX_VALUE ), "INT64" ) ); + assertTrue( validateVal( Long.toString( Long.MIN_VALUE ), "INT64" ) ); + assertTrue( !validateVal( "9223372036854775808", "INT64" ) ); + assertTrue( !validateVal( "-9223372036854775809", "INT64" ) ); + log( "" ); + assertTrue( validateVal( "0", "INT8U" ) ); + assertTrue( validateVal( "255", "INT8U" ) ); + assertTrue( !validateVal( "256", "INT8U" ) ); + assertTrue( !validateVal( "-1", "INT8U" ) ); + assertTrue( !validateVal( "-2", "INT8U" ) ); + log( "" ); + assertTrue( validateVal( "0", "INT16U" ) ); + assertTrue( validateVal( "65535", "INT16U" ) ); + assertTrue( !validateVal( "65536", "INT16U" ) ); + assertTrue( !validateVal( "-1", "INT16U" ) ); + assertTrue( !validateVal( "-2", "INT16U" ) ); + log( "" ); + assertTrue( validateVal( "0", "INT32U" ) ); + assertTrue( validateVal( "4294967295", "INT32U" ) ); + assertTrue( !validateVal( "4294967296", "INT32U" ) ); + assertTrue( !validateVal( "-1", "INT32U" ) ); + assertTrue( !validateVal( "-2", "INT32U" ) ); + log( "" ); + assertTrue( validateVal( "0.0", "FLOAT32" ) ); + assertTrue( validateVal( "1.2345", "FLOAT32" ) ); + assertTrue( validateVal( "-1.2345", "FLOAT32" ) ); + assertTrue( validateVal( "100", "FLOAT32" ) ); + assertTrue( validateVal( Float.toString( Float.MAX_VALUE ), "FLOAT32" ) ); + assertTrue( validateVal( Float.toString( -Float.MAX_VALUE ), "FLOAT32" ) ); + assertTrue( !validateVal( "3.4028236E38", "FLOAT32" ) ); + assertTrue( !validateVal( "-3.4028236E38", "FLOAT32" ) ); + assertTrue( !validateVal( "string", "FLOAT32" ) ); + log( "" ); + assertTrue( validateVal( "1234567890123456789012345678901234567890123456789012345678901234", "Octet64" ) ); + assertTrue( !validateVal( "12345678901234567890123456789012345678901234567890123456789012345", "Octet64" ) ); + log( "" ); + assertTrue( validateVal( "1234567890123456789012345678901234567890123456789012345678901234", "VisString64" ) ); + assertTrue( + !validateVal( "12345678901234567890123456789012345678901234567890123456789012345", "VisString64" ) ); + log( "" ); + assertTrue( validateVal( "1234567890123456789012345678901234567890123456789012345678901234" + + "12345678901234567890123456789012345678901234567890123456789012345", "VisString129" ) ); + assertTrue( !validateVal( "1234567890123456789012345678901234567890123456789012345678901234" + + "123456789012345678901234567890123456789012345678901234567890123456", "VisString129" ) ); + log( "" ); + assertTrue( validateVal( "1234567890123456789012345678901234567890123456789012345678901234" + + "1234567890123456789012345678901234567890123456789012345678901234" + + "1234567890123456789012345678901234567890123456789012345678901234" + + "123456789012345678901234567890123456789012345678901234567890123", "VisString255" ) ); + assertTrue( !validateVal( "1234567890123456789012345678901234567890123456789012345678901234" + + "1234567890123456789012345678901234567890123456789012345678901234" + + "1234567890123456789012345678901234567890123456789012345678901234" + + "1234567890123456789012345678901234567890123456789012345678901234", "VisString255" ) ); } - public void assertTrue(Boolean b) { - if(b) { - log("Check"); - } else { - log("Error"); - } + public void assertTrue( Boolean b ) { + if( b ) { + log( "Check" ); + } + else { + log( "Error" ); + } } - - public void log(String message) { - AbstractRiseClipseConsole.getConsole().info(message); + + public void log( String message ) { + AbstractRiseClipseConsole.getConsole().info( message ); } } diff --git a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdEObjectValidator.java b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdEObjectValidator.java index 06161bc..26df2e6 100644 --- a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdEObjectValidator.java +++ b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdEObjectValidator.java @@ -19,62 +19,41 @@ package fr.centralesupelec.edf.riseclipse.iec61850.scl.validator; import java.util.HashMap; -import java.util.HashSet; -import java.util.List; import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; - import org.eclipse.emf.common.util.DiagnosticChain; -import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EDataType; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EValidator; -import org.eclipse.emf.ecore.resource.Resource; - -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DocumentRoot; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.FunctionalConstraint; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.BasicType; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.CDC; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DataAttribute; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.DataObject; import fr.centralesupelec.edf.riseclipse.iec61850.nsd.LNClass; -import fr.centralesupelec.edf.riseclipse.iec61850.nsd.NS; import fr.centralesupelec.edf.riseclipse.iec61850.nsd.util.NsdResourceSetImpl; import fr.centralesupelec.edf.riseclipse.iec61850.scl.AnyLN; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.DA; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.DAI; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.DOI; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.DOType; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.LNode; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.LNodeType; -import fr.centralesupelec.edf.riseclipse.iec61850.scl.Val; import fr.centralesupelec.edf.riseclipse.util.AbstractRiseClipseConsole; public class NsdEObjectValidator implements EValidator { - + private NsdResourceSetImpl nsdResourceSet; - private HashMap<String, AnyLNValidator> lnMap; - + private HashMap< String, AnyLNValidator > lnMap; + public NsdEObjectValidator( NsdResourceSetImpl nsdResourceSet ) { this.nsdResourceSet = nsdResourceSet; } public void initializeValidationData() { - this.lnMap= this.nsdResourceSet.getLNClassStream() - .map(lnClass -> generateValidators(lnClass)) - .reduce((a, b) -> {a.putAll(b); return a;}).get(); + this.lnMap = this.nsdResourceSet.getLNClassStream() + .map( lnClass -> generateValidators( lnClass ) ) + .reduce( ( a, b ) -> { + a.putAll( b ); + return a; + } ).get(); } - public HashMap<String, AnyLNValidator> generateValidators(LNClass lnClass) { - HashMap<String, AnyLNValidator> lnMap = new HashMap<>(); - lnMap.put(lnClass.getName(), new AnyLNValidator(lnClass)); - return lnMap; + public HashMap< String, AnyLNValidator > generateValidators( LNClass lnClass ) { + HashMap< String, AnyLNValidator > lnMap = new HashMap<>(); + lnMap.put( lnClass.getName(), new AnyLNValidator( lnClass ) ); + return lnMap; } - @Override public boolean validate( EObject eObject, DiagnosticChain diagnostics, Map< Object, Object > context ) { return validate( eObject.eClass(), eObject, diagnostics, context ); @@ -83,18 +62,18 @@ public class NsdEObjectValidator implements EValidator { @Override public boolean validate( EClass eClass, EObject eObject, DiagnosticChain diagnostics, Map< Object, Object > context ) { - - if(this.lnMap == null) { - this.initializeValidationData(); - } - - switch(eClass.getName()) { + + if( this.lnMap == null ) { + this.initializeValidationData(); + } + + switch( eClass.getName() ) { case "LN0": case "LN": - AnyLN ln = (AnyLN) eObject; - return validateLN(ln); + AnyLN ln = ( AnyLN ) eObject; + return validateLN( ln ); default: - return false; + return false; } } @@ -102,29 +81,27 @@ public class NsdEObjectValidator implements EValidator { public boolean validate( EDataType eDataType, Object value, DiagnosticChain diagnostics, Map< Object, Object > context ) { //AbstractRiseClipseConsole.getConsole().info( "NSDEObjectValidator.validate( EDataType ): " + eDataType.getName() ); - + // TODO: use nsdResource to validate value - return true; } - - - public boolean validateLN(AnyLN ln) { - AbstractRiseClipseConsole.getConsole().info(""); + + public boolean validateLN( AnyLN ln ) { + AbstractRiseClipseConsole.getConsole().info( "" ); AbstractRiseClipseConsole.getConsole().info( "NSDEObjectValidator.validateLN( " + ln.getLnClass() + " )" ); - + //LN has valid LNClass - if( ! this.lnMap.containsKey(ln.getLnClass()) ) { + if( !this.lnMap.containsKey( ln.getLnClass() ) ) { AbstractRiseClipseConsole.getConsole().error( "LNClass " + ln.getLnClass() + " not found in NSD files" ); - return false; + return false; } AbstractRiseClipseConsole.getConsole().info( "found LNClass " + ln.getLnClass() + " in NSD files" ); - + //AnyLNValidator validates LN content - return lnMap.get(ln.getLnClass()).validateLN(ln); + return lnMap.get( ln.getLnClass() ).validateLN( ln ); } - + /* public boolean validateLN(AnyLN ln) { AbstractRiseClipseConsole.getConsole().info(""); @@ -133,17 +110,17 @@ public class NsdEObjectValidator implements EValidator { if( ! this.lnMap.containsKey(ln.getLnClass()) ) { AbstractRiseClipseConsole.getConsole().error( "LNClass " + ln.getLnClass() + " not found in NSD files" ); - return false; + return false; } AbstractRiseClipseConsole.getConsole().info( "found LNClass " + ln.getLnClass() + " in NSD files" ); - + HashSet<String> checkedDO = new HashSet<>(); /* Optional< LNClass > lnClassFound = nsdResourceSet.getLNClassStream().filter( lNClass -> lNClass.getName().equals( ln.getLnClass() )).findAny(); if( ! lnClassFound.isPresent() ) { AbstractRiseClipseConsole.getConsole().error( "LNClass " + ln.getLnClass() + " not found in NSD files" ); - return false; + return false; } AbstractRiseClipseConsole.getConsole().info( "found LNClass " + ln.getLnClass() + " in NSD files" ); @@ -152,7 +129,7 @@ public class NsdEObjectValidator implements EValidator { for( DOI doi : ln.getDOI() ) { if(!this.lnMap.get(ln.getLnClass()).containsKey(doi.getName())) { - AbstractRiseClipseConsole.getConsole().error( "DO " + doi.getName() + " not found in LNClass " + ln.getLnClass()); + AbstractRiseClipseConsole.getConsole().error( "DO " + doi.getName() + " not found in LNClass " + ln.getLnClass()); return false; } @@ -172,9 +149,9 @@ public class NsdEObjectValidator implements EValidator { return false; } - if( ! validateDO(doi) ){ - return false; - } + if( ! validateDO(doi) ){ + return false; + } } // TODO: check that compulsory DataObject in lnClassFound are present in ln if(!this.lnMap.get(ln.getLnClass()).entrySet().stream().map(x -> checkCompulsory(x.getKey(), x.getValue(), checkedDO)).reduce((a, b) -> a && b).get()) { @@ -184,7 +161,7 @@ public class NsdEObjectValidator implements EValidator { return true; } */ - + /* public boolean checkCompulsory(String dObj, String presCond, HashSet<String> checked) { switch(presCond) { @@ -197,39 +174,39 @@ public class NsdEObjectValidator implements EValidator { return true; } */ - + /* public boolean validateDO(DOI doi) { return true; } */ - + /* - public boolean validateDO(EList<DOI> lnDOI, LNClass lnClassFound) { + public boolean validateDO(EList<DOI> lnDOI, LNClass lnClassFound) { for( DOI doi : lnDOI ) { - Optional< DataObject > dataObjectFound = lnClassFound.getDataObject().stream().filter( dataObject -> dataObject.getName().equals( doi.getName()) ).findAny(); - AbstractRiseClipseConsole.getConsole().info(" "); - AbstractRiseClipseConsole.getConsole().info( "validateDO( " + doi.getName() + " )" ); - if( ! dataObjectFound.isPresent() ) { - AbstractRiseClipseConsole.getConsole().error( "DO " + doi.getName() + " not found in LNClass " + lnClassFound.getName()); - return false; - } - - try { - presenceDO = updatePresenceDO(presenceDO, dataObjectFound.get()); - } catch(Exception e) { - AbstractRiseClipseConsole.getConsole().error( "LN contains forbidden DO for class " + lnClassFound.getName()); - return false; - } - - // dataObjectFound refers to a CDC which describes allowed DAI in DOI - CDC cdcFound = dataObjectFound.get().getRefersToCDC(); - AbstractRiseClipseConsole.getConsole().info( "found DO " + doi.getName() + " (CDC: " + cdcFound.getName() + ") in LNClass " + lnClassFound.getName()); - if( ! validateDA(doi.getDAI(), cdcFound) ) { - return false; - } - - // TODO: check that compulsory DataAttribute in cdcFound are present in doi + Optional< DataObject > dataObjectFound = lnClassFound.getDataObject().stream().filter( dataObject -> dataObject.getName().equals( doi.getName()) ).findAny(); + AbstractRiseClipseConsole.getConsole().info(" "); + AbstractRiseClipseConsole.getConsole().info( "validateDO( " + doi.getName() + " )" ); + if( ! dataObjectFound.isPresent() ) { + AbstractRiseClipseConsole.getConsole().error( "DO " + doi.getName() + " not found in LNClass " + lnClassFound.getName()); + return false; + } + + try { + presenceDO = updatePresenceDO(presenceDO, dataObjectFound.get()); + } catch(Exception e) { + AbstractRiseClipseConsole.getConsole().error( "LN contains forbidden DO for class " + lnClassFound.getName()); + return false; + } + + // dataObjectFound refers to a CDC which describes allowed DAI in DOI + CDC cdcFound = dataObjectFound.get().getRefersToCDC(); + AbstractRiseClipseConsole.getConsole().info( "found DO " + doi.getName() + " (CDC: " + cdcFound.getName() + ") in LNClass " + lnClassFound.getName()); + if( ! validateDA(doi.getDAI(), cdcFound) ) { + return false; + } + + // TODO: check that compulsory DataAttribute in cdcFound are present in doi } if(presenceDO.get("mandatory").size() > 0) { @@ -259,7 +236,7 @@ public class NsdEObjectValidator implements EValidator { } sets.put("mandatory", mandatory); sets.put("forbidden", forbidden); - return sets; + return sets; } public HashMap <String, HashSet<String>> updatePresenceDO(HashMap <String, HashSet<String>> sets, DataObject dObj) throws Exception { @@ -268,8 +245,8 @@ public class NsdEObjectValidator implements EValidator { switch(dObj.getPresCond()) { case "M": case "AtLeastOne": - mandatory.remove(dObj.getName()); - break; + mandatory.remove(dObj.getName()); + break; case "AtMostOne": if(forbidden.contains(dObj.getName())) { throw new Exception("Forbidden"); @@ -278,46 +255,46 @@ public class NsdEObjectValidator implements EValidator { break; case "F": throw new Exception("Forbidden"); - default: - break; + default: + break; } return sets; } */ - + /* public boolean validateDA(EList<DAI> doiDAI, CDC cdcFound) { HashMap<String, HashSet<String>> presenceDA = generatePresenceDA(cdcFound); for( DAI dai : doiDAI ) { - AbstractRiseClipseConsole.getConsole().info(" "); - AbstractRiseClipseConsole.getConsole().info( "validateDA( " + dai.getName() + " )" ); - Optional< DataAttribute > dataAttributeFound = cdcFound.getDataAttribute().stream().filter( dataAttribute -> dataAttribute.getName().equals( dai.getName() ) ).findAny(); - - if( ! dataAttributeFound.isPresent() ) { - AbstractRiseClipseConsole.getConsole().error( "DA " + dai.getName() + " not found in CDC " + cdcFound.getName()); - return false; - } - AbstractRiseClipseConsole.getConsole().info( "found DA " + dai.getName() + " in CDC " + cdcFound.getName()); - - try { - presenceDA = updatePresenceDA(presenceDA, dataAttributeFound.get()); - } catch(Exception e) { - AbstractRiseClipseConsole.getConsole().error( "DO contains forbidden DA for class " + cdcFound.getName()); - return false; - } - - // dataAttributeFound that are BASIC have a BasicType which describes allowed Val of DA - if(dataAttributeFound.get().getTypeKind().getName().equals("BASIC")) { - for(Val val : dai.getVal()) { - if( ! validateVal(val.getValue(), dataAttributeFound.get().getType()) ) { - AbstractRiseClipseConsole.getConsole().error( "Val " + val.getValue() + " of DA " + dai.getName() + - " is not of type " + dataAttributeFound.get().getType());; - return false; - } - AbstractRiseClipseConsole.getConsole().info( "Val " + val.getValue() + " of DA " + dai.getName() + - " is of type " + dataAttributeFound.get().getType()); - } - } + AbstractRiseClipseConsole.getConsole().info(" "); + AbstractRiseClipseConsole.getConsole().info( "validateDA( " + dai.getName() + " )" ); + Optional< DataAttribute > dataAttributeFound = cdcFound.getDataAttribute().stream().filter( dataAttribute -> dataAttribute.getName().equals( dai.getName() ) ).findAny(); + + if( ! dataAttributeFound.isPresent() ) { + AbstractRiseClipseConsole.getConsole().error( "DA " + dai.getName() + " not found in CDC " + cdcFound.getName()); + return false; + } + AbstractRiseClipseConsole.getConsole().info( "found DA " + dai.getName() + " in CDC " + cdcFound.getName()); + + try { + presenceDA = updatePresenceDA(presenceDA, dataAttributeFound.get()); + } catch(Exception e) { + AbstractRiseClipseConsole.getConsole().error( "DO contains forbidden DA for class " + cdcFound.getName()); + return false; + } + + // dataAttributeFound that are BASIC have a BasicType which describes allowed Val of DA + if(dataAttributeFound.get().getTypeKind().getName().equals("BASIC")) { + for(Val val : dai.getVal()) { + if( ! validateVal(val.getValue(), dataAttributeFound.get().getType()) ) { + AbstractRiseClipseConsole.getConsole().error( "Val " + val.getValue() + " of DA " + dai.getName() + + " is not of type " + dataAttributeFound.get().getType());; + return false; + } + AbstractRiseClipseConsole.getConsole().info( "Val " + val.getValue() + " of DA " + dai.getName() + + " is of type " + dataAttributeFound.get().getType()); + } + } } if(presenceDA.get("mandatory").size() > 0) { @@ -347,7 +324,7 @@ public class NsdEObjectValidator implements EValidator { } sets.put("mandatory", mandatory); sets.put("forbidden", forbidden); - return sets; + return sets; } public HashMap <String, HashSet<String>> updatePresenceDA(HashMap <String, HashSet<String>> sets, DataAttribute da) throws Exception { @@ -356,8 +333,8 @@ public class NsdEObjectValidator implements EValidator { switch(da.getPresCond()) { case "M": case "AtLeastOne": - mandatory.remove(da.getName()); - break; + mandatory.remove(da.getName()); + break; case "AtMostOne": if(forbidden.contains(da.getName())) { throw new Exception("Forbidden"); @@ -366,13 +343,13 @@ public class NsdEObjectValidator implements EValidator { break; case "F": throw new Exception("Forbidden"); - default: - break; + default: + break; } return sets; } - */ - + */ + /* public boolean validateVal(String val, String type) { int v; @@ -421,10 +398,9 @@ public class NsdEObjectValidator implements EValidator { } } */ - - public void log(String message) { - AbstractRiseClipseConsole.getConsole().info(message); + + public void log( String message ) { + AbstractRiseClipseConsole.getConsole().info( message ); } - } diff --git a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdModelLoader.java b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdModelLoader.java index 68fc9a8..3e7058a 100644 --- a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdModelLoader.java +++ b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdModelLoader.java @@ -18,7 +18,6 @@ */ package fr.centralesupelec.edf.riseclipse.iec61850.scl.validator; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -42,25 +41,25 @@ import fr.centralesupelec.edf.riseclipse.util.IRiseClipseConsole; import fr.centralesupelec.edf.riseclipse.util.RiseClipseModelLoader; import fr.centralesupelec.edf.riseclipse.util.TextRiseClipseConsole; -public class NsdModelLoader extends RiseClipseModelLoader { - +public class NsdModelLoader extends RiseClipseModelLoader { + public NsdModelLoader( IRiseClipseConsole console ) { super( console ); } @Override public void reset() { - super.reset(new NsdResourceSetImpl(true, console) ); + super.reset( new NsdResourceSetImpl( true, console ) ); // Register the appropriate resource factory to handle all file // extensions. getResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap() - .put( Resource.Factory.Registry.DEFAULT_EXTENSION, new NsdResourceFactoryImpl() ); + .put( Resource.Factory.Registry.DEFAULT_EXTENSION, new NsdResourceFactoryImpl() ); // Register the package to ensure it is available during loading. getResourceSet().getPackageRegistry().put( NsdPackage.eNS_URI, NsdPackage.eINSTANCE ); } - + @Override public NsdResourceSetImpl getResourceSet() { return ( NsdResourceSetImpl ) super.getResourceSet(); @@ -70,48 +69,49 @@ public class NsdModelLoader extends RiseClipseModelLoader { Object eValidator = EValidator.Registry.INSTANCE.remove( NsdPackage.eINSTANCE ); Resource resource = load( name ); - + if( eValidator != null ) { EValidator.Registry.INSTANCE.put( NsdPackage.eINSTANCE, eValidator ); } return resource; } - + public static void main( String[] args ) { IRiseClipseConsole console = new TextRiseClipseConsole(); //console.setLevel( IRiseClipseConsole.ERROR_LEVEL ); NsdModelLoader loader = new NsdModelLoader( console ); - + org.eclipse.ocl.xtext.oclinecore.OCLinEcoreStandaloneSetup.doSetup(); - - Map<Object, Object> context = new HashMap< Object, Object >(); + + Map< Object, Object > context = new HashMap< Object, Object >(); SubstitutionLabelProvider substitutionLabelProvider = new EValidator.SubstitutionLabelProvider() { - + @Override public String getValueLabel( EDataType eDataType, Object value ) { return Diagnostician.INSTANCE.getValueLabel( eDataType, value ); } - + @Override public String getObjectLabel( EObject eObject ) { NsdItemProviderAdapterFactory adapter = new NsdItemProviderAdapterFactory(); - IItemLabelProvider labelProvider = ( IItemLabelProvider ) adapter .adapt( eObject, IItemLabelProvider.class ); + IItemLabelProvider labelProvider = ( IItemLabelProvider ) adapter.adapt( eObject, + IItemLabelProvider.class ); return labelProvider.getText( eObject ); } - + @Override public String getFeatureLabel( EStructuralFeature eStructuralFeature ) { return Diagnostician.INSTANCE.getFeatureLabel( eStructuralFeature ); } }; - context.put(EValidator.SubstitutionLabelProvider.class, substitutionLabelProvider ); + context.put( EValidator.SubstitutionLabelProvider.class, substitutionLabelProvider ); for( int i = 0; i < args.length; ++i ) { Resource resource = loader.load( args[i] ); if( resource == null ) continue; if( resource.getContents().size() == 0 ) continue; Diagnostic diagnostic = Diagnostician.INSTANCE.validate( resource.getContents().get( 0 ), context ); - + if( diagnostic.getSeverity() == Diagnostic.ERROR || diagnostic.getSeverity() == Diagnostic.WARNING ) { for( Iterator< Diagnostic > d = diagnostic.getChildren().iterator(); d.hasNext(); ) { Diagnostic childDiagnostic = d.next(); diff --git a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdValidator.java b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdValidator.java index 94431f5..03548c4 100644 --- a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdValidator.java +++ b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/NsdValidator.java @@ -45,9 +45,9 @@ public class NsdValidator { public NsdValidator( @NonNull ComposedEValidator validator, IRiseClipseConsole console ) { nsdLoader = new NsdModelLoader( console ); - validator.addChild( new NsdEObjectValidator( nsdLoader.getResourceSet() )); + validator.addChild( new NsdEObjectValidator( nsdLoader.getResourceSet() ) ); } - + public void addNsdDocument( String nsdFile, IRiseClipseConsole console ) { console.info( "Loading nsd: " + nsdFile ); nsdLoader.load( nsdFile ); @@ -55,31 +55,32 @@ public class NsdValidator { public void validate( Resource resource, final AdapterFactory adapter, IRiseClipseConsole console ) { nsdLoader.getResourceSet().finalizeLoad( console ); - - Map<Object, Object> context = new HashMap< Object, Object >(); + + Map< Object, Object > context = new HashMap< Object, Object >(); SubstitutionLabelProvider substitutionLabelProvider = new EValidator.SubstitutionLabelProvider() { - + @Override public String getValueLabel( EDataType eDataType, Object value ) { return Diagnostician.INSTANCE.getValueLabel( eDataType, value ); } - + @Override public String getObjectLabel( EObject eObject ) { - IItemLabelProvider labelProvider = ( IItemLabelProvider ) adapter.adapt( eObject, IItemLabelProvider.class ); + IItemLabelProvider labelProvider = ( IItemLabelProvider ) adapter.adapt( eObject, + IItemLabelProvider.class ); return labelProvider.getText( eObject ); } - + @Override public String getFeatureLabel( EStructuralFeature eStructuralFeature ) { return Diagnostician.INSTANCE.getFeatureLabel( eStructuralFeature ); } }; - context.put(EValidator.SubstitutionLabelProvider.class, substitutionLabelProvider ); - + context.put( EValidator.SubstitutionLabelProvider.class, substitutionLabelProvider ); + for( int n = 0; n < resource.getContents().size(); ++n ) { Diagnostic diagnostic = Diagnostician.INSTANCE.validate( resource.getContents().get( n ), context ); - + if( diagnostic.getSeverity() == Diagnostic.ERROR || diagnostic.getSeverity() == Diagnostic.WARNING ) { //EObject root = ( EObject ) diagnostic.getData().get( 0 ); //URI uri = root.eResource().getURI(); @@ -97,7 +98,9 @@ public class NsdValidator { else if( data.get( 1 ) instanceof EAttribute ) { EAttribute attribute = ( EAttribute ) data.get( 1 ); if( attribute == null ) continue; - console.error( "\tAttribute " + attribute.getName() + " of " + substitutionLabelProvider.getObjectLabel( object ) + " : " + childDiagnostic.getChildren().get( 0 ).getMessage() ); + console.error( "\tAttribute " + attribute.getName() + " of " + + substitutionLabelProvider.getObjectLabel( object ) + " : " + + childDiagnostic.getChildren().get( 0 ).getMessage() ); } else { console.error( "\t" + childDiagnostic.getMessage() ); diff --git a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/RiseClipseValidatorSCL.java b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/RiseClipseValidatorSCL.java index c4ff345..afa0175 100644 --- a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/RiseClipseValidatorSCL.java +++ b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/RiseClipseValidatorSCL.java @@ -18,7 +18,6 @@ */ package fr.centralesupelec.edf.riseclipse.iec61850.scl.validator; -import java.io.File; import java.util.ArrayList; import fr.centralesupelec.edf.riseclipse.iec61850.nsd.provider.NsdItemProviderAdapterFactory; @@ -26,10 +25,6 @@ import fr.centralesupelec.edf.riseclipse.iec61850.scl.SclPackage; import fr.centralesupelec.edf.riseclipse.iec61850.scl.provider.SclItemProviderAdapterFactory; import fr.centralesupelec.edf.riseclipse.util.IRiseClipseConsole; import fr.centralesupelec.edf.riseclipse.util.TextRiseClipseConsole; -import fr.centralesupelec.edf.riseclipse.validation.ocl.OCLValidator; - -import org.eclipse.emf.ecore.EValidator; -//import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.ocl.pivot.validation.ComposedEValidator; @@ -46,33 +41,34 @@ public class RiseClipseValidatorSCL { public static void usage( IRiseClipseConsole console ) { console.setLevel( IRiseClipseConsole.INFO_LEVEL ); - console.info( "java -jar RiseClipseValidatorSCL.jar [--verbose] [--make-explicit-links] [<oclFile> | <nsdFile> | <sclFile>]*" ); + console.info( + "java -jar RiseClipseValidatorSCL.jar [--verbose] [--make-explicit-links] [<oclFile> | <nsdFile> | <sclFile>]*" ); console.info( /*"Files ending with \".ocl\" are considered OCL files, " - +*/ "files ending with \\\".nsd\\\" are considered NSD files, " - + "all others are considered SCL files" ); + +*/ "files ending with \\\".nsd\\\" are considered NSD files, " + + "all others are considered SCL files" ); System.exit( -1 ); } public static void main( String[] args ) { - + final IRiseClipseConsole console = new TextRiseClipseConsole(); - + console.setLevel( IRiseClipseConsole.INFO_LEVEL ); displayLegal( console ); console.setLevel( IRiseClipseConsole.WARNING_LEVEL ); - + if( args.length == 0 ) usage( console ); - + boolean make_explicit_links = false; int posFiles = 0; for( int i = 0; i < args.length; ++i ) { - if( args[i].startsWith( "--" )) { + if( args[i].startsWith( "--" ) ) { posFiles = i + 1; - if( "--verbose".equals( args[i] )) { + if( "--verbose".equals( args[i] ) ) { console.setLevel( IRiseClipseConsole.INFO_LEVEL ); } - else if( "--make-explicit-links".equals( args[i] )) { + else if( "--make-explicit-links".equals( args[i] ) ) { make_explicit_links = true; } else { @@ -90,7 +86,7 @@ public class RiseClipseValidatorSCL { oclFiles.add( args[i] ); oclValidation = true; } - else*/ if( args[i].endsWith( ".nsd" )) { + else*/ if( args[i].endsWith( ".nsd" ) ) { nsdFiles.add( args[i] ); nsdValidation = true; } @@ -98,17 +94,19 @@ public class RiseClipseValidatorSCL { sclFiles.add( args[i] ); } } - + prepare( console, /*oclFiles,*/ nsdFiles ); for( int i = 0; i < sclFiles.size(); ++i ) { - run( console, make_explicit_links, sclFiles.get( i )); + run( console, make_explicit_links, sclFiles.get( i ) ); } } - + public static void displayLegal( IRiseClipseConsole console ) { console.info( "Copyright (c) 2019 CentraleSupélec & EDF." ); - console.info( "All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0" ); - console.info( "which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html" ); + console.info( + "All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0" ); + console.info( + "which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html" ); console.info( "" ); console.info( "This file is part of the RiseClipse tool." ); console.info( "Contributors:" ); @@ -124,18 +122,19 @@ public class RiseClipseValidatorSCL { console.info( "" ); } - public static void prepare( IRiseClipseConsole console, /*ArrayList< String > oclFiles,*/ ArrayList< String > nsdFiles ) { + public static void prepare( IRiseClipseConsole console, + /*ArrayList< String > oclFiles,*/ ArrayList< String > nsdFiles ) { @NonNull ComposedEValidator validator = ComposedEValidator.install( SclPackage.eINSTANCE ); - + /*if( oclValidation ) { oclValidator = new OCLValidator( validator, true ); - + for( int i = 0; i < oclFiles.size(); ++i ) { oclValidator.addOCLDocument( oclFiles.get( i ), console ); } }*/ - + if( nsdValidation ) { nsdValidator = new NsdValidator( validator, console ); for( int i = 0; i < nsdFiles.size(); ++i ) { @@ -146,13 +145,13 @@ public class RiseClipseValidatorSCL { sclLoader = new SCLModelLoader( console ); sclAdapter = new SclItemProviderAdapterFactory(); - - /*for(EValidator v: validator.getChildren()) { - if(v.getClass() == NsdEObjectValidator.class) { - NsdEObjectValidator nsdValidator = (NsdEObjectValidator) v; - nsdValidator.initializeValidationData(); - } - }*/ + + /*for(EValidator v: validator.getChildren()) { + if(v.getClass() == NsdEObjectValidator.class) { + NsdEObjectValidator nsdValidator = (NsdEObjectValidator) v; + nsdValidator.initializeValidationData(); + } + }*/ } public static void run( IRiseClipseConsole console, boolean make_explicit_links, String sclFile ) { @@ -171,8 +170,7 @@ public class RiseClipseValidatorSCL { console.info( "Validating file: " + sclFile + " with NSD" ); nsdValidator.validate( resource, sclAdapter, console ); } - } + } } } - diff --git a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/SCLModelLoader.java b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/SCLModelLoader.java index 59c99bd..f725dab 100644 --- a/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/SCLModelLoader.java +++ b/fr.centralesupelec.edf.riseclipse.iec61850.scl.tools/src/fr/centralesupelec/edf/riseclipse/iec61850/scl/validator/SCLModelLoader.java @@ -27,9 +27,8 @@ import fr.centralesupelec.edf.riseclipse.util.IRiseClipseConsole; import fr.centralesupelec.edf.riseclipse.util.RiseClipseModelLoader; import fr.centralesupelec.edf.riseclipse.util.TextRiseClipseConsole; - public class SCLModelLoader extends RiseClipseModelLoader { - + public SCLModelLoader( IRiseClipseConsole console ) { super( console ); } @@ -41,27 +40,27 @@ public class SCLModelLoader extends RiseClipseModelLoader { // Register the appropriate resource factory to handle all file // extensions. getResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap() - .put( Resource.Factory.Registry.DEFAULT_EXTENSION, new SclResourceFactoryImpl() ); + .put( Resource.Factory.Registry.DEFAULT_EXTENSION, new SclResourceFactoryImpl() ); // Register the package to ensure it is available during loading. getResourceSet().getPackageRegistry().put( SclPackage.eNS_URI, SclPackage.eINSTANCE ); } - + public Resource loadWithoutValidation( String name ) { Object eValidator = EValidator.Registry.INSTANCE.remove( SclPackage.eINSTANCE ); Resource resource = load( name ); - + if( eValidator != null ) { EValidator.Registry.INSTANCE.put( SclPackage.eINSTANCE, eValidator ); } return resource; } - + public static void main( String[] args ) { IRiseClipseConsole console = new TextRiseClipseConsole(); SCLModelLoader loader = new SCLModelLoader( console ); - + for( int i = 0; i < args.length; ++i ) { @SuppressWarnings( "unused" ) Resource resource = loader.load( args[i] ); -- GitLab