forked from eclipse/vorto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DevtoolReferenceLinker.java
157 lines (136 loc) · 6.45 KB
/
DevtoolReferenceLinker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package org.eclipse.vorto.server.devtool.utils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel;
import org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty;
import org.eclipse.vorto.core.api.model.informationmodel.InformationModel;
import org.eclipse.vorto.core.api.model.informationmodel.InformationModelFactory;
import org.eclipse.vorto.core.api.model.model.Model;
import org.eclipse.vorto.core.api.model.model.ModelReference;
import org.eclipse.vorto.core.api.model.model.ModelType;
import org.eclipse.vorto.http.model.ModelId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class DevtoolReferenceLinker {
@Autowired
DevtoolRestClient informationModelEditorRestClient;
public void linkFunctionBlockToInfoModel(String infoModelResourceId, ModelId functionBlockModelId,
ResourceSet resourceSet, Set<String> referencedResourceSet) {
if (!containsResource(infoModelResourceId, resourceSet)) {
throw new RuntimeException("No resource with resourceId : " + infoModelResourceId);
}
ModelType modelType = informationModelEditorRestClient.getModelType(functionBlockModelId);
if (!modelType.equals(ModelType.Functionblock)) {
throw new RuntimeException("No FunctionBlock [" + functionBlockModelId.toString() + "]");
}
linkReferenceToModel(infoModelResourceId, functionBlockModelId, resourceSet, referencedResourceSet);
Resource targetResource = resourceSet.getResource(URI.createURI(infoModelResourceId), true);
InformationModel informationModel = (InformationModel) targetResource.getContents().get(0);
Resource referencedResource = resourceSet.getResource(getURI(functionBlockModelId), true);
EObject eObject = referencedResource.getContents().get(0);
FunctionblockModel funtionblockModel = (FunctionblockModel) eObject;
informationModel.getProperties().add(
createFunctionblockProperty(funtionblockModel, getVariableNames(informationModel.getProperties())));
referencedResource.getContents().add(eObject);
}
public void linkDataTypeToFunctionBlock(String functionBlockResourceId, ModelId datatypeModelId,
ResourceSet resourceSet, Set<String> referencedResourceSet) {
if (!containsResource(functionBlockResourceId, resourceSet)) {
throw new RuntimeException("No resource with resourceId : " + functionBlockResourceId);
}
ModelType modelType = informationModelEditorRestClient.getModelType(datatypeModelId);
if (!modelType.equals(ModelType.Datatype)) {
throw new RuntimeException("No DataType [" + datatypeModelId.toString() + "]");
}
linkReferenceToModel(functionBlockResourceId, datatypeModelId, resourceSet, referencedResourceSet);
}
private void linkReferenceToModel(String modelResourceId, ModelId referenceModelId, ResourceSet resourceSet,
Set<String> referencedResourceSet) {
ModelType referenceModelType = informationModelEditorRestClient.getModelType(referenceModelId);
String fileName = getFileName(referenceModelId);
URI uri = getURI(referenceModelId);
Resource targetResource = resourceSet.getResource(URI.createURI(modelResourceId), true);
Model model = (Model) targetResource.getContents().get(0);
ModelReference modelReference = new org.eclipse.vorto.core.api.model.model.ModelId(referenceModelType,
referenceModelId.getName(), referenceModelId.getNamespace(), referenceModelId.getVersion())
.asModelReference();
if (!containsModelReference(model, modelReference)) {
Resource resource = null;
EObject eObject = null;
if (!referencedResourceSet.contains(fileName)) {
String fileContents = informationModelEditorRestClient.getModelFile(referenceModelId);
try {
resource = resourceSet.createResource(uri);
resource.load(new ByteArrayInputStream(fileContents.getBytes((StandardCharsets.UTF_8))),
resourceSet.getLoadOptions());
referencedResourceSet.add(fileName);
System.out.println(resourceSet);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
resource = resourceSet.getResource(uri, true);
eObject = resource.getContents().get(0);
targetResource.getContents().add(eObject);
model.getReferences().add(modelReference);
resource.getContents().add(eObject);
}
}
private boolean containsResource(String resourceId, ResourceSet resourceSet) {
URI uri = URI.createURI(resourceId);
Resource resource = resourceSet.getResource(uri, true);
if (resource == null) {
return false;
} else {
return true;
}
}
private boolean containsModelReference(Model model, ModelReference reference) {
for (ModelReference ref : model.getReferences()) {
if (ref.getImportedNamespace().equals(reference.getImportedNamespace())) {
return true;
}
}
return false;
}
private Set<String> getVariableNames(EList<FunctionblockProperty> properties) {
Set<String> variableNames = new HashSet<String>();
for (FunctionblockProperty property : properties) {
variableNames.add(property.getName());
}
return variableNames;
}
private FunctionblockProperty createFunctionblockProperty(FunctionblockModel functionblockModel,
Set<String> existingVariableNames) {
FunctionblockProperty functionblockProperty = InformationModelFactory.eINSTANCE.createFunctionblockProperty();
functionblockProperty.setType(functionblockModel);
functionblockProperty.setName(generateFunctionBlockVariableName(functionblockModel, existingVariableNames));
return functionblockProperty;
}
private String generateFunctionBlockVariableName(FunctionblockModel fbm, Set<String> variableNames) {
String variableName = fbm.getName().toLowerCase();
int i = 0;
while (variableNames.contains(variableName)) {
variableName = fbm.getName().toLowerCase() + ++i;
}
return variableName;
}
private String getFileName(ModelId modelId) {
ModelType modelType = informationModelEditorRestClient.getModelType(modelId);
return modelId.getNamespace().replace(".", "_") + "_" + modelId.getName() + "_"
+ modelId.getVersion().replace(".", "_") + modelType.getExtension();
}
private URI getURI(ModelId modelId) {
URI uri = URI.createURI("fake:/" + getFileName(modelId));
return uri;
}
}