eclipse / epsilon

Epsilon is a family of Java-based scripting languages for automating common model-based software engineering tasks, such as code generation, model-to-model transformation and model validation, that work out of the box with EMF (including Xtext and Sirius), UML (including Cameo/MagicDraw), Simulink, XML and other types of models.
https://eclipse.org/epsilon
Eclipse Public License 2.0
57 stars 11 forks source link

Cached EMF resources not disposed correctly #2

Open kolovos opened 1 year ago

kolovos commented 1 year ago

After the following ANT build file

<project default="main">
    <target name="main">
        <epsilon.emf.loadModel name="M1" modelfile="m1.ecore" metamodeluri="http://www.eclipse.org/emf/2002/Ecore" read="true" store="false"/>
        <epsilon.emf.loadModel name="M2" modelfile="m2.ecore" metamodeluri="http://www.eclipse.org/emf/2002/Ecore" read="true" store="false"/>

        <epsilon.eol>
            M1!EClass.all.first().eSuperTypes.println();
            M2!EClass.all.first().eSuperTypes.println();
            <model ref="M1"/>
            <model ref="M2"/>
        </epsilon.eol>

    </target>
</project>

is executed against the following models m1.ecore and m2.ecore that reference each other, the models are not disposed properly. As a result, if we re-execute the same build file in the same JVM, the contents of m2.ecore are not loaded from disk the second time.

<?xml version="1.0" encoding="ASCII"?>
<ecore:EClass xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmi:id="_rnSosFwLEe2eQLwPX_T8WQ" name="C11">
  <eSuperTypes href="m2.ecore#/"/>
</ecore:EClass>
<?xml version="1.0" encoding="ASCII"?>
<ecore:EClass xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmi:id="_sBzacFwLEe2eQLwPX_T8WQ" name="C221">
  <eSuperTypes href="m1.ecore#/"/>
</ecore:EClass>

A workaround for this is to add the following JavaScript task to the build file to clear the EMF driver's resource cache.

<script language="javascript">
with(new JavaImporter(org.eclipse.epsilon.emc.emf)){
  CachedResourceSet.getCache().clear();
}
</script>
arcanefoam commented 10 months ago

This also seems to happen from within Eclipse using a RunConfiguration. I am running an ETL transformation and the output Ecore model is not "unloaded" resulting in duplicate elements each time I run the transformation.

kolovos commented 10 months ago

Until this gets properly fixed, a workaround would be to manually dispose of the problematic models using the Model Cache view.

arcanefoam commented 10 months ago

If you point me to some suspects I could take a look. So far I have not been able to identify any static field or 'registry' implementations that might be causing it.

kolovos commented 10 months ago

The culprit seems to be org.eclipse.epsilon.emc.emf.CachedResourceSet which is a global cache for EMF resources. The rationale behind this class is that if you load the same model file twice as an EmfModel, the two models will be underpinned by the same resource and therefore their EObjects will be identical.