Project

General

Profile

Revision 7

Added by Willibald K. over 8 years ago

changing java, cpp, hpp files to unix line endings

View differences:

OoaIdentifierCompressVisitor.java
1
/**

2
  *

3
  *                      OOAS Compiler

4
  *

5
  *       Copyright 2015, AIT Austrian Institute of Technology.

6
  * This code is based on the C# Version of the OOAS Compiler, which is

7
  * copyright 2015 by the Institute of Software Technology, Graz University

8
  * of Technology with portions copyright by the AIT Austrian Institute of

9
  * Technology. All rights reserved.

10
  *

11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.

12
  *

13
  * If you modify the file please update the list of contributors below to in-

14
  * clude your name. Please also stick to the coding convention of using TABs

15
  * to do the basic (block-level) indentation and spaces for anything after

16
  * that. (Enable the display of special chars and it should be pretty obvious

17
  * what this means.) Also, remove all trailing whitespace.

18
  *

19
  * Contributors:

20
  *               Willibald Krenn (AIT)

21
  *               Stephan Zimmerer (AIT)

22
  *               Markus Demetz (AIT)

23
  *               Christoph Czurda (AIT)

24
  *

25
  */

1
/**
2
  *
3
  *                      OOAS Compiler
4
  *
5
  *       Copyright 2015, AIT Austrian Institute of Technology.
6
  * This code is based on the C# Version of the OOAS Compiler, which is
7
  * copyright 2015 by the Institute of Software Technology, Graz University
8
  * of Technology with portions copyright by the AIT Austrian Institute of
9
  * Technology. All rights reserved.
10
  *
11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
12
  *
13
  * If you modify the file please update the list of contributors below to in-
14
  * clude your name. Please also stick to the coding convention of using TABs
15
  * to do the basic (block-level) indentation and spaces for anything after
16
  * that. (Enable the display of special chars and it should be pretty obvious
17
  * what this means.) Also, remove all trailing whitespace.
18
  *
19
  * Contributors:
20
  *               Willibald Krenn (AIT)
21
  *               Stephan Zimmerer (AIT)
22
  *               Markus Demetz (AIT)
23
  *               Christoph Czurda (AIT)
24
  *
25
  */
26 26

  
27 27

  
28
package org.momut.ooas.visitors;
29

  
30
import java.util.ArrayList;
31
import java.util.EnumMap;
32
import java.util.List;
33
import java.util.Set;
34
import java.util.TreeMap;
28
package org.momut.ooas.visitors;
35 29

  
30
import java.util.ArrayList;
31
import java.util.EnumMap;
32
import java.util.List;
33
import java.util.Set;
34
import java.util.TreeMap;
35

  
36 36
import org.momut.ooas.CompilerConfiguration.LabelCompression;
37 37
import org.momut.ooas.ast.identifiers.AttributeIdentifier;
38 38
import org.momut.ooas.ast.identifiers.Identifier;
......
45 45
import org.momut.ooas.parser.ParserState;
46 46
import org.momut.ooas.utils.exceptions.ArgumentException;
47 47
import org.momut.ooas.utils.exceptions.NotImplementedException;
48

  
49
public class OoaIdentifierCompressVisitor extends OoaCompleteAstTraversalVisitor {
50

  
51
	public static final String s_methodPrefix    = "m_";
52
	public static final String s_actionPrefix    = "a_";
53
	public static final String s_attributePrefix = "at_";
54
	public static final String s_objectPrefix    = "o_";
55
	public static final String s_typePrefix      = "t_";
56

  
57
	private final EnumMap<IdentifierKind, TreeMap<String, String> > m_identifierMappings = new EnumMap<>(IdentifierKind.class);
58
	private final TreeMap<String, String> m_objectMappings = new TreeMap<>();
59
	private final LabelCompression m_compression;
60

  
61
	private void putMapping(String label, Identifier identifier) {
62
		TreeMap<String, String> map = m_identifierMappings.get(identifier.kind());
63
		if (map == null) {
64
			map = new TreeMap<>();
65
			m_identifierMappings.put(identifier.kind(), map);
66
		}
67
		map.put(label, identifier.tokenText());
68
	}
69

  
70
	public String mappingFor(Identifier identifier)	{
71
		final IdentifierKind kind = identifier.kind();
72
		final TreeMap<String, String> map = m_identifierMappings.get(kind);
73
		return map.get(identifier.tokenText());
74
	}
75

  
76
	@Override
77
	public /*override*/ void visit(AttributeIdentifier attributeIdentifier)	{
78
		if (m_compression != LabelCompression.All)
79
			return;
80
		final String attributeName = labelFor(attributeIdentifier);
81
		attributeIdentifier.SetTokenText(attributeName);
82
	}
83

  
84
	@Override
85
	public /*override*/ void visit(MethodIdentifier methodIdentifier)	{
86
		final String renamedName = labelFor(methodIdentifier);
87
		methodIdentifier.SetTokenText(renamedName);
88
	}
89

  
90
	@Override
91
	public /*override*/ void visit(NamedActionIdentifier namedActionIdentifier)	{
92
		final FunctionType functionType = (FunctionType) namedActionIdentifier.type();
93
		if (functionType.functionType() == FunctionTypeEnum.Internal) {
94
				final String renamedName = labelFor(namedActionIdentifier);
95
				namedActionIdentifier.SetTokenText(renamedName);
96
		}
97
	}
98

  
99
	public String labelFor(Identifier identifier) {
100
		String label = identifier.tokenText();
101

  
102
		switch(identifier.kind()) {
103
			case MethodIdentifier:
104
				label = s_methodPrefix + convert(methodCount++);
105
				break;
106
			case NamedActionIdentifier:
107
				label = s_actionPrefix + convert(actionCount++);
108
				break;
109
			case AttributeIdentifier:
110
				label = s_attributePrefix + convert(attributeCount++);
111
				break;
112
			default:
113
				throw new NotImplementedException();
114
		}
115
		putMapping(label, identifier);
116
		return label;
117
	}
118

  
119
	public String labelFor(OoActionSystemInstance instance) {
120
		final String label = s_objectPrefix + convert(objectCount++);
121
		m_objectMappings.put(label, instance.Name);
122
		return label;
123
	}
124

  
125
	public String originalName(String key) {
126
		if(m_objectMappings.get(key) != null) {
127
			return m_objectMappings.get(key);
128
		}
129
		return key;
130
	}
131

  
132
	public List<String> mappingInformation() {
133
		final List<String> lines = new ArrayList<>();
134
		final Set<IdentifierKind> kinds = m_identifierMappings.keySet();
135

  
136
		for (final IdentifierKind kind: kinds) {
137
			final TreeMap<String, String> map = m_identifierMappings.get(kind);
138
			final Set<String> keys = map.keySet();
139
			lines.add(String.format("[ Mappings for '%s' ]", kind.name()));
140
			for(final String key: keys) {
141
				lines.add(String.format("'%s' => '%s'", key, map.get(key)));
142
			}
143
			lines.add("");
144
		}
145

  
146
		if (m_objectMappings.isEmpty())
147
			return lines;
148

  
149
		lines.add(String.format("[ Object mappings ]"));
150
		final Set<String> keys = m_objectMappings.keySet();
151
		for (final String key: keys) {
152
			lines.add(String.format("'%s' => '%s'", key, originalName(key)));
153
		}
154
		lines.add("");
155
		return lines;
156
	}
157

  
158
	public LabelCompression compression() {
159
		return m_compression;
160
	}
161

  
162
	static final String CHARS = "abcdefghijklmnopqrstuvwxyz"; /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
163
	static final int CHARS_LENGTH = CHARS.length();
164

  
165
	int actionCount = 0;
166
	int methodCount = 0;
167
	int attributeCount = 0;
168
	int objectCount = 0;
169

  
170
	private String convert(int id) {
171
		final StringBuilder sb = new StringBuilder();
172
		do {
173
			sb.append(CHARS.charAt(id % CHARS_LENGTH));
174
			id = id / CHARS_LENGTH;
175
		} while(id != 0);
176
		return sb.toString();
177
	}
178

  
179
	public OoaIdentifierCompressVisitor(ParserState aState, LabelCompression compression)
180
	{
181
		super (aState);
182
		if (aState == null || compression == null)
183
			throw new ArgumentException();
184
		m_compression = compression;
185
	}
186
}
48

  
49
public class OoaIdentifierCompressVisitor extends OoaCompleteAstTraversalVisitor {
50

  
51
	public static final String s_methodPrefix    = "m_";
52
	public static final String s_actionPrefix    = "a_";
53
	public static final String s_attributePrefix = "at_";
54
	public static final String s_objectPrefix    = "o_";
55
	public static final String s_typePrefix      = "t_";
56

  
57
	private final EnumMap<IdentifierKind, TreeMap<String, String> > m_identifierMappings = new EnumMap<>(IdentifierKind.class);
58
	private final TreeMap<String, String> m_objectMappings = new TreeMap<>();
59
	private final LabelCompression m_compression;
60

  
61
	private void putMapping(String label, Identifier identifier) {
62
		TreeMap<String, String> map = m_identifierMappings.get(identifier.kind());
63
		if (map == null) {
64
			map = new TreeMap<>();
65
			m_identifierMappings.put(identifier.kind(), map);
66
		}
67
		map.put(label, identifier.tokenText());
68
	}
69

  
70
	public String mappingFor(Identifier identifier)	{
71
		final IdentifierKind kind = identifier.kind();
72
		final TreeMap<String, String> map = m_identifierMappings.get(kind);
73
		return map.get(identifier.tokenText());
74
	}
75

  
76
	@Override
77
	public /*override*/ void visit(AttributeIdentifier attributeIdentifier)	{
78
		if (m_compression != LabelCompression.All)
79
			return;
80
		final String attributeName = labelFor(attributeIdentifier);
81
		attributeIdentifier.SetTokenText(attributeName);
82
	}
83

  
84
	@Override
85
	public /*override*/ void visit(MethodIdentifier methodIdentifier)	{
86
		final String renamedName = labelFor(methodIdentifier);
87
		methodIdentifier.SetTokenText(renamedName);
88
	}
89

  
90
	@Override
91
	public /*override*/ void visit(NamedActionIdentifier namedActionIdentifier)	{
92
		final FunctionType functionType = (FunctionType) namedActionIdentifier.type();
93
		if (functionType.functionType() == FunctionTypeEnum.Internal) {
94
				final String renamedName = labelFor(namedActionIdentifier);
95
				namedActionIdentifier.SetTokenText(renamedName);
96
		}
97
	}
98

  
99
	public String labelFor(Identifier identifier) {
100
		String label = identifier.tokenText();
101

  
102
		switch(identifier.kind()) {
103
			case MethodIdentifier:
104
				label = s_methodPrefix + convert(methodCount++);
105
				break;
106
			case NamedActionIdentifier:
107
				label = s_actionPrefix + convert(actionCount++);
108
				break;
109
			case AttributeIdentifier:
110
				label = s_attributePrefix + convert(attributeCount++);
111
				break;
112
			default:
113
				throw new NotImplementedException();
114
		}
115
		putMapping(label, identifier);
116
		return label;
117
	}
118

  
119
	public String labelFor(OoActionSystemInstance instance) {
120
		final String label = s_objectPrefix + convert(objectCount++);
121
		m_objectMappings.put(label, instance.Name);
122
		return label;
123
	}
124

  
125
	public String originalName(String key) {
126
		if(m_objectMappings.get(key) != null) {
127
			return m_objectMappings.get(key);
128
		}
129
		return key;
130
	}
131

  
132
	public List<String> mappingInformation() {
133
		final List<String> lines = new ArrayList<>();
134
		final Set<IdentifierKind> kinds = m_identifierMappings.keySet();
135

  
136
		for (final IdentifierKind kind: kinds) {
137
			final TreeMap<String, String> map = m_identifierMappings.get(kind);
138
			final Set<String> keys = map.keySet();
139
			lines.add(String.format("[ Mappings for '%s' ]", kind.name()));
140
			for(final String key: keys) {
141
				lines.add(String.format("'%s' => '%s'", key, map.get(key)));
142
			}
143
			lines.add("");
144
		}
145

  
146
		if (m_objectMappings.isEmpty())
147
			return lines;
148

  
149
		lines.add(String.format("[ Object mappings ]"));
150
		final Set<String> keys = m_objectMappings.keySet();
151
		for (final String key: keys) {
152
			lines.add(String.format("'%s' => '%s'", key, originalName(key)));
153
		}
154
		lines.add("");
155
		return lines;
156
	}
157

  
158
	public LabelCompression compression() {
159
		return m_compression;
160
	}
161

  
162
	static final String CHARS = "abcdefghijklmnopqrstuvwxyz"; /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
163
	static final int CHARS_LENGTH = CHARS.length();
164

  
165
	int actionCount = 0;
166
	int methodCount = 0;
167
	int attributeCount = 0;
168
	int objectCount = 0;
169

  
170
	private String convert(int id) {
171
		final StringBuilder sb = new StringBuilder();
172
		do {
173
			sb.append(CHARS.charAt(id % CHARS_LENGTH));
174
			id = id / CHARS_LENGTH;
175
		} while(id != 0);
176
		return sb.toString();
177
	}
178

  
179
	public OoaIdentifierCompressVisitor(ParserState aState, LabelCompression compression)
180
	{
181
		super (aState);
182
		if (aState == null || compression == null)
183
			throw new ArgumentException();
184
		m_compression = compression;
185
	}
186
}

Also available in: Unified diff