Esempio n. 1
0
  /**
   * writeData Write a DataItem on the node pointed by path.
   *
   * @param dsData DataItem item to be written
   * @param paPath path to set datas in current file (can be absolute or local)
   * @note if path don't exists it will be automatically created
   */
  public void writeData(DataItem dsData, PathNexus paPath) throws NexusException {
    if (dsData.getData() instanceof PathNexus) {
      // Write link
      writeLink((PathNexus) dsData.getData(), paPath);

      // Return to document root
      closeAll();
    } else {
      // Create or open DataItem
      boolean bNeedDataCheck = createDataItem(dsData, paPath, true);

      // Check data compatiblity if needed
      if (bNeedDataCheck) {
        checkDataMatch(dsData);
      }

      // Write data
      putData(dsData);

      // Write attributes
      putAttr(dsData);

      // Return to document root
      closeAll();
    }
  }
Esempio n. 2
0
  /**
   * convertArray Returns an array of values corresponding to writable type by the HDF library. It
   * converts automatically the data type into its corresponding type. i.e: if dsData is a boolean
   * array it converts it into byte array, if it's a byte array it converts it into a bool array...
   *
   * @param dsData the data to be converted into
   * @throws NexusException
   * @note: conversions are the following: bool => byte, byte => bool
   */
  protected Object convertArray(DataItem dsData) throws NexusException {
    String sClassName = dsData.getData().getClass().getName();
    sClassName = sClassName.substring(sClassName.lastIndexOf('[') + 1);
    Object oOutput;

    int[] iShape = detectArrayShape(dsData.getData());

    // Converting boolean to byte because Nexus API don't accept it
    if (sClassName.startsWith("Ljava.lang.Boolean") || sClassName.equals("Z"))
      oOutput = java.lang.reflect.Array.newInstance(Byte.TYPE, iShape);
    else if (sClassName.startsWith("Ljava.lang.Byte") || sClassName.equals("B"))
      oOutput = java.lang.reflect.Array.newInstance(Boolean.TYPE, iShape);

    /*
     * ******** Warning: jnexus API doesn't support NX_INT64 for writing
     * those lines convert NX_INT64 into NX_FLOAT64, so long to double //
     * Converting long as double because the JAVA Nexus API uses C++ native
     * methods whom don't support integer 64 bit else if(
     * sClassName.startsWith("Ljava.lang.Long") || sClassName.equals("J") )
     * oOutput = java.lang.reflect.Array.newInstance( Double.class,
     * dsData.getSize());
     */
    // Nothing to do
    else throw new NexusException("No data conversion requested!");

    return convertArray(oOutput, dsData.getData());
  }
Esempio n. 3
0
  /**
   * putData Put the item value in the currently opened DataItem
   *
   * @param dsData is a DataItem item (properly initiated), having dimensions, type and size
   *     corresponding to opened DataItem
   */
  protected void putData(DataItem dsData) throws NexusException {
    // Ensures the DataItem has an array value
    dsData.arrayify();

    // Getting data from DataItem
    Object iData = dsData.getData();

    // Converting from string to byte[]
    if (dsData.getType() == NexusFile.NX_CHAR) iData = ((String) iData).getBytes();

    // Converting from boolean[] to byte[]
    else if (dsData.getType() == NexusFile.NX_BOOLEAN) iData = convertArray(dsData);

    /* ******** Warning: jnexus API doesn't support NX_INT64 for writing
     *  those lines convert NX_INT64 into NX_FLOAT64, so long to double
    // Converting from long[] to double[]
    else if( dsData.getType() == NexusFile.NX_INT64 )
    {
      iData = convertArray(dsData);
      dsData.setType(NexusFile.NX_FLOAT64);
    }
     */

    // Putting data into opened DataItem
    getNexusFile().putdata(iData);
  }
  // 按照时间,sizeWindow和stepWindow将数据分成项集,再调用Itemset2File写入文件
  public static String[] movingdivide(DataItems datainput, TaskElement task, boolean FP)
      throws IOException {
    int sizeWindow = (int) ((ParamsSM) task.getMiningParams()).getSizeWindow() * 1000; // seconds
    int stepWindow = (int) ((ParamsSM) task.getMiningParams()).getStepWindow() * 1000; // seconds

    int len = datainput.getLength();
    List<Date> time = datainput.getTime();
    List<String> data = datainput.getData();

    List<String> DataSets = new ArrayList<String>();

    Date win_start_time = time.get(0);
    Date win_end_time = getDateAfter(win_start_time, sizeWindow);
    Date win_start_next = getDateAfter(win_start_time, stepWindow);
    int ind_next = -1;

    StringBuilder sb = new StringBuilder();
    int i = 0;
    do {
      DataItem item = datainput.getElementAt(i);
      i++;
      Date date = item.getTime();
      String val = item.getData();
      if (!date.before(win_start_time) && !date.after(win_end_time)) {
        if (sb.length() != 0) sb.append(" ");
        sb.append(val + " -1");
        if (!date.before(win_start_next) && ind_next == -1) ind_next = i - 1;
      } else {
        sb.append(" -2");
        DataSets.add(sb.toString());
        sb = new StringBuilder();

        if (ind_next == -1) {
          if (!date.before(getDateAfter(win_end_time, stepWindow))) {
            win_start_time = date;
            if (sb.length() != 0) sb.append(" ");
            sb.append(val + " -1");
          } else {
            win_start_time = win_start_next; // getDateAfter(win_start_time, stepWindow);
            if (sb.length() != 0) sb.append(" ");
            sb.append(val + " -1");
          }
        } else {
          i = ind_next;
          ind_next = -1;
          win_start_time = win_start_next;
        }
        win_end_time = getDateAfter(win_start_time, sizeWindow);
        win_start_next = getDateAfter(win_start_time, stepWindow);
      }

    } while (i < len);
    sb.append(" -2");
    DataSets.add(sb.toString());
    return DataSets.toArray(new String[0]);
  }
 private DataMap create(DataItem dataItem) {
   if ((dataItem.getData() == null) && (dataItem.getAssets().size() > 0))
     throw new IllegalArgumentException("Cannot create DataMapItem from an empty DataItem.");
   if (dataItem.getData() == null) return new DataMap();
   try {
     byte[] data = dataItem.getData();
     DataMap dataMap = DataMap.fromByteArray(data);
     if (dataItem.getAssets() != null) {
       Iterator<String> iterator = dataItem.getAssets().keySet().iterator();
       while (iterator.hasNext()) {
         String key = (String) iterator.next();
         DataItemAsset dataItemAsset = (DataItemAsset) dataItem.getAssets().get(key);
         String[] pathArray = key.split("_@_");
         Log.e("spencer", "key = " + key + " pathArray = " + Arrays.toString(pathArray));
         DataMap localDataMap2 = dataMap;
         for (int i = 0; i < pathArray.length - 1; i++) {
           String str2 = pathArray[(i + 1)];
           String[] indexArray = str2.split("_#_");
           if (indexArray.length == 1) {
             localDataMap2 = localDataMap2.getDataMap(pathArray[i]);
           } else {
             int j = Integer.parseInt(indexArray[0]);
             localDataMap2 = (DataMap) localDataMap2.getDataMapArrayList(pathArray[i]).get(j);
             pathArray[(i + 1)] = indexArray[1];
           }
         }
         Log.e(
             "spencer",
             "putAsset -> " + pathArray[(pathArray.length - 1)] + " " + dataItemAsset.getId());
         localDataMap2.putAsset(
             pathArray[(pathArray.length - 1)], Asset.createFromRef(dataItemAsset.getId()));
       }
     }
     return dataMap;
   } catch (Exception e) {
     Log.e("DataMapItem", "parse a DataItem failed.", e);
     throw new IllegalStateException("parse a DataItem failed.", e);
   }
 }
Esempio n. 6
0
  /**
   * putAttr Write the attribute in the currently opened DataItem
   *
   * @param sAttrName name of the attribute
   * @param tData array of values for the attribute
   */
  @SuppressWarnings("unchecked")
  protected <type> void putAttr(String sAttrName, type tData) throws NexusException {
    Object tArrayData;
    if (!(tData instanceof String) && !tData.getClass().isArray()) {
      tArrayData = (type[]) java.lang.reflect.Array.newInstance(tData.getClass(), 1);
      java.lang.reflect.Array.set(tArrayData, 0, tData);
    } else tArrayData = tData;

    // Changing the array into a DataItem object to apply conversion methods if needed
    Object iData;
    DataItem dsData = new DataItem(tArrayData);

    // Converting from string to byte[]
    if (dsData.getType() == NexusFile.NX_CHAR) iData = ((String) dsData.getData()).getBytes();

    // Converting from boolean[] to byte[]
    else if (dsData.getType() == NexusFile.NX_BOOLEAN) iData = convertArray(dsData);

    /* ******** Warning: jnexus API doesn't support NX_INT64 for writing
     *  those lines convert NX_INT64 into NX_FLOAT64, so long to double
    // Converting from long[] to double[]
    else if( dsData.getType() == NexusFile.NX_INT64 )
    {
      iData = convertArray(dsData);
      dsData.setType(NexusFile.NX_FLOAT64);
    }
     */

    // Setting datas as they are
    else iData = dsData.getData();

    // Putting datas into the attribute
    getNexusFile().putattr(sAttrName, iData, dsData.getType());

    // Free ressources
    iData = null;
  }
 protected void publishFromLocalQueue() throws InterruptedException {
   try {
     for (; ; ) {
       synchronized (dataQueue) {
         if (dataQueue.isEmpty()) {
           dataQueue.wait(1000);
           // if the queue stays empty for more then one second, disconnect and
           // wait offline
           if (dataQueue.isEmpty()) {
             System.out.println("disconnected for inactivity");
             disconnect();
             dataQueue.wait();
             waitForConnection();
           }
         }
       }
       DataItem item = dataQueue.peek();
       BasicProperties messageProperties =
           new BasicProperties.Builder()
               .messageId(Long.toString(item.getId()))
               .deliveryMode(2)
               .build();
       long deliveryTag = channel.getNextPublishSeqNo();
       channel.basicPublish("", Constants.queue, messageProperties, item.getData().getBytes());
       // only after successfully publishing, move the item to the
       // container of pending items. They will be removed from it only
       // upon the
       // reception of the confirms from the broker.
       synchronized (pendingItems) {
         pendingItems.put(deliveryTag, item);
       }
       dataQueue.remove();
       if (Thread.interrupted()) {
         throw new InterruptedException();
       }
     }
   } catch (IOException e) {
     // do nothing: the connection will be closed and then retried
   }
 }