forked from pascal-lab/Tai-e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanConfig.java
More file actions
135 lines (120 loc) · 4.24 KB
/
PlanConfig.java
File metadata and controls
135 lines (120 loc) · 4.24 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
/*
* Tai-e: A Static Analysis Framework for Java
*
* Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn>
* Copyright (C) 2022 Yue Li <yueli@nju.edu.cn>
*
* This file is part of Tai-e.
*
* Tai-e 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
* of the License, or (at your option) any later version.
*
* Tai-e 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 Tai-e. If not, see <https://www.gnu.org/licenses/>.
*/
package pascal.taie.config;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
/**
* Configuration for an analysis to be executed.
* <p>
* Different from {@link AnalysisConfig} which is specified by configuration file,
* {@link PlanConfig} is specified by either plan file or options.
*
* @see AnalysisConfig
*/
public class PlanConfig {
private static final Logger logger = LogManager.getLogger(PlanConfig.class);
private static final String PLAN_FILE = "tai-e-plan.yml";
/**
* Unique identifier of the analysis.
*/
@JsonProperty
private final String id;
/**
* Options for the analysis.
*/
@JsonProperty
private final AnalysisOptions options;
@JsonCreator
public PlanConfig(
@JsonProperty("id") String id,
@JsonProperty("options") AnalysisOptions options) {
this.id = id;
this.options = Objects.requireNonNullElse(options,
AnalysisOptions.emptyOptions());
}
public String getId() {
return id;
}
public AnalysisOptions getOptions() {
return options;
}
@Override
public String toString() {
return "PlanConfig{" +
"id='" + id + '\'' +
", options=" + options +
'}';
}
/**
* Read a list of PlanConfig from given file.
*/
public static List<PlanConfig> readConfigs(File file) {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
JavaType type = mapper.getTypeFactory()
.constructCollectionType(List.class, PlanConfig.class);
try {
return mapper.readValue(file, type);
} catch (IOException e) {
throw new ConfigException("Failed to read plan file " + file, e);
}
}
/**
* Reads a list of PlanConfig from options.
*/
public static List<PlanConfig> readConfigs(Options options) {
return options.getAnalyses().entrySet()
.stream()
.map(entry -> {
String id = entry.getKey();
AnalysisOptions analysisOptions = entry.getValue();
return new PlanConfig(id, analysisOptions);
})
.toList();
}
/**
* Writes a list of PlanConfigs to given file.
*/
public static void writeConfigs(List<PlanConfig> planConfigs, File outputDir) {
File outFile = new File(outputDir, PLAN_FILE);
ObjectMapper mapper = new ObjectMapper(
new YAMLFactory()
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES));
try {
logger.info("Writing analysis plan to {}", outFile.getAbsolutePath());
mapper.writeValue(outFile, planConfigs);
} catch (IOException e) {
throw new ConfigException("Failed to write plan file to "
+ outFile.getAbsolutePath(), e);
}
}
}