All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.seninp.jmotif.sax.datastructures.SAXRecords Maven / Gradle / Ivy

package net.seninp.jmotif.sax.datastructures;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

/**
 * The collection for SAXRecords. This datastructure is used in the parallel SAX implementation.
 * 
 * @author psenin
 * 
 */
// public class SAXRecords {
public class SAXRecords implements Iterable {

  /** The id is used to identify the chunk. */
  private final long id;

  /** All the SAX records. */
  private final HashMap records;

  /** The index of occurrences, key is the position in the time series. */
  private final HashMap realTSindex;

  /** The mapping from SAX string positions to real time series positions. */
  private HashMap stringPosToRealPos;

  /**
   * Disable this.
   */
  public SAXRecords() {
    super();
    this.id = System.currentTimeMillis();
    this.records = new HashMap();
    this.realTSindex = new HashMap();
  }

  /**
   * The recommended constructor.
   * 
   * @param id The structure id.
   */
  public SAXRecords(long id) {
    super();
    this.id = id;
    this.records = new HashMap();
    this.realTSindex = new HashMap();
  }

  /**
   * Get the id.
   * 
   * @return the id.
   */
  public long getId() {
    return this.id;
  }

  /**
   * Returns an iterator which is backed by a hash map (i.e. there is no guarantee for keys
   * ordering).
   * 
   * @return an iterator.
   */
  @Override
  public Iterator iterator() {
    return this.realTSindex.values().iterator();
  }

  /**
   * Gets an entry by the index.
   * 
   * @param idx The index.
   * @return The entry.
   */
  public SAXRecord getByIndex(int idx) {
    return realTSindex.get(idx);
  }

  /**
   * Get a SAX record by the string key.
   * 
   * @param str The query string.
   * @return the record if exists.
   */
  public SAXRecord getByWord(String str) {
    return records.get(str);
  }

  /**
   * Drops a single entry.
   * 
   * @param idx the index.
   */
  public void dropByIndex(int idx) {
    SAXRecord entry = realTSindex.get(idx);
    if (null != entry) {
      realTSindex.remove(idx);
      entry.removeIndex(idx);
      if (entry.getIndexes().isEmpty()) {
        records.remove(String.valueOf(entry.getPayload()));
      }
    }
  }

  /**
   * Adds a single string and index entry by creating a SAXRecord.
   * 
   * @param str The string.
   * @param idx The index.
   */
  public void add(char[] str, int idx) {
    SAXRecord rr = records.get(String.valueOf(str));
    if (null == rr) {
      rr = new SAXRecord(str, idx);
      this.records.put(String.valueOf(str), rr);
    }
    else {
      rr.addIndex(idx);
    }
    this.realTSindex.put(idx, rr);
  }

  /**
   * Adds all entries from the collection.
   * 
   * @param records The collection.
   */
  public void addAll(SAXRecords records) {
    for (SAXRecord record : records) {
      char[] payload = record.getPayload();
      for (Integer i : record.getIndexes()) {
        this.add(payload, i);
      }
    }
  }

  /**
   * This adds all to the internal store. Used by the parallel SAX conversion engine.
   * 
   * @param records the data to add.
   */
  public void addAll(HashMap records) {
    for (Entry e : records.entrySet()) {
      this.add(e.getValue(), e.getKey());
    }
  }

  /**
   * Finds the minimal index value.
   * 
   * @return the minimal index value.
   */
  public int getMinIndex() {
    return Collections.min(this.realTSindex.keySet());
  }

  /**
   * Finds the maximal index value.
   * 
   * @return the maximal index value.
   */
  public int getMaxIndex() {
    return Collections.max(this.realTSindex.keySet());
  }

  /**
   * Get the collection size in indexes.
   * 
   * @return the collection size in indexes.
   */
  public int size() {
    return this.realTSindex.size();
  }

  /**
   * Get all the indexes.
   * 
   * @return all the indexes.
   */
  public Set getIndexes() {
    return this.realTSindex.keySet();
  }

  /**
   * Get the SAX string of this whole collection.
   * 
   * @param separatorToken The separator token to use for the string.
   * 
   * @return The whole data as a string.
   */
  public String getSAXString(String separatorToken) {
    StringBuffer sb = new StringBuffer();
    ArrayList index = new ArrayList();
    index.addAll(this.realTSindex.keySet());
    Collections.sort(index, new Comparator() {
      public int compare(Integer int1, Integer int2) {
        return int1.compareTo(int2);
      }
    });
    for (int i : index) {
      sb.append(this.realTSindex.get(i).getPayload()).append(separatorToken);
    }
    return sb.toString();
  }

  /**
   * Get all indexes, sorted.
   * 
   * @return all the indexes.
   */
  public ArrayList getAllIndices() {
    ArrayList res = new ArrayList(this.realTSindex.size());
    res.addAll(this.realTSindex.keySet());
    Collections.sort(res);
    return res;
  }

  /**
   * This builds an index that aids in mapping of a SAX word to the real timeseries index.
   */
  public void buildIndex() {
    this.stringPosToRealPos = new HashMap();
    int counter = 0;
    for (Integer idx : getAllIndices()) {
      this.stringPosToRealPos.put(counter, idx);
      counter++;
    }
  }

  /**
   * This maps an index of the word in the output string to the real position in time-series.
   * 
   * @param idx the index to map.
   * @return the position in the time-series.
   */
  public Integer mapStringIndexToTSPosition(int idx) {
    return this.stringPosToRealPos.get(idx);
  }

  /**
   * Removes saxRecord occurrences that correspond to these positions.
   * 
   * @param positions The positions to clear.
   */
  public void excludePositions(ArrayList positions) {
    for (Integer p : positions) {
      if (realTSindex.containsKey(p)) {
        SAXRecord rec = realTSindex.get(p);
        rec.removeIndex(p);
        if (rec.getIndexes().isEmpty()) {
          this.records.remove(String.valueOf(rec.getPayload()));
        }
        realTSindex.remove(p);
      }
    }
  }

  /**
   * Get motifs.
   * 
   * @param num how many motifs to report.
   * @return the array of motif SAXRecords.
   */
  public ArrayList getMotifs(int num) {
    ArrayList res = new ArrayList(num);
    DoublyLinkedSortedList> list = new DoublyLinkedSortedList>(
        num, new Comparator>() {
          @Override
          public int compare(Entry o1, Entry o2) {
            int f1 = o1.getValue().getIndexes().size();
            int f2 = o2.getValue().getIndexes().size();
            return Integer.compare(f1, f2);
          }
        });
    for (Entry e : this.records.entrySet()) {
      list.addElement(e);
    }
    Iterator> i = list.iterator();
    while (i.hasNext()) {
      res.add(i.next().getValue());
    }
    return res;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy