View Javadoc

1   /*
2    * @(#)$Id: BatchTools.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose      : 	Some helper methods.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Dec 26, 2006     ukurmann    Initial Release
15   * Mar 22, 2007     bsigner     Cleanup
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.batch;
28  
29  import java.io.File;
30  import java.util.HashSet;
31  
32  import org.apache.commons.io.FileUtils;
33  import org.ximtec.igesture.util.XMLTool;
34  
35  
36  /**
37   * Some helper methods.
38   * 
39   * @version 1.0 Dec 2006
40   * @author Ueli Kurmann, igesture@uelikurmann.ch
41   * @author Beat Signer, signer@inf.ethz.ch
42   */
43  public class BatchTools {
44  
45     private static final String XSL_HTML = "xml/batch.xsl";
46     private static final String OUT_FILE_HTML = "result.html";
47     private static final String OUT_FILE_XML = "result.xml";
48  
49  
50     /**
51      * Filters a set of sets. The condition are the number of elements in the set.
52      * 
53      * @param powerSet the set to be filtered.
54      * @param min the minimal number of elements in the set.
55      * @param max the maximal number of elements in the set.
56      * @return the filtered set.
57      */
58     public static HashSet<HashSet<String>> filterSet(
59           HashSet<HashSet<String>> powerSet, int min, int max) {
60        final HashSet<HashSet<String>> result = new HashSet<HashSet<String>>();
61  
62        for (final HashSet<String> set : powerSet) {
63  
64           if (set.size() >= min && set.size() <= max) {
65              result.add(set);
66           }
67  
68        }
69        return result;
70     } // filterSet
71  
72  
73     /**
74      * Creates the power set of a given set.
75      * 
76      * @param set the set a power set has to be created from.
77      * @return the power set.
78      */
79     public static HashSet<HashSet<String>> getPowerSet(HashSet<String> set) {
80        final HashSet<HashSet<String>> powerSet = new HashSet<HashSet<String>>();
81        final HashSet<String> currSet = new HashSet<String>();
82        return createPowerSet(powerSet, currSet, 0, set);
83     } // getPowerSet
84  
85  
86     /**
87      * Creates the power set (recursively).
88      */
89     @SuppressWarnings("unchecked")
90     private static HashSet<HashSet<String>> createPowerSet(
91           HashSet<HashSet<String>> powerSet, HashSet<String> currentSet,
92           int index, HashSet<String> set) {
93  
94        if (index == set.size()) {
95           final HashSet<HashSet<String>> ps = (HashSet<HashSet<String>>)powerSet
96                 .clone();
97           ps.add(currentSet);
98           return ps;
99        }
100       else {
101          powerSet = createPowerSet(powerSet, currentSet, index + 1, set);
102          currentSet = (HashSet<String>)currentSet.clone();
103          currentSet.add((String)set.toArray()[index]);
104          powerSet = createPowerSet(powerSet, currentSet, index + 1, set);
105          return powerSet;
106       }
107 
108    } // createPowerSet
109 
110 
111    public static void writeResultsOnDisk(File outputDirectory,
112          BatchResultSet resultSet) {
113 
114       try {
115 
116          XMLTool.exportBatchResultSet(resultSet, new File(outputDirectory,
117                OUT_FILE_XML));
118 
119          String html = XMLTool.transform(
120                XMLTool.exportBatchResultSet(resultSet), BatchTools.class
121                      .getClassLoader().getResourceAsStream(XSL_HTML));
122          FileUtils.writeStringToFile(new File(outputDirectory, OUT_FILE_HTML),
123                html);
124 
125       }
126       catch (Exception e) {
127          e.printStackTrace();
128       }
129    }
130 
131 }