Commit 2acabf47 authored by Ferdinand Alexander Mehlan's avatar Ferdinand Alexander Mehlan
Browse files

change templates for simpletagtypecreator

parent e57c6123
Pipeline #62991 passed with stage
in 50 seconds
......@@ -223,12 +223,18 @@ public class TagSchemaGenerator extends GeneratorEngine {
generate("templates.de.monticore.lang.tagschema.ValuedTagType",
Paths.get(createPackagePath(packageName).toString(), tagSchemaUnit.getName(), valuedTagType.getName() + "Symbol.java"),
tagSchemaUnit, packageName, tagSchemaUnit.getName(), valuedTagType.getName(), dataType, isUnit);
String importSymbols = "de.monticore.lang.montiarc._symboltable.*";
List<String> imports = new LinkedList<String>();
for(ASTImportStatement importStatement : tagSchemaUnit.getImportStatementList()) {
String importString = Joiners.DOT.join(importStatement.getImportList());
if(importStatement.isStar()) importString += ".*";
imports.add(importString);
}
String scopeSymbol = valuedTagType.getScopeOpt().get().getScopeIdentifierList().get(0).getScopeName();
String nameScopeType = Log.errorIfNull(symbolScopeMap.get(scopeSymbol), String.format("For the scope symbol '%s' is no scope type defined.", scopeSymbol));
generate("templates.de.monticore.lang.tagschema.ValuedTagTypeCreator",
Paths.get(createPackagePath(packageName).toString(),tagSchemaUnit.getName(), valuedTagType.getName() + "SymbolCreator.java"),
tagSchemaUnit, packageName, tagSchemaUnit.getName(), valuedTagType.getName(), importSymbols, scopeSymbol + "Symbol", nameScopeType, dataType, isUnit);
tagSchemaUnit, packageName, tagSchemaUnit.getName(), valuedTagType.getName(), imports, scopeSymbol + "Symbol", nameScopeType, dataType, isUnit);
}
public static Path createPackagePath(String packageName) {
......@@ -245,11 +251,16 @@ public class TagSchemaGenerator extends GeneratorEngine {
generate("templates.de.monticore.lang.tagschema.SimpleTagType",
Paths.get(createPackagePath(packageName).toString(), tagSchemaUnit.getName(), simpleTagType.getName() + "Symbol.java"),
tagSchemaUnit, packageName, tagSchemaUnit.getName(), simpleTagType.getName());
String importSymbols = "de.monticore.lang.montiarc._symboltable.*";
String scopeSymbol = simpleTagType.getScopeOpt().get().getScopeIdentifierList().get(0).getScopeName();
List<String> imports = new LinkedList<String>();
for(ASTImportStatement importStatement : tagSchemaUnit.getImportStatementList()) {
String importString = Joiners.DOT.join(importStatement.getImportList());
if(importStatement.isStar()) importString += ".*";
imports.add(importString);
} String scopeSymbol = simpleTagType.getScopeOpt().get().getScopeIdentifierList().get(0).getScopeName();
String nameScopeType = Log.errorIfNull(symbolScopeMap.get(scopeSymbol), String.format("For the scope symbol '%s' is no scope type defined.", scopeSymbol));
generate("templates.de.monticore.lang.tagschema.SimpleTagTypeCreator",
Paths.get(createPackagePath(packageName).toString(), tagSchemaUnit.getName(), simpleTagType.getName() + "SymbolCreator.java"),
tagSchemaUnit, packageName, tagSchemaUnit.getName(), simpleTagType.getName(), importSymbols, scopeSymbol + "Symbol", nameScopeType);
tagSchemaUnit, packageName, tagSchemaUnit.getName(), simpleTagType.getName(), imports, scopeSymbol + "Symbol", nameScopeType);
}
}
${tc.signature("packageName", "schemaName", "tagTypeName", "importSymbols", "scopeSymbol", "nameScopeType")}
${tc.signature("packageName", "schemaName", "tagTypeName", "imports", "scopeSymbol", "nameScopeType")}
package ${packageName}.${schemaName};
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;
import ${importSymbols};
<#list imports as import>
import ${import};
</#list>
import de.monticore.lang.tagging._ast.ASTNameScope;
import de.monticore.lang.tagging._ast.ASTScope;
import de.monticore.lang.tagging._ast.ASTTag;
import de.monticore.lang.tagging._ast.ASTTaggingUnit;
import de.monticore.lang.tagging._symboltable.TagSymbolCreator;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import de.monticore.symboltable.SymbolKind;
import de.se_rwth.commons.Joiners;
import de.se_rwth.commons.logging.Log;
......@@ -31,57 +31,35 @@ public class ${tagTypeName}SymbolCreator implements TagSymbolCreator {
return s;
}
public void create(ASTTaggingUnit unit, Scope gs) {
if (unit.getQualifiedNames().stream()
public void create(ASTTaggingUnit unit, TaggingResolver tagging) {
if (unit.getQualifiedNameList().stream()
.map(q -> q.toString())
.filter(n -> n.endsWith("${schemaName}"))
.count() == 0) {
return; // the tagging model is not conform to the ${schemaName} tagging schema
}
final String packageName = Joiners.DOT.join(unit.getPackage());
final String packageName = Joiners.DOT.join(unit.getPackageList());
final String rootCmp = // if-else does not work b/c of final (required by streams)
(unit.getTagBody().getTargetModel().isPresent()) ?
Joiners.DOT.join(packageName, ((ASTNameScope) unit.getTagBody().getTargetModel().get())
(unit.getTagBody().getTargetModelOpt().isPresent()) ?
Joiners.DOT.join(packageName, ((ASTNameScope) unit.getTagBody().getTargetModelOpt().get())
.getQualifiedName().toString()) :
packageName;
for (ASTTag element : unit.getTagBody().getTagList()) {
element.getTagElements().stream()
element.getTagElementList().stream()
.filter(t -> t.getName().equals("${tagTypeName}"))
.filter(t -> !t.getTagValue().isPresent()) // only marker tag with no value
.filter(t -> !t.getTagValueOpt().isPresent()) // only marker tag with no value
.forEachOrdered(t ->
element.getScopeList().stream()
.filter(this::checkScope)
.map(s -> (ASTNameScope) s)
.map(s -> getGlobalScope(gs).<Symbol>resolveDownMany(
Joiners.DOT.join(rootCmp, s.getQualifiedName().toString()),
SymbolKind.KIND))
.filter(s -> !s.isEmpty())
.map(this::checkKind)
.filter(s -> s != null)
.forEachOrdered(s -> s.addTag(new ${tagTypeName}Symbol())));
}
}
protected ${scopeSymbol} checkKind(Collection<Symbol> symbols) {
${scopeSymbol} ret = null;
for (Symbol symbol : symbols) {
if (symbol.getKind().isSame(${scopeSymbol}.KIND)) {
if (ret != null) {
Log.error(String.format("0xA4095 Found more than one symbol: '%s' and '%s'",
ret, symbol));
return null;
}
ret = (${scopeSymbol})symbol;
.map(s -> tagging.resolve(Joiners.DOT.join(rootCmp, // resolve down does not try to reload symbol
s.getQualifiedName().toString()), ${scopeSymbol}.KIND))
.filter(Optional::isPresent)
.map(Optional::get)
.forEachOrdered(s -> tagging.addTag(s, new ${tagTypeName}Symbol())));
}
}
if (ret == null) {
Log.error(String.format("0xT0001 Invalid symbol kinds: %s. tagTypeName expects as symbol kind '${scopeSymbol}.KIND'.",
symbols.stream().map(s -> "'" + s.getKind().toString() + "'").collect(Collectors.joining(", "))));
return null;
}
return ret;
}
protected boolean checkScope(ASTScope scope) {
if (scope.getScopeKind().equals("${nameScopeType}")) {
......
${tc.signature("packageName", "schemaName", "tagTypeName", "importSymbols", "scopeSymbol", "nameScopeType", "dataType", "isUnit")}
${tc.signature("packageName", "schemaName", "tagTypeName", "imports", "scopeSymbol", "nameScopeType", "dataType", "isUnit")}
package ${packageName}.${schemaName};
......@@ -6,8 +6,10 @@ import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;
import ${importSymbols};
import de.monticore.lang.montiarc._symboltable.ConnectorSymbol;
<#list imports as import>
import ${import};
</#list>
import de.monticore.lang.tagging._ast.ASTNameScope;
import de.monticore.lang.tagging._ast.ASTScope;
import de.monticore.lang.tagging._ast.ASTTag;
......
/* generated by template templates.de.monticore.lang.tagschema.TagSchema*/
package nfp.PhysicalTags;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.resolving.CommonResolvingFilter;
/**
* generated by TagSchema.ftl
*/
public class PhysicalTags {
protected static PhysicalTags instance = null;
protected PhysicalTags() {}
protected static PhysicalTags getInstance() {
if (instance == null) {
instance = new PhysicalTags();
}
return instance;
}
protected void doRegisterTagTypes(TaggingResolver tagging) {
tagging.addTagSymbolCreator(new WeightSymbolCreator());
tagging.addTagSymbolResolvingFilter(CommonResolvingFilter.create(WeightSymbol.KIND));
tagging.addTagSymbolCreator(new SizeSymbolCreator());
tagging.addTagSymbolResolvingFilter(CommonResolvingFilter.create(SizeSymbol.KIND));
}
public static void registerTagTypes(TaggingResolver tagging) {
getInstance().doRegisterTagTypes(tagging);
}
}
\ No newline at end of file
/* generated by template templates.de.monticore.lang.tagschema.ComplexTagType*/
package nfp.PhysicalTags;
import de.monticore.lang.tagging._symboltable.TagKind;
import de.monticore.lang.tagging._symboltable.TagSymbol;
import org.jscience.physics.amount.Amount;
import javax.measure.quantity.*;
/**
* Created by ValuedTagType.ftl
*/
public class SizeSymbol extends TagSymbol {
public static final SizeKind KIND = SizeKind.INSTANCE;
public SizeSymbol(
Amount<?> width,
Amount<?> depth,
Amount<?> height
) {
super(KIND,
(Amount<Length>)width,
(Amount<Length>)depth,
(Amount<Length>)height
);
}
public Amount<Length> getWidth() {
return getValue(0);
}
public Amount<Length> getDepth() {
return getValue(1);
}
public Amount<Length> getHeight() {
return getValue(2);
}
@Override
public String toString() {
return String.format("Size = " +
"%s"+
"%s"+
"%s"
,
getWidth(),
getDepth(),
getHeight()
);
}
public static class SizeKind extends TagKind {
public static final SizeKind INSTANCE = new SizeKind();
protected SizeKind() {
}
}
}
\ No newline at end of file
/* generated by template templates.de.monticore.lang.tagschema.ValuedTagType*/
package nfp.PhysicalTags;
import de.monticore.lang.tagging._symboltable.TagKind;
import de.monticore.lang.tagging._symboltable.TagSymbol;
import org.jscience.physics.amount.Amount;
import javax.measure.quantity.Mass;
import javax.measure.unit.Unit;
/**
* Created by ValuedTagType.ftl
*/
public class WeightSymbol extends TagSymbol {
public static final WeightKind KIND = WeightKind.INSTANCE;
public WeightSymbol(Amount<Mass> value) {
this(KIND, value);
}
public WeightSymbol(Number number, Unit<Mass> unit) {
this(KIND, number, unit);
}
protected WeightSymbol(WeightKind kind, Amount<Mass> value) {
super(kind, value);
}
protected WeightSymbol(WeightKind kind, Number number, Unit<Mass> unit) {
this(kind, number.toString().contains(".") ?
Amount.valueOf(number.doubleValue(), unit) :
Amount.valueOf(number.intValue(),
unit));
}
public Amount<Mass> getValue() {
return getValue(0);
}
public Number getNumber() {
return getValue().getExactValue();
}
public Unit<Mass> getUnit() {
return getValue().getUnit();
}
@Override
public String toString() {
return String.format("Weight = %s %s",
getNumber(), getUnit());
}
public static class WeightKind extends TagKind {
public static final WeightKind INSTANCE = new WeightKind();
protected WeightKind() {
}
}
}
\ No newline at end of file
/* generated by template templates.de.monticore.lang.tagschema.ValuedTagTypeCreator*/
package nfp.PhysicalTags;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;
import de.monticore.lang.montiarc._symboltable.*;
import de.monticore.lang.montiarc._symboltable.ConnectorSymbol;
import de.monticore.lang.tagging._ast.ASTNameScope;
import de.monticore.lang.tagging._ast.ASTScope;
import de.monticore.lang.tagging._ast.ASTTag;
import de.monticore.lang.tagging._ast.ASTTaggingUnit;
import de.monticore.lang.tagging._symboltable.TagSymbolCreator;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import de.se_rwth.commons.Joiners;
import de.se_rwth.commons.logging.Log;
import de.monticore.lang.tagvalue._parser.TagValueParser;
import javax.measure.quantity.Mass;
import javax.measure.unit.Unit;
import de.monticore.lang.tagging.helper.NumericLiteral;
import de.monticore.lang.tagvalue._ast.ASTUnitTagValue;
/**
* created by ValuedTagTypeCreator.ftl
*/
public class WeightSymbolCreator implements TagSymbolCreator {
public static Scope getGlobalScope(final Scope scope) {
Scope s = scope;
while (s.getEnclosingScope().isPresent()) {
s = s.getEnclosingScope().get();
}
return s;
}
public void create(ASTTaggingUnit unit, TaggingResolver tagging) {
if (unit.getQualifiedNameList().stream()
.map(q -> q.toString())
.filter(n -> n.endsWith("PhysicalTags"))
.count() == 0) {
return; // the tagging model is not conform to the PhysicalTags tagging schema
}
final String packageName = Joiners.DOT.join(unit.getPackageList());
final String rootCmp = // if-else does not work b/c of final (required by streams)
(unit.getTagBody().getTargetModelOpt().isPresent()) ?
Joiners.DOT.join(packageName, ((ASTNameScope) unit.getTagBody().getTargetModelOpt().get())
.getQualifiedName().toString()) :
packageName;
for (ASTTag element : unit.getTagBody().getTagList()) {
element.getTagElementList().stream()
.filter(t -> t.getName().equals("Weight"))
.filter(t -> !t.getTagValueOpt().isPresent())
.map(t -> checkContent(t.getTagValueOpt().get()))
.filter(r -> r != null)
.filter(this::checkUnit)
.forEachOrdered(v ->
element.getScopeList().stream()
.filter(this::checkScope)
.map(s -> (ASTNameScope) s)
.map(s -> tagging.resolve(Joiners.DOT.join(rootCmp, // resolve down does not try to reload symbol
s.getQualifiedName().toString()), EMAComponentInstanceSymbol.KIND))
.filter(Optional::isPresent)
.map(Optional::get)
.forEachOrdered(s -> tagging.addTag(s, new WeightSymbol(
NumericLiteral.getValue(v.getNumericLiteral()),
Unit.valueOf(v.getUnit()).asType(Mass.class)))));
}
}
protected boolean checkUnit(ASTUnitTagValue unitTag) {
Unit unit;
try {
unit = Unit.valueOf(unitTag.getUnit());
} catch (IllegalArgumentException e) {
Log.error(String.format("0xT0003 Could not parse unit '%s'. This unit is not supported. All supported units are %s and %s",
unitTag.getUnit(), "http://jscience.org/api/javax/measure/unit/SI.html",
"http://jscience.org/api/javax/measure/unit/NonSI.html"), e);
return false;
}
if (!unit.isCompatible(Mass.UNIT)) {
Log.error(String.format("0xT0002 The unit '%s' is not compatible to quantity kind 'Mass'",
unitTag.getUnit()));
return false;
}
return true;
}
protected ASTUnitTagValue checkContent(String s) {
TagValueParser parser = new TagValueParser();
Optional<ASTUnitTagValue> ast;
try {
boolean enableFailQuick = Log.isFailQuickEnabled();
Log.enableFailQuick(false);
long errorCount = Log.getErrorCount();
ast = parser.parseUnitTagValue(s);
Log.enableFailQuick(enableFailQuick);
if (Log.getErrorCount() > errorCount) {
throw new Exception("Error occured during parsing.");
}
} catch (Exception e) {
Log.error(String.format("0xT0004 Could not parse '%s' with TagValueParser#parseUnitTagValue",
s), e);
return null;
}
if (!ast.isPresent()) {
return null;
}
return ast.get();
}
protected EMAComponentInstanceSymbol checkKind(Collection<Symbol> symbols) {
EMAComponentInstanceSymbol ret = null;
for (Symbol symbol : symbols) {
if (symbol.getKind().isSame(EMAComponentInstanceSymbol.KIND)) {
if (ret != null) {
Log.error(String.format("0xA4095 Found more than one symbol: '%s' and '%s'",
ret, symbol));
return null;
}
ret = (EMAComponentInstanceSymbol)symbol;
}
}
if (ret == null) {
Log.error(String.format("0xT0001 Invalid symbol kinds: %s. tagTypeName expects as symbol kind 'EMAComponentInstanceSymbol.KIND'.",
symbols.stream().map(s -> "'" + s.getKind().toString() + "'").collect(Collectors.joining(", "))));
return null;
}
return ret;
}
protected boolean checkScope(ASTScope scope) {
if (scope.getScopeKind().equals("NameScope")) {
return true;
}
Log.error(String.format("0xT0005 Invalid scope kind: '%s'. Weight expects as scope kind 'NameScope'.",
scope.getScopeKind()), scope.get_SourcePositionStart());
return false;
}
}
\ No newline at end of file
......@@ -3,19 +3,16 @@
package de.monticore.lang.montiarc.tag.drawing.TraceabilityTagSchema;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;
import de.monticore.lang.montiarc._symboltable.*;
import de.monticore.lang.tagging._ast.ASTNameScope;
import de.monticore.lang.tagging._ast.ASTScope;
import de.monticore.lang.tagging._ast.ASTTag;
import de.monticore.lang.tagging._ast.ASTTaggingUnit;
import de.monticore.lang.tagging._symboltable.TagSymbolCreator;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import de.monticore.symboltable.SymbolKind;
import de.se_rwth.commons.Joiners;
import de.se_rwth.commons.logging.Log;
......@@ -32,57 +29,35 @@ public class IsTraceableSymbolCreator implements TagSymbolCreator {
return s;
}
public void create(ASTTaggingUnit unit, Scope gs) {
if (unit.getQualifiedNames().stream()
public void create(ASTTaggingUnit unit, TaggingResolver tagging) {
if (unit.getQualifiedNameList().stream()
.map(q -> q.toString())
.filter(n -> n.endsWith("TraceabilityTagSchema"))
.count() == 0) {
return; // the tagging model is not conform to the TraceabilityTagSchema tagging schema
}
final String packageName = Joiners.DOT.join(unit.getPackage());
final String packageName = Joiners.DOT.join(unit.getPackageList());
final String rootCmp = // if-else does not work b/c of final (required by streams)
(unit.getTagBody().getTargetModel().isPresent()) ?
Joiners.DOT.join(packageName, ((ASTNameScope) unit.getTagBody().getTargetModel().get())
(unit.getTagBody().getTargetModelOpt().isPresent()) ?
Joiners.DOT.join(packageName, ((ASTNameScope) unit.getTagBody().getTargetModelOpt().get())
.getQualifiedName().toString()) :
packageName;
for (ASTTag element : unit.getTagBody().getTagList()) {
element.getTagElements().stream()
element.getTagElementList().stream()
.filter(t -> t.getName().equals("IsTraceable"))
.filter(t -> !t.getTagValue().isPresent()) // only marker tag with no value
.filter(t -> !t.getTagValueOpt().isPresent()) // only marker tag with no value
.forEachOrdered(t ->
element.getScopeList().stream()
.filter(this::checkScope)
.map(s -> (ASTNameScope) s)
.map(s -> getGlobalScope(gs).<Symbol>resolveDownMany(
Joiners.DOT.join(rootCmp, s.getQualifiedName().toString()),
SymbolKind.KIND))
.filter(s -> !s.isEmpty())
.map(this::checkKind)
.filter(s -> s != null)
.forEachOrdered(s -> s.addTag(new IsTraceableSymbol())));
}
}
protected ComponentSymbol checkKind(Collection<Symbol> symbols) {
ComponentSymbol ret = null;
for (Symbol symbol : symbols) {
if (symbol.getKind().isSame(ComponentSymbol.KIND)) {
if (ret != null) {
Log.error(String.format("0xA4095 Found more than one symbol: '%s' and '%s'",
ret, symbol));
return null;
}
ret = (ComponentSymbol)symbol;
.map(s -> tagging.resolve(Joiners.DOT.join(rootCmp, // resolve down does not try to reload symbol
s.getQualifiedName().toString()), ComponentSymbol.KIND))
.filter(Optional::isPresent)
.map(Optional::get)
.forEachOrdered(s -> tagging.addTag(s, new IsTraceableSymbol())));
}
}
if (ret == null) {
Log.error(String.format("0xT0001 Invalid symbol kinds: %s. tagTypeName expects as symbol kind 'ComponentSymbol.KIND'.",
symbols.stream().map(s -> "'" + s.getKind().toString() + "'").collect(Collectors.joining(", "))));
return null;
}
return ret;