<commit_before>package generator.generators.shape

import java.util.UUID
import generator.model.shapecontainer.shape.geometrics.layouts.CommonLayout
import scala.collection.mutable
import generator.model.shapecontainer.shape.Shape
import generator.model.shapecontainer.shape.geometrics._
import generator.model.shapecontainer.shape.geometrics.GeometricModel

/**
  * Created by julian on 19.01.16.
  * The actual ShapeGenerator
  */
object GeneratorShapeDefinition {
  val attrs = mutable.HashMap[String, mutable.MutableList[String]]()
  val attrsInspector = mutable.HashMap[String, mutable.HashMap[GeometricModel, String]]()

  def head(packageName: String) = {
    raw"""
    /*
     * This is a generated ShapeFile for JointJS
     */

    if (typeof exports === 'object') {

      var joint = {
        util: require('../src/core').util,
        shapes: {
          basic: require('./joint.shapes.basic')
        },
        dia: {
          ElementView: require('../src/joint.dia.element').ElementView,
          Link: require('../src/joint.dia.link').Link
        }
      };
    }

    joint.shapes.$packageName = {};

    """
  }

  def generate(shape: Shape, packageName: String) = {
    s"""
    joint.shapes.$packageName.${shape.name} = joint.shapes.basic.Generic.extend({
      markup: ${generateSvgMarkup(shape)},
        defaults: joint.util.deepSupplement({
        type: '$packageName.${shape.name}',
        'init-size': {
          width: ${calculateWidth(shape)},
          height: ${calculateHeight(shape)}},
        size: {${getStencilSize(shape)}},
        resize:{${getResizingPolicies(shape)}},
        attrs: {
          'rect.bounding-box':{
            height: ${calculateHeight(shape)},
            width: ${calculateWidth(shape)}
          },
        ${generateAttrs(shape.name)}
        }
      }, joint.dia.Element.prototype.defaults)
    });
    """
  }

  protected def getStencilSize(shape: Shape): String = {
    val height = calculateHeight(shape).asInstanceOf[Double]
    val width = calculateWidth(shape).asInstanceOf[Double]
    var newHeight = 0.0
    var newWidth = 0.0
    if(height <= 80 && width <= 80) {
      newHeight = height
      newWidth = width
    }else if (height > width) {
      newHeight = 80
      newWidth = width / (height/80.0)
    } else {
      newWidth = 80
      newHeight = height / (width/80.0)
    }
    s"""
      width: ${newWidth.asInstanceOf[Int]}, height: ${newHeight.asInstanceOf[Int]}
     """
  }

  protected def getResizingPolicies(shape: Shape) = {
    s"""
       horizontal: ${shape.stretching_horizontal.getOrElse("true")},
       vertical: ${shape.stretching_vertical.getOrElse("true")},
       proportional: ${shape.proportional.getOrElse("true")}
     """
  }

  protected def generateSvgMarkup(shape: Shape) = {
    """ '<g class="rotatable"><g class="scalable"><rect class="bounding-box" />""" + {
      for (s <- shape.shapes.getOrElse(List())) yield {
        generateSvgShape(s, shape.name, "scalable")
      }
    }.mkString("") + "</g></g>'"
  }

  private def generateSvgShape(g: GeometricModel, shapeName: String, parentClass: String): String = {
    g match {
      case g: Line => generateSvgShape(g.asInstanceOf[Line], shapeName, parentClass)
      case g: Ellipse => generateSvgShape(g.asInstanceOf[Ellipse], shapeName, parentClass)
      case g: Rectangle => generateSvgShape(g.asInstanceOf[Rectangle], shapeName, parentClass)
      case g: Polygon => generateSvgShape(g.asInstanceOf[Polygon], shapeName, parentClass)
      case g: PolyLine => generateSvgShape(g.asInstanceOf[PolyLine], shapeName, parentClass)
      case g: RoundedRectangle => generateSvgShape(g.asInstanceOf[RoundedRectangle], shapeName, parentClass)
      case g: Text => generateSvgShape(g.asInstanceOf[Text], shapeName, parentClass)
    }
  }

  protected def generateSvgShape(shape: Line, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<line class="$className" />"""
  }

  protected def generateSvgShape(shape: Rectangle, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<rect class="$className" />""" + shape.children.map { subShape => generateSvgShape(subShape, rootShapeName, className.toString) }.mkString
  }

  protected def generateSvgShape(shape: Ellipse, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<ellipse class="$className" />""" + shape.children.map { subShape => generateSvgShape(subShape, rootShapeName, className.toString) }.mkString
  }

  protected def generateSvgShape(shape: Polygon, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<polygon class="$className" />""" + shape.children.map { subShape => generateSvgShape(subShape, rootShapeName, className.toString) }.mkString
  }

  protected def generateSvgShape(shape: PolyLine, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<polyline class="$className" />"""
  }

  protected def generateSvgShape(shape: RoundedRectangle, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<rect class="$className" />""" + shape.children.map { subShape => generateSvgShape(subShape, rootShapeName, className.toString) }.mkString
  }

  protected def generateSvgShape(shape: Text, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    val shapeValue = shape.textBody
    val shapeClass = shapeValue.substring(1, shapeValue.length -1)
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<text class="$className" > </text>"""
  }


  protected def generateAttrs(shapeName: String): String = {
    val classes = attrs.get(shapeName)
    if (classes isEmpty) return ""
    val text = classes.get.map { c => c + {
      if (c != classes.get.last) "," else ""
    }
    }.mkString
    attrs.clear()
    text
  }

  protected def buildAttrs(shape: GeometricModel, shapeName: String, className: String, parentClass: String) = {
    val attributes =
      s"""
    '${if (shape.isInstanceOf[Text]) "text" else ""}.$className':{
      """ + getAttributes(shape, parentClass) + "\n}"

    if (attrs.contains(shapeName)) {
      attrs(shapeName) += attributes
      attrsInspector(shapeName).put(shape, className)
    } else {
      val list = mutable.MutableList[String]()
      list += attributes

      val att = new mutable.HashMap[GeometricModel, String]
      att.put(shape, className)
      attrs.put(shapeName, list)
      attrsInspector.put(shapeName, att)
    }
<commit_msg>added Compartment info to generated shapes<commit_after>package generator.generators.shape

import java.util.UUID

import generator.model.shapecontainer.shape.geometrics.layouts.CommonLayout

import scala.collection.mutable
import generator.model.shapecontainer.shape.{Compartment, Shape}
import generator.model.shapecontainer.shape.geometrics._
import generator.model.shapecontainer.shape.geometrics.GeometricModel

/**
  * Created by julian on 19.01.16.
  * The actual ShapeGenerator
  */
object GeneratorShapeDefinition {
  val attrs = mutable.HashMap[String, mutable.MutableList[String]]()
  val attrsInspector = mutable.HashMap[String, mutable.HashMap[GeometricModel, String]]()
  private val compartmentMap = mutable.HashMap[String, mutable.ListBuffer[(String, Compartment)]]()


  def head(packageName: String) = {
    raw"""
    /*
     * This is a generated ShapeFile for JointJS
     */

    if (typeof exports === 'object') {

      var joint = {
        util: require('../src/core').util,
        shapes: {
          basic: require('./joint.shapes.basic')
        },
        dia: {
          ElementView: require('../src/joint.dia.element').ElementView,
          Link: require('../src/joint.dia.link').Link
        }
      };
    }

    joint.shapes.$packageName = {};

    """
  }

  def generate(shape: Shape, packageName: String) = {
    s"""
    joint.shapes.$packageName.${shape.name} = joint.shapes.basic.Generic.extend({
      markup: ${generateSvgMarkup(shape)},
        defaults: joint.util.deepSupplement({
        type: '$packageName.${shape.name}',
        'init-size': {
          width: ${calculateWidth(shape)},
          height: ${calculateHeight(shape)}},
        size: {${getStencilSize(shape)}},
        resize:{${getResizingPolicies(shape)}},
        attrs: {
          'rect.bounding-box':{
            height: ${calculateHeight(shape)},
            width: ${calculateWidth(shape)}
          },
        ${generateAttrs(shape.name)}
        },
        compartments: [${generateCompartmentProperties(shape.name).mkString(",")}]
      }, joint.dia.Element.prototype.defaults)
    });
    """
  }

  protected def getStencilSize(shape: Shape): String = {
    val height = calculateHeight(shape).asInstanceOf[Double]
    val width = calculateWidth(shape).asInstanceOf[Double]
    var newHeight = 0.0
    var newWidth = 0.0
    if(height <= 80 && width <= 80) {
      newHeight = height
      newWidth = width
    }else if (height > width) {
      newHeight = 80
      newWidth = width / (height/80.0)
    } else {
      newWidth = 80
      newHeight = height / (width/80.0)
    }
    s"""
      width: ${newWidth.asInstanceOf[Int]}, height: ${newHeight.asInstanceOf[Int]}
     """
  }

  protected def getResizingPolicies(shape: Shape) = {
    s"""
       horizontal: ${shape.stretching_horizontal.getOrElse("true")},
       vertical: ${shape.stretching_vertical.getOrElse("true")},
       proportional: ${shape.proportional.getOrElse("true")}
     """
  }

  protected def generateSvgMarkup(shape: Shape) = {
    """ '<g class="rotatable"><g class="scalable"><rect class="bounding-box" />""" + {
      for (s <- shape.shapes.getOrElse(List())) yield {
        generateSvgShape(s, shape.name, "scalable")
      }
    }.mkString("") + "</g></g>'"
  }

  private def generateSvgShape(g: GeometricModel, shapeName: String, parentClass: String): String = {
    g match {
      case g: Line => generateSvgShape(g.asInstanceOf[Line], shapeName, parentClass)
      case g: Ellipse => generateSvgShape(g.asInstanceOf[Ellipse], shapeName, parentClass)
      case g: Rectangle => generateSvgShape(g.asInstanceOf[Rectangle], shapeName, parentClass)
      case g: Polygon => generateSvgShape(g.asInstanceOf[Polygon], shapeName, parentClass)
      case g: PolyLine => generateSvgShape(g.asInstanceOf[PolyLine], shapeName, parentClass)
      case g: RoundedRectangle => generateSvgShape(g.asInstanceOf[RoundedRectangle], shapeName, parentClass)
      case g: Text => generateSvgShape(g.asInstanceOf[Text], shapeName, parentClass)
    }
  }

  protected def generateSvgShape(shape: Line, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<line class="$className" />"""
  }

  protected def generateSvgShape(shape: Rectangle, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<rect class="$className" />""" + shape.children.map { subShape => generateSvgShape(subShape, rootShapeName, className.toString) }.mkString
  }

  protected def generateSvgShape(shape: Ellipse, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<ellipse class="$className" />""" + shape.children.map { subShape => generateSvgShape(subShape, rootShapeName, className.toString) }.mkString
  }

  protected def generateSvgShape(shape: Polygon, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<polygon class="$className" />""" + shape.children.map { subShape => generateSvgShape(subShape, rootShapeName, className.toString) }.mkString
  }

  protected def generateSvgShape(shape: PolyLine, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<polyline class="$className" />"""
  }

  protected def generateSvgShape(shape: RoundedRectangle, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<rect class="$className" />""" + shape.children.map { subShape => generateSvgShape(subShape, rootShapeName, className.toString) }.mkString
  }

  protected def generateSvgShape(shape: Text, rootShapeName: String, parentClass: String): String = {
    val className = UUID.randomUUID
    val shapeValue = shape.textBody
    val shapeClass = shapeValue.substring(1, shapeValue.length -1)
    buildAttrs(shape, rootShapeName, className.toString, parentClass)
    s"""<text class="$className" > </text>"""
  }


  protected def generateAttrs(shapeName: String): String = {
    val classes = attrs.get(shapeName)
    if (classes isEmpty) return ""
    val text = classes.get.map { c => c + {
      if (c != classes.get.last) "," else ""
    }
    }.mkString
    attrs.clear()
    text
  }

  protected def buildAttrs(shape: GeometricModel, shapeName: String, className: String, parentClass: String) = {
    buildCompartmentClassMap(shapeName, shape, className)
    val attributes =
      s"""
    '${if (shape.isInstanceOf[Text]) "text" else ""}.$className':{
      """ + getAttributes(shape, parentClass) + "\n}"

    if (attrs.contains(shapeName)) {
      attrs(shapeName) += attributes
      attrsInspector(shapeName).put(shape, className)
    } else {
      val list = mutable.MutableList[String]()
      list += attributes

      val att = new mutable.HashMap[GeometricModel, String]
      att.put(shape, className)
      attrs.put(shapeName, list)
      attrsInspector.put(shapeName, att)
    }

  }

  protected def buildCompartmentClassMap(shapeName: String, shape: GeometricModel, className: String) = {
    shape match {
      case e: Ellipse => if(e.compartment.isDefined) addToCompartmentMap(shapeName,(className ,e.compartment.get))
      case r: Rectangle => if(r.compartment.isDefined) addToCompartmentMap(shapeName,(className ,r.compartment.get))
      case _ =>
    }
  }

  protected def addToCompartmentMap(shapeName: String, tuple: (String, Compartment)) = {
    if(compartmentMap.keySet.exists(_ == shapeName)) {
      compartmentMap(shapeName) += tuple
    } else {
      compartmentMap(shapeName) = mutable.ListBuffer[(String, Compartment)](tuple)
    }
  }

  protected def generateCompartmentProperties(shapeName: String) = {
    if(compartmentMap.keySet.exists(_ == shapeName))compartmentMap(shapeName) map {
      case (className, comp) =>
        s"""
          {
            className: "$className",
            id: "${comp.compartment_id}"
          }
         """
      case _ => ""
    } else List()
{'fraction_non_alphanumeric': 0.13527099310433718, 'fraction_numerical': 0.0033511632403170717, 'mean_word_length': 3.5938425103611604, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 60, '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': '1464146', 'n_tokens_mistral': 4731, 'n_tokens_neox': 4652, 'n_words': 961}