00001 import weka.classifiers.trees.J48;
00002
00003 import weka.core.Attribute;
00004 import weka.core.FastVector;
00005 import weka.core.Instance;
00006 import weka.core.Instances;
00007 import edu.tum.cs.srldb.Database;
00008 import edu.tum.cs.srldb.Object;
00009 import edu.tum.cs.srldb.datadict.DDAttribute;
00010 import edu.tum.cs.srldb.datadict.domain.Domain;
00011 import edu.tum.cs.srldb.datadict.DDException;
00012
00013 import java.io.IOException;
00014 import java.io.FileNotFoundException;
00015 import java.io.FileInputStream;
00016 import java.io.ObjectInputStream;
00017 import java.util.HashMap;
00018 import java.util.Map.Entry;
00019
00020 public class J48Reader {
00021
00022 public static void main(String[] args){
00023 try{
00024 String path = "./";
00025
00026 String dbdir = path + args[1];
00027 J48 j48 = readJ48(dbdir);
00028 Instances instances = readDB(args[0]);
00029
00030 for (int i = 0; i < instances.numInstances(); i++){
00031 Instance inst = instances.instance(i);
00032
00033
00034
00035
00036
00037
00038
00039
00040 double dist[] = j48.distributionForInstance(inst);
00041 int j = 0;
00042 for (double d : dist){
00043 Attribute att = instances.attribute(instances.classIndex());
00044 String classification = att.value(j);
00045 System.out.println(d + " "+ classification);
00046 j++;
00047 }
00048 }
00049 }
00050 catch (Exception e){
00051 e.printStackTrace();
00052 }
00053 }
00054
00055 public static J48 readJ48(String dbdir) throws IOException, ClassNotFoundException{
00056 String path = dbdir + "/pcc.j48";
00057
00058 ObjectInputStream objstream = new ObjectInputStream(new FileInputStream(path));
00059 J48 j48 = (J48) objstream.readObject();
00060 objstream.close();
00061 return j48;
00062 }
00063
00064 public static Instances readDB(String dbname) throws IOException, ClassNotFoundException, DDException, FileNotFoundException, Exception{
00065 Database db = Database.fromFile(new FileInputStream(dbname));
00066 edu.tum.cs.srldb.datadict.DataDictionary dd = db.getDataDictionary();
00067
00068 FastVector fvAttribs = new FastVector();
00069 HashMap<String,Attribute> mapAttrs = new HashMap<String,Attribute>();
00070 for(DDAttribute attribute : dd.getObject("object").getAttributes().values()){
00071 if(attribute.isDiscarded() && !attribute.getName().equals("objectT")){
00072 continue;
00073 }
00074 FastVector attValues = new FastVector();
00075 Domain dom = attribute.getDomain();
00076 for(String s : dom.getValues())
00077 attValues.addElement(s);
00078 Attribute attr = new Attribute(attribute.getName(), attValues);
00079 fvAttribs.addElement(attr);
00080 mapAttrs.put(attribute.getName(), attr);
00081 }
00082
00083 Instances instances = new Instances("name",fvAttribs,10000);
00084 instances.setClass(mapAttrs.get("objectT"));
00085
00086 for(Object o : db.getObjects()){
00087 if (o.hasAttribute("objectT")){
00088 Instance instance = new Instance(fvAttribs.size());
00089 for(Entry<String,String> e : o.getAttributes().entrySet()) {
00090 if (!dd.getAttribute(e.getKey()).isDiscarded()){
00091 instance.setValue(mapAttrs.get(e.getKey()), e.getValue());
00092 }
00093 }
00094 instances.add(instance);
00095 }
00096 }
00097 return instances;
00098 }
00099 }