Skip to content
Snippets Groups Projects
Commit 20dcb1df authored by Dominique Marcadet's avatar Dominique Marcadet
Browse files

move execution of validation from validator to main

parent 02d9b600
No related branches found
No related tags found
1 merge request!3Resolve "Implement NSD based validation"
......@@ -18,22 +18,7 @@
*/
package fr.centralesupelec.edf.riseclipse.iec61850.scl.validator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EValidator;
import org.eclipse.emf.ecore.EValidator.SubstitutionLabelProvider;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.ocl.pivot.validation.ComposedEValidator;
......@@ -43,9 +28,8 @@ public class NsdValidator {
private NsdModelLoader nsdLoader;
public NsdValidator( @NonNull ComposedEValidator validator, IRiseClipseConsole console ) {
public NsdValidator( @NonNull EPackage modelPackage, IRiseClipseConsole console ) {
nsdLoader = new NsdModelLoader( console );
validator.addChild( new NsdEObjectValidator( nsdLoader.getResourceSet() ) );
}
public void addNsdDocument( String nsdFile, IRiseClipseConsole console ) {
......@@ -55,69 +39,9 @@ public class NsdValidator {
public void prepare( @NonNull ComposedEValidator validator, IRiseClipseConsole console ) {
nsdLoader.getResourceSet().finalizeLoad( console );
for( EValidator v : validator.getChildren() ) {
if( v.getClass() == NsdEObjectValidator.class ) {
NsdEObjectValidator nsdValidator = ( NsdEObjectValidator ) v;
nsdValidator.initializeValidationData();
}
}
}
public void validate( Resource resource, final AdapterFactory adapter, IRiseClipseConsole console ) {
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 );
return labelProvider.getText( eObject );
}
@Override
public String getFeatureLabel( EStructuralFeature eStructuralFeature ) {
return Diagnostician.INSTANCE.getFeatureLabel( eStructuralFeature );
}
};
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();
//console.error( "in file " + uri.lastSegment() );
for( Iterator< Diagnostic > i = diagnostic.getChildren().iterator(); i.hasNext(); ) {
Diagnostic childDiagnostic = i.next();
switch( childDiagnostic.getSeverity() ) {
case Diagnostic.ERROR:
case Diagnostic.WARNING:
List< ? > data = childDiagnostic.getData();
EObject object = ( EObject ) data.get( 0 );
if( data.size() == 1 ) {
console.error( "\t" + childDiagnostic.getMessage() );
}
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() );
}
else {
console.error( "\t" + childDiagnostic.getMessage() );
}
}
}
}
}
NsdEObjectValidator nsdEObjectValidator = new NsdEObjectValidator( nsdLoader.getResourceSet() );
nsdEObjectValidator.initializeValidationData();
validator.addChild( nsdEObjectValidator );
}
}
......@@ -19,6 +19,10 @@
package fr.centralesupelec.edf.riseclipse.iec61850.scl.validator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.SclPackage;
import fr.centralesupelec.edf.riseclipse.iec61850.scl.provider.SclItemProviderAdapterFactory;
......@@ -27,7 +31,17 @@ import fr.centralesupelec.edf.riseclipse.util.RiseClipseFatalException;
import fr.centralesupelec.edf.riseclipse.util.TextRiseClipseConsole;
import fr.centralesupelec.edf.riseclipse.validation.ocl.OCLValidator;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EValidator;
import org.eclipse.emf.ecore.EValidator.SubstitutionLabelProvider;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.ocl.pivot.validation.ComposedEValidator;
public class RiseClipseValidatorSCL {
......@@ -41,10 +55,12 @@ public class RiseClipseValidatorSCL {
private static boolean oclValidation = false;
private static boolean nsdValidation = false;
public static void usage( IRiseClipseConsole console ) {
private static final IRiseClipseConsole console = new TextRiseClipseConsole();
private static void usage() {
console.setLevel( IRiseClipseConsole.INFO_LEVEL );
console.info(
"java -jar RiseClipseValidatorSCL.jar [--verbose] [--make-explicit-links] [<oclFile> | <nsdFile> | <sclFile>]*" );
"java -jar RiseClipseValidatorSCL.jar [--info | --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" );
......@@ -53,15 +69,13 @@ public class RiseClipseValidatorSCL {
public static void main( String[] args ) {
final IRiseClipseConsole console = new TextRiseClipseConsole();
console.setLevel( IRiseClipseConsole.INFO_LEVEL );
displayLegal( console );
displayLegal();
console.setLevel( IRiseClipseConsole.WARNING_LEVEL );
console.doNotDisplayIdenticalMessages();
if( args.length == 0 ) usage( console );
if( args.length == 0 ) usage();
boolean make_explicit_links = false;
......@@ -69,15 +83,18 @@ public class RiseClipseValidatorSCL {
for( int i = 0; i < args.length; ++i ) {
if( args[i].startsWith( "--" ) ) {
posFiles = i + 1;
if( "--verbose".equals( args[i] ) ) {
if( "--info".equals( args[i] ) ) {
console.setLevel( IRiseClipseConsole.INFO_LEVEL );
}
else if( "--verbose".equals( args[i] ) ) {
console.setLevel( IRiseClipseConsole.VERBOSE_LEVEL );
}
else if( "--make-explicit-links".equals( args[i] ) ) {
make_explicit_links = true;
}
else {
console.error( "Unrecognized option " + args[i] );
usage( console );
usage();
}
}
}
......@@ -99,13 +116,13 @@ public class RiseClipseValidatorSCL {
}
}
prepare( console, oclFiles, nsdFiles );
prepare( oclFiles, nsdFiles );
for( int i = 0; i < sclFiles.size(); ++i ) {
run( console, make_explicit_links, sclFiles.get( i ) );
run( make_explicit_links, sclFiles.get( i ));
}
}
public static void displayLegal( IRiseClipseConsole console ) {
private static void displayLegal() {
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" );
......@@ -122,12 +139,11 @@ public class RiseClipseValidatorSCL {
console.info( "Web site:" );
console.info( " http://wdi.supelec.fr/software/RiseClipse/" );
console.info( "" );
console.info( "RiseClipseValidatorSCL version: 1.0.0 (28 January 2019)" );
console.info( "RiseClipseValidatorSCL version: 1.0.0 (2 april 2019)" );
console.info( "" );
}
public static void prepare( IRiseClipseConsole console,
ArrayList< String > oclFiles, ArrayList< String > nsdFiles ) {
private static void prepare( ArrayList< String > oclFiles, ArrayList< String > nsdFiles ) {
SclPackage sclPg = SclPackage.eINSTANCE;
if( sclPg == null ) {
throw new RiseClipseFatalException( "SCL package not found", null );
......@@ -136,17 +152,16 @@ public class RiseClipseValidatorSCL {
ComposedEValidator validator = ComposedEValidator.install( sclPg );
if( oclValidation ) {
// TODO: change OCLValidator constructor to take same arguments as NsdValidator ?
//oclValidator = new OCLValidator( validator, true );
oclValidator = new OCLValidator( sclPg, true );
oclValidator = new OCLValidator( sclPg, console );
for( int i = 0; i < oclFiles.size(); ++i ) {
oclValidator.addOCLDocument( oclFiles.get( i ), console );
}
oclValidator.prepare( validator, console );
}
if( nsdValidation ) {
nsdValidator = new NsdValidator( validator, console );
nsdValidator = new NsdValidator( sclPg, console );
for( int i = 0; i < nsdFiles.size(); ++i ) {
nsdValidator.addNsdDocument( nsdFiles.get( i ), console );
}
......@@ -158,7 +173,7 @@ public class RiseClipseValidatorSCL {
}
public static void run( IRiseClipseConsole console, boolean make_explicit_links, String sclFile ) {
private static void run( boolean make_explicit_links, String sclFile ) {
sclLoader.reset();
Resource resource = sclLoader.loadWithoutValidation( sclFile );
if( make_explicit_links ) {
......@@ -166,13 +181,60 @@ public class RiseClipseValidatorSCL {
sclLoader.finalizeLoad();
}
if( resource != null ) {
if( oclValidation ) {
console.info( "Validating file: " + sclFile + " with OCL" );
oclValidator.validate( resource, sclAdapter, console );
console.info( "Validating file: " + sclFile );
validate( resource, sclAdapter );
}
}
private static void validate( Resource resource, final AdapterFactory adapter ) {
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 );
return labelProvider.getText( eObject );
}
if( nsdValidation ) {
console.info( "Validating file: " + sclFile + " with NSD" );
nsdValidator.validate( resource, sclAdapter, console );
@Override
public String getFeatureLabel( EStructuralFeature eStructuralFeature ) {
return Diagnostician.INSTANCE.getFeatureLabel( eStructuralFeature );
}
};
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 ) {
for( Iterator< Diagnostic > i = diagnostic.getChildren().iterator(); i.hasNext(); ) {
Diagnostic childDiagnostic = i.next();
switch( childDiagnostic.getSeverity() ) {
case Diagnostic.ERROR:
case Diagnostic.WARNING:
List< ? > data = childDiagnostic.getData();
EObject object = ( EObject ) data.get( 0 );
if( data.size() == 1 ) {
console.error( "\t" + childDiagnostic.getMessage() );
}
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() );
}
else {
console.error( "\t" + childDiagnostic.getMessage() );
}
}
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment