1+ package de .fraunhofer .iem .swan .model .toolkit ;
2+
3+ import de .fraunhofer .iem .swan .cli .SwanOptions ;
4+ import de .fraunhofer .iem .swan .features .WekaFeatureSet ;
5+ import de .fraunhofer .iem .swan .model .ModelEvaluator ;
6+ import de .fraunhofer .iem .swan .util .Util ;
7+ import org .slf4j .Logger ;
8+ import org .slf4j .LoggerFactory ;
9+ import weka .classifiers .Evaluation ;
10+ import weka .classifiers .meta .AutoWEKAClassifier ;
11+ import weka .core .Instances ;
12+
13+ import java .util .HashMap ;
14+ import java .util .LinkedHashMap ;
15+
16+
17+ public class AutoWeka {
18+
19+ private WekaFeatureSet features ;
20+ private SwanOptions options ;
21+ private static final Logger logger = LoggerFactory .getLogger (AutoWeka .class );
22+
23+ public AutoWeka (WekaFeatureSet features , SwanOptions options ) {
24+ this .features = features ;
25+ this .options = options ;
26+ }
27+
28+ /**
29+ * Trains and evaluates the model with the given training data and specified classification mode.
30+ *
31+ * @return Hashmap containing the name of the classifier and it's F-Measure
32+ */
33+ public HashMap <String , HashMap <String , String >> trainModel () {
34+
35+ switch (ModelEvaluator .Phase .valueOf (options .getPhase ().toUpperCase ())) {
36+ case VALIDATE :
37+
38+ //Phase 1: classify SRM classes
39+ for (String srm : options .getSrmClasses ())
40+ runManualEvaluation (features .getTrainInstances ().get (srm ));
41+
42+ //Filter methods from CWE instances that were not classified into one of the SRM classes
43+ //Phase 2: classify CWE classes
44+ for (String cwe : options .getCweClasses ())
45+ runManualEvaluation (features .getTrainInstances ().get (cwe ));
46+
47+ return null ;
48+ case PREDICT :
49+
50+ }
51+ return null ;
52+ }
53+
54+ /**
55+ * @return
56+ */
57+ public HashMap <String , HashMap <String , String >> runManualEvaluation (Instances instances ) {
58+
59+ String category = instances .attribute (instances .numAttributes () - 1 ).name ();
60+ instances .setClass (instances .attribute (instances .numAttributes () - 1 ));
61+ String instancesFile = Util .exportInstancesToArff (instances , category + "-autoweka" );
62+
63+ LinkedHashMap <String , HashMap <String , String >> fMeasure = new LinkedHashMap <>();
64+ logger .info ("Selecting model for {} using Auto-Weka: timelimit= {}, instances={}" ,
65+ category , options .getTimeLimit (), instancesFile );
66+
67+ try {
68+
69+ String [] args = {"-t" , instancesFile ,
70+ "-seed" , "1" ,
71+ "-no-cv" ,
72+ "-timeLimit" , Integer .toString (options .getTimeLimit ())};
73+
74+ String out = Evaluation .evaluateModel (new AutoWEKAClassifier (), args );
75+ logger .info ("Auto-Weka Results: {]}" ,out );
76+
77+ //autoWekaClassifier.buildClassifier(instances);
78+ } catch (Exception e ) {
79+ throw new RuntimeException (e );
80+ }
81+ return fMeasure ;
82+ }
83+ }
0 commit comments