<commit_before>/*
 * Copyright 2009-2011 Collaborative Research Centre SFB 632
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.gui.visualizers.iframe.dependency;

import annis.gui.MatchedNodeColors;
import annis.gui.visualizers.VisualizerInput;
import annis.gui.visualizers.iframe.WriterVisualizer;
import static annis.model.AnnisConstants.*;
import de.hu_berlin.german.korpling.saltnpepper.salt.graph.Edge;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SDataSourceSequence;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SDocumentGraph;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SPointingRelation;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.STYPE_NAME;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.STextualDS;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SToken;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SAnnotation;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SFeature;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SNode;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SRelation;
import java.io.IOException;
import java.io.Writer;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

/**
 * <p>Provides a dependence visualization based on vakyartha visualization,
 * which was developed by <NAME>.</p>
 *
 * <p>Originally this visualization was token based. Now Vakyartha extended to
 * visualize dependence between any nodes. This must be configured in the
 * resolver_vis_map table. There exist a mapping column. Please read the section
 * <strong>Configuring Visualizations</strong> in the ANNIS-Manual how to
 * configure the visualization. Supported values for this visaulization are:
 *
 * <ul>
 *
 * <li>node_key:&lt;annotation key&gt;</li>
 *
 * <li>token_visibility:&lt;visible|shown&gt;</li>
 *
 * </ul> </p>
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME><<EMAIL>>
 * @author <NAME>
 */
@PluginImplementation
public class VakyarthaDependencyTree extends WriterVisualizer
{

  private static final org.slf4j.Logger log = LoggerFactory.
    getLogger(VakyarthaDependencyTree.class);

  private Writer theWriter;

  private VisualizerInput input;

  /**
   * If this mapping is not set in the resolver_vis_map table this visualization
   * is only based on the token level.
   */
  private final String MAPPING_NODE_KEY = "node_key";

  /**
   * Only select edges with the edge namespace. If this mappging is not defined
   * the {@link VakyarthaDependencyTree#MAPPING_NODE_KEY} is used.
   */
  private final String MAPPING_EDGE_NS = "edge_ns";

  /**
   * Behaviour:
   *
   *
   * 1. Node key mapping is not set: this mapping has no effect and the token
   * switcher button is not shown.
   *
   * 2. Node key mapping is set:
   *
   * 2.1 Token visibility mapping is not set: The button is visible, but the
   * token level is hidden.
   *
   * 2.2 Token visibility mapping is set to visible: The button and the token
   * level is visible.
   *
   * 2.3 Token visiblity mapping is set to hidden The button is visible but the
   * token level is not visible.
   */
  private final String MAPPING_TOKEN_VISIBILITY = "token_visibility";

  // the one of two possible states of the token switcher button
  private final String VISIBLE = "visible";

  // the one of two possible states of the token switcher button
  private final String HIDDEN = "hidden";

  private Properties mappings;

  /**
   * Try to create a sorted map of nodes. The left annis feature token index is
   * used for sorting the nodes. It is possible the different nodes has the same
   * left token index, but the probability of this is small and it seem's not to
   * make much sense to visualize this. Mabye we should use the node id.
   */
  private Map<SNode, Integer> selectedNodes = new TreeMap<SNode, Integer>(
    new Comparator<SNode>()
    {
      private int getIdx(SNode snode)
      {
        if (snode instanceof SToken)
        {
          SFeature sF = snode.getSFeature(ANNIS_NS, FEAT_TOKENINDEX);
          return sF != null ? (int) (long) sF.getSValueSNUMERIC() : -1;
        }
        else
        {
          SFeature sF = snode.getSFeature(ANNIS_NS, FEAT_LEFTTOKEN);
          return sF != null ? (int) (long) sF.getSValueSNUMERIC() : -1;
        }
      }

      @Override
      public int compare(SNode o1, SNode o2)
      {
        int tok1 = getIdx(o1);
        int tok2 = getIdx(o2);

        if (tok1 < tok2)
        {
          return -1;
        }

        if (tok1 == tok2)
        {
          return 0;
        }
        else
        {
          return 1;
        }

      }
    });

  @Override
  public String getShortName()
  {
    return "arch_dependency";
  }

  @Override
  public void writeOutput(VisualizerInput input, Writer writer)
  {
    theWriter = writer;
    this.input = input;
    this.mappings = input.getMappings();<commit_msg>Always initialize global fields in the servlets with the writeOutput method or mysterious things will happen and maybe you get insane.<commit_after>/*
 * Copyright 2009-2011 Collaborative Research Centre SFB 632
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.gui.visualizers.iframe.dependency;

import annis.gui.MatchedNodeColors;
import annis.gui.visualizers.VisualizerInput;
import annis.gui.visualizers.iframe.WriterVisualizer;
import static annis.model.AnnisConstants.*;
import de.hu_berlin.german.korpling.saltnpepper.salt.graph.Edge;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SDataSourceSequence;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SDocumentGraph;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SPointingRelation;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.STYPE_NAME;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.STextualDS;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SToken;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SAnnotation;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SFeature;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SNode;
import java.io.IOException;
import java.io.Writer;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

/**
 * <p>Provides a dependence visualization based on vakyartha visualization,
 * which was developed by <NAME>.</p>
 *
 * <p>Originally this visualization was token based. Now Vakyartha extended to
 * visualize dependence between any nodes. This must be configured in the
 * resolver_vis_map table. There exist a mapping column. Please read the section
 * <strong>Configuring Visualizations</strong> in the ANNIS-Manual how to
 * configure the visualization. Supported values for this visaulization are:
 *
 * <ul>
 *
 * <li>node_key:&lt;annotation key&gt;</li>
 *
 * <li>token_visibility:&lt;visible|shown&gt;</li>
 *
 * </ul> </p>
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME><<EMAIL>>
 * @author <NAME>
 */
@PluginImplementation
public class VakyarthaDependencyTree extends WriterVisualizer
{

  private static final org.slf4j.Logger log = LoggerFactory.
    getLogger(VakyarthaDependencyTree.class);

  private Writer theWriter;

  private VisualizerInput input;

  /**
   * If this mapping is not set in the resolver_vis_map table this visualization
   * is only based on the token level.
   */
  private final String MAPPING_NODE_KEY = "node_key";

  /**
   * Only select edges with the edge namespace. If this mappging is not defined
   * the {@link VakyarthaDependencyTree#MAPPING_NODE_KEY} is used.
   */
  private final String MAPPING_EDGE_NS = "edge_ns";

  /**
   * Behaviour:
   *
   *
   * 1. Node key mapping is not set: this mapping has no effect and the token
   * switcher button is not shown.
   *
   * 2. Node key mapping is set:
   *
   * 2.1 Token visibility mapping is not set: The button is visible, but the
   * token level is hidden.
   *
   * 2.2 Token visibility mapping is set to visible: The button and the token
   * level is visible.
   *
   * 2.3 Token visiblity mapping is set to hidden The button is visible but the
   * token level is not visible.
   */
  private final String MAPPING_TOKEN_VISIBILITY = "token_visibility";

  // the one of two possible states of the token switcher button
  private final String VISIBLE = "visible";

  // the one of two possible states of the token switcher button
  private final String HIDDEN = "hidden";

  private Properties mappings;

  /**
   * contains only token, if mappings does not contain "node_key"
   */
  private Map<SNode, Integer> selectedNodes;

  @Override
  public String getShortName()
  {
    return "arch_dependency";
  }

  @Override
  public void writeOutput(VisualizerInput input, Writer writer)
  {
    theWriter = writer;
    this.input = input;
    this.mappings = input.getMappings();

    /**
     * Try to create a sorted map of nodes. The left annis feature token index
     * is used for sorting the nodes. It is possible the different nodes has the
     * same left token index, but the probability of this is small and it seem's
     * not to make much sense to visualize this. Mabye we should use the node
     * id.
     */
    this.selectedNodes = new TreeMap<SNode, Integer>(
      new Comparator<SNode>()
      {
        private int getIdx(SNode snode)
        {
          if (snode instanceof SToken)
          {
            SFeature sF = snode.getSFeature(ANNIS_NS, FEAT_TOKENINDEX);
            return sF != null ? (int) (long) sF.getSValueSNUMERIC() : -1;
          }
          else
          {
            SFeature sF = snode.getSFeature(ANNIS_NS, FEAT_LEFTTOKEN);
            return sF != null ? (int) (long) sF.getSValueSNUMERIC() : -1;
          }
        }

        @Override
        public int compare(SNode o1, SNode o2)
        {
          int tok1 = getIdx(o1);
          int tok2 = getIdx(o2);

          if (tok1 < tok2)
          {
            return -1;
          }

          if (tok1 == tok2)
          {
            return 0;
          }
          else
          {
            return 1;
          }

        }
      });
{'fraction_non_alphanumeric': 0.08841564468014584, 'fraction_numerical': 0.006629101756711966, 'mean_word_length': 3.3320172290021537, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3114789', 'n_tokens_mistral': 3836, 'n_tokens_neox': 3651, 'n_words': 1119}