• JENA的基本语法

    普通类
    • 支持
    • 批判
    • 提问
    • 解释
    • 补充
    • 删除
    • 一些例子:



    1、

    static String personURI = "http://somewhere/JohnSmith";
    static String fullName = "John Smith";

    public static void main (String args[])
    {
    // create an empty model
    Model model = ModelFactory.createDefaultModel();

    // create the resource
    Resource johnSmith = model.createResource(personURI);

    // add the property
    johnSmith.addProperty(VCARD.FN, fullName);

    Resource jj=model.createResource(personURI).addProperty(VCARD.EMAIL, fullName);

    System.out.println(jj.getProperty(null).toString());

    model.write(System.out);

    }
    }

    2、

    public static void main (String args[])
    {

    // some definitions
    String personURI = "http://somewhere/LinChunbin";
    String givenName = "Lin";
    String familyName = "Chunbin";
    String fullName = givenName + " " + familyName;
    // create an empty model
    Model model = ModelFactory.createDefaultModel();

    // create the resource
    // and add the properties cascading style
    Resource johnSmith
    = model.createResource(personURI)
    .addProperty(VCARD.FN, fullName)
    .addProperty(VCARD.N,
    model.createResource()
    .addProperty(VCARD.Given, givenName)
    .addProperty(VCARD.Family, familyName));



    // list the statements in the graph
    // StmtIterator iter = model.listStatements();
    StmtIterator iter= model.listStatements();
    // print out the predicate, subject and object of each statement
    while(iter.hasNext())
    {
    Statement stmt =iter.nextStatement();

    Resource subject=stmt.getSubject();
    Property predicate=stmt.getPredicate();
    RDFNode object =stmt.getObject();

    System.out.print("主语:"+subject.toString());
    System.out.print(" 谓语:"+predicate.toString());
    if(object instanceof Resource)
    {
    System.out.print(" 还是个资源,主语:"+object.toString());
    }
    else
    {
    System.out.print(" 宾语:\""+object.toString()+"\"");
    }
    System.out.println(".");

    }

    }

    3、

    public class Tutorial08 extends Object
    {

    static final String inputFileName = "vc-db-1.rdf";

    public static void main (String args[])
    {
    // create an empty model
    Model model = ModelFactory.createDefaultModel();

    // use the FileManager to find the input file
    InputStream in = FileManager.get().open(inputFileName);
    if (in == null)
    {
    throw new IllegalArgumentException( "File: " + inputFileName + " not found");
    }

    // read the RDF/XML file
    model.read( in, "" );

    // select all the resources with a VCARD.FN property
    // whose value ends with "Smith"

    StmtIterator iter = model.listStatements
    (
    new
    SimpleSelector(null, VCARD.FN, (RDFNode) null)//指明了返回谓语为“CARD.FN”的东西。
    {
    @Override
    public boolean selects(Statement s)
    {
    //return s.getString().endsWith("bin");
    return s.getObject().toString().contains("c");//写明查询条件
    }
    }
    );

    if (iter.hasNext())
    {
    System.out.println("The database contains vcards for:");
    while (iter.hasNext())
    {
    System.out.println(" " + iter.nextStatement()
    .getString());
    }
    }
    else
    {
    System.out.println("No Smith's were found in the database");
    }
    }

    具体的其他例子由于时间关系就不写了,但是上面三个包括了些基本的运用,下一篇我将介绍“JENA+MySql”写入与查询的例子

    • 标签:
    • system.out.print
    • static
    • string
    • resource
    • 基本语法
    • 学习元
    • public
    • addproperty
    • create
    • model
    • system.out.println
    • jena
    • object
  • 加入的知识群:
    学习元评论 (0条)

    评论为空
    聪明如你,不妨在这 发表你的看法与心得 ~



    登录之后可以发表学习元评论
      
暂无内容~~
顶部