Beispiel #1
0
  /** 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;
      }
    }
  }
Beispiel #2
0
  @Commit
  @SuppressWarnings("unused")
  private void afterDeserialization() throws Exception {
    if (otherAttributesForSerialization != null) {
      attributes.putAll(SimpleXmlWrappers.unwrap(otherAttributesForSerialization));
    }

    phrasesView = Collections.unmodifiableList(phrases);
    subclustersView = Collections.unmodifiableList(subclusters);
    // Documents will be restored on the ProcessingResult level
  }
Beispiel #3
0
  /** 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);
    }
  }
Beispiel #4
0
  /**
   * 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);
  }
Beispiel #5
0
  /** Transfers document and cluster lists to the attributes map after deserialization. */
  @Commit
  private void afterDeserialization() throws Exception {
    if (otherAttributesForSerialization != null) {
      attributes = SimpleXmlWrappers.unwrap(otherAttributesForSerialization);
    }

    attributesView = Collections.unmodifiableMap(attributes);

    attributes.put(AttributeNames.QUERY, query != null ? query.trim() : null);
    attributes.put(AttributeNames.DOCUMENTS, documents);
    attributes.put(AttributeNames.CLUSTERS, clusters);

    // Convert document ids to the actual references
    if (clusters != null && documents != null) {
      final Map<String, Document> documentsById = Maps.newHashMap();
      for (Document document : documents) {
        documentsById.put(document.getStringId(), document);
      }

      for (Cluster cluster : clusters) {
        documentIdToReference(cluster, documentsById);
      }
    }
  }
Beispiel #6
0
  /**
   * 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;
  }
Beispiel #7
0
 /** For JSON and XML serialization only. */
 @JsonProperty("attributes")
 @SuppressWarnings("unused")
 private Map<String, Object> getOtherAttributes() {
   final Map<String, Object> otherAttributes = Maps.newHashMap(attributesView);
   return otherAttributes.isEmpty() ? null : otherAttributes;
 }
Beispiel #8
0
 /**
  * Associates an attribute with this cluster.
  *
  * @param key for the attribute
  * @param value for the attribute
  * @return this cluster for convenience
  */
 public <T> Cluster setAttribute(String key, T value) {
   synchronized (attributes) {
     attributes.put(key, value);
   }
   return this;
 }
Beispiel #9
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);
 }
Beispiel #10
0
 /**
  * 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);
 }
Beispiel #11
0
 /**
  * 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);
 }