/** Extracts document and cluster lists before serialization. */
  @Persist
  private void beforeSerialization() {
    /*
     * See http://issues.carrot2.org/browse/CARROT-693; this monitor does not save us
     * in multi-threaded environment anyway. A better solution would be to prepare
     * this eagerly in the constructor, but we try to balance overhead and full
     * correctness here.
     */
    synchronized (this) {
      query = (String) attributes.get(AttributeNames.QUERY);

      if (getDocuments() != null) {
        documents = Lists.newArrayList(getDocuments());
      } else {
        documents = null;
      }

      if (getClusters() != null) {
        clusters = Lists.newArrayList(getClusters());
      } else {
        clusters = null;
      }

      otherAttributesForSerialization = MapUtils.asHashMap(SimpleXmlWrappers.wrap(attributes));
      otherAttributesForSerialization.remove(AttributeNames.QUERY);
      otherAttributesForSerialization.remove(AttributeNames.CLUSTERS);
      otherAttributesForSerialization.remove(AttributeNames.DOCUMENTS);
      if (otherAttributesForSerialization.isEmpty()) {
        otherAttributesForSerialization = null;
      }
    }
  }
  /** Replace document refids with the actual references upon deserialization. */
  private void documentIdToReference(Cluster cluster, Map<String, Document> documents) {
    if (cluster.documentIds != null) {
      for (Cluster.DocumentRefid documentRefid : cluster.documentIds) {
        cluster.addDocuments(documents.get(documentRefid.refid));
      }
    }

    for (Cluster subcluster : cluster.getSubclusters()) {
      documentIdToReference(subcluster, documents);
    }
  }
  /**
   * Creates a {@link ProcessingResult} with the provided <code>attributes</code>. Assigns unique
   * document identifiers if documents are present in the <code>attributes</code> map (under the key
   * {@link AttributeNames#DOCUMENTS}).
   */
  @SuppressWarnings("unchecked")
  ProcessingResult(Map<String, Object> attributes) {
    this.attributes = attributes;

    // Replace a modifiable collection of documents with an unmodifiable one
    final List<Document> documents = (List<Document>) attributes.get(AttributeNames.DOCUMENTS);
    if (documents != null) {
      Document.assignDocumentIds(documents);
      attributes.put(AttributeNames.DOCUMENTS, Collections.unmodifiableList(documents));
    }

    // Replace a modifiable collection of clusters with an unmodifiable one
    final List<Cluster> clusters = (List<Cluster>) attributes.get(AttributeNames.CLUSTERS);
    if (clusters != null) {
      Cluster.assignClusterIds(clusters);
      attributes.put(AttributeNames.CLUSTERS, Collections.unmodifiableList(clusters));
    }

    // Store a reference to attributes as an unmodifiable map
    this.attributesView = Collections.unmodifiableMap(attributes);
  }
  /**
   * Prepares a temporary attributes map for serialization purposes. Includes only the requested
   * elements in the map.
   */
  private Map<String, Object> prepareAttributesForSerialization(
      boolean saveDocuments, boolean saveClusters, boolean saveOtherAttributes) {
    final Map<String, Object> tempAttributes = Maps.newHashMap();

    if (saveOtherAttributes) {
      tempAttributes.putAll(attributes);
      tempAttributes.remove(AttributeNames.DOCUMENTS);
      tempAttributes.remove(AttributeNames.CLUSTERS);
    } else {
      tempAttributes.put(AttributeNames.QUERY, attributes.get(AttributeNames.QUERY));
    }

    if (saveDocuments) {
      tempAttributes.put(AttributeNames.DOCUMENTS, getDocuments());
    }

    if (saveClusters) {
      tempAttributes.put(AttributeNames.CLUSTERS, getClusters());
    }

    return tempAttributes;
  }
Exemple #5
0
 /**
  * Returns the attribute associated with this cluster under the provided <code>key</code>. If
  * there is no attribute under the provided <code>key</code>, <code>null</code> will be returned.
  *
  * @param key of the attribute
  * @return attribute value of <code>null</code>
  */
 @SuppressWarnings("unchecked")
 public <T> T getAttribute(String key) {
   return (T) attributes.get(key);
 }
 /**
  * Returns the clusters that have been created during processing. The returned list is
  * unmodifiable.
  *
  * @return clusters created during processing or <code>null</code> if no clusters were present in
  *     the result.
  */
 @SuppressWarnings("unchecked")
 public List<Cluster> getClusters() {
   return (List<Cluster>) attributes.get(AttributeNames.CLUSTERS);
 }
 /**
  * Returns the documents that have been processed. The returned collection is unmodifiable.
  *
  * @return documents that have been processed or <code>null</code> if no documents are present in
  *     the result.
  */
 @SuppressWarnings("unchecked")
 public List<Document> getDocuments() {
   return (List<Document>) attributes.get(AttributeNames.DOCUMENTS);
 }