zoukankan      html  css  js  c++  java
  • Java 语义网编程系列四: Jena实现语义数据操作

    建立语义网开发环境

    .编译及执行工具: JDK 1.6

    .代码编辑工具:Eclipse 3.4

    .语义网开源框架: Jena 下载地址:http://jena.sourceforge.net/

    .语义网推理机 pellet 下载地址:http://clarkparsia.com/pellet/

     

    Jena框架介绍

    是使用java编写的一种广泛使用的语义web框架。该框架提供了sparql接口、rdfowl接口,已经对推理的支持。此外、该框架还提供了多种存储机制和推理机制,而且也允许集成定制的机制。

     

    View Code
      1 package com.biview.jena.spaqrl;
    2
    3
    4
    5 import java.io.InputStream;
    6
    7 import com.hp.hpl.jena.ontology.Individual;
    8
    9 import com.hp.hpl.jena.ontology.OntClass;
    10
    11 import com.hp.hpl.jena.ontology.OntModel;
    12
    13 import com.hp.hpl.jena.ontology.OntModelSpec;
    14
    15 import com.hp.hpl.jena.rdf.model.Model;
    16
    17 import com.hp.hpl.jena.rdf.model.ModelFactory;
    18
    19 import com.hp.hpl.jena.rdf.model.Property;
    20
    21 import com.hp.hpl.jena.rdf.model.Statement;
    22
    23 import com.hp.hpl.jena.rdf.model.StmtIterator;
    24
    25 import com.hp.hpl.jena.util.FileManager;
    26
    27
    28
    29 /**
    30
    31 * Jena框架数据Model装载
    32
    33 * @author Stephen
    34
    35 * http:// www.biview.cn
    36
    37 */
    38
    39 public final class JenaModel
    40
    41 {
    42
    43 OntModel defaultModel,tempModel ;
    44
    45 /**
    46
    47 * 创建本体Model
    48
    49 * @param spec
    50
    51 * @return
    52
    53 */
    54
    55 public OntModel createOntologyModel( OntModelSpec spec , String filePath)
    56
    57 {
    58
    59 defaultModel = ModelFactory.createOntologyModel(spec);
    60
    61 defaultModel.read(getStreambyFile(filePath),null);
    62
    63 return defaultModel;
    64
    65 }
    66
    67 /**
    68
    69 * 获取本地owl文件流
    70
    71 * @param filePath
    72
    73 * @return
    74
    75 */
    76
    77 private InputStream getStreambyFile( String filePath )
    78
    79 {
    80
    81 InputStream inFoafInstance = FileManager.get().open(filePath);
    82
    83 return inFoafInstance ;
    84
    85 }
    86
    87 /**
    88
    89 * 循环输出Model
    90
    91 * @param model
    92
    93 */
    94
    95 private void outPut( 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 * @param model
    120
    121 */
    122
    123 public void getModelDetails( OntModelSpec spec ,String filePath )
    124
    125 {
    126
    127 outPut(createOntologyModel(spec, filePath));
    128
    129 }
    130
    131
    132
    133 /**
    134
    135 * 新增一个实例
    136
    137 * @param spec
    138
    139 * @param filePath
    140
    141 * @param defaultNameSpace
    142
    143 * @param className
    144
    145 * @param s
    146
    147 * @param p
    148
    149 * @param o
    150
    151 */
    152
    153 public void addIndividual(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 }

     

     

  • 相关阅读:
    Apache RocketMQ + Hudi 快速构建 Lakehouse
    如何快速调度 PTS 的百万并发能力
    flask
    第三方登录————微博
    python定时获取树莓派硬件参数并使用MQTT进行数据推送
    树莓派修改时区
    windows编辑shell,报错syntax error near unexpected token `elif'
    树莓派挂载移动硬盘
    关闭树莓派摄像头红色LED指示灯
    树莓派拍照和录制视频
  • 原文地址:https://www.cnblogs.com/vipyoumay/p/2138029.html
Copyright © 2011-2022 走看看