Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

98
LINES

< > BotCompany Repo | #285 - GetDiskStats2.java

Java source code

package ai.d.ai18;

import drjava.util.Log;
import drjava.util.StringUtil;
import net.luaos.tb.tb15.CentralDatabase;
import net.luaos.tb.tb20.Database;
import net.luaos.tb.tb20.DatabaseAPI;
import net.luaos.tb.tb20.FloraUtil;
import net.luaos.tb.tb20.ListEntry;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetDiskStats2 {
  public static void main(String[] args) {
    DatabaseAPI db = CentralDatabase.copyToMemory("df", "GetDiskStats2");
    String dfOutputID1 = "#t1-kokjwjhpwuas-qbtsjoyahagl";
    ListEntry dfOutput1 = db.get(dfOutputID1);
    ListEntry rowMarking = db.oneOfType_flex("RowMarking");
    List<ListEntry> colMarkings = db.allOfType("ColMarking");

    DatabaseAPI db2 = CentralDatabase.copyToMemory("df2", "GetDiskStats2");
    ListEntry dfOutput2 = db2.oneOfType_flex("DFOutput");

  /*
(adapts to differing widths in a table. assumes spaces are used as field
separator. looks only at one row.)

function adaptColMarkings(text1, text2, rowMarking, colMarkings)
  row1 = text1.getRow(rowMarking)
  row2 = text2.getRow(rowMarking)
  cols1 = findColumns(row1)
  cols2 = findColumns(row2)
  newColMarkings = []
  for colMarking in colMarkings:
    idx = cols1.indexOf(colMarking)
    col2 = cols2.get(idx)
    add col2 to newColMarkings with label of colMarking
  end
  return newColMarkings
end   */

    String row1 = StringUtil.toLines(dfOutput1.desc).get(rowMarking.getInt("textRow")-1);
    String row2 = StringUtil.toLines(dfOutput2.desc).get(rowMarking.getInt("textRow")-1);
    List<String> cols1 = findColumns(row1);
    List<String> cols2 = findColumns(row2);
    Database dbOut = new Database();
    for (ListEntry colMarking : colMarkings) {
      int idx = findColumn(cols1, colMarking.getPointer("textCol"));
      if (idx < 0)
        Log.surprise("Column not found: " + colMarking.desc);
      else
        dbOut.emit("ColMarking", colMarking.desc, "onText", dfOutput2.id, "textCol", cols2.get(idx));
    }

    FloraUtil.copyNoRewriting(db2, dbOut);
    System.out.println("\nResult DB:");
    FloraUtil.copyNoRewriting(db, dbOut, rowMarking.id);
    FloraUtil.printDatabase(dbOut);

    GetDiskStats.doIt(dbOut, dfOutput2.id);
  }

  private static int findColumn(List<String> cols, String textCol) {
    System.out.println("findColumn " + cols + " " + textCol);
    String start = textCol.split("\\-")[0];
    for (int i = 0; i < cols.size(); i++) {
      String[] fromTo = cols.get(i).split("\\-");
      if (rangeContains(fromTo, start))
        return i;
    }
    return -1;
  }

  private static boolean rangeContains(String[] fromTo, String start) {
    if (Integer.parseInt(start) < Integer.parseInt(fromTo[0])) return false;
    if (fromTo[1].equals("end"))
      return true;
    else if (fromTo[1].endsWith(" (inc)"))
      return Integer.parseInt(start) <= Integer.parseInt(fromTo[1].substring(0, fromTo[1].length() - " (inc)".length()));
    else
      throw new RuntimeException("Unparsable range end: " + fromTo[1]);
  }

  private static List<String> findColumns(String row) {
    int i = 0;
    System.out.println("findColumns: " + row);
    Matcher matcher = Pattern.compile("[^ ]+").matcher(row);
    List<String> list = new ArrayList<String>();
    while (matcher.find()) {
      int start = matcher.start(), end = matcher.end();
      list.add((start+1) + "-" + end + " (inc)");
    }
    return list;
  }
}

download  show line numbers   

Travelled to 8 computer(s): aoiabmzegqzx, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 3547 [visualize]

Snippet ID: #285
Snippet name: GetDiskStats2.java
Eternal ID of this version: #285/1
Text MD5: 4677274266a67145f852012cd7d4cf63
Author: stefan
Category:
Type: Java source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2014-11-08 22:25:28
Source code size: 3547 bytes / 98 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 545 / 119
Referenced in: [show references]

Formerly at http://tinybrain.de/285 & http://285.tinybrain.de