我尝试从我的 SQL 服务器数据库构建本体。表被转换为类,它们的列被转换为类的数据类型属性。我得到了桌子并以这种方式上课:
/**************** create connection***************/
String dbURL = "jdbc:sqlserver://localhost:1433;databaseName=test;";
String user = "m";
String pass = "m";
conn = DriverManager.getConnection(dbURL, user, pass);
stmt = conn.createStatement();
String sql;
/*************create ontology model**********************/
OntModel my_model = ModelFactory.createOntologyModel();
/*************************get table name********************************/
sql="select * FROM information_schema.tables WHERE table_type = 'BASE TABLE'";
ResultSet rs = stmt.executeQuery(sql);
int count_table=0;
while(rs.next()){
table_list.add( rs.getString(3));
count_table+=1;
}
rs.close();
/*************************Get number of columns of primary key ******************************/
for (int t = 0; t <=count_table-1 ; t++){
sql="exec sp_pkeys '" + table_list.get(t) + "', 'dbo'";
ResultSet rs1 = stmt.executeQuery(sql);
pry_number=0;
while(rs1.next()){
pry_number =rs1.getInt(5);
}
rs1.close();
if (pry_number==1){
my_class=null;
/**********************create ontology class**********************/
my_class = my_model.createClass(ns+table_list.get(t));
/********************* column name ***************************/
sql="SELECT COLUMN_NAME,i.TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS,information_schema.TABLE_CONSTRAINTS i WHERE i.TABLE_NAME = '" + table_list.get(t) + "' AND i.CONSTRAINT_TYPE != 'FOREIGN KEY'";
ResultSet rs3 = stmt.executeQuery(sql);
int count_column=0;
while(rs3.next()){
column_list.add( rs3.getString(1));
Datatype_domain=rs3.getString(2);
count_column+=1;
}
rs3.close();
/******************create ontology DatatypeProperty************/
for (int j = 0; j <= count_column - 1; j++)
{
dtp = my_model.createDatatypeProperty(ns+ column_list.get(j));
dtp.addDomain(my_model.createClass(ns+Datatype_domain));
}
/*************************get foreignkey******************************/
sql="SELECT object_name(parent_object_id),object_name(referenced_object_id), name FROM sys.foreign_keys WHERE parent_object_id = object_id('" + table_list.get(t) + "')";
ResultSet rs2 = stmt.executeQuery(sql);
while(rs2.next()){
parent=rs2.getNString(1);
reference=rs2.getNString(2);
foreign_key= rs2.getString(3);
}
rs2.close();
/****************create ontology class ObjecProperty********************/
op = my_model.createObjectProperty(ns + foreign_key);
op.addDomain(my_model.createClass(ns+parent));
op.addRange(my_model.createClass(ns+reference));
/**********************Check if primarynkey is foreign key***************************/
sql="SELECT Tab.TABLE_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col WHERE Col.Constraint_Name = Tab.Constraint_Name AND Col.Table_Name = Tab.Table_Name AND Constraint_Type = 'PRIMARY KEY ' INTERSECT SELECT Tab.TABLE_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col WHERE Col.Constraint_Name = Tab.Constraint_Name AND Col.Table_Name = Tab.Table_Name AND Constraint_Type = 'FOREIGN KEY ' and Tab.TABLE_NAME = '" + table_list.get(t) + "' ";
ResultSet rs4 = stmt.executeQuery(sql);
while(rs4.next()){
sub_class= rs4.getString(1);
}
rs4.close();
sql="SELECT object_name(referenced_object_id) FROM sys.foreign_keys WHERE parent_object_id = object_id('" + sub_class + "')";
ResultSet rs6 = stmt.executeQuery(sql);
while(rs6.next()){
super_class= rs6.getString(1);
}
rs6.close();
/****************create ontology Sub_class ********************/
my_model.createClass(ns+ sub_class).addSuperClass(my_model.createClass(super_class));
}
if (pry_number>1){
/****************create ontology ObjecProperty********************/
sql="SELECT object_name(parent_object_id),object_name(referenced_object_id), name FROM sys.foreign_keys WHERE parent_object_id = object_id('" + table_list.get(t) + "')";
ResultSet rs5 = stmt.executeQuery(sql);
while(rs5.next()){
Obj_domain.add(rs5.getString(2));
}
rs5.close();
op = my_model.createObjectProperty(ns + table_list.get(t));
op.addDomain(my_model.createClass(ns+Obj_domain.get(1)));
op.addRange(my_model.createClass(ns+Obj_domain.get(0)));
op2 = my_model.createObjectProperty(ns + table_list.get(t));
op2.addDomain(my_model.createClass(ns+Obj_domain.get(0)));
op2.addRange(my_model.createClass(ns+Obj_domain.get(1)));
}
/***********************END_FOR**************************/
}
/*******************create owl file****************************/
FileOutputStream university_File = new FileOutputStream("d:/uni.owl");
my_model.write(university_File,"RDF/XML-ABBREV",ns);
/***************** close connection****************/
stmt.close();
conn.close();
它使类和数据类型属性正确:
<owl:Class rdf:ID="phdstudent"/>
<owl:Class rdf:ID="student"/>
<owl:Class rdf:ID="employee"/>
<owl:Class rdf:ID="department"/>
<owl:Class rdf:ID="town"/>
但是所有类都将是每个数据类型属性的域,例如:
<owl:DatatypeProperty rdf:ID="pcode">
<rdfs:domain rdf:resource="#employee"/>
<rdfs:domain rdf:resource="#department"/>
<rdfs:domain rdf:resource="#phdstudent"/>
<rdfs:domain rdf:resource="#town"/>
<rdfs:domain rdf:resource="#student"/>
</owl:DatatypeProperty>
怎么了?