<commit_before>/*
 * Copyright (c) 2016, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package in.twizmwaz.cardinal.module.region;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.AbstractModule;
import in.twizmwaz.cardinal.module.ModuleEntry;
import in.twizmwaz.cardinal.module.ModuleError;
import in.twizmwaz.cardinal.module.region.exception.RegionAttributeException;
import in.twizmwaz.cardinal.module.region.exception.RegionPropertyException;
import in.twizmwaz.cardinal.module.region.exception.attribute.InvalidRegionAttributeException;
import in.twizmwaz.cardinal.module.region.exception.attribute.MissingRegionAttributeException;
import in.twizmwaz.cardinal.module.region.exception.property.InvalidRegionPropertyException;
import in.twizmwaz.cardinal.module.region.exception.property.MissingRegionPropertyException;
import in.twizmwaz.cardinal.module.region.parser.AboveRegionParser;
import in.twizmwaz.cardinal.module.region.parser.BelowRegionParser;
import in.twizmwaz.cardinal.module.region.parser.BlockRegionParser;
import in.twizmwaz.cardinal.module.region.parser.CircleRegionParser;
import in.twizmwaz.cardinal.module.region.parser.CuboidRegionParser;
import in.twizmwaz.cardinal.module.region.parser.CylinderRegionParser;
import in.twizmwaz.cardinal.module.region.parser.HalfRegionParser;
import in.twizmwaz.cardinal.module.region.parser.RectangleRegionParser;
import in.twizmwaz.cardinal.module.region.parser.SphereRegionParser;
import in.twizmwaz.cardinal.module.region.type.AboveRegion;
import in.twizmwaz.cardinal.module.region.type.BelowRegion;
import in.twizmwaz.cardinal.module.region.type.BlockRegion;
import in.twizmwaz.cardinal.module.region.type.CircleRegion;
import in.twizmwaz.cardinal.module.region.type.CuboidRegion;
import in.twizmwaz.cardinal.module.region.type.CylinderRegion;
import in.twizmwaz.cardinal.module.region.type.EmptyRegion;
import in.twizmwaz.cardinal.module.region.type.EverywhereRegion;
import in.twizmwaz.cardinal.module.region.type.HalfRegion;
import in.twizmwaz.cardinal.module.region.type.NowhereRegion;
import in.twizmwaz.cardinal.module.region.type.RectangleRegion;
import in.twizmwaz.cardinal.module.region.type.SphereRegion;
import lombok.NonNull;
import org.jdom2.Element;

import java.util.List;
import java.util.Map;

@ModuleEntry
public class RegionModule extends AbstractModule {

  private Map<Match, Map<String, Region>> regions = Maps.newHashMap();

  @Override
  public boolean loadMatch(@NonNull Match match) {
    regions.put(match, Maps.newHashMap());
    for (Element regionsElement : match.getMap().getDocument().getRootElement().getChildren("regions")) {
      for (Element regionElement : regionsElement.getChildren()) {
        try {
          getRegion(match, regionElement);
        } catch (RegionException e) {
          errors.add(new ModuleError(this, match.getMap(), new String[]{getRegionError(e, "region", null)}, false));
        }
      }
    }
    return true;
  }

  /**
   * @param id The ID of the region that is returned.
   * @return The region that has the given ID.
   */
  public Region getRegionById(@NonNull Match match, @NonNull String id) {
    return regions.get(match).get(id);
  }

  /**
   * Parses a region from an element.
   *
   * @param element             The element.
   * @param alternateAttributes Any alternative attributes to look for regions from.
   * @return The parsed region.
   * @throws RegionAttributeException Thrown if there are missing or invalid attributes for a region.
   */
  public Region getRegion(Match match, Element element, String... alternateAttributes) throws RegionException {
    String id = element.getAttributeValue("id");
    switch (element.getName()) {
      case "cuboid":
        return checkRegion(match, id, new CuboidRegion(new CuboidRegionParser(element)));
      case "cylinder":
        return checkRegion(match, id, new CylinderRegion(new CylinderRegionParser(element)));
      case "block":
        return checkRegion(match, id, new BlockRegion(new BlockRegionParser(element)));
      case "sphere":
        return checkRegion(match, id, new SphereRegion(new SphereRegionParser(element)));
      case "rectangle":
        return checkRegion(match, id, new RectangleRegion(new RectangleRegionParser(element)));
      case "circle":
        return checkRegion(match, id, new CircleRegion(new CircleRegionParser(element)));
      case "half":
        return checkRegion(match, id, new HalfRegion(new HalfRegionParser(element)));
      case "below":
        return checkRegion(match, id, new BelowRegion(new BelowRegionParser(element)));
      case "above":
        return checkRegion(match, id, new AboveRegion(new AboveRegionParser(element)));
      case "empty":
        return checkRegion(match, id, new EmptyRegion());
      case "nowhere":
        return checkRegion(match, id, new NowhereRegion());
      case "everywhere":
        return checkRegion(match, id, new EverywhereRegion());
      case "region": {
        List<String> attributes = Lists.newArrayList(alternateAttributes);
        attributes.add("id");
        Region region = fromAttributes(match, element, attributes);
        if (region != null) {
          return checkRegion(match, id, region);
        }
        return getRegion(match, element.getChildren().get(0), alternateAttributes);
      }
      default: {
        List<String> attributes = Lists.newArrayList(alternateAttributes);
        attributes.add("id");
        Region region = fromAttributes(match, element, attributes);<commit_msg>Implement checks when getting regions for modification regions<commit_after>/*
 * Copyright (c) 2016, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package in.twizmwaz.cardinal.module.region;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.AbstractModule;
import in.twizmwaz.cardinal.module.ModuleEntry;
import in.twizmwaz.cardinal.module.ModuleError;
import in.twizmwaz.cardinal.module.region.exception.RegionAttributeException;
import in.twizmwaz.cardinal.module.region.exception.RegionPropertyException;
import in.twizmwaz.cardinal.module.region.exception.attribute.InvalidRegionAttributeException;
import in.twizmwaz.cardinal.module.region.exception.attribute.MissingRegionAttributeException;
import in.twizmwaz.cardinal.module.region.exception.property.InvalidRegionPropertyException;
import in.twizmwaz.cardinal.module.region.exception.property.MissingRegionPropertyException;
import in.twizmwaz.cardinal.module.region.parser.AboveRegionParser;
import in.twizmwaz.cardinal.module.region.parser.BelowRegionParser;
import in.twizmwaz.cardinal.module.region.parser.BlockRegionParser;
import in.twizmwaz.cardinal.module.region.parser.CircleRegionParser;
import in.twizmwaz.cardinal.module.region.parser.CuboidRegionParser;
import in.twizmwaz.cardinal.module.region.parser.CylinderRegionParser;
import in.twizmwaz.cardinal.module.region.parser.HalfRegionParser;
import in.twizmwaz.cardinal.module.region.parser.RectangleRegionParser;
import in.twizmwaz.cardinal.module.region.parser.SphereRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.ComplementRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.IntersectRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.MirroredRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.NegativeRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.TranslatedRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.UnionRegionParser;
import in.twizmwaz.cardinal.module.region.type.AboveRegion;
import in.twizmwaz.cardinal.module.region.type.BelowRegion;
import in.twizmwaz.cardinal.module.region.type.BlockRegion;
import in.twizmwaz.cardinal.module.region.type.CircleRegion;
import in.twizmwaz.cardinal.module.region.type.CuboidRegion;
import in.twizmwaz.cardinal.module.region.type.CylinderRegion;
import in.twizmwaz.cardinal.module.region.type.EmptyRegion;
import in.twizmwaz.cardinal.module.region.type.EverywhereRegion;
import in.twizmwaz.cardinal.module.region.type.HalfRegion;
import in.twizmwaz.cardinal.module.region.type.NowhereRegion;
import in.twizmwaz.cardinal.module.region.type.RectangleRegion;
import in.twizmwaz.cardinal.module.region.type.SphereRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.ComplementRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.IntersectRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.MirroredRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.NegativeRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.TranslatedRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.UnionRegion;
import lombok.NonNull;
import org.jdom2.Element;

import java.util.List;
import java.util.Map;

@ModuleEntry
public class RegionModule extends AbstractModule {

  private Map<Match, Map<String, Region>> regions = Maps.newHashMap();

  @Override
  public boolean loadMatch(@NonNull Match match) {
    regions.put(match, Maps.newHashMap());
    for (Element regionsElement : match.getMap().getDocument().getRootElement().getChildren("regions")) {
      for (Element regionElement : regionsElement.getChildren()) {
        try {
          getRegion(match, regionElement);
        } catch (RegionException e) {
          errors.add(new ModuleError(this, match.getMap(), new String[]{getRegionError(e, "region", null)}, false));
        }
      }
    }
    return true;
  }

  /**
   * @param id The ID of the region that is returned.
   * @return The region that has the given ID.
   */
  public Region getRegionById(@NonNull Match match, @NonNull String id) {
    return regions.get(match).get(id);
  }

  /**
   * Parses a region from an element.
   *
   * @param element             The element.
   * @param alternateAttributes Any alternative attributes to look for regions from.
   * @return The parsed region.
   * @throws RegionAttributeException Thrown if there are missing or invalid attributes for a region.
   */
  public Region getRegion(Match match, Element element, String... alternateAttributes) throws RegionException {
    String id = element.getAttributeValue("id");
    switch (element.getName()) {
      case "cuboid":
        return checkRegion(match, id, new CuboidRegion(new CuboidRegionParser(element)));
      case "cylinder":
        return checkRegion(match, id, new CylinderRegion(new CylinderRegionParser(element)));
      case "block":
        return checkRegion(match, id, new BlockRegion(new BlockRegionParser(element)));
      case "sphere":
        return checkRegion(match, id, new SphereRegion(new SphereRegionParser(element)));
      case "rectangle":
        return checkRegion(match, id, new RectangleRegion(new RectangleRegionParser(element)));
      case "circle":
        return checkRegion(match, id, new CircleRegion(new CircleRegionParser(element)));
      case "half":
        return checkRegion(match, id, new HalfRegion(new HalfRegionParser(element)));
      case "below":
        return checkRegion(match, id, new BelowRegion(new BelowRegionParser(element)));
      case "above":
        return checkRegion(match, id, new AboveRegion(new AboveRegionParser(element)));
      case "empty":
        return checkRegion(match, id, new EmptyRegion());
      case "nowhere":
        return checkRegion(match, id, new NowhereRegion());
      case "everywhere":
        return checkRegion(match, id, new EverywhereRegion());
      case "region": {
        List<String> attributes = Lists.newArrayList(alternateAttributes);
        attributes.add("id");
        Region region = fromAttributes(match, element, attributes);
        if (region != null) {
          return checkRegion(match, id, region);
        }
      }
      case "negative":
        return checkRegion(match, id, new NegativeRegion(new NegativeRegionParser(element)));
      case "union":
        return checkRegion(match, id, new UnionRegion(new UnionRegionParser(element)));
      case "complement":
        return checkRegion(match, id, new ComplementRegion(new ComplementRegionParser(element)));
      case "intersect":
        return checkRegion(match, id, new IntersectRegion(new IntersectRegionParser(element)));
      case "translate":
        return checkRegion(match, id, new TranslatedRegion(new TranslatedRegionParser(element)));
      case "mirror":
        return checkRegion(match, id, new MirroredRegion(new MirroredRegionParser(element)));
      default: {
        List<String> attributes = Lists.newArrayList(alternateAttributes);
        attributes.add("id");
        Region region = fromAttributes(match, element, attributes);
{'fraction_non_alphanumeric': 0.0964470284237726, 'fraction_numerical': 0.0009689922480620155, 'mean_word_length': 4.548745519713262, 'pattern_counts': {'":': 32, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4940247', 'n_tokens_mistral': 4473, 'n_tokens_neox': 4253, 'n_words': 1110}