@@ -31,7 +31,7 @@ public static void main(String[] args) {
3131 }
3232 Main main = new Main ();
3333 main .run (args );
34- System .out .println ("Done." );
34+ // System.out.println("Done.");
3535 } catch (IOException e ) {
3636 e .printStackTrace ();
3737 }
@@ -52,88 +52,118 @@ public static void main(String[] args) {
5252 private static final boolean runAuthentications = true ;
5353 private static final boolean runCwes = true ;
5454
55+ private static final boolean runOAT = true ; // run one at a time analysis
56+
5557 private void run (String [] args ) throws IOException {
56- // Cache the list of classes and the CP.
57- System .out .println ("***** Loading CP" );
58- Set <String > testClasses = Util .getAllClassesFromDirectory (args [0 ]);
59- String testCp = Util .buildCP (args [0 ]);
60- String trainingCp = Util .buildCP (args [1 ]);
61- outputPath = args [3 ];
62- System .out
63- .println ("Training set cp: " + trainingCp + "\n Test set cp: " + testCp );
64-
65- // Cache the features.
66- System .out .println ("***** Loading features" );
67- featureHandler = new FeatureHandler (
68- trainingCp + System .getProperty ("path.separator" ) + testCp );
69- featureHandler .initializeFeatures ();
70-
71- // Cache the methods from the training set.
72- System .out .println ("***** Loading train data" );
73- parser = new Parser (trainingCp );
74- parser .loadTrainingSet (Collections .singleton (args [2 ]));
75-
76- // Cache the methods from the testing set.
77- System .out .println ("***** Loading test data" );
78- loader = new Loader (testCp );
79- loader .loadTestSet (testClasses , parser .methods ());
80-
81- // Prepare classifier.
82- System .out .println ("***** Preparing classifier" );
83- writer = new Writer (loader .methods ());
84- learner = new Learner (writer );
85-
86- // Classify.
87- if (runSources )
88- runClassifier (
89- new HashSet <Category >(Arrays .asList (Category .SOURCE , Category .NONE )),
90- false );
91- if (runSinks )
92- runClassifier (
93- new HashSet <Category >(Arrays .asList (Category .SINK , Category .NONE )),
94- false );
95- if (runSanitizers )
96- runClassifier (
97- new HashSet <Category >(Arrays .asList (Category .SANITIZER , Category .NONE )),
98- false );
99- if (runAuthentications )
100- runClassifier (new HashSet <Category >(Arrays .asList (
101- Category .AUTHENTICATION_TO_HIGH , Category .AUTHENTICATION_TO_LOW ,
102- Category .AUTHENTICATION_NEUTRAL , Category .NONE )), false );
103-
104- // Save data from last classification.
105- loader .resetMethods ();
106-
107- // Cache the methods from the second test set.
108- System .out .println ("***** Loading 2nd test set" );
109- loader .pruneNone ();
110-
111- if (runCwes ) {
112- //Run classifications for all cwes in JSON file.
113- for (String cweId : parser .cwe ()) {
114- runClassifier (
115- new HashSet <Category >(Arrays
116- .asList (Category .getCategoryForCWE (cweId ), Category .NONE )),
117- true );
118- }
119- }
120-
121- System .out .println ("***** Writing final results" );
122- Set <String > tmpFiles = Util .getFiles (args [3 ]);
123- writer .printResultsTXT (loader .methods (), tmpFiles , args [3 ] + File .separator + "txt" + File .separator + "output.txt" );
124- writer .writeResultsQWEL (loader .methods (), args [3 ] + File .separator + "qwel" + File .separator + "output.qwel" );
125- writer .writeResultsSoot (loader .methods (), args [3 ] + File .separator + "soot-qwel" + File .separator + "output.sqwel" );
126- writer .printResultsJSON (loader .methods (), tmpFiles , args [3 ] + File .separator + "json" + File .separator + "output.json" );
58+ int iterations = 0 ;
59+ if (runOAT )
60+ iterations = 206 ; // number of features //TODO: improve code: better borders here.
61+
62+ // for OAT analysis. Each feature is disabled once.
63+ for (int i = 0 ; i <=iterations ; i ++)
64+ {
65+ if (i == 0 )
66+ System .out .println ("***** Running with all features." );
67+ else {
68+ System .out .println ("***** Running without " + i + "th feature" );
69+ }
70+ // Cache the list of classes and the CP.
71+ //System.out.println("***** Loading CP");
72+ Set <String > testClasses = Util .getAllClassesFromDirectory (args [0 ]);
73+ String testCp = Util .buildCP (args [0 ]);
74+ String trainingCp = Util .buildCP (args [1 ]);
75+ outputPath = args [3 ];
76+ //System.out.println("Training set cp: " + trainingCp + "\nTest set cp: " + testCp);
77+
78+
79+ // Cache the features.
80+ //System.out.println("***** Loading features");
81+ featureHandler = new FeatureHandler (
82+ trainingCp + System .getProperty ("path.separator" ) + testCp );
83+ featureHandler .initializeFeatures (i ); // use 0 for all feature instances
84+
85+ // Cache the methods from the training set.
86+ //System.out.println("***** Loading train data");
87+ parser = new Parser (trainingCp );
88+ parser .loadTrainingSet (Collections .singleton (args [2 ]));
89+
90+ // Cache the methods from the testing set.
91+ //System.out.println("***** Loading test data");
92+ loader = new Loader (testCp );
93+ loader .loadTestSet (testClasses , parser .methods ());
94+
95+ // Prepare classifier.
96+ //System.out.println("***** Preparing classifier");
97+ writer = new Writer (loader .methods ());
98+ learner = new Learner (writer );
99+
100+ double averageF =0 ;
101+ int iter =0 ;
102+ // Classify.
103+ if (runSources ){
104+ averageF += runClassifier (
105+ new HashSet <Category >(Arrays .asList (Category .SOURCE , Category .NONE )),
106+ false );
107+ iter ++;
108+ }
109+ if (runSinks ) {
110+ averageF += runClassifier (
111+ new HashSet <Category >(Arrays .asList (Category .SINK , Category .NONE )),
112+ false );
113+ iter ++;
114+ }
115+
116+ if (runSanitizers ) {
117+ averageF += runClassifier (
118+ new HashSet <Category >(Arrays .asList (Category .SANITIZER , Category .NONE )),
119+ false );
120+ iter ++;
121+ }
122+
123+ if (runAuthentications )
124+ {
125+ averageF +=runClassifier (new HashSet <Category >(Arrays .asList (
126+ Category .AUTHENTICATION_TO_HIGH , Category .AUTHENTICATION_TO_LOW ,
127+ Category .AUTHENTICATION_NEUTRAL , Category .NONE )), false );
128+ iter ++;
129+ }
130+ // Save data from last classification.
131+ loader .resetMethods ();
132+
133+ // Cache the methods from the second test set.
134+ //System.out.println("***** Loading 2nd test set");
135+ loader .pruneNone ();
136+
137+ if (runCwes ) {
138+ //Run classifications for all cwes in JSON file.
139+ for (String cweId : parser .cwe ()) {
140+ averageF += runClassifier (
141+ new HashSet <Category >(Arrays
142+ .asList (Category .getCategoryForCWE (cweId ), Category .NONE )),
143+ true );
144+ iter ++;
145+ }
146+ }
147+ System .out .println ("***** F Measure is " + averageF /iter );
148+
149+ //System.out.println("***** Writing final results");
150+ Set <String > tmpFiles = Util .getFiles (args [3 ]);
151+ writer .printResultsTXT (loader .methods (), tmpFiles , args [3 ] + File .separator + "txt" + File .separator + "output.txt" );
152+ writer .writeResultsQWEL (loader .methods (), args [3 ] + File .separator + "qwel" + File .separator + "output.qwel" );
153+ writer .writeResultsSoot (loader .methods (), args [3 ] + File .separator + "soot-qwel" + File .separator + "output.sqwel" );
154+ writer .printResultsJSON (loader .methods (), tmpFiles , args [3 ] + File .separator + "json" + File .separator + "output.json" );
155+ }
156+
127157 }
128158
129- private void runClassifier (HashSet <Category > categories , boolean cweMode )
159+ private double runClassifier (HashSet <Category > categories , boolean cweMode )
130160 throws IOException {
131161 parser .resetMethods ();
132162 loader .resetMethods ();
133- System .out
134- .println ("***** Starting classification for " + categories .toString ());
135- learner .classify (parser .methods (), loader .methods (),
163+ //System.out.println("***** Starting classification for " + categories.toString());
164+ return learner .classify (parser .methods (), loader .methods (),
136165 featureHandler .features (), categories , outputPath + File .separator + "txt" + File .separator +"output.txt" , cweMode );
137166 }
138167
139- }
168+
169+ }
0 commit comments