如何将资源传递给Jena中的方法?

时间:2017-07-20 09:22:50

标签: java jena

我有一个名为printStatements的方法(Model m,Resource s,Property p,Resource o)。如果我试图找到附加到“亚当”的所有谓词和对象。 Eclipse抛出的adam无法解析为变量'。如何使用printStatements查询模型中的主题,谓词或对象?以下是代码:

     package jenaHelloWorld;

    import java.util.*;

    import org.apache.jena.rdf.model.Model;
    import org.apache.jena.rdf.model.Property;
    import org.apache.jena.rdf.model.Resource;
    import org.apache.jena.rdf.model.Statement;
    import org.apache.jena.rdf.model.StmtIterator;
    import org.apache.jena.util.PrintUtil;

    import org.apache.jena.rdf.model.*;  

    /** 
    * A small family tree held in a Jena Model 
     */  
    public class FamilyModel {  

      // Namespace declarations  
      static final String familyUri = "http://family/";  
      static final String relationshipUri = "http://purl.org/vocab relationship/";  

  // Jena model representing the family  
  private Model model = ModelFactory.createDefaultModel();  

  /** 
   * Creates a model and populates it with family members and their 
   * relationships 
   */  
  private FamilyModel() {  

    // Create an empty Model  


    // Create the types of Property we need to describe relationships  
    // in the model  
    Property childOf = model.createProperty(relationshipUri,"childOf");  
    Property parentOf = model.createProperty(relationshipUri,"parentOf");  
    Property siblingOf = model.createProperty(relationshipUri,"siblingOf");  
    Property spouseOf = model.createProperty(relationshipUri,"spouseOf");  

    // Create resources representing the people in our model  
    Resource adam = model.createResource(familyUri+"adam");  
    Resource beth = model.createResource(familyUri+"beth");  
    Resource chuck = model.createResource(familyUri+"chuck");  
    Resource dotty = model.createResource(familyUri+"dotty");  
    Resource edward = model.createResource(familyUri+"edward");  
    Resource fran = model.createResource(familyUri+"fran");  
    Resource greg = model.createResource(familyUri+"greg");  
    Resource harriet = model.createResource(familyUri+"harriet");  



    // Add properties to describing the relationships between them  
    adam.addProperty(siblingOf,beth);  
    adam.addProperty(spouseOf,dotty);  
    adam.addProperty(parentOf,edward);  
    adam.addProperty(parentOf,fran);  

    beth.addProperty(siblingOf,adam);  
    beth.addProperty(spouseOf,chuck);  

    chuck.addProperty(spouseOf,beth);  

    dotty.addProperty(spouseOf,adam);  
    dotty.addProperty(parentOf,edward);  
    dotty.addProperty(parentOf,fran);  

    // Statements can also be directly created ...  
    Statement statement1 = model.createStatement(edward,childOf,adam);  
    Statement statement2 = model.createStatement(edward,childOf,dotty);  
    Statement statement3 = model.createStatement(edward,siblingOf,fran);  

    // ... then added to the model:  
    model.add(statement1);  
    model.add(statement2);  
    model.add(statement3);  

    // Arrays of Statements can also be added to a Model:  
    Statement statements[] = new Statement[5];  
    statements[0] = model.createStatement(fran,childOf,adam);  
    statements[1] = model.createStatement(fran,childOf,dotty);  
    statements[2] = model.createStatement(fran,siblingOf,edward);  
    statements[3] = model.createStatement(fran,spouseOf,greg);  
    statements[4] = model.createStatement(fran,parentOf,harriet);  
    model.add(statements);  

    // A List of Statements can also be added  
    List list = new ArrayList();  

    list.add(model.createStatement(greg,spouseOf,fran));  
    list.add(model.createStatement(greg,parentOf,harriet));  

    list.add(model.createStatement(harriet,childOf,fran));  
    list.add(model.createStatement(harriet,childOf,greg));  

    model.add(list);  
  }  

  /** 
   * Creates a FamilyModel and dumps the content of its RDF representation 
   */  
  public static void main(String args[]) {  

    // Create a model representing the family  
    FamilyModel theFamily = new FamilyModel();  

    // Dump out a String representation of the model  
    //System.out.println(theFamily.model);  
    //StmtIterator stmts = theFamily.listStatements( adam, null, (RDFNode) null );

    printStatements(theFamily.model, adam,null, null);

  }  

  private static void printStatements(Model m,Resource s,Property p,Resource o) {
         for (StmtIterator i = m.listStatements(s,p,o); i.hasNext(); ) {
           Statement stmt = i.nextStatement();
           System.out.println(PrintUtil.print(stmt));
         }
       }

}  

0 个答案:

没有答案