|
| 1 | +package postprocessor |
| 2 | + |
| 3 | +import scala.util.control.Breaks._ |
| 4 | +import java.text.DecimalFormat |
| 5 | +import collection.JavaConverters.* // asScala |
| 6 | +import org.deidentifier.arx.{ARXResult, Data, DataHandle} |
| 7 | + |
| 8 | +/** |
| 9 | + * Utility class to print on the console ARXResult instances |
| 10 | + * Adapted from https://github.com/arx-deidentifier/arx/blob/master/src/example/org/deidentifier/arx/examples/Example.java |
| 11 | + */ |
| 12 | +object ResultPrinter { |
| 13 | + |
| 14 | + def printResult(result: ARXResult, data: Data): Unit = { // Print time |
| 15 | + |
| 16 | + |
| 17 | + val df1 = new DecimalFormat("#####0.00") |
| 18 | + val sTotal = df1.format(result.getTime / 1000d) + "s" |
| 19 | + System.out.println(" - Time needed: " + sTotal) |
| 20 | + // Extract |
| 21 | + val optimum = result.getGlobalOptimum |
| 22 | + val dataDef = data.getDefinition |
| 23 | + val attrs: Set[String] = dataDef.getQuasiIdentifyingAttributes.asScala.toSet[String] |
| 24 | + |
| 25 | + val qis = attrs.toArray[String] |
| 26 | + |
| 27 | + if (optimum == null) { |
| 28 | + System.out.println(" - No solution found!") |
| 29 | + return |
| 30 | + } |
| 31 | + // Initialize |
| 32 | + val identifiers = new Array[StringBuffer](qis.size) |
| 33 | + val generalizations = new Array[StringBuffer](qis.size) |
| 34 | + var lengthI = 0 |
| 35 | + var lengthG = 0 |
| 36 | + |
| 37 | + for (i <- 0 until qis.size) { |
| 38 | + identifiers(i) = new StringBuffer |
| 39 | + generalizations(i) = new StringBuffer |
| 40 | + identifiers(i).append(qis(i)) |
| 41 | + generalizations(i).append(optimum.getGeneralization(qis(i))) |
| 42 | + if (data.getDefinition.isHierarchyAvailable(qis(i))) generalizations(i).append("/").append(data.getDefinition.getHierarchy(qis(i))(0).length - 1) |
| 43 | + lengthI = Math.max(lengthI, identifiers(i).length) |
| 44 | + lengthG = Math.max(lengthG, generalizations(i).length) |
| 45 | + } |
| 46 | + |
| 47 | + // Padding |
| 48 | + for (i <- 0 until qis.size) { |
| 49 | + while ( { |
| 50 | + identifiers(i).length < lengthI |
| 51 | + }) identifiers(i).append(" ") |
| 52 | + while ( { |
| 53 | + generalizations(i).length < lengthG |
| 54 | + }) generalizations(i).insert(0, " ") |
| 55 | + } |
| 56 | + // Print |
| 57 | + System.out.println(" - Information loss: " + result.getGlobalOptimum.getLowestScore + " / " + result.getGlobalOptimum.getHighestScore) |
| 58 | + System.out.println(" - Optimal generalization") |
| 59 | + for (i <- 0 until qis.size) { |
| 60 | + System.out.println(" * " + identifiers(i) + ": " + generalizations(i)) |
| 61 | + } |
| 62 | + System.out.println(" - Statistics") |
| 63 | + System.out.println(result.getOutput(result.getGlobalOptimum, false).getStatistics.getEquivalenceClassStatistics) |
| 64 | + } |
| 65 | + |
| 66 | + def printHandle(handle: DataHandle): Unit = { |
| 67 | + |
| 68 | + val transformed = handle.iterator |
| 69 | + while (transformed.hasNext) { |
| 70 | + System.out.print(" ") |
| 71 | + val item = transformed.next |
| 72 | + System.out.println(item.mkString(" ")) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Print the n top items in the handle |
| 78 | + */ |
| 79 | + def printHandleTop(handle: DataHandle, n: Int): Unit = { |
| 80 | + |
| 81 | + val transformed = handle.iterator |
| 82 | + var counter = 0 |
| 83 | + |
| 84 | + breakable{ |
| 85 | + while (transformed.hasNext) { |
| 86 | + System.out.print(" ") |
| 87 | + val item = transformed.next |
| 88 | + System.out.println(item.mkString(" ")) |
| 89 | + counter += 1 |
| 90 | + |
| 91 | + if(counter >= n) break |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments