-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontractCreationProcess.java
More file actions
285 lines (238 loc) · 14.3 KB
/
Copy pathcontractCreationProcess.java
File metadata and controls
285 lines (238 loc) · 14.3 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package soloditysmartcontractgenerator.ContractCreator;
import Description.Event;
import Description.EventOutputParameter;
import Description.Input;
import Description.Output;
import Description.function;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.sound.midi.SysexMessage;
import soloditysmartcontractgenerator.Component.templates;
/**
*
* @author wfabe
*/
public class contractCreationProcess {
public static List<function> fns = new ArrayList<>();
public static List<Event> evs = new ArrayList<>();
public static List<function> FunctionsRecapFromTheJSONFile() {
// Chemin du fichier JSON
String filePath = "C:\\Users\\wfabe\\Documents\\NetBeansProjects\\SmartConverter\\SmartConverter\\src\\main\\java\\soloditysmartcontractgenerator\\Component\\twitter2.json";
// Nom de l'objet à récupérer
String objectName = "fonction";
try {
// Lire le fichier JSON
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(new File(filePath));
// Récupérer l'objet "ffon" de la perspective opérationnelle
JsonNode operationalPerspectiveNode = rootNode.get("operationalPerspective");
if (operationalPerspectiveNode != null && operationalPerspectiveNode.isObject()) {
JsonNode ffonNode = operationalPerspectiveNode.get("Functions");
if (ffonNode != null && ffonNode.isArray()) {
// Le premier objet dans la liste "Functions" est l'objet "ffon"
for (int i = 0; i < ffonNode.size(); i++) {
JsonNode firstFFONObject = ffonNode.get(i);
if (firstFFONObject != null && firstFFONObject.isObject()) {
// Convertir l'objet JSON en une classe Java correspondante
//function yourObjectFFON = objectMapper.treeToValue(firstFFONObject, function.class);
function fn = new function();
fn.setName(firstFFONObject.get("name").textValue());
fn.setType(firstFFONObject.get("Type").textValue());
fn.setScope(firstFFONObject.get("scope").textValue());
fn.setDescription(firstFFONObject.get("description").textValue());
// JsonNode functionsNode = operationalPerspectiveNode.get("Functions");
// Récupérer les objets "inputs" de chaque fonction
List<Input> inputsList = new ArrayList<Input>();
JsonNode inputsNode = firstFFONObject.get("inputs");
if (inputsNode != null && inputsNode.isArray()) {
for (JsonNode inputnode : inputsNode) {
// Convertir l'objet JSON en une instance de la classe Java correspondante
Input input = new Input();
input.setName(inputnode.get("name").textValue());
input.setType(inputnode.get("Type").textValue());
inputsList.add(input);
}
fn.setInputs(inputsList);
}
System.out.println("Objet 'inputsList' waaaa777 : " + inputsList);
// Récupérer les objets "inputs" de chaque fonction
List<Output> outputList = new ArrayList<>();
JsonNode outputsNode = firstFFONObject.get("outputs");
if (outputsNode != null && outputsNode.isArray()) {
for (JsonNode outputnode : outputsNode) {
// Convertir l'objet JSON en une instance de la classe Java correspondante
Output output = new Output();
output.setName( outputnode.get("name").textValue());
output.setType( outputnode.get("Type").textValue());
outputList.add(output);
}
}
fn.setOutputs(outputList);
fns.add(fn);
// Utiliser l'objet "ffon" récupéré
System.out.println("Objet 'ffon' récupéré : " + fn.toString());
} else {
System.out.println("L'objet 'ffon' n'a pas été trouvé dans la liste 'Functions'.");
}}
} else {
System.out.println("La liste 'Functions' n'a pas été trouvée dans la perspective opérationnelle.");
}
} else {
System.out.println("La perspective opérationnelle n'a pas été trouvée dans le fichier JSON.");
}
} catch (IOException e) {
System.out.println("Une erreur s'est produite lors de la lecture du fichier JSON : " + e.getMessage());
}
return fns ;
}
public static List<Event> EventsRecapFromTheJSONFile() throws IOException {
// Chemin du fichier JSON
String filePath = "C:\\Users\\wfabe\\Documents\\NetBeansProjects\\SmartConverter\\SmartConverter\\src\\main\\java\\soloditysmartcontractgenerator\\Component\\twitter2.json";
// Nom de l'objet à récupérer
// Lire le fichier JSON
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(new File(filePath));
// Récupérer l'objet "ffon" de la perspective opérationnelle
JsonNode EventsNode = rootNode.path("operationalPerspective").path("Events");
System.out.println("waaaw"+EventsNode);
if (EventsNode != null && EventsNode.isArray()) {
// Le premier objet dans la liste "Functions" est l'objet "ffon"
for (int i = 0; i < EventsNode.size(); i++) {
JsonNode firstFFONObject = EventsNode.get(i);
System.out.println(i+ "waaaw"+firstFFONObject);
if (firstFFONObject != null && firstFFONObject.isObject()) {
// Convertir l'objet JSON en une classe Java correspondante
//function yourObjectFFON = objectMapper.treeToValue(firstFFONObject, function.class);
Event ev = new Event();
ev.setName(firstFFONObject.get("name").textValue());
ev.setType(firstFFONObject.get("Type").textValue());
ev.setDescription(firstFFONObject.get("description").textValue());
System.out.println(i+ "waaaw"+ev.toString());
// Récupérer les objets "outputs" de chaque fonction
List<EventOutputParameter> outputList = new ArrayList<EventOutputParameter>();
System.out.println("wachh" +firstFFONObject.get("outputs"));
JsonNode inputsNode = firstFFONObject.get("outputs");
if (inputsNode != null && inputsNode.isArray()) {
for (JsonNode inputNode : inputsNode) {
// Convertir l'objet JSON en une instance de la classe Java correspondante
EventOutputParameter output = new EventOutputParameter();
output.setName(inputNode.get("name").textValue());
output.setType(inputNode.get("Type").textValue());
if(inputNode.get("is_indexed").asBoolean()){
output.setIs_indexed(true);
} else {
output.setIs_indexed(false);
}
outputList.add(output);
}
}
ev.setOutputs(outputList);
evs.add(ev);
// Utiliser l'objet "ffon" récupéré
System.out.println("Objet 'ffon' récupéré : " + ev.toString());
}}
}else{
System.err.println("objet event est vide");
}
return evs ;
}
public static String GetElementvaluefromJasonFile(String elementToFind){
String value="";
// Chemin du fichier JSON
String filePath = "C:\\Users\\wfabe\\Documents\\NetBeansProjects\\SmartConverter\\SmartConverter\\src\\main\\java\\soloditysmartcontractgenerator\\Component\\twitter2.json";
try {
// Lire le fichier JSON
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(new File(filePath));
// Rechercher l'élément et récupérer sa valeur
JsonNode elementNode = rootNode.get(elementToFind);
if (elementNode != null) {
String elementValue = elementNode.asText();
value = elementValue;
} else {
System.out.println("L'élément '" + elementToFind + "' n'a pas été trouvé dans le fichier JSON.");
}
} catch (IOException e) {
System.out.println("Une erreur s'est produite lors de la lecture du fichier JSON : " + e.getMessage());
}
return value;
}
public static File createContractSkeleton()throws Exception{
File file = new File("C:\\Users\\wfabe\\Documents\\GeneratedContract\\Alphacontract.txt");
try (FileWriter fileWriter = new FileWriter(file, false)) {
fileWriter.write (templates.getEntete());
fileWriter.write(System.lineSeparator());
fileWriter.write (templates.getFirst()+GetElementvaluefromJasonFile("udl-SC_version"));
fileWriter.write(System.lineSeparator());
fileWriter.write (templates.getContract()+" "+GetElementvaluefromJasonFile("name")+templates.getStart());
fileWriter.write(System.lineSeparator());
fns = FunctionsRecapFromTheJSONFile();
for (int i = 0; i < fns.size(); i++) {
fileWriter.write(templates.FNkeyword+" "+fns.get(i).getName()+templates.BeginAcolade);
if(fns.get(i).getInputs()==null ){
System.out.println("vide");
}else{
for (int j = 0; j< fns.get(i).getInputs().size(); j++) {
fileWriter.write(fns.get(i).getInputs().get(j).getType()+" "+fns.get(i).getInputs().get(j).getName());
System.out.println("j : : "+ j ); System.out.println("size : : "+ fns.get(i).getInputs().size() );
if (j<fns.get(i).getInputs().size()){
fileWriter.write(",");
}
}
}
fileWriter.write(templates.FinAcolade);
fileWriter.write(fns.get(i).getScope()+ " "+fns.get(i).getType()+" "+templates.Start);
fileWriter.write(System.lineSeparator());
fileWriter.write("//"+fns.get(i).getDescription());
fileWriter.write(System.lineSeparator());
fileWriter.write (templates.getEnd());
fileWriter.write(System.lineSeparator());
}
fileWriter.write(System.lineSeparator());
//get events
evs = EventsRecapFromTheJSONFile();
for (int i = 0; i < evs.size(); i++) {
fileWriter.write(templates.eventkeyword+" "+evs.get(i).getName()+templates.BeginAcolade);
if(evs.get(i).getOutputs()==null ){
System.out.println("vide");
}else{
for (int j = 0; j< evs.get(i).getOutputs().size(); j++) {
fileWriter.write(evs.get(i).getOutputs().get(j).getType()+" "+evs.get(i).getOutputs().get(j).getName());
if (j<evs.get(i).getOutputs().size()){
fileWriter.write(",");
}
}
}
fileWriter.write(templates.FinAcolade+templates.Fin);
fileWriter.write("//"+fns.get(i).getDescription());
fileWriter.write(System.lineSeparator());
fileWriter.write (templates.getEnd());
fileWriter.write(System.lineSeparator());
}
fileWriter.write(System.lineSeparator());
fileWriter.write (templates.getEnd());
// Write the extracted string to the file
// fileWriter.write(transformJsonToaTree());
System.out.println("String added to the file successfully.");
} catch (IOException e) {
System.out.println("An error occurred while writing to the file: " + e.getMessage());
}
return file;
}
public static void main(String[] args) throws Exception{
File file = new File("C:\\Users\\wfabe\\Documents\\GeneratedContract\\contract.txt");
//contract, constructor skeleton creation
file = createContractSkeleton();
//function and event skeleton creation
//function and event logic construction
}
}