Implementation of ontology access operation based on MySQL database in Jena framework
Transferred from: http://blog.csdn.net/jtz_mpp/article/details/6224311
Recently, we are doing an ontology-based management system. One of the operating parts of the ontology, I use the Jena framework; The data model is a semantic model based on ontology; data storage is MySQL version 5.5.9. From this perspective, it is common practice to store the ontology model in the database and take the model out of the database, and here is a summary of the methods I have learned.
The development environment I am using is Eclipse3.6, and it is not detailed here to add the necessary Jena-related class packages to the Java build path before development. For an ontology, you first need to have a model corresponding to it, so create a model first. In Jena, you can create a model based on a file store, or a model based on a relational database store, which is used here. The code is as follows:
1. Modelmaker maker = Modelfactory.createmodelrdbmaker (conn);
2. Model Tempmodel = null;
Tempmodel = Maker.createmodel ("Testdbmodel");
3. Ontmodelspec spec = new Ontmodelspec (ONTMODELSPEC.OWL_MEM);
Dbmodel = Modelfactory.createontologymodel (spec, Tempmodel);
As you can see, the creation process is divided into three steps: first creating a Modelmaker object, which is responsible for creating the model-specific operations, and also linking the model to the DB. The parameter conn of the Modelfactory.createmodelrdbmaker method is a database connection object. Next, create a temporary, default model (RDF format) using the object you just created, where you can specify the name of the model. Finally, the staging model is converted to the cost body model (owl format), where the spec parameter indicates that the model exists in memory. The model is finished creating.
Here's the database connection. You need to specify the JDBC Driver when using JDBC to connect to the database, which is MySQL connector/j for MySQL data, here is a download URL: http://www.mysql.com/downloads/connector /j/, I'm using the 5.1.15 version. How it's used I synthesized some of the information summarized as follows:
Unpack the package to a directory, such as D:/temp, and make sure the jar file with the name Mysql-connector-java-5.1.15-bin is in this directory. Then open the Environment Variables editor (Windows system) and add the CLASSPATH variable to a newly unzipped directory, which is d:/temp. I started off with a line that was added, and later in Eclipse, I couldn't find the JDBC driver error. It has been investigated that if you use the command line to edit and compile your code, it is OK to set classpath as you just did, but if you use the IDE, such as Eclipse, you must add the corresponding CLASSPATH variable in the IDE again. Here's how:
On the Eclipse menu, open window->preferences->java->build path->classpath Variables, click the New button on the right, set the variable name, Then specify the directory that the variable points to, and click OK to either. Finally, add the variables you just created in the Java Build path option of the project properties.
With the connection driver, the database connection code is as follows:
1.IDBConnection conn = null;
2.class.forname ("Com.mysql.jdbc.Driver"). newinstance ();
3.String Db_url = "Jdbc:mysql://localhost:3306/testdb";
4.String db_user = "root";
5.String Db_pass = "MVP";
6.String Db_type = "MySQL";
7.conn = new DBConnection (Db_url, Db_user, Db_pass, Db_type);
The 2nd line is to specify the JDBC connector, and the application needs to call the Class.forName method to register or load the driver, com.mysql.jdbc.Driver the driver class name. This statement is best handled with exception because the Java.lang.ClassNotFoundException exception is thrown when the driver is not found. In addition, there will be a matching official document when downloading connector, with detailed examples and explanations.
Let's talk about the model reading and saving. Look directly at the code:
Tempmodel = Maker.openmodel ("Testdbmodel", true);
This sentence takes the model named "Testdbmodel" from the database and saves it to Tempmodel, and the parameter true indicates that if the specified model does not exist, the new model is not created and the doesnotexistexception exception is thrown, Jena API See also: http://jena.sourceforge.net/javadoc/index.html.
After a series of operations are performed using the in-memory model, it executes:
Dbmodel.commit ();
It saves the model to the database.
Finally, in the first load of the model, the database is empty, you can read from the file ontology to the model, the code is as follows:
InputStream intest = Filemanager.get (). open (ont1);//ont1 is the file path
Dbmodel.read (Intest, testnamespace);//testnamespace is the namespace specified in the ontology
Intest.close ();
The first time the model is stored in the database, the following tables are generated:
Jena_g1t0_reif
Jena_g1t1_stmt
Jena_graph
Jena_long_lit
Jena_long_uri
Jena_prefix
Jena_sys_stmt
The role of each table can be found in: http://blog.csdn.net/longfei8812/archive/2009/12/25/5078366.aspx, you can also use the MySQL graphical tool to study their own, here is not detailed.
Finally, attach the complete code I wrote, please give me more advice!
1 [Java] View plain copy2 PackageTestDB; 3 4 Importjava.io.IOException; 5 ImportJava.io.InputStream; 6 Importjava.sql.SQLException; 7 //import Java.sql.DriverManager; 8 9 Importcom.hp.hpl.jena.db.DBConnection; Ten Importcom.hp.hpl.jena.db.IDBConnection; One ImportCom.hp.hpl.jena.ontology.OntModel; A ImportCom.hp.hpl.jena.ontology.OntModelSpec; - ImportCom.hp.hpl.jena.rdf.model.Model; - Importcom.hp.hpl.jena.rdf.model.ModelFactory; the ImportCom.hp.hpl.jena.rdf.model.ModelMaker; - ImportCom.hp.hpl.jena.util.FileManager; - - Public classDbmodeltest { + - /** + * @paramargs A */ at PrivateOntmodel Dbmodel; - StaticString ont1 = "E://DESIGN//SW data//ontology//test.owl"; - StaticString testnamespace = "http://www.semanticweb.org/ontologies/2011/2/21/test.owl#"; - - Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub inDbmodeltest dbtest =Newdbmodeltest (); -System.out.println ("Test MySql DB now.")); to //get model from DB, if not, import from file + Try - { the Dbtest.acquiredbfordata (); * } $ Catch(Exception e)Panax Notoginseng { - System.out.println (E.getmessage (). toString ()); the } +System.out.println ("Save the model to DB ..."); A Try the { + Dbtest.DBModel.commit (); - } $ Catch(Exception e) $ { - System.out.println (E.getmessage (). toString ()); - } the } - Private voidAcquiredbfordata ()throwsSQLException, ClassNotFoundExceptionWuyi { theIDbConnection conn =NULL; -Model Tempmodel =NULL; Wu Try - { AboutClass.forName ("Com.mysql.jdbc.Driver"). newinstance (); $ } - Catch(Exception e) - { -System.out.println (E.getclass (). toString () + "" +e.getmessage (). toString ()); A } +System.out.println ("JDBC Driver found"); theString Db_url = "Jdbc:mysql://localhost:3306/testdb"; -String db_user = "root"; $String Db_pass = "MVP"; theString Db_type = "MySQL"; theconn =NewDBConnection (Db_url, Db_user, Db_pass, Db_type); the if(Conn.getconnection ()! =NULL) theSystem.out.println ("Connection Successful"); - Else inSystem.out.println ("Connection failed!"); the //The following statement links the model to the database. theModelmaker maker =Modelfactory.createmodelrdbmaker (conn); About if(Conn.containsmodel ("Testdbmodel")) the { theSystem.out.println ("Opening Existing model"); theTempmodel = Maker.openmodel ("Testdbmodel",true); + } - Else the { BayiSystem.out.println ("Creating New Model"); theTempmodel = Maker.createmodel ("Testdbmodel"); the //import data from a file - Try - { the Adddatafromfile (); the } the Catch(Exception e) the { - System.out.println (E.getmessage (). toString ()); the } the } theOntmodelspec spec =NewOntmodelspec (ONTMODELSPEC.OWL_MEM); 94Dbmodel =Modelfactory.createontologymodel (spec, Tempmodel); the } the Private voidAdddatafromfile ()throwsIOException the { 98System.out.println ("Loading from Test Owl file ..."); AboutInputStream intest =filemanager.get (). open (Ont1); - Dbmodel.read (Intest, testnamespace); 101 Intest.close (); 102 } 103 104}
Implementation of ontology access operation based on MySQL database in Jena framework