Commit a0646bbb authored by Sascha Niklas Schneiders's avatar Sascha Niklas Schneiders
Browse files

updated

parent c1258ff6
Pipeline #207251 failed with stage
in 56 seconds
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.tagging._symboltable;
import de.monticore.lang.tagging._ast.ASTTagElement;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Sascha Schneiders
*/
public class TagPair {
protected String taggedSymbolName;
protected ArrayList<Object> values = new ArrayList<>();
protected ArrayList<ASTTagElement> tagElements = new ArrayList<>();
public TagPair(String name, Object... values){
this.taggedSymbolName = name;
this.values.addAll(Arrays.asList(values));
}
public TagPair(String name, ASTTagElement... values){
this.taggedSymbolName = name;
this.values.addAll(Arrays.asList(values));
}
public String getTaggedSymbolName() {
return taggedSymbolName;
}
public void setTaggedSymbolName(String taggedSymbolName) {
this.taggedSymbolName = taggedSymbolName;
}
public void setValues(ArrayList<Object> values) {
this.values = values;
}
public List<Object> getValues() {
return this.values;
}
/**
* has no range check, it is should be only used for classes
* which extends this class, they know how many objects are stored
*/
protected <T> T getValue(int index) {
return (T) this.values.get(index);
}
/**
* is only needed for multiple inheritance chains
*/
protected void addValues(Object... values) {
for (Object value : values) {
this.values.add(value);
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getTaggedSymbolName());
if (!values.isEmpty()) {
sb.append("{").append(values.stream().map(v -> v.toString()).collect(Collectors.joining(";"))).append("}");
}
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof TagPair))
return false;
TagPair tagPair = (TagPair) o;
return values.equals(tagPair.values);
}
@Override
public int hashCode() {
return values.hashCode();
}
}
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.tagging._symboltable;
import de.monticore.lang.tagging._ast.ASTTaggingUnit;
import de.monticore.lang.tagging._parser.TaggingParser;
import de.monticore.lang.tagging.helper.RangeFixer;
import de.monticore.symboltable.ISymbol;
import de.se_rwth.commons.Names;
import de.se_rwth.commons.logging.Log;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
/**
* @author Sascha Schneiders
*/
public class TagResolver {
public List<TagSymbol> allCreatedTagSymbols = new ArrayList<>();
protected boolean tagModelsLoaded = false;
public void loadTagModels(ITaggingGlobalScope globalScope, String... modelPath) {
ArrayList<String> list = new ArrayList();
list.addAll(Arrays.asList(modelPath));
loadTagModels(list, new TaggingLanguage().getSymbolTableCreator(globalScope).taggingSTC);
}
public void loadTagModels(TaggingSymbolTableCreator taggingSymbolTableCreator, String... modelPath) {
ArrayList<String> list = new ArrayList();
list.addAll(Arrays.asList(modelPath));
loadTagModels(list, taggingSymbolTableCreator);
}
public void loadTagModels(Collection<String> modelPaths, ITaggingGlobalScope globalScope) {
loadTagModels(modelPaths, new TaggingLanguage().getSymbolTableCreator(globalScope).taggingSTC);
}
public void loadTagModels(Collection<String> modelPaths,
TaggingSymbolTableCreator taggingSymbolTableCreator) {
// parse all tag files in the model paths and
Collection<ASTTaggingUnit> tags = parseTags(modelPaths);
taggingSymbolTableCreator.setTagResolver(this);
for (ASTTaggingUnit tag : tags) {
// for each tag file apply real tagSymbolCreator
tag.accept(taggingSymbolTableCreator);
}
}
protected Collection<ASTTaggingUnit> parseTags(final Collection<String> modelPaths) {
final Collection<ASTTaggingUnit> foundModels = new ArrayList<>();
for (String mp : modelPaths) {
final Path completePath = Paths.get(mp);
final File f = completePath.toFile();
if (f != null && f.isDirectory()) {
Collection<File> tagFiles = FileUtils.listFiles(f,
new String[] { TaggingLanguage.fileEnding }, true);
tagFiles.stream().forEachOrdered(t -> {
final TaggingParser parser = new TaggingParser();
Optional<ASTTaggingUnit> ast = Optional.empty();
try {
ast = parser.parse(t.getAbsolutePath());
}
catch (IOException e) {
Log.error("could not open file " + t, e);
}
if (ast.isPresent()) {
// todo check package conformity
RangeFixer.fixTaggingUnit(ast.get());
foundModels.add(ast.get());
}
});
}
}
return foundModels;
}
/**
* Method does not load tag symbols anymore!
*
* @param symbol
* @return
*/
public Collection<TagSymbol> getTagSymbolsFor(String symbolName) {
List<TagSymbol> list = new ArrayList<>();
for (TagSymbol tagSymbol : allCreatedTagSymbols) {
if (tagSymbol.getTaggedSymbolName().equals(symbolName)) {
list.add(tagSymbol);
}
}
return list;
}
/**
* Method does not load tags anymore!
*
* @param symbol
* @return
*/
public Collection<TagPair> getTagsFor(String symbolName) {
List<TagPair> list = new ArrayList<>();
for (TagSymbol tagSymbol : allCreatedTagSymbols) {
for (TagPair tagPair : tagSymbol.getTagPairs()) {
String tagSymbolName = tagPair.getTaggedSymbolName();
if (tagSymbolName.equals(symbolName)) {
list.add(tagPair);
}
}
}
return list;
}
}
......@@ -21,57 +21,56 @@
package de.monticore.lang.tagging._symboltable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Symboltable entry for tag.
* This is the base class for tags, if you define a new tag
* this class must be extended
*
* @author Michael von Wenckstern
* This is the base class for tags, all tags can be represented with it.
* The TagPairs store the definition of the abtract syntax tree, but they can also be processed
* further to store specific object inside.
*/
public class TagSymbol extends TagSymbolTOP {
protected String taggedSymbolName;
protected ArrayList<Object> values = new ArrayList<>();
protected List<TagPair> tagPairs = new ArrayList<>();
public TagSymbol(String name, Object... values) {
public TagSymbol(String name) {
super(name);
for (Object value : values) {
this.values.add(value);
}
}
public List<Object> getValues() {
return this.values;
public void addTagPair(TagPair tagPair) {
this.tagPairs.add(tagPair);
}
public List<TagPair> getTagPairs() {
return this.tagPairs;
}
/**
* has no range check, it is should be only used for classes
* which extends this class, they know how many objects are stored
*/
protected <T> T getValue(int index) {
return (T) this.values.get(index);
protected <T> T getTagPair(int index) {
return (T) this.tagPairs.get(index);
}
/**
* is only needed for multiple inheritance chains
*/
protected void addValues(Object... values) {
for (Object value : values) {
this.values.add(value);
}
protected void addValues(TagPair... tagPairs) {
this.tagPairs.addAll(Arrays.asList(tagPairs));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getName());
if (!values.isEmpty()) {
sb.append("{")
.append(values.stream().map(v -> v.toString()).collect(Collectors.joining(";")))
.append("}");
if (!tagPairs.isEmpty()) {
sb.append("{").append(tagPairs.stream().map(v -> v.toString()).collect(Collectors.joining(
";"))).append("}");
}
return sb.toString();
}
......@@ -85,11 +84,20 @@ public class TagSymbol extends TagSymbolTOP {
TagSymbol tagSymbol = (TagSymbol) o;
return values.equals(tagSymbol.values);
return tagPairs.equals(tagSymbol.tagPairs);
}
@Override
public int hashCode() {
return values.hashCode();
return tagPairs.hashCode();
}
public String getTaggedSymbolName() {
return taggedSymbolName;
}
public void setTaggedSymbolName(String taggedSymbolName) {
this.taggedSymbolName = taggedSymbolName;
}
}
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.tagging._symboltable;
public class TaggingLanguage extends TaggingLanguageTOP {
public static String fileEnding = "tag";
public TaggingLanguage() {
super("Tagging Language", fileEnding);
}
@Override
protected TaggingModelLoader provideModelLoader() {
return new TaggingModelLoader(this);
}
}
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.tagging._symboltable;
import de.monticore.io.paths.ModelPath;
import de.monticore.lang.tagging._ast.*;
import de.monticore.lang.tagging._parser.TaggingParser;
import de.monticore.lang.tagging.helper.RangeFixer;
import de.monticore.symboltable.ISymbol;
import de.monticore.symboltable.ImportStatement;
import de.se_rwth.commons.Names;
import de.se_rwth.commons.logging.Log;
import external1.types._ast.ASTImportStatement;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class TaggingSymbolTableCreator extends TaggingSymbolTableCreatorTOP {
protected TagResolver tagResolver = new TagResolver();
public TaggingSymbolTableCreator(ITaggingScope enclosingScope) {
super(enclosingScope);
}
public TaggingSymbolTableCreator(Deque<? extends ITaggingScope> scopeStack) {
super(scopeStack);
}
protected ASTTaggingUnit unit = null;
public TaggingArtifactScope createFromAST(de.monticore.lang.tagging._ast.ASTTaggingUnit rootNode) {
Log.errorIfNull(rootNode, "0xA7004x227 Error by creating of the TaggingSymbolTableCreatorTOP "
+ "symbol table: top ast node is null");
// imports
List<ImportStatement> imports = new ArrayList<>();
for (ASTImportStatement astImportStatement : rootNode.getImportStatementList()) {
String qualifiedImport = Names.getQualifiedName(astImportStatement.getImportList());
ImportStatement importStatement = new ImportStatement(qualifiedImport,
astImportStatement.isStar());
imports.add(importStatement);
}
TaggingArtifactScope artifactScope = new TaggingArtifactScope(Optional.empty(),
Names.getQualifiedName(rootNode.getPackageList()), imports);
putOnStack(artifactScope);
rootNode.accept(getRealThis());
return artifactScope;
}
@Override
public void visit(ASTTaggingUnit node) {
super.visit(node);
unit = node;
}
@Override
public void visit(ASTTag node) {
super.visit(node);
if (node.getTargetModelOpt().isPresent())
node.getSymbol().setTaggedSymbolName(node.getTargetModel().getQualifiedNameString());
tagResolver.allCreatedTagSymbols.add(node.getSymbol());
}
@Override
public void visit(ASTTagSingle node) {
super.visit(node);
for (ASTScope astScope : node.getScopeList()) {
if (astScope instanceof ASTNameScope) {
TagPair tagPair = new TagPair(astScope.getQualifiedNameString(), node.getTagElementList());
TagSymbol currentTagSymbol =
tagResolver.allCreatedTagSymbols.get(tagResolver.allCreatedTagSymbols.size() - 1);
currentTagSymbol.addTagPair(tagPair);
}
}
}
public TagResolver getTagResolver() {
return tagResolver;
}
public void setTagResolver(TagResolver tagResolver) {
this.tagResolver = tagResolver;
}
}
......@@ -28,12 +28,13 @@ import javax.measure.Measure;
import javax.measure.quantity.Power;
import javax.measure.unit.Unit;
import de.monticore.lang.tagging._symboltable.TagPair;
import de.monticore.lang.tagging._symboltable.TagSymbol;
/**
* Created by ValuedTagType.ftl
*/
public class PowerBooleanSymbol extends TagSymbol {
public class PowerBooleanSymbol extends TagPair {
protected PowerBooleanSymbol(String name, Measure<? extends Number, Power> value) {
super(name, value);
......
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.tagging;
import de.monticore.io.paths.ModelPath;
import de.monticore.lang.tagging._symboltable.*;
import org.junit.Test;
import java.nio.file.Paths;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
/**
* @author Sascha Schneiders
*/
public class SymtabTest {
public static ITaggingScope createTaggingSymTab(String... modelPath) {
ModelPath mp = new ModelPath();
for (String s : modelPath) {
mp.addEntry(Paths.get(s));
}
//TODO IMPLEMENT ME!D
ITaggingGlobalScope scope = new TaggingGlobalScope(mp, new TaggingLanguage());
return scope;
}
@Test
public void testAdaptableSymbolParsing() {
ITaggingScope taggingResolver = createTaggingSymTab("src/test/resources/");
TagSymbol tagSymbol = taggingResolver.resolveTag("industry.LayoutInformation").orElse(null);
assertNotNull(tagSymbol);
for (int i = 0; i < 3; ++i) {
Object tags1 = tagSymbol.getTagPairs().get(0);
assertNotNull(tags1);
}
}
}
package industry;
conforms to nfp.LayoutTagSchema;
// in the example are all units in mW, but here I changed it for testing reasons
tags LayoutInformation for turbineController {
tag piController with ComponentLayout =
{ id = 2, pos = (10, 20), size = (15, 10), layoutPosition = 1,
reservedHorizontalSpace = 10 } ; // isOnTop is false (b/c it is not mentioned)
tag filtering with ComponentLayout =
{ id = 2, pos = (10, 20), size = (15, 10), layoutPosition = 1,