我尝试从我的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>
有什么问题?
答案 0 :(得分:0)
虽然你在其间放了一些文字,但是缩进表明你的for
循环是嵌套的,所以你正在做:
for (int t = 0; t <=count_table-1 ; t++){
my_class = my_model.createClass(ns+table_list.get(t));
…
for (int j = 0; j <= count_column - 1; j++) {
dtp = my_model.createDatatypeProperty(ns+ column_list.get(j));
dtp.addDomain(my_model.createClass(ns+table_list.get(t)));
}
…
}
如果第二个SQL查询返回所有数据类型属性,则此代码基本上是:“对于每个类,遍历数据类型属性并将类添加为数据类型属性的域。”您确定第二个查询正在执行您想要的操作吗?
暂且不谈:为什么
dtp.addDomain(my_model.createClass(...));
当你可以这样做时:
dtp.addDomain(my_class);
这不是定义my_class
的目的吗?