Package com.hp.hpl.jena.rdf.model

Examples of com.hp.hpl.jena.rdf.model.Model


   * @return A configuration model
   * @throws SQLException
   */
  public static Model process(String server, String driver, String namespace, String user, String pass, String[] tables, DatabaseMetaData md) throws SQLException
  {
    Model model = ModelFactory.createDefaultModel();

    model.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
    model.setNsPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    model.setNsPrefix("owl", "http://www.w3.org/2002/07/owl");
    model.setNsPrefix("db", DbMap.NS);
    model.setNsPrefix("ex", namespace);

    Resource map = model.createResource(namespace + "map");

    map.addProperty(RDF.type, DbMap.Map);

    Resource serverR = model.createResource(server);
    serverR.addProperty(RDF.type, DbMap.Database);
    serverR.addProperty(DbMap.user, user);
    serverR.addProperty(DbMap.pass, pass);
    serverR.addProperty(DbMap.driver, driver);

View Full Code Here


   * @param serverR
   * @throws SQLException
   */
  private static void handleTable(DatabaseMetaData md, ResultSet rs, Resource map,
      Resource serverR) throws SQLException {
    Model model = map.getModel();

    String ex = model.getNsPrefixURI("ex");

    while (rs.next()) {
      String tableName = rs.getString("TABLE_NAME");
      Resource tableRes = model
          .createResource(ex + tableName, RDFS.Class);
      map.addProperty(DbMap.mapsClass, tableRes);
      tableRes.addProperty(DbMap.database, serverR);
      tableRes.addProperty(DbMap.table, tableName);

      ResultSet cols = md.getColumns(null, null, tableName, null);
      while (cols.next()) {
        String colName = cols.getString("COLUMN_NAME");
        String type = cols.getString("TYPE_NAME");
        Resource col = model.createResource(ex + tableName + "_"
            + colName, RDF.Property);
        col.addProperty(RDFS.domain, tableRes);
        col.addProperty(DbMap.col, colName);
        col.addProperty(DbMap.colType, type);
      }
      cols.close();
     
      ResultSet forkeys = md.getExportedKeys(null, null, tableName);
      while (forkeys.next()) {
        String colName = forkeys.getString("PKCOLUMN_NAME");
        String fColName = forkeys.getString("FKCOLUMN_NAME");
        String fTableName = forkeys.getString("FKTABLE_NAME");
        Resource col = model.createResource(ex + tableName + "_"
            + colName, RDF.Property);
        Resource fcol = model.createResource(ex + fTableName + "_"
            + fColName, RDF.Property);
        col.addProperty(DbMap.foreignKey, fcol);
      }
      forkeys.close();
           
      ResultSet rowIdentifiers = md.getPrimaryKeys(null, null, tableName);
      while (rowIdentifiers.next())
      {
        String colName = rowIdentifiers.getString("COLUMN_NAME");
        Resource col = model.createResource(ex + tableName + "_"
            + colName, RDF.Property);
        tableRes.addProperty(DbMap.primaryKey, col);
      }
    }

View Full Code Here

    {
      usage();
      System.exit(0);
    }
   
    Model config = FileManager.get().loadModel(args[0]);
   
    String query;
   
    if (args.length == 1 || !args[1].contains(" ")) // stdin or file name
    {
      InputStream in;
      if (args.length == 1) in = System.in;
      else in = new FileInputStream(args[1]);
     
      query = readFromStream(in);
    }
    else query = args[1];
   
    com.hp.hpl.jena.query.Query q = QueryFactory.create(query);
   
    QueryEngine qe;
   
    if (config.contains(null, RDF.type, DbMap.Map))
    {
      qe = new SQLQueryEngine(q, config);
    }
    else
    {
View Full Code Here

 
  // extension of complex content
 
  public int toRDF(boolean mixed, Resource subject, Element elem, int index, Seq seq, Set<restriction> restrictions, Context ctx)
  throws Exception {
    Model m = ctx.getModel();
    schema xs = (schema) this.get_owner();
    complexType c = (complexType) get_baseType(ctx.getModel(),ctx); // extension of base
    if (c!=null) index = c.toRDF(subject,elem, index, seq, null, false, ctx);
    else { // it may be a schema type
      String type = expandQName(ctx.getDefaultNS(), base, m);
      if (type!=null && type.startsWith(schema.XSD_URI)) {
        // predefined schema datatype
        Statement stmt = schema.toRDFStatement(subject, elem, RDF.value, getValue(elem), type, ctx);
        if (stmt!=null) m.add(stmt);
      }
    }
   
    if (sequence!=null) index = sequence.toRDF(subject,elem,index,seq, mixed, ctx);
    else if (choice!=null) index = choice.toRDF(subject,elem,index,seq, mixed, ctx);
View Full Code Here

 
  // a schema list is modelled as an rdf:list of simple values
 
  public boolean toRDF(Resource subject, Property prop, Seq seq, Node node, String value, Set<restriction> restrictions, Context ctx)
  throws Exception {
    Model m = ctx.getModel();
    schema xs = (schema) this.get_owner();
    RDFList l= m.createList();

    simpleType t = get_type(ctx);
    String type = expandQName(ctx.getDefaultNS(),itemType, m);
   
    StringTokenizer tok = new StringTokenizer(value);
    while (tok.hasMoreTokens()) {
      String v = tok.nextToken();
      if (t!=null) l = t.toRDFList(node,v,l,null,ctx);
      else if ((l=schema.toRDFList(node,v,type,l,null,ctx))==null) return false;
    }   
    if (l!=null) {
      // are all restrictions observed
      if (restrictions!=null)
        for (restriction r: restrictions)
          if (!r.isValid(value, null, l, ctx)) return false;

      Statement stmt = m.createStatement(subject, prop, l);
      m.add(stmt);
      if (seq != null) seq.add(stmt.createReifiedStatement());
      return true;
    }
    else return false;
  }
View Full Code Here

    return ok;
  }

  public boolean toXML(Attr attr, RDFNode rdf, Context ctx) {
    boolean ok=true;
    Model m = ctx.getModel();
    try {
      schema xsd = (schema) this.get_owner();
      simpleType simple = ctx.getSimpleType(xsd.expandQName(ctx.getDefaultNS(),itemType,m));
      String type = null;
      if (simple==null) type = expandQName(ctx.getDefaultNS(),itemType, m);
View Full Code Here

   */
 
  public boolean toRDF(Resource subject, Element elem, Seq seq, Context ctx)
  throws Exception {
    populate();
    Model m = ctx.getModel();
    String value = getValue(elem);
    if (value==null && _default != null) value = _default;
    String uri = createURI(m,ctx);
    Property prop = null;
    if (uri != null) prop = m.createProperty(uri);
    Statement stmt=null;
    // the element may be defined locally or globally (follow ref)
    element def = getDefinition(ctx.getModel(),ctx);
   
    // explicit xsi:type
    if (elem.hasAttributeNS(schema.XSI,"type")) {
      String t = elem.getAttributeNS(schema.XSI,"type");
      String fullname = expandQName(ctx.getDefaultNS(),t,elem,ctx.getModel());
      complexType c = ctx.getComplexType(fullname);
      if (c!=null) {
        String id = c.getID(elem,ctx);
        Resource o = id==null?m.createResource():m.createResource(addFragment(ctx.getBaseMap(), id).toString());
        stmt = m.createStatement(subject,prop,o);
        // the new resource, o, becomes the subject
       
        if (is_nillable()
         && elem.hasAttributeNS(schema.XSI,"nil")
         && elem.getAttributeNS(schema.XSI,"nil").equals("true"))
          o.addProperty(RDF.value,RDF.nil);
       
        Seq subSeq = null;
        if (ctx.isSequenced() && elem.hasChildNodes() && c.needSeq(new HashSet<String>(), ctx))
          subSeq = m.getSeq(o.addProperty(RDF.type, RDF.Seq));
 
        int index = c.toRDF(o, elem, 0, subSeq,null,true,ctx);
        // mop up remaining values in sequence
        produceMixed(subSeq, index, elem);

        m.add(stmt);
        if (seq != null) seq.add(stmt.createReifiedStatement());
        return true;
      }
      else Gloze.logger.warn("undefined type: "+fullname);
    }
    // predefined schema datatype
    String type = expandQName(ctx.getDefaultNS(),def.getType(),m);
    if (type!=null && type.startsWith(schema.XSD_URI)) {
      if (is_nillable() && elem.hasAttributeNS(schema.XSI,"nil") && elem.getAttributeNS(schema.XSI,"nil").equals("true"))
        stmt = m.createStatement(subject,prop,RDF.nil);
      else {
        if (type.equals(schema.XSD_URI+"#ID") && value!=null && !value.trim().equals("")) {
          Resource o = m.createResource(addFragment(ctx.getBaseMap(), value.trim()).toString());
          stmt = m.createStatement(subject,prop,o);
        }
        else stmt = schema.toRDFStatement(subject, elem, prop, value, type, ctx);
      }
    }

    // user defined simple type
    XMLBean t = def.get_type(ctx);
    if (t instanceof simpleType) {
      // an empty, as opposed to nil, element is null
      if (value==null) value = "";
      if (is_nillable() && elem.hasAttributeNS(schema.XSI,"nil") && elem.getAttributeNS(schema.XSI,"nil").equals("true"))
        stmt = m.createStatement(subject,prop,RDF.nil);
     
      // if a simple type is nil there is no content or attributes
      // initialise restriction set according to level of restriction checking required
      else {
        Boolean b = ((simpleType) t).toRDF(subject, prop, elem, value, seq, null, ctx);
        if (!b) Gloze.logger.warn("cannot map value "+value+" into simpleType "+type);
      }
    }
    else if (t instanceof complexType) {
      // look for a global ID
      String id = ((complexType) t).getID(elem,ctx);
      Resource o = id==null?m.createResource():m.createResource(addFragment(ctx.getBaseMap(), id).toString());
      stmt = m.createStatement(subject,prop,o);
      // the new resource, o, becomes the subject
      if (is_nillable() && elem.hasAttributeNS(schema.XSI,"nil") && elem.getAttributeNS(schema.XSI,"nil").equals("true"))
        o.addProperty(RDF.value,RDF.nil);

      Seq subSeq = null;
      if (ctx.isSequenced() && elem.hasChildNodes() && ((complexType) t).needSeq(new HashSet<String>(),ctx))
        subSeq = m.getSeq(o.addProperty(RDF.type, RDF.Seq));
     
      int index = ((complexType) t).toRDF(o, elem, 0, subSeq, null,true,ctx);
      // mop up remaining values in sequence
      produceMixed(subSeq, index, elem);
    }
    else if (value!=null && stmt==null){ // untyped!
      Literal l = m.createLiteral(schema.processWhitespace(elem,value,null,ctx));
      stmt = m.createStatement(subject,prop,l);     
    }
    else if (type==null){ // empty
      if (is_nillable() && elem.hasAttributeNS(schema.XSI,"nil") && elem.getAttributeNS(schema.XSI,"nil").equals("true"))
        stmt = m.createStatement(subject,prop,RDF.nil);
      // link to anonymous resource
      else stmt = m.createStatement(subject,prop,m.createResource());
    }

    // if the statement was boxed, only add the link to the box
    if (stmt !=null) {
      m.add(stmt);
      if (seq != null) seq.add(stmt.createReifiedStatement());
    }
    return true;
  }
View Full Code Here

  }
   
  public boolean toXML(Node xml, RDFNode rdf, Context ctx) {
    populate();
    Element e;
    Model m = ctx.getModel();
    Document doc = (Document) ((xml instanceof Document)?xml:xml.getOwnerDocument());

    // the element may be defined locally or globally
    element def = getDefinition(ctx.getModel(),ctx);
    schema xs = (schema) def.get_owner();
View Full Code Here

      XMLUtility.write(doc, new PrintWriter(System.out));
    }
  }

  private void xml_to_rdf( URL source, String name) throws Exception {
    Model m = ModelFactory.createDefaultModel();
    String suffix = lang.toLowerCase().equals("n3") ? "n3" : "rdf" ;
   
    // derive target
    File t = this.target!=null?new File(this.target):null, target = t;
    if (t!=null && (t.isDirectory() || t.getName().indexOf('.')<0)) {
View Full Code Here

    else if (!silent) XMLUtility.write(doc, new PrintWriter(System.out));
    return doc;
  }
 
  private void rdf_to_xml( URL source, String name) throws Exception {
    Model m = ModelFactory.createDefaultModel();
   
    // derive target
    File t = this.target!=null?new File(this.target):null, target = t;
    if (t!=null && (t.isDirectory() || t.getName().indexOf('.')<0)) {
      if (!t.exists()) t.mkdirs();
View Full Code Here

TOP

Related Classes of com.hp.hpl.jena.rdf.model.Model

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.