1 packagecom.biview.jena.spaqrl;2 3 4 5 importjava.io.InputStream;6 7 importcom.hp.hpl.jena.ontology.Individual;8 9 importcom.hp.hpl.jena.ontology.OntClass;10 11 importcom.hp.hpl.jena.ontology.OntModel;12 13 importcom.hp.hpl.jena.ontology.OntModelSpec;14 15 importcom.hp.hpl.jena.rdf.model.Model;16 17 importcom.hp.hpl.jena.rdf.model.ModelFactory;18 19 importcom.hp.hpl.jena.rdf.model.Property;20 21 importcom.hp.hpl.jena.rdf.model.Statement;22 23 importcom.hp.hpl.jena.rdf.model.StmtIterator;24 25 importcom.hp.hpl.jena.util.FileManager;26 27 28 29 /**30 31 * Jena框架数据Model装载32 33 *@authorStephen34 35 * http:// www.biview.cn36 37 */38 39 publicfinalclassJenaModel40 41 {42 43 OntModel defaultModel,tempModel ;44 45 /**46 47 * 创建本体Model48 49 *@paramspec50 51 *@return52 53 */54 55 publicOntModel createOntologyModel( OntModelSpec spec , String filePath)56 57 {58 59 defaultModel=ModelFactory.createOntologyModel(spec);60 61 defaultModel.read(getStreambyFile(filePath),null);62 63 returndefaultModel;64 65 }66 67 /**68 69 * 获取本地owl文件流70 71 *@paramfilePath72 73 *@return74 75 */76 77 privateInputStream getStreambyFile( String filePath )78 79 {80 81 InputStream inFoafInstance=FileManager.get().open(filePath);82 83 returninFoafInstance ;84 85 }86 87 /**88 89 * 循环输出Model90 91 *@parammodel92 93 */94 95 privatevoidoutPut( Model model )96 97 {98 99 StmtIterator stmtIterator=model.listStatements();100 101 Statement statement=null;102 103 while( stmtIterator.hasNext() )104 105 {106 107 statement=stmtIterator.nextStatement();108 109 System.out.println(statement.getSubject()+"---"+statement.getPredicate()+"---"+statement.getObject());//获取断言主语110 111 }112 113 }114 115 /**116 117 * 获取本体Model详细内容118 119 *@parammodel120 121 */122 123 publicvoidgetModelDetails( OntModelSpec spec ,String filePath )124 125 {126 127 outPut(createOntologyModel(spec, filePath));128 129 }130 131 132 133 /**134 135 * 新增一个实例136 137 *@paramspec138 139 *@paramfilePath140 141 *@paramdefaultNameSpace142 143 *@paramclassName144 145 *@params146 147 *@paramp148 149 *@paramo150 151 */152 153 publicvoidaddIndividual(OntModelSpec spec ,String filePath, String defaultNameSpace,String className, String s, String p , Object o)154 155 {156 157 tempModel=createOntologyModel(spec, filePath);158 159 OntClass ontClass=tempModel.createClass(defaultNameSpace+className);160 161 Individual individual=ontClass.createIndividual(defaultNameSpace+s);162 163 Property property=tempModel.createProperty(defaultNameSpace, p);164 165 individual.addLiteral(property,o);166 167 outPut(tempModel);168 169 }170 171 }