-- 作者:lavender_22
-- 发布时间:9/5/2011 4:58:00 PM
--
一直有事没上来公布答案的,抱歉啦,各位,谢谢楼上童鞋的帮助,感谢感谢,特将代码全奉上,有需要的盆友可以看看下回去调试 public class Tree extends JFrame{ public final String strDriver = "com.mysql.jdbc.Driver"; public final String strURL = "jdbc:mysql://localhost/course"; public final String strUser = "root"; public final String strPassword =""; public final String strDB = "MySQL"; public static IDBConnection connectDB(String DBURL, String DBUSER, String DBPASSWORD, String DBTYPE){ try { Class.forName("com.mysql.jdbc.Driver"); System.out.print("数据库连接成功, 已经连接到"); }catch (ClassNotFoundException e){ e.printStackTrace(); System.out.println("数据库连接不成功"); } return new DBConnection(DBURL, DBUSER,DBPASSWORD,DBTYPE); } public static OntModelSpec getModelSpec(ModelMaker maker) { OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM); spec.setImportModelGetter(maker); return spec; } public static OntModel ceateDBModelFromFile(IDBConnection con, String name, String filePath){ ModelMaker maker = ModelFactory.createModelRDBMaker(con); Model base = maker.createModel(name); OntModel newmodel = ModelFactory.createOntologyModel(getModelSpec(maker),base); newmodel.read(filePath); System.out.println("已将本体文件存入数据库。"); System.out.println(); return newmodel; } public static OntModel getModelFromDB(IDBConnection con, String name){ ModelMaker maker = ModelFactory.createModelRDBMaker(con); Model base = maker.getModel(name); OntModel newmodel = ModelFactory.createOntologyModel(getModelSpec(maker),base); System.out.println("已从数据库中读出本体文件。"); System.out.println(); return newmodel; } public static ArrayList<OntResource> getRecord(OntClass targetClass, ArrayList<OntResource> record) { if (! targetClass.isAnon()) { record.add(targetClass); OntClass tempClass = targetClass; while (tempClass.hasSuperClass()) { Iterator<OntClass> superClassIterator = tempClass.listSuperClasses(true); OntClass superClass = null; while ( superClassIterator.hasNext()) { superClass = superClassIterator.next(); if ( !superClass.isAnon()) { break; } } if ( superClass != null && !superClass.isAnon()) { if ( tempClass.getURI() == superClass.getURI()) break; record.add(superClass); tempClass = superClass; } else break; } } return record; } public static void addRecordToTree(DefaultMutableTreeNode sourceRoot, ArrayList<OntResource> record) { DefaultMutableTreeNode lastNode = sourceRoot; DefaultMutableTreeNode insertingNode=null; for ( int i = 0; i < record.size(); ++i) { boolean alreadyExisted = false; for ( int j = 0; j< lastNode.getChildCount(); ++ j) { if ( ((OntResource)((DefaultMutableTreeNode)lastNode.getChildAt(j)).getUserObject()).getURI() == record.get(record.size()-i-1).getURI()) { lastNode = (DefaultMutableTreeNode) lastNode.getChildAt(j); alreadyExisted = true; break; } } if( alreadyExisted == false) { insertingNode = new DefaultMutableTreeNode(); insertingNode.setUserObject(record.get(record.size()-i-1)); lastNode.add(insertingNode); lastNode = insertingNode; } } } @SuppressWarnings("unchecked") public void treeinit(){ JTree jTree=new JTree(); GridLayout lay=new GridLayout(1,1); DefaultMutableTreeNode root=new DefaultMutableTreeNode(""); DefaultMutableTreeNode father=new DefaultMutableTreeNode("root"); root.add(father); TreeModel treeModel = new DefaultTreeModel(root); jTree.setModel(treeModel); this.setSize(400,400); this.setLayout(lay); this.add(jTree); this.setVisible(true); jTree.setCellRenderer(new IndividualTreeCellRenderer()); IDBConnection con = connectDB(strURL, strUser, strPassword, strDB); System.out.println(con + ";"); System.out.println(); String filePath = ""; String prefix = " "; OntModel defModel = ceateDBModelFromFile(con, "Course1", filePath); OntModel model = getModelFromDB(con, "Course1"); ArrayList<OntResource> record = new ArrayList<OntResource>(); Iterator<OntClass> classIterator = model.listClasses(); while ( classIterator.hasNext()) { OntClass currentClass = classIterator.next(); getRecord(currentClass, record); addRecordToTree((DefaultMutableTreeNode)root.getChildAt(0),record); record.clear(); } } public static void main(String[] args) { Tree tree =new Tree(); tree.treeinit(); }}
|