Skip to content

Commit 2c69522

Browse files
author
Rob Tjalma
authored
Merge pull request #31 from com-pas/fix-closing-ocl
Fix bug that old validation results are also returned
2 parents c42be8b + 119c48b commit 2c69522

File tree

13 files changed

+98
-87
lines changed

13 files changed

+98
-87
lines changed

app/src/test/data/ocl/Busbar.ocl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// SPDX-FileCopyrightText: 2022 Alliander N.V.
2-
//
3-
// SPDX-License-Identifier: Apache-2.0
1+
/*
2+
** SPDX-FileCopyrightText: 2022 Alliander N.V.
3+
**
4+
** SPDX-License-Identifier: Apache-2.0
5+
*/
46

57
/*
68
*************************************************************************
@@ -29,8 +31,6 @@ package scl
2931

3032
context Bay
3133
-- extends EquipmentContainer
32-
33-
-- The name attribute is verified in Naming.ocl
3434
inv Busbar_nothing
3535
:
3636
true

riseclipse/validator-riseclipse/src/main/java/org/lfenergy/compas/scl/validator/impl/OclFileLoader.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55

66
import fr.centralesupelec.edf.riseclipse.iec61850.scl.SclPackage;
77
import org.eclipse.emf.common.util.URI;
8+
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
89
import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
910
import org.eclipse.ocl.pivot.resource.CSResource;
1011
import org.eclipse.ocl.pivot.utilities.OCL;
1112
import org.eclipse.ocl.pivot.validation.ComposedEValidator;
1213
import org.eclipse.ocl.xtext.completeocl.validation.CompleteOCLEObjectValidator;
14+
import org.lfenergy.compas.scl.extensions.model.SclFileType;
1315
import org.lfenergy.compas.scl.validator.exception.SclValidatorException;
16+
import org.lfenergy.compas.scl.validator.util.OclUtil;
1417
import org.slf4j.Logger;
1518
import org.slf4j.LoggerFactory;
1619

@@ -26,16 +29,18 @@
2629
public class OclFileLoader {
2730
private static final Logger LOGGER = LoggerFactory.getLogger(OclFileLoader.class);
2831

32+
private final List<URI> oclFiles;
2933
private final Path oclTempFile;
3034
private final OCL ocl;
3135

32-
public OclFileLoader(OCL ocl, Path tempDirectoryPath) {
33-
this.ocl = ocl;
36+
public OclFileLoader(Path tempDirectoryPath, List<URI> oclFiles) {
37+
this.oclFiles = oclFiles;
3438

35-
// *.ocl Complete OCL documents support required
36-
org.eclipse.ocl.xtext.completeocl.CompleteOCLStandaloneSetup.doSetup();
37-
// *.oclstdlib OCL Standard Library support required
38-
org.eclipse.ocl.xtext.oclstdlib.OCLstdlibStandaloneSetup.doSetup();
39+
// Create an EPackage.Registry for the SclPackage.
40+
var registry = new EPackageRegistryImpl();
41+
registry.put(SclPackage.eNS_URI, SclPackage.eINSTANCE);
42+
// Create an OCL that creates a ResourceSet using the minimal EPackage.Registry
43+
this.ocl = OCL.newInstance(registry);
3944

4045
// First make sure the directory for temporary file exists.
4146
var tempDirectory = tempDirectoryPath.toFile();
@@ -50,6 +55,12 @@ public OclFileLoader(OCL ocl, Path tempDirectoryPath) {
5055
}
5156
}
5257

58+
public void loadOCLDocuments(SclFileType type) {
59+
oclFiles.stream()
60+
.filter(uri -> OclUtil.includeOnType(uri, type))
61+
.forEach(this::addOCLDocument);
62+
}
63+
5364
public void addOCLDocument(URI oclUri) {
5465
if (oclUri == null) {
5566
throw new SclValidatorException(NO_URI_PASSED, "Unable to create URI for temporary file");
@@ -97,6 +108,8 @@ public void prepareValidator(ComposedEValidator validator) {
97108
}
98109

99110
public void cleanup() {
111+
ocl.dispose();
112+
100113
try {
101114
if (!Files.deleteIfExists(oclTempFile)) {
102115
LOGGER.warn("Unable to remove temporary file '{}'.", oclTempFile);

riseclipse/validator-riseclipse/src/main/java/org/lfenergy/compas/scl/validator/impl/SclModelLoader.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
package org.lfenergy.compas.scl.validator.impl;
55

66
import fr.centralesupelec.edf.riseclipse.iec61850.scl.util.SclResourceFactoryImpl;
7+
import fr.centralesupelec.edf.riseclipse.iec61850.scl.util.SclResourceSetImpl;
78
import org.eclipse.emf.common.util.URI;
89
import org.eclipse.emf.ecore.resource.Resource;
910
import org.eclipse.emf.ecore.resource.ResourceSet;
10-
import org.eclipse.emf.ecore.xmi.XMLResource;
11-
import org.eclipse.ocl.pivot.utilities.OCL;
1211
import org.lfenergy.compas.scl.validator.exception.SclValidatorException;
1312
import org.slf4j.Logger;
1413
import org.slf4j.LoggerFactory;
@@ -24,26 +23,22 @@ public class SclModelLoader {
2423
private static final Logger LOGGER = LoggerFactory.getLogger(SclModelLoader.class);
2524

2625
private final ResourceSet resourceSet;
27-
private final HashMap<String, Boolean> options;
2826

29-
public SclModelLoader(OCL ocl) {
30-
this.resourceSet = ocl.getResourceSet();
27+
public SclModelLoader() {
28+
this.resourceSet = new SclResourceSetImpl(false);
3129

32-
// Register the appropriate resource factory to handle all file extensions.
30+
// Register the appropriate resource factory to handle all file extensions.
3331
this.resourceSet.getResourceFactoryRegistry()
3432
.getExtensionToFactoryMap()
3533
.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new SclResourceFactoryImpl());
36-
37-
this.options = new HashMap<>();
38-
this.options.put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, true);
3934
}
4035

4136
public Resource load(String sclData) {
4237
LOGGER.debug("Loading SCL Data in RiseClipse.");
4338
try {
4439
UUID uuid = UUID.randomUUID();
4540
Resource resource = resourceSet.createResource(URI.createURI(uuid.toString()));
46-
resource.load(new ByteArrayInputStream(sclData.getBytes(StandardCharsets.UTF_8)), options);
41+
resource.load(new ByteArrayInputStream(sclData.getBytes(StandardCharsets.UTF_8)), new HashMap<>());
4742
return resource;
4843
} catch (Exception exp) {
4944
throw new SclValidatorException(LOADING_SCL_FILE_ERROR_CODE, "Problem loading SCL Data", exp);

riseclipse/validator-riseclipse/src/main/java/org/lfenergy/compas/scl/validator/impl/SclRiseClipseValidator.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,23 @@
88
import org.eclipse.emf.common.util.Diagnostic;
99
import org.eclipse.emf.common.util.URI;
1010
import org.eclipse.emf.ecore.EObject;
11-
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
11+
import org.eclipse.emf.ecore.impl.EValidatorRegistryImpl;
1212
import org.eclipse.emf.ecore.resource.Resource;
1313
import org.eclipse.emf.ecore.util.Diagnostician;
1414
import org.eclipse.emf.ecore.util.EObjectValidator;
15-
import org.eclipse.ocl.pivot.utilities.OCL;
1615
import org.eclipse.ocl.pivot.validation.ComposedEValidator;
1716
import org.lfenergy.compas.scl.extensions.model.SclFileType;
1817
import org.lfenergy.compas.scl.validator.SclValidator;
1918
import org.lfenergy.compas.scl.validator.collector.OclFileCollector;
20-
import org.lfenergy.compas.scl.validator.exception.SclValidatorException;
2119
import org.lfenergy.compas.scl.validator.model.ValidationError;
22-
import org.lfenergy.compas.scl.validator.util.OclFileUtil;
20+
import org.lfenergy.compas.scl.validator.util.OclUtil;
2321
import org.slf4j.Logger;
2422
import org.slf4j.LoggerFactory;
2523

2624
import java.nio.file.Path;
2725
import java.util.ArrayList;
2826
import java.util.List;
2927

30-
import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.OCL_MODEL_PACKAGE_NOT_FOUND;
3128
import static org.lfenergy.compas.scl.validator.util.MessageUtil.cleanupMessage;
3229

3330
public class SclRiseClipseValidator implements SclValidator {
@@ -40,43 +37,34 @@ public SclRiseClipseValidator(OclFileCollector oclFileCollector, Path tempDirect
4037
this.oclFiles.addAll(oclFileCollector.getOclFiles());
4138
this.tempDirectory = tempDirectory;
4239

43-
// Check if the SclPackage can be initialized.
44-
var sclPck = SclPackage.eINSTANCE;
45-
if (sclPck == null) {
46-
throw new SclValidatorException(OCL_MODEL_PACKAGE_NOT_FOUND, "SCL package not found");
47-
}
40+
// Initialize the OCL Libraries
41+
OclUtil.setupOcl();
4842
}
4943

5044
@Override
5145
public List<ValidationError> validate(SclFileType type, String sclData) {
5246
// List with Validation Error Results if there are any.
5347
var validationErrors = new ArrayList<ValidationError>();
5448

55-
// Create an EPackage.Registry for the SclPackage.
56-
var registry = new EPackageRegistryImpl();
57-
registry.put(SclPackage.eNS_URI, SclPackage.eINSTANCE);
58-
// Create an OCL that creates a ResourceSet using the minimal EPackage.Registry
59-
var ocl = OCL.newInstance(registry);
49+
// Create the validator and prepare it with the OCL Files.
50+
var validatorRegistry = new EValidatorRegistryImpl();
51+
var validator = new ComposedEValidator(null);
52+
validatorRegistry.put(SclPackage.eINSTANCE, validator);
6053

61-
OclFileLoader oclFileLoader = new OclFileLoader(ocl, tempDirectory);
54+
OclFileLoader oclFileLoader = new OclFileLoader(tempDirectory, oclFiles);
6255
try {
6356
// Load all the OCL Files, adding them to the OCL Instance.
6457
LOGGER.info("Loading OCL Files for type '{}'.", type);
65-
oclFiles.stream()
66-
.filter(uri -> OclFileUtil.includeOnType(uri, type))
67-
.forEach(oclFileLoader::addOCLDocument);
68-
69-
// Create the validator and prepare it with the OCL Files.
70-
var validator = ComposedEValidator.install(SclPackage.eINSTANCE);
58+
oclFileLoader.loadOCLDocuments(type);
7159
oclFileLoader.prepareValidator(validator);
7260

7361
// Load the SCL File as Resource ready to be processed.
7462
LOGGER.info("Loading SCL Data for type '{}'.", type);
75-
var sclLoader = new SclModelLoader(ocl);
63+
var sclLoader = new SclModelLoader();
7664
var resource = sclLoader.load(sclData);
7765

7866
LOGGER.info("Validating SCL Data for type '{}'.", type);
79-
var diagnostician = new CompasDiagnostician();
67+
var diagnostician = new CompasDiagnostician(validatorRegistry);
8068
var diagnostic = diagnostician.validate(resource);
8169
processDiagnostic(diagnostic, validationErrors);
8270
} finally {
@@ -105,6 +93,10 @@ private void processDiagnostic(Diagnostic diagnostic, List<ValidationError> vali
10593
* Simple extension of the Diagnostician to make working with the Resource easier.
10694
*/
10795
private static class CompasDiagnostician extends Diagnostician {
96+
public CompasDiagnostician(Registry eValidatorRegistry) {
97+
super(eValidatorRegistry);
98+
}
99+
108100
/**
109101
* Create a basic diagnostic instance from the resource.
110102
*
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,39 @@
33
// SPDX-License-Identifier: Apache-2.0
44
package org.lfenergy.compas.scl.validator.util;
55

6+
import fr.centralesupelec.edf.riseclipse.iec61850.scl.SclPackage;
67
import org.eclipse.emf.common.util.URI;
78
import org.lfenergy.compas.scl.extensions.model.SclFileType;
9+
import org.lfenergy.compas.scl.validator.exception.SclValidatorException;
810
import org.slf4j.Logger;
911
import org.slf4j.LoggerFactory;
1012

1113
import static java.io.File.separator;
14+
import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.OCL_MODEL_PACKAGE_NOT_FOUND;
15+
16+
public class OclUtil {
17+
private static final Logger LOGGER = LoggerFactory.getLogger(OclUtil.class);
1218

13-
public class OclFileUtil {
14-
private static final Logger LOGGER = LoggerFactory.getLogger(OclFileUtil.class);
15-
1619
private static final String FILE_SPECIFICS_DIR_NAME = "FileSpecifics";
1720
private static final String COMMON_DIR_NAME = "Common";
1821

19-
OclFileUtil() {
22+
OclUtil() {
2023
throw new UnsupportedOperationException("OclFileUtil class");
2124
}
2225

26+
public static void setupOcl() {
27+
// *.ocl Complete OCL documents support required
28+
org.eclipse.ocl.xtext.completeocl.CompleteOCLStandaloneSetup.doSetup();
29+
// *.oclstdlib OCL Standard Library support required
30+
org.eclipse.ocl.xtext.oclstdlib.OCLstdlibStandaloneSetup.doSetup();
31+
32+
// Check if the SclPackage can be initialized.
33+
var sclPck = SclPackage.eINSTANCE;
34+
if (sclPck == null) {
35+
throw new SclValidatorException(OCL_MODEL_PACKAGE_NOT_FOUND, "SCL package not found");
36+
}
37+
}
38+
2339
public static boolean includeOnType(URI uri, SclFileType type) {
2440
var fullPath = uri.path();
2541
// OCL Files that are not in the directory 'FileSpecifics' will always be included.

riseclipse/validator-riseclipse/src/test/data/ocl/Busbar.ocl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
SPDX-FileCopyrightText: 2022 Alliander N.V.
3-
4-
SPDX-License-Identifier: Apache-2.0
5-
*/
2+
** SPDX-FileCopyrightText: 2022 Alliander N.V.
3+
**
4+
** SPDX-License-Identifier: Apache-2.0
5+
*/
66

77
/*
88
*************************************************************************
@@ -31,8 +31,6 @@ package scl
3131

3232
context Bay
3333
-- extends EquipmentContainer
34-
35-
-- The name attribute is verified in Naming.ocl
3634
inv Busbar_nothing
3735
:
3836
true

riseclipse/validator-riseclipse/src/test/java/org/lfenergy/compas/scl/validator/impl/OclFileLoaderTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
import org.junit.jupiter.api.BeforeEach;
1111
import org.junit.jupiter.api.Test;
1212
import org.junit.jupiter.api.extension.ExtendWith;
13+
import org.lfenergy.compas.scl.extensions.model.SclFileType;
1314
import org.lfenergy.compas.scl.validator.exception.SclValidatorException;
15+
import org.lfenergy.compas.scl.validator.util.OclUtil;
1416
import org.mockito.junit.jupiter.MockitoExtension;
1517

1618
import java.io.File;
1719
import java.io.IOException;
1820
import java.nio.file.Files;
1921
import java.nio.file.Path;
22+
import java.util.List;
2023

2124
import static org.junit.jupiter.api.Assertions.assertEquals;
2225
import static org.junit.jupiter.api.Assertions.assertThrows;
2326
import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.NO_URI_PASSED;
24-
import static org.lfenergy.compas.scl.validator.util.TestSupportUtil.createSclOcl;
2527
import static org.mockito.Mockito.*;
2628

2729
@ExtendWith(MockitoExtension.class)
@@ -31,15 +33,27 @@ class OclFileLoaderTest {
3133

3234
@BeforeEach
3335
void setup() throws IOException {
36+
// Initialize the OCL Libraries
37+
OclUtil.setupOcl();
38+
3439
var tempDirectory = "./target/data/temp";
3540
var tempDirectoryPath = Path.of(tempDirectory);
36-
loader = new OclFileLoader(createSclOcl(), tempDirectoryPath);
41+
var oclFile = findOCL("example.ocl");
42+
43+
loader = new OclFileLoader(tempDirectoryPath, List.of(oclFile));
3744
tempFile = Files.walk(tempDirectoryPath)
3845
.filter(path -> path.toString().contains(File.separator + "allConstraints"))
3946
.findFirst()
4047
.orElseThrow();
4148
}
4249

50+
@Test
51+
void loadOCLDocuments_WhenCalled_ThenFilesFromListAreLoaded() throws IOException {
52+
loader.loadOCLDocuments(SclFileType.CID);
53+
54+
assertEquals(1, Files.lines(tempFile).count());
55+
}
56+
4357
@Test
4458
void addOCLDocument_WhenCalledWithNull_ThenExceptionThrown() {
4559
var exception = assertThrows(SclValidatorException.class,

riseclipse/validator-riseclipse/src/test/java/org/lfenergy/compas/scl/validator/impl/SclModelLoaderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
import static org.junit.jupiter.api.Assertions.*;
1313
import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.LOADING_SCL_FILE_ERROR_CODE;
14-
import static org.lfenergy.compas.scl.validator.util.TestSupportUtil.createSclOcl;
1514
import static org.lfenergy.compas.scl.validator.util.TestSupportUtil.readSCL;
1615

1716
class SclModelLoaderTest {
1817
private SclModelLoader loader;
1918

2019
@BeforeEach
2120
void setup() {
22-
loader = new SclModelLoader(createSclOcl());
21+
loader = new SclModelLoader();
2322
}
2423

2524
@Test

riseclipse/validator-riseclipse/src/test/java/org/lfenergy/compas/scl/validator/impl/SclRiseClipseValidatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void setup() {
2525
}
2626

2727
@Test
28-
void validate_WhenCalled_ThenEmptyListReturned() throws IOException {
28+
void validate_WhenCalled_ThenExpectedValidationErrorsReturned() throws IOException {
2929
var type = SclFileType.CID;
3030
var sclData = readSCL("example.scd");
3131

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
import static org.junit.jupiter.api.Assertions.*;
1313

14-
class OclFileUtilTest {
14+
class OclUtilTest {
1515
private OclFileCollector collector = new CompasOclFileCollector(null);
1616

1717
@Test
1818
void constructor_WhenConstructorCalled_ThenShouldThrowExceptionCauseForbidden() {
19-
assertThrows(UnsupportedOperationException.class, OclFileUtil::new);
19+
assertThrows(UnsupportedOperationException.class, OclUtil::new);
2020
}
2121

2222
@Test
@@ -41,7 +41,7 @@ void includeOnType_WhenCalledWithNoFileSpecificUri_ThenReturnTrue() {
4141

4242
private boolean executeTest(String oclFileName) {
4343
var uri = getResource(oclFileName);
44-
return OclFileUtil.includeOnType(uri, SclFileType.CID);
44+
return OclUtil.includeOnType(uri, SclFileType.CID);
4545
}
4646

4747
private URI getResource(String oclFileName) {

0 commit comments

Comments
 (0)