protected static Resource _processProperty(
      String propertyID, NativeObject propertyNO, String baseURL, SailConnection sailConnection)
      throws SailException {
    String label = _getStringProperty(propertyNO, "label");
    String pluralLabel = _getStringProperty(propertyNO, "pluralLabel");
    String uri = _getStringProperty(propertyNO, "uri");
    String valueType = _getStringProperty(propertyNO, "valueType");

    if (label == null) {
      label = propertyID;
    }
    if (pluralLabel == null) {
      pluralLabel = label;
    }
    if (valueType == null) {
      valueType = "item";
    }

    Resource resource;
    try {
      resource = new URIImpl(uri);
    } catch (Exception e) {
      uri = baseURL + _encode(propertyID);
      resource = new URIImpl(uri);
    }

    sailConnection.addStatement(resource, RDF.TYPE, RDF.PROPERTY);
    sailConnection.addStatement(resource, ExhibitOntology.ID, new LiteralImpl(propertyID));
    sailConnection.addStatement(resource, RDFS.LABEL, new LiteralImpl(label));
    sailConnection.addStatement(
        resource, ExhibitOntology.PLURAL_LABEL, new LiteralImpl(pluralLabel));
    sailConnection.addStatement(resource, ExhibitOntology.VALUE_TYPE, new LiteralImpl(valueType));

    return resource;
  }
  protected static Resource _processType(
      String typeID, NativeObject typeNO, String baseURL, SailConnection sailConnection)
      throws SailException {
    String label = _getStringProperty(typeNO, "label");
    String pluralLabel = _getStringProperty(typeNO, "pluralLabel");
    String uri = _getStringProperty(typeNO, "uri");

    if (label == null) {
      label = typeID;
    }
    if (pluralLabel == null) {
      pluralLabel = label;
    }
    if (uri == null) {
      uri = baseURL + _encode(typeID);
    }

    Resource resource = new URIImpl(uri);
    sailConnection.addStatement(resource, RDF.TYPE, OWL.CLASS);
    sailConnection.addStatement(resource, ExhibitOntology.ID, new LiteralImpl(typeID));
    sailConnection.addStatement(resource, RDFS.LABEL, new LiteralImpl(label));
    sailConnection.addStatement(
        resource, ExhibitOntology.PLURAL_LABEL, new LiteralImpl(pluralLabel));

    return resource;
  }
 // Note: adding statements does not change the configuration of cached
 // values.
 public void addStatement(
     final Resource subj, final URI pred, final Value obj, final Resource... contexts)
     throws SailException {
   cacheConnection.addStatement(subj, pred, obj, contexts);
   baseSailConnection.addStatement(subj, pred, obj, contexts);
   uncommittedChanges = true;
 }
 protected void addStatementInternal(
     final Resource subj, final URI pred, final Value obj, final Resource... contexts)
     throws SailException {
   if (0 == contexts.length) {
     baseSailConnection.addStatement(subj, pred, obj, singleContext);
   } else {
     for (Resource context : contexts) {
       if (null != context && context.equals(singleContext)) {
         baseSailConnection.addStatement(subj, pred, obj, singleContext);
         break;
       }
     }
   }
 }
  protected static URI _ensurePropertyExists(
      String propertyID, String baseURL, SailConnection sailConnection) throws SailException {
    URI predicate =
        (URI) _getSubject(ExhibitOntology.ID, new LiteralImpl(propertyID), sailConnection);
    if (predicate == null) {
      String label = propertyID;
      String uri = baseURL + _encode(propertyID);

      predicate = new URIImpl(uri);
      sailConnection.addStatement(predicate, RDF.TYPE, OWL.CLASS);
      sailConnection.addStatement(predicate, ExhibitOntology.ID, new LiteralImpl(propertyID));
      sailConnection.addStatement(predicate, RDFS.LABEL, new LiteralImpl(label));
      sailConnection.commit();
    }
    return predicate;
  }
  protected static void _addItemProperty(
      Resource itemResource,
      URI predicate,
      Object valueO,
      String baseURL,
      SailConnection dataConnection,
      SailConnection metaConnection,
      Map<String, String> itemIDToURI)
      throws SailException {
    if (valueO == null) return;
    String valueType = _getObjectString(predicate, ExhibitOntology.VALUE_TYPE, metaConnection);
    Value object = null;

    if ("item".equals(valueType)) {
      String id = valueO.toString();
      String uri = itemIDToURI.get(id);
      try {
        object = new URIImpl(uri);
      } catch (Exception e) {
        object = new URIImpl(baseURL + _encode(id));
      }
    } else if (valueO instanceof Double) {
      double d = ((Double) valueO).doubleValue();
      if (d != Math.floor(d)) {
        object = new LiteralImpl(valueO.toString() /*, XMLSchema.DOUBLE*/);
      } else {
        object = new LiteralImpl(Long.toString(((Double) valueO).longValue()) /*, XMLSchema.LONG*/);
      }
    } else {
      object = new LiteralImpl(valueO.toString());
    }

    dataConnection.addStatement(itemResource, predicate, object);
  }
 // Note: adding statements does not change the configuration of cached
 // values.
 public void addStatement(
     final Resource subj, final URI pred, final Value obj, final Resource... contexts)
     throws SailException {
   if (config.logWriteOperations) {
     queryHandler.handle(new AddStatementCall(id, subj, pred, obj, contexts));
   }
   baseSailConnection.addStatement(subj, pred, obj, contexts);
 }
  protected static Resource _ensureTypeExists(
      String typeID, String baseURL, SailConnection sailConnection) throws SailException {
    Resource resource = _getSubject(ExhibitOntology.ID, new LiteralImpl(typeID), sailConnection);
    if (resource == null) {
      String label = typeID;
      String pluralLabel = label;
      String uri = baseURL + _encode(typeID);

      resource = new URIImpl(uri);
      sailConnection.addStatement(resource, RDF.TYPE, OWL.CLASS);
      sailConnection.addStatement(resource, ExhibitOntology.ID, new LiteralImpl(typeID));
      sailConnection.addStatement(resource, RDFS.LABEL, new LiteralImpl(label));
      sailConnection.addStatement(
          resource, ExhibitOntology.PLURAL_LABEL, new LiteralImpl(pluralLabel));
      sailConnection.commit();
    }
    return resource;
  }
Esempio n. 9
0
  @Override
  public void add(Iterable<Statement> triples, String namedGraphURI) {
    for (Statement stmt : triples)
      try {
        if (namedGraphURI == null) {
          connection.addStatement(stmt.getSubject(), stmt.getPredicate(), stmt.getObject());
        } else {
          final URI graph = new ValueFactoryImpl().createURI(namedGraphURI);
          connection.addStatement(stmt.getSubject(), stmt.getPredicate(), stmt.getObject(), graph);
        }
      } catch (SailException e) {
        try {
          connection.rollback();
        } catch (SailException e1) {
          e1.printStackTrace();
        }
      }

    try {
      connection.commit();
    } catch (SailException e2) {
      e2.printStackTrace();
    }
  }
  private void cacheStatements(final Resource subj, final URI pred, final Value obj)
      throws SailException {
    boolean includeInferred = false;

    CloseableIteration<? extends Statement, SailException> iter =
        baseSailConnection.getStatements(subj, pred, obj, includeInferred);

    while (iter.hasNext()) {
      Statement st = iter.next();
      cacheConnection.addStatement(
          st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
    }

    iter.close();

    cacheConnection.commit();
  }
Esempio n. 11
0
  public void handleStatement(Statement arg0) {

    try {
      // avoid self-cycles
      if (arg0.getSubject().stringValue().equals(arg0.getObject().stringValue())) return;

      sc.addStatement(arg0.getSubject(), arg0.getPredicate(), arg0.getObject());
      manager.incrCounter();
      if (manager.atCommit()) System.out.print(".");
    } catch (SailException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(
          "Subject: "
              + arg0.getSubject().toString()
              + " Predicate: "
              + arg0.getPredicate().toString()
              + " Object: "
              + arg0.getObject().toString());
    }
  }
  protected static Resource _processItem(
      NativeObject itemNO,
      String baseURL,
      SailConnection dataConnection,
      SailConnection metaConnection,
      Map<String, String> itemIDToURI)
      throws SailException {
    String id = _getStringProperty(itemNO, "id");
    String label = _getStringProperty(itemNO, "label");
    String uri = _getStringProperty(itemNO, "uri");

    if (id == null) {
      if (label == null) {
        throw new InternalError("Missing both label and id");
      } else {
        id = label;
      }
    }

    Resource itemResource;
    try {
      itemResource = new URIImpl(uri);
    } catch (Exception e) {
      uri = baseURL + _encode(id);
      itemResource = new URIImpl(uri);
    }

    String typeID = _getStringProperty(itemNO, "type");
    Resource type;
    if (typeID == null) {
      type = ExhibitOntology.ITEM;
    } else {
      type = _ensureTypeExists(typeID, baseURL, metaConnection);
    }

    dataConnection.addStatement(itemResource, RDF.TYPE, type);
    dataConnection.addStatement(itemResource, ExhibitOntology.ID, new LiteralImpl(id));
    if (label != null) {
      dataConnection.addStatement(itemResource, RDFS.LABEL, new LiteralImpl(label));
    }

    Object[] propertiesIds = ScriptableObject.getPropertyIds(itemNO);
    for (Object propertyId : propertiesIds) {
      String propertyID = propertyId.toString();
      if (!propertyID.equals("type")
          && !propertyID.equals("label")
          && !propertyID.equals("uri")
          && !propertyID.equals("id")) {

        URI predicate = _ensurePropertyExists(propertyID, baseURL, metaConnection);

        Object valueO = ScriptableObject.getProperty(itemNO, propertyID);
        if (valueO instanceof NativeArray) {
          NativeArray valuesArray = (NativeArray) valueO;

          long count = valuesArray.getLength();
          for (int i = 0; i < count; i++) {
            Object itemO = valuesArray.get(i, valuesArray);

            _addItemProperty(
                itemResource,
                predicate,
                itemO,
                baseURL,
                dataConnection,
                metaConnection,
                itemIDToURI);
          }
        } else {
          _addItemProperty(
              itemResource,
              predicate,
              valueO,
              baseURL,
              dataConnection,
              metaConnection,
              itemIDToURI);
        }
      }
    }

    return itemResource;
  }
  public static void postProcess(
      Map<String, NativeObject> types,
      Map<String, NativeObject> properties,
      List<NativeObject> items,
      String urlSpec,
      Sail dataSail,
      Sail metaSail)
      throws Exception {
    Map<String, Resource> typeIDToResource = new HashMap<String, Resource>();
    Map<String, Resource> propertyIDToResource = new HashMap<String, Resource>();

    String baseURL = _getBaseURL(urlSpec);
    Literal origin = (urlSpec == null || urlSpec.length() == 0) ? null : new LiteralImpl(urlSpec);

    SailConnection metaConnection = metaSail.getConnection();
    try {
      for (String typeID : types.keySet()) {
        Resource typeResource = _processType(typeID, types.get(typeID), baseURL, metaConnection);
        if (origin != null) {
          metaConnection.addStatement(typeResource, ExhibitOntology.ORIGIN, origin);
        }

        typeIDToResource.put(typeID, typeResource);
      }

      for (String propertyID : properties.keySet()) {
        if (!propertyID.equals("type")
            && !propertyID.equals("label")
            && !propertyID.equals("uri")
            && !propertyID.equals("id")) {

          Resource propertyResource =
              _processProperty(propertyID, properties.get(propertyID), baseURL, metaConnection);
          if (origin != null) {
            metaConnection.addStatement(propertyResource, ExhibitOntology.ORIGIN, origin);
          }

          propertyIDToResource.put(propertyID, propertyResource);
        }
      }

      metaConnection.commit();

      Map<String, String> itemIDToURI = new HashMap<String, String>();
      for (NativeObject itemNO : items) {
        String uri = _getStringProperty(itemNO, "uri");
        if (uri != null) {
          String id = _getStringProperty(itemNO, "id");
          if (id == null) {
            id = _getStringProperty(itemNO, "label");
          }
          if (id != null) {
            itemIDToURI.put(id, uri);
          }
        }
      }

      SailConnection dataConnection = dataSail.getConnection();
      try {
        for (NativeObject itemNO : items) {
          Resource itemResource =
              _processItem(itemNO, baseURL, dataConnection, metaConnection, itemIDToURI);
          if (origin != null) {
            dataConnection.addStatement(itemResource, ExhibitOntology.ORIGIN, origin);
          }
        }

        dataConnection.commit();
      } catch (Exception e) {
        dataConnection.rollback();
        throw e;
      } finally {
        dataConnection.close();
      }
    } catch (Exception e) {
      metaConnection.rollback();
      throw e;
    } finally {
      metaConnection.close();
    }
  }