Commit a48c455e authored by Martti Käärik's avatar Martti Käärik
Browse files

We have custom model impl classes now, so enumeration values can be typed by...

We have custom model impl classes now, so enumeration values can be typed by the model without derived state (which messed up serialization).
parent e149523c
Loading
Loading
Loading
Loading
Loading
+19 −0
Changes for plugins/org.etsi.mts.tdl.common/src/org/etsi/mts/tdl/TDLTransientValueService.java: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
package org.etsi.mts.tdl;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.serializer.sequencer.TransientValueService;
import org.etsi.mts.tdl.impl.SimpleDataInstanceImplCustom;

public class TDLTransientValueService extends TransientValueService {

	@Override
	public ValueTransient isValueTransient(EObject semanticObject, EStructuralFeature feature) {
		if (feature == tdlPackage.Literals.DATA_INSTANCE__DATA_TYPE && semanticObject instanceof SimpleDataInstanceImplCustom
				&& !((SimpleDataInstanceImplCustom) semanticObject).isDataTypeExplicit()) {
			// Keeps implicitly typed enumeration values implicit when a model is serialised
			return ValueTransient.PREFERABLY;
		}
		return super.isValueTransient(semanticObject, feature);
	}
}
+37 −0
Changes for plugins/org.etsi.mts.tdl.model/src/org/etsi/mts/tdl/impl/SimpleDataInstanceImplCustom.java: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
package org.etsi.mts.tdl.impl;

import org.etsi.mts.tdl.DataType;
import org.etsi.mts.tdl.EnumDataType;
import org.etsi.mts.tdl.navigation.TdlNavigation;
import org.etsi.mts.tdl.tdlPackage;

/**
 * Enumeration values are typed by their enclosing {@link EnumDataType}.
 */
public class SimpleDataInstanceImplCustom extends SimpleDataInstanceImpl {

	@Override
	public DataType getDataType() {
		DataType explicit = super.getDataType();
		return explicit != null ? explicit : TdlNavigation.enumTypeOf(this);
	}

	@Override
	public DataType basicGetDataType() {
		DataType explicit = super.basicGetDataType();
		return explicit != null ? explicit : TdlNavigation.enumTypeOf(this);
	}

	/** True when the type is stated in the model (the only case the serialiser writes it). */
	public boolean isDataTypeExplicit() {
		return super.basicGetDataType() != null;
	}

	@Override
	public boolean eIsSet(int featureID) {
		if (featureID == tdlPackage.SIMPLE_DATA_INSTANCE__DATA_TYPE) {
			return super.eIsSet(featureID) || TdlNavigation.enumTypeOf(this) != null;
		}
		return super.eIsSet(featureID);
	}
}
+4 −0
Changes for plugins/org.etsi.mts.tdl.tx.tests/src/org/etsi/mts/tdl/tx/tests/EnumTypingTest.java: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ import org.eclipse.xtext.validation.Issue;
import org.etsi.mts.tdl.EnumDataType;
import org.etsi.mts.tdl.Package;
import org.etsi.mts.tdl.SimpleDataInstance;
import org.etsi.mts.tdl.impl.SimpleDataInstanceImplCustom;
import org.etsi.mts.tdl.tdlPackage;
import org.etsi.mts.tdl.tx.tests.oracle.LinkOracle;
import org.junit.jupiter.api.Assertions;
@@ -43,10 +44,13 @@ public class EnumTypingTest extends TDLtxAbstractTest {
		EnumDataType colour = colour();
		Assertions.assertEquals(3, colour.getValue().size());
		for (SimpleDataInstance v : colour.getValue()) {
			Assertions.assertTrue(v instanceof SimpleDataInstanceImplCustom, "factory must create the custom impl: " + v.getClass());
			Assertions.assertSame(colour, v.getDataType(), v.getName() + ".getDataType()");
			Assertions.assertSame(colour, v.eGet(tdlPackage.Literals.DATA_INSTANCE__DATA_TYPE, false), v.getName() + " eGet(resolve=false)");
			Assertions.assertTrue(v.eIsSet(tdlPackage.Literals.DATA_INSTANCE__DATA_TYPE), v.getName() + " eIsSet");
		}
		Assertions.assertTrue(((SimpleDataInstanceImplCustom) colour.getValue().get(0)).isDataTypeExplicit(), "red is typed explicitly");
		Assertions.assertFalse(((SimpleDataInstanceImplCustom) colour.getValue().get(1)).isDataTypeExplicit(), "green is typed implicitly");
	}

	@Test
+0 −36
Changes for plugins/org.etsi.mts.tdl.tx/src/org/etsi/mts/tdl/TDLtxDerivedStateComputer.java: 0 added lines, 36 removed lines.
Original line number Diff line number Diff line
package org.etsi.mts.tdl;

import java.util.List;

import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.resource.DerivedStateAwareResource;
import org.eclipse.xtext.resource.IDerivedStateComputer;

//TODO: move upstream
class TDLtxDerivedStateComputer implements IDerivedStateComputer {
	
	@Override
	public void installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) {
		List<EnumDataType> enums = EcoreUtil2.getAllContentsOfType(resource.getContents().get(0), EnumDataType.class);
		enums.forEach(e-> {
			e.getValue().forEach(v -> v.setDataType(e));
		});
//		//TODO: also for gate references (instead of in scope?)
//		List<GateReference> grs = EcoreUtil2.getAllContentsOfType(resource.getContents().get(0), GateReference.class);
//		grs.forEach(e-> e.setName(e.getName()==null ? 
//				null //e.getComponent().getName()+"_"+e.getGate().getName() 
//				: e.getName()));
//		grs.forEach(e-> System.out.println(e.getName()));
	}

	@Override
	public void discardDerivedState(DerivedStateAwareResource resource) {
		List<EnumDataType> enums = EcoreUtil2.getAllContentsOfType(resource.getContents().get(0), EnumDataType.class);
		enums.forEach(e-> {
			e.getValue().forEach(v -> v.setDataType(null));
		});
//		//TODO: also for gate references (instead of in scope?)
//		List<GateReference> grs = EcoreUtil2.getAllContentsOfType(resource.getContents().get(0), GateReference.class);
//		grs.forEach(e-> e.setName(null));
	}
}
 No newline at end of file
+2 −19
Changes for plugins/org.etsi.mts.tdl.tx/src/org/etsi/mts/tdl/TDLtxRuntimeModule.java: 2 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -6,13 +6,6 @@ package org.etsi.mts.tdl;
import org.eclipse.xtext.conversion.IValueConverterService;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.resource.DerivedStateAwareResource;
import org.eclipse.xtext.resource.DerivedStateAwareResourceDescriptionManager;
import org.eclipse.xtext.resource.IDefaultResourceDescriptionStrategy;
import org.eclipse.xtext.resource.IDerivedStateComputer;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.scoping.IGlobalScopeProvider;
import org.etsi.mts.tdl.scoping.TDLQualifiedNameConverter;
import org.etsi.mts.tdl.scoping.TdlQualifiedNameProvider;

@@ -32,20 +25,10 @@ public class TDLtxRuntimeModule extends AbstractTDLtxRuntimeModule {
		return TdlQualifiedNameProvider.class;
	}

	public Class<? extends IDerivedStateComputer> bindIDerivedStateComputer() {
		return TDLtxDerivedStateComputer.class;
	public Class<? extends org.eclipse.xtext.serializer.sequencer.ITransientValueService> bindSequencerITransientValueService() { // name differs from the legacy parsetree service binding; the key is the return type
		return org.etsi.mts.tdl.TDLTransientValueService.class;
	}

	// Not needed for Xbase-projects
	@Override
	public Class<? extends XtextResource> bindXtextResource() {
		return DerivedStateAwareResource.class;
	}

	// Not needed for Xbase-projects
	public Class<? extends IResourceDescription.Manager> bindIResourceDescriptionManager() {
		return DerivedStateAwareResourceDescriptionManager.class;
	}

//	public Class<? extends IDefaultResourceDescriptionStrategy> bindIDefaultResourceDescriptionStrategy() {
//		return RDS.class;
Loading