<commit_before>/*
 * Copyright (C) 2012 mathieu
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openmole.ide.core.implementation.data

import org.openmole.core.implementation.mole._
import org.openmole.core.implementation.validation._
import org.openmole.core.model.data._
import org.openmole.core.model.mole._
import org.openmole.ide.core.implementation.prototype.GenericPrototypeDataUI
import org.openmole.ide.core.implementation.dataproxy.Proxys
import org.openmole.ide.core.implementation.dialog.StatusBar
import org.openmole.ide.core.implementation.registry._
import org.openmole.core.model.task._
import org.openmole.ide.core.implementation.dataproxy.PrototypeDataProxyUI
import org.openmole.ide.core.implementation.serializer.MoleMaker
import org.openmole.ide.core.implementation.workflow.BuildMoleScene
import org.openmole.ide.core.model.dataproxy.IPrototypeDataProxyUI
import org.openmole.ide.core.model.dataproxy.ITaskDataProxyUI
import org.openmole.ide.core.model.workflow.ICapsuleUI
import org.openmole.ide.core.model.workflow.IMoleScene
import org.openmole.misc.exception.UserBadDataError
import org.openmole.misc.tools.service.Logger
import scala.collection.JavaConversions._

object CheckData extends Logger {

  def checkMole(scene: IMoleScene,
                clear: Boolean = true) = {
    if (clear) StatusBar().clear
    scene match {
      case y: BuildMoleScene ⇒
        y.manager.startingCapsule match {
          case Some(x: ICapsuleUI) ⇒
            MoleMaker.buildMole(y.manager) match {
              case Right((mole, cMap, pMap, errs)) ⇒
                val error_capsules = y.manager.capsules.values.partition {
                  _.dataUI.task.isDefined
                }
                error_capsules._1.foreach(_.setAsValid)
                error_capsules._2.foreach {
                  _.setAsInvalid("A capsule has to be encapsulated to be run")
                }

                val capsuleMap: Map[ICapsule, ICapsuleUI] = cMap.map {
                  case (k, v) ⇒ v -> k
                }
                val prototypeMap: Map[Prototype[_], IPrototypeDataProxyUI] = pMap.map {
                  case (k, v) ⇒ v -> k
                }.toMap

                // Compute implicit input / output
                capsuleMap.foreach {
                  case (caps, capsUI) ⇒
                    capsUI.dataUI.task match {
                      case Some(x: ITaskDataProxyUI) ⇒
                        buildUnknownPrototypes(mole, caps)
                        computeImplicitPrototypes(mole, caps, x)
                      case _ ⇒
                    }
                }

                // Formal validation
                val errors = Validation(mole)
                errors.isEmpty match {
                  case false ⇒
                    errors.flatMap {
                      _ match {
                        case x: DataflowProblem ⇒
                          displayCapsuleErrors(capsuleMap(x.capsule), x.toString)
                          Some(capsuleMap(x.capsule) -> x)
                        case x ⇒
                          logger.info("Error " + x + " not taken into account in the GUI yet.")
                          None
                      }
                    }.groupBy(_._1).map {
                      case (k, v) ⇒ (k, v.map(_._2))
                    }.foreach {
                      case (capsuleUI, e) ⇒
                        capsuleUI.updateErrors(e.toList)
                    }
                  case true ⇒ y.manager.capsules.values.foreach {
                    _.updateErrors(List.empty)
                  }
                }
                errs.foreach {
                  case (cui, e) ⇒
                    cui.setAsInvalid(e.getMessage)
                    displayCapsuleErrors(cui, e.getMessage)
                }
                Right(mole, cMap, pMap, errs)
              case Left(l) ⇒ Left(l)
            }
          case _ ⇒
            Left(("No starting capsule is defined, the Mole can not be built"))
        }
      case _ ⇒ Left("")
    }
  }

  def displayCapsuleErrors(capsule: ICapsuleUI,
                           errorMsg: String) = {
    capsule.dataUI.task match {
      case Some(x: ITaskDataProxyUI) ⇒ StatusBar().warn(errorMsg, Some(x))
      case None ⇒ StatusBar().warn(errorMsg)
    }
  }

  def buildUnknownPrototypes(mole: IMole, coreCapsule: ICapsule) = {
    (coreCapsule.inputs(mole).toList ++ coreCapsule.outputs(mole)) foreach {
      d ⇒
        if (!MoleMaker.keyPrototypeMapping.keys.contains(KeyPrototypeGenerator(d.prototype))) {
          val (key, dim) = KeyGenerator(d.prototype)
          Proxys.prototypes +=
            new PrototypeDataProxyUI(GenericPrototypeDataUI(d.prototype.name, dim)(KeyGenerator.stripArrays(d.prototype.`type`)._1), true)
        }
    }
  }

  def computeImplicitPrototypes(
    mole: IMole,
    proxy: ITaskDataProxyUI,
    protoMapping: Map[PrototypeKey, IPrototypeDataProxyUI],
    coreCapsule: ICapsule): Unit = {
    coreCapsule.inputs(mole).foreach { p ⇒ println("PP : " + p.prototype.`type`) }
    proxy.dataUI.updateImplicits(coreCapsule.inputs(mole).map {
      i ⇒
        KeyPrototypeGenerator(i.prototype)
    }.toList
      .filterNot {
        n ⇒
          proxy.dataUI.prototypesIn.map {
            p ⇒
              KeyPrototypeGenerator(p)
          }.contains(n)
      }.map {<commit_msg>clean up<commit_after>/*
 * Copyright (C) 2012 mathieu
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openmole.ide.core.implementation.data

import org.openmole.core.implementation.mole._
import org.openmole.core.implementation.validation._
import org.openmole.core.model.data._
import org.openmole.core.model.mole._
import org.openmole.ide.core.implementation.prototype.GenericPrototypeDataUI
import org.openmole.ide.core.implementation.dataproxy.Proxys
import org.openmole.ide.core.implementation.dialog.StatusBar
import org.openmole.ide.core.implementation.registry._
import org.openmole.core.model.task._
import org.openmole.ide.core.implementation.dataproxy.PrototypeDataProxyUI
import org.openmole.ide.core.implementation.serializer.MoleMaker
import org.openmole.ide.core.implementation.workflow.BuildMoleScene
import org.openmole.ide.core.model.dataproxy.IPrototypeDataProxyUI
import org.openmole.ide.core.model.dataproxy.ITaskDataProxyUI
import org.openmole.ide.core.model.workflow.ICapsuleUI
import org.openmole.ide.core.model.workflow.IMoleScene
import org.openmole.misc.exception.UserBadDataError
import org.openmole.misc.tools.service.Logger
import scala.collection.JavaConversions._

object CheckData extends Logger {

  def checkMole(scene: IMoleScene,
                clear: Boolean = true) = {
    if (clear) StatusBar().clear
    scene match {
      case y: BuildMoleScene ⇒
        y.manager.startingCapsule match {
          case Some(x: ICapsuleUI) ⇒
            MoleMaker.buildMole(y.manager) match {
              case Right((mole, cMap, pMap, errs)) ⇒
                val error_capsules = y.manager.capsules.values.partition {
                  _.dataUI.task.isDefined
                }
                error_capsules._1.foreach(_.setAsValid)
                error_capsules._2.foreach {
                  _.setAsInvalid("A capsule has to be encapsulated to be run")
                }

                val capsuleMap: Map[ICapsule, ICapsuleUI] = cMap.map {
                  case (k, v) ⇒ v -> k
                }
                val prototypeMap: Map[Prototype[_], IPrototypeDataProxyUI] = pMap.map {
                  case (k, v) ⇒ v -> k
                }.toMap

                // Compute implicit input / output
                capsuleMap.foreach {
                  case (caps, capsUI) ⇒
                    capsUI.dataUI.task match {
                      case Some(x: ITaskDataProxyUI) ⇒
                        buildUnknownPrototypes(mole, caps)
                        computeImplicitPrototypes(mole, caps, x)
                      case _ ⇒
                    }
                }

                // Formal validation
                val errors = Validation(mole)
                errors.isEmpty match {
                  case false ⇒
                    errors.flatMap {
                      _ match {
                        case x: DataflowProblem ⇒
                          displayCapsuleErrors(capsuleMap(x.capsule), x.toString)
                          Some(capsuleMap(x.capsule) -> x)
                        case x ⇒
                          logger.info("Error " + x + " not taken into account in the GUI yet.")
                          None
                      }
                    }.groupBy(_._1).map {
                      case (k, v) ⇒ (k, v.map(_._2))
                    }.foreach {
                      case (capsuleUI, e) ⇒
                        capsuleUI.updateErrors(e.toList)
                    }
                  case true ⇒ y.manager.capsules.values.foreach {
                    _.updateErrors(List.empty)
                  }
                }
                errs.foreach {
                  case (cui, e) ⇒
                    cui.setAsInvalid(e.getMessage)
                    displayCapsuleErrors(cui, e.getMessage)
                }
                Right(mole, cMap, pMap, errs)
              case Left(l) ⇒ Left(l)
            }
          case _ ⇒
            Left(("No starting capsule is defined, the Mole can not be built"))
        }
      case _ ⇒ Left("")
    }
  }

  def displayCapsuleErrors(capsule: ICapsuleUI,
                           errorMsg: String) = {
    capsule.dataUI.task match {
      case Some(x: ITaskDataProxyUI) ⇒ StatusBar().warn(errorMsg, Some(x))
      case None ⇒ StatusBar().warn(errorMsg)
    }
  }

  def buildUnknownPrototypes(mole: IMole, coreCapsule: ICapsule) = {
    (coreCapsule.inputs(mole).toList ++ coreCapsule.outputs(mole)) foreach {
      d ⇒
        if (!MoleMaker.keyPrototypeMapping.keys.contains(KeyPrototypeGenerator(d.prototype))) {
          val (key, dim) = KeyGenerator(d.prototype)
          Proxys.prototypes +=
            new PrototypeDataProxyUI(GenericPrototypeDataUI(d.prototype.name, dim)(KeyGenerator.stripArrays(d.prototype.`type`)._1), true)
        }
    }
  }

  def computeImplicitPrototypes(
    mole: IMole,
    proxy: ITaskDataProxyUI,
    protoMapping: Map[PrototypeKey, IPrototypeDataProxyUI],
    coreCapsule: ICapsule): Unit = {
    proxy.dataUI.updateImplicits(coreCapsule.inputs(mole).map {
      i ⇒
        KeyPrototypeGenerator(i.prototype)
    }.toList
      .filterNot {
        n ⇒
          proxy.dataUI.prototypesIn.map {
            p ⇒
              KeyPrototypeGenerator(p)
          }.contains(n)
      }.map {
{'fraction_non_alphanumeric': 0.09137611588344281, 'fraction_numerical': 0.0016843523665150749, 'mean_word_length': 1.8436302681992338, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '5665982', 'n_tokens_mistral': 3515, 'n_tokens_neox': 3403, 'n_words': 776}