Skip to content

Commit

Permalink
Merge branch 'SIDeser' into 'dev'
Browse files Browse the repository at this point in the history
Si deser

Closes #4319

See merge request monticore/monticore!1052
  • Loading branch information
SE-FDr committed Oct 30, 2024
2 parents af74234 + 65ad5e9 commit 9c9f3c2
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ public class SymTypeExpressionDeSer {

protected SymTypeOfIntersectionDeSer symTypeOfIntersectionDeSer;

protected SymTypeOfNumericWithSIUnitDeSer symTypeOfNumericWithSIUnitDeSer;

protected SymTypeOfObjectDeSer symTypeOfObjectDeSer;

protected SymTypeOfRegExDeSer symTypeOfRegExDeSer;

protected SymTypeOfSIUnitDeSer symTypeOfSIUnitDeSer;

protected SymTypeOfTupleDeSer symTypeOfTupleDeSer;

protected SymTypeOfUnionDeSer symTypeOfUnionDeSer;
Expand All @@ -56,8 +60,10 @@ protected SymTypeExpressionDeSer() {
this.symTypePrimitiveDeSer = new SymTypePrimitiveDeSer();
this.symTypeOfGenericsDeSer = new SymTypeOfGenericsDeSer();
this.symTypeOfIntersectionDeSer = new SymTypeOfIntersectionDeSer();
this.symTypeOfNumericWithSIUnitDeSer = new SymTypeOfNumericWithSIUnitDeSer();
this.symTypeOfObjectDeSer = new SymTypeOfObjectDeSer();
this.symTypeOfRegExDeSer = new SymTypeOfRegExDeSer();
this.symTypeOfSIUnitDeSer = new SymTypeOfSIUnitDeSer();
this.symTypeOfTupleDeSer = new SymTypeOfTupleDeSer();
this.symTypeOfUnionDeSer = new SymTypeOfUnionDeSer();
this.symTypeVariableDeSer = new SymTypeVariableDeSer();
Expand Down Expand Up @@ -174,12 +180,18 @@ public String serialize(SymTypeExpression toSerialize) {
if(toSerialize.isIntersectionType()) {
return symTypeOfIntersectionDeSer.serialize((SymTypeOfIntersection) toSerialize);
}
if(toSerialize.isNumericWithSIUnitType()) {
return symTypeOfNumericWithSIUnitDeSer.serialize(toSerialize.asNumericWithSIUnitType());
}
if(toSerialize.isObjectType()) {
return symTypeOfObjectDeSer.serialize((SymTypeOfObject) toSerialize);
}
if(toSerialize.isRegExType()) {
return symTypeOfRegExDeSer.serialize((SymTypeOfRegEx) toSerialize);
}
if(toSerialize.isSIUnitType()) {
return symTypeOfSIUnitDeSer.serialize(toSerialize.asSIUnitType());
}
if(toSerialize.isTupleType()) {
return symTypeOfTupleDeSer.serialize((SymTypeOfTuple) toSerialize);
}
Expand Down Expand Up @@ -250,12 +262,16 @@ public SymTypeExpression deserialize(JsonElement serialized,
return symTypeOfGenericsDeSer.deserialize(o, enclosingScope);
case SymTypeOfIntersectionDeSer.SERIALIZED_KIND:
return symTypeOfIntersectionDeSer.deserialize(o, enclosingScope);
case SymTypeOfNumericWithSIUnitDeSer.SERIALIZED_KIND:
return symTypeOfNumericWithSIUnitDeSer.deserialize(o, enclosingScope);
case SymTypeOfObjectDeSer.SERIALIZED_KIND:
return symTypeOfObjectDeSer.deserialize(o, enclosingScope);
case SymTypeOfRegExDeSer.SERIALIZED_KIND:
return symTypeOfRegExDeSer.deserialize(o);
case SymTypeOfSIUnitDeSer.SERIALIZED_KIND:
return symTypeOfSIUnitDeSer.deserialize(o);
case SymTypeOfTupleDeSer.SERIALIZED_KIND:
return symTypeOfTupleDeSer.deserialize(o);
return symTypeOfTupleDeSer.deserialize(o, enclosingScope);
case SymTypeOfUnionDeSer.SERIALIZED_KIND:
return symTypeOfUnionDeSer.deserialize(o, enclosingScope);
case SymTypeVariableDeSer.SERIALIZED_KIND:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.types.check;

import de.monticore.symbols.basicsymbols._symboltable.IBasicSymbolsScope;
import de.monticore.symboltable.serialization.JsonDeSers;
import de.monticore.symboltable.serialization.JsonParser;
import de.monticore.symboltable.serialization.JsonPrinter;
import de.monticore.symboltable.serialization.json.JsonObject;
import de.se_rwth.commons.logging.Log;

public class SymTypeOfNumericWithSIUnitDeSer {

// Care: the following String needs to be adapted if the package was renamed
public static final String SERIALIZED_KIND =
"de.monticore.types.check.SymTypeOfNumericWithSIUnit";
protected static final String SERIALIZED_NUMERIC = "numeric";
protected static final String SERIALIZED_SIUNIT = "siunit";

public String serialize(SymTypeOfNumericWithSIUnit toSerialize) {
JsonPrinter jp = new JsonPrinter();
jp.beginObject();
jp.member(JsonDeSers.KIND, SERIALIZED_KIND);

SymTypeExpressionDeSer
.serializeMember(jp, SERIALIZED_NUMERIC, toSerialize.getNumericType());
SymTypeExpressionDeSer
.serializeMember(jp, SERIALIZED_SIUNIT, toSerialize.getSIUnitType());

jp.endObject();
return jp.getContent();
}

public SymTypeOfNumericWithSIUnit deserialize(String serialized) {
return deserialize(JsonParser.parseJsonObject(serialized), null);
}

public SymTypeOfNumericWithSIUnit deserialize(JsonObject serialized, IBasicSymbolsScope enclosingScope) {
if (serialized.hasMember(SERIALIZED_NUMERIC) &&
serialized.hasMember(SERIALIZED_SIUNIT)
) {
SymTypeExpression numeric = SymTypeExpressionDeSer
.deserializeMember(SERIALIZED_NUMERIC, serialized, enclosingScope);
SymTypeExpression siunit = SymTypeExpressionDeSer
.deserializeMember(SERIALIZED_SIUNIT, serialized, enclosingScope);
return SymTypeExpressionFactory.createNumericWithSIUnit(siunit.asSIUnitType(), numeric);
}
else {
Log.error("0x9E2E2 internal error:"
+ " loading ill-structured SymTab: missing" + SERIALIZED_NUMERIC
+ ", or " + SERIALIZED_SIUNIT
+ " of SymTypeOfNumericWithSIUnit " + serialized);
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.types.check;

import de.monticore.symboltable.serialization.JsonDeSers;
import de.monticore.symboltable.serialization.JsonParser;
import de.monticore.symboltable.serialization.JsonPrinter;
import de.monticore.symboltable.serialization.json.JsonElement;
import de.monticore.symboltable.serialization.json.JsonObject;
import de.se_rwth.commons.logging.Log;

import java.util.ArrayList;
import java.util.List;

public class SymTypeOfSIUnitDeSer {

// Care: the following String needs to be adapted if the package was renamed
public static final String SERIALIZED_KIND = "de.monticore.types.check.SymTypeOfSIUnit";
protected static final String SERIALIZED_NUMERATOR = "numerator";
protected static final String SERIALIZED_DENOMINATOR = "denominator";

// SIUnitBasic
protected static final String SERIALIZED_DIMENSION = "dimension";
protected static final String SERIALIZED_PREFIX = "prefix";
protected static final String SERIALIZED_EXPONENT = "exponent";

public String serialize(SymTypeOfSIUnit toSerialize) {
JsonPrinter jp = new JsonPrinter();
jp.beginObject();
jp.member(JsonDeSers.KIND, SERIALIZED_KIND);

jp.beginArray(SERIALIZED_NUMERATOR);
toSerialize.getNumerator().stream()
.map(this::serialize)
.forEach(jp::valueJson);
jp.endArray();

jp.beginArray(SERIALIZED_DENOMINATOR);
toSerialize.getDenominator().stream()
.map(this::serialize)
.forEach(jp::valueJson);
jp.endArray();

jp.endObject();
return jp.getContent();
}

protected String serialize(SIUnitBasic toSerialize) {
JsonPrinter jp = new JsonPrinter();
jp.beginObject();

jp.member(SERIALIZED_DIMENSION, toSerialize.getDimension());
jp.member(SERIALIZED_PREFIX, toSerialize.getPrefix());
jp.memberNoDef(SERIALIZED_EXPONENT, toSerialize.getExponent());

jp.endObject();
return jp.getContent();
}

public SymTypeOfSIUnit deserialize(String serialized) {
return deserialize(JsonParser.parseJsonObject(serialized));
}

public SymTypeOfSIUnit deserialize(JsonObject serialized) {
List<SIUnitBasic> numerator = new ArrayList<>();
if (serialized.hasArrayMember(SERIALIZED_NUMERATOR)) {
for (JsonElement serializedBasic : serialized.getArrayMember(SERIALIZED_NUMERATOR)) {
numerator.add(deserializeSIUnitBasic(serializedBasic.getAsJsonObject()));
}
}
List<SIUnitBasic> denominator = new ArrayList<>();
if (serialized.hasArrayMember(SERIALIZED_DENOMINATOR)) {
for (JsonElement serializedBasic : serialized.getArrayMember(SERIALIZED_DENOMINATOR)) {
denominator.add(deserializeSIUnitBasic(serializedBasic.getAsJsonObject()));
}
}
return SymTypeExpressionFactory.createSIUnit(numerator, denominator);
}

protected SIUnitBasic deserializeSIUnitBasic(JsonObject serialized) {
if (serialized.hasMember(SERIALIZED_DIMENSION) &&
serialized.hasMember(SERIALIZED_EXPONENT)
) {
SIUnitBasic result;
String dimension = serialized.getStringMember(SERIALIZED_DIMENSION);
int exponent = serialized.getIntegerMember(SERIALIZED_EXPONENT);
if (serialized.hasMember(SERIALIZED_PREFIX)) {
String prefix = serialized.getStringMember(SERIALIZED_PREFIX);
result = SymTypeExpressionFactory.createSIUnitBasic(dimension, prefix, exponent);
}
else {
result = SymTypeExpressionFactory.createSIUnitBasic(dimension, exponent);
}
return result;
}
else {
Log.error("0x9E2E0 internal error:"
+ " loading ill-structured SymTab: missing" + SERIALIZED_DIMENSION
+ ", " + SERIALIZED_PREFIX
+ ", or " + SERIALIZED_EXPONENT
+ " of SIUnitBasic " + serialized);
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.types.check;

import de.monticore.symbols.basicsymbols._symboltable.IBasicSymbolsScope;
import de.monticore.symboltable.serialization.JsonDeSers;
import de.monticore.symboltable.serialization.JsonParser;
import de.monticore.symboltable.serialization.JsonPrinter;
Expand Down Expand Up @@ -31,13 +32,13 @@ public String serialize(SymTypeOfTuple toSerialize) {
}

public SymTypeOfTuple deserialize(String serialized) {
return deserialize(JsonParser.parseJsonObject(serialized));
return deserialize(JsonParser.parseJsonObject(serialized), null);
}

public SymTypeOfTuple deserialize(JsonObject serialized) {
public SymTypeOfTuple deserialize(JsonObject serialized, IBasicSymbolsScope enclosingScope) {
if (serialized.hasMember(SERIALIZED_TYPES)) {
List<SymTypeExpression> typesList =
SymTypeExpressionDeSer.deserializeListMember(SERIALIZED_TYPES, serialized);
List<SymTypeExpression> typesList = SymTypeExpressionDeSer
.deserializeListMember(SERIALIZED_TYPES, serialized, enclosingScope);
return SymTypeExpressionFactory.createTuple(typesList);
}
Log.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public class SymTypeExpressionDeSerTest {

SymTypePrimitive teInt;

SymTypeOfSIUnit teSI1;

SymTypeOfSIUnit teSI2;

SymTypeOfSIUnit teSI3;

SymTypeOfNumericWithSIUnit teNumSI1;

SymTypeVariable teVarA;

SymTypeVariable teVarB;
Expand Down Expand Up @@ -94,6 +102,20 @@ public void init() {

teInt = createPrimitive("int");

teSI1 = createSIUnit(
List.of(createSIUnitBasic("m"), createSIUnitBasic("s", "d", 1)),
List.of()
);

teSI2 = createSIUnit(
List.of(),
List.of(createSIUnitBasic("g", 4))
);

teSI3 = createSIUnit(teSI1.getNumerator(), teSI2.getDenominator());

teNumSI1 = createNumericWithSIUnit(teSI3, teInt);

teVarA = createTypeVariable("A", scope);

teVarB = createTypeVariable("B", scope);
Expand Down Expand Up @@ -168,6 +190,10 @@ public void init() {
public void testRoundtripSerialization() {
performRoundTripSerialization(teDouble);
performRoundTripSerialization(teInt);
performRoundTripSerialization(teSI1);
performRoundTripSerialization(teSI2);
performRoundTripSerialization(teSI3);
performRoundTripSerialization(teNumSI1);
performRoundTripSerialization(teVarA);
performRoundTripSerialization(teVarB);
performRoundTripSerialization(teP);
Expand Down Expand Up @@ -195,6 +221,10 @@ public void testRoundtripSerialization() {

performRoundTripSerializationSymTypePrimitive(teDouble);
performRoundTripSerializationSymTypePrimitive(teInt);
performRoundTripSerializationSymTypeOfSIUnit(teSI1);
performRoundTripSerializationSymTypeOfSIUnit(teSI2);
performRoundTripSerializationSymTypeOfSIUnit(teSI3);
performRoundTripSerializationSymTypeOfNumericWithSIUnit(teNumSI1);
performRoundTripSerializationSymTypeVariable(teVarA);
performRoundTripSerializationSymTypeVariable(teVarB);
performRoundTripSerializationSymTypeOfObject(teP);
Expand Down Expand Up @@ -226,8 +256,11 @@ protected void performRoundTripSerialization(SymTypeExpression expr) {
// and assert that the serialized and deserialized symtype expression equals the one before
Assertions.assertEquals(expr.print(), deserialized.print());
Assertions.assertEquals(expr.printAsJson(), deserialized.printAsJson());
if (!(deserialized instanceof SymTypeOfWildcard)
&& !(deserialized instanceof SymTypeOfRegEx)) {
if (!deserialized.isWildcard() &&
!deserialized.isRegExType() &&
!deserialized.isSIUnitType() &&
!deserialized.isNumericWithSIUnitType()
) {
TypeSymbol expectedTS = deserialized.getTypeInfo();
TypeSymbol actualTS = expr.getTypeInfo();
Assertions.assertEquals(expectedTS.getName(), actualTS.getName());
Expand Down Expand Up @@ -326,6 +359,32 @@ protected void performRoundTripSerializationSymTypePrimitive(SymTypePrimitive ex
Assertions.assertEquals(expectedTS.getName(), actualTS.getName());
}

protected void performRoundTripSerializationSymTypeOfSIUnit(SymTypeOfSIUnit expr) {
SymTypeOfSIUnitDeSer deser = new SymTypeOfSIUnitDeSer();

String serialized = deser.serialize(expr);

SymTypeExpression deserialized = deser.deserialize(serialized);
Assertions.assertNotNull(deserialized);

Assertions.assertEquals(expr.print(), deserialized.print());
Assertions.assertEquals(expr.printAsJson(), deserialized.printAsJson());
}

protected void performRoundTripSerializationSymTypeOfNumericWithSIUnit(
SymTypeOfNumericWithSIUnit expr
) {
SymTypeOfNumericWithSIUnitDeSer deser = new SymTypeOfNumericWithSIUnitDeSer();

String serialized = deser.serialize(expr);

SymTypeExpression deserialized = deser.deserialize(serialized);
Assertions.assertNotNull(deserialized);

Assertions.assertEquals(expr.print(), deserialized.print());
Assertions.assertEquals(expr.printAsJson(), deserialized.printAsJson());
}

protected void performRoundTripSerializationSymTypeOfUnion(SymTypeOfUnion expr) {
SymTypeOfUnionDeSer deser = new SymTypeOfUnionDeSer();

Expand Down Expand Up @@ -357,6 +416,10 @@ protected void performRoundTripSerializationSymTypeOfRegEx(SymTypeOfRegEx expr)
public void testRoundtrip2() {
performRoundtrip2(teDouble);
performRoundtrip2(teInt);
performRoundtrip2(teSI1);
performRoundtrip2(teSI2);
performRoundtrip2(teSI3);
performRoundtrip2(teNumSI1);
performRoundtrip2(teVarA);
performRoundtrip2(teVarB);
performRoundtrip2(teP);
Expand Down Expand Up @@ -394,8 +457,11 @@ protected void performRoundtrip2(SymTypeExpression expr) {
// and assert that the serialized and deserialized symtype expression equals the one before
Assertions.assertEquals(expr.print(), loaded.print());
Assertions.assertEquals(expr.printAsJson(), loaded.printAsJson());
if (!(loaded instanceof SymTypeOfWildcard)
&& !(loaded instanceof SymTypeOfRegEx)) {
if (!loaded.isWildcard() &&
!loaded.isRegExType() &&
!loaded.isSIUnitType() &&
!loaded.isNumericWithSIUnitType()
) {
TypeSymbol expectedTS = loaded.getTypeInfo();
TypeSymbol actualTS = expr.getTypeInfo();
Assertions.assertEquals(expectedTS.getName(), actualTS.getName());
Expand Down

0 comments on commit 9c9f3c2

Please sign in to comment.