Coding new File Test Rules and new File Import Rules - Georgetown-University-Libraries/File-Analyzer GitHub Wiki

See the File Test Rule and File Import Rule pages for specific information about each type of rule.

Stats Object

As a rule is executed, the results are saved in a Stats object.

Every Stats object has a key.

A Stats object may have one or more fixed values. The fixed values are defined in a Java Enum (StatsItemEnum). Fixed values can easily be modified and reordered by rearranging the enum ordering. If the definition of a Stats object will not be shared by multiple rules, the StatsItemEnum will be defined with the class.

A Stats object might also contain variable values that are defined at runtime. Variable values are accessed by a key/value pair rather than by an Enum value.

The StatsItem class contains several helper methods that can be used to construct a StatsItemEnum. These helper methods define the data type, header text, width, and filterable properties for each fixed value.

The File Analyzer has the ability to validate report data in a CSV file according to the Counter Compliance standard. In this rule, a series of texts will be applied against each cell of a CSV file. Here is the StatsItemEnum that defines the report results.

public static enum CounterStatsItems implements StatsItemEnum {
    File_Cell(StatsItem.makeStringStatsItem("File [cell row, cell col]", 100)),
    Filename(StatsItem.makeStringStatsItem("File", 150).makeFilter(true)),
    Cell(StatsItem.makeStringStatsItem("Cell", 50)),
    Rec(StatsItem.makeEnumStatsItem(CounterRec.class, "Record").setWidth(60)),
    Stat(StatsItem.makeEnumStatsItem(CounterStat.class, "Compliance").setWidth(170)),
    Fixable(StatsItem.makeEnumStatsItem(FIXABLE.class, "Fixable").setWidth(40)),
    Report(StatsItem.makeStringStatsItem("Counter Report", 150).makeFilter(true)),
    Version(StatsItem.makeEnumStatsItem(REV.class, "Rev").setWidth(40)),
    Message(StatsItem.makeStringStatsItem("Message", 400)),
    CellValue(StatsItem.makeStringStatsItem("Cell Value", 250)),
    Replacement(StatsItem.makeStringStatsItem("Replacement", 250)),
    ;
    StatsItem si;
    CounterStatsItems(StatsItem si) {this.si=si;}
    public StatsItem si() {return si;}
}

A StatsItemConfig class is created for the StatsItemEnum.

public static StatsItemConfig details = StatsItemConfig.create(CounterStatsItems.class);

A Generator class is defined to create Stats objects that conform to the StatsItemEnum. The generator class is a singleton class with only one instance named INSTANCE.

public static enum Generator implements StatsGenerator {
    INSTANCE;
    class CounterStats extends Stats {
        public CounterStats(String key) {
            super(details, key);
            this.setVal(CounterStatsItems.Rec, CounterRec.FILE);
        }
        public CounterStats(File f, String cellname) {
            super(details, getKey(f, cellname));
            this.setVal(CounterStatsItems.Rec, CounterRec.CELL);
        }

    }

    public CounterStats create(String key) {return new CounterStats(key);}
    public CounterStats create(File f, String cellname) {return new CounterStats(f, cellname);}

}

When a Stats item is needed, the Generator is used to create it.

public Stats createStats(String key){ 
	return Generator.INSTANCE.create(key);
}

As Stats objects are produced, they are saved to a TreeMap named types. When the rule is complete, this TreeMap is returned in an an ActionResult class. This class indicated the action that was performed, the time it took to run the task, and the final results.

return new ActionResult(selectedFile, selectedFile.getName(), this.toString(), details, dt.types, true, timer.getDuration());