<commit_before>package models

import java.io.File

import akka.actor.{Actor, ActorRef, Props}
import models.DiagramWSActor.DataVisInvalidError
import modigen.util.MetamodelBuilder
import modigen.util.datavis.domain.Conditional
import modigen.util.datavis.generator.ListenersGenerator
import modigen.util.datavis.parser.DataVisParsers
import modigen.util.datavis.validator.ConstrainedDataVisValidator
import modigen.util.domain.{MClass, MReference, Metamodel, ObjectWithAttributes}
import play.api.Logger
import play.api.libs.json.{JsObject, Json}
import shared.DiagramWSMessage.{DataVisCodeMessage, DataVisScopeQuery}
import shared.DiagramWSOutMessage.DataVisScope

import scala.concurrent.ExecutionContext.Implicits.global

class DataVisActor(socket:ActorRef, instanceId:String, graphType:String) extends Actor with DataVisParsers{
  val log = Logger(this getClass() getName())
  var metamodel:Metamodel = null
  val generator = new ListenersGenerator

  MetaModelDatabase.loadModel(graphType) onComplete{
    case scala.util.Success(opt) => opt match {
      case None => log.error("No metamodel present")
      case Some(mm) => metamodel = MetamodelBuilder().fromJson(Json.parse(mm.model).asInstanceOf[JsObject])
    }
    case scala.util.Failure(t) => log.error("Unable to load metamodel")
  }

  override def receive = {
    case msg:DataVisCodeMessage => handleDataVisCode(msg)
    case DataVisScopeQuery(mClass) => handleScopeQuery(mClass)
    case _ => log.error("Unknown message received")
  }

  private def handleDataVisCode(msg:DataVisCodeMessage) = {
    log.debug("DataVis Code for object " + msg.context + ": " + msg.code)

    metamodel.getObjectByName(msg.classname) match {
      case None => socket ! DiagramWSActor.DataVisInvalidError(List("Unable to load class " + msg.classname + " from metamodel."), msg.context)
      case Some(mObject) => parseAll(script, msg.code) match {
        case NoSuccess(error, _) => socket ! DiagramWSActor.DataVisParseError("Could not parse code: " + error, msg.context)
        case Success(conditionals, _) => validateAndGenerateDataVisCode(mObject, conditionals, msg)
      }
    }
  }

  private def handleScopeQuery(classname:String) = {
    log.debug("Scope query for MObj" + classname)
    metamodel.getObjectByName(classname) match {
      case None => socket ! DiagramWSActor.DataVisInvalidError(List("Unable to load class " + classname + " from metamodel."), "")
      case Some(mObject) => mObject match {
        case mClass:MClass => socket ! DataVisScope(mClass.allAttributes.map(_.name), classname)
        case mReference:MReference => socket ! DataVisScope(mReference.attributes.map(_.name), classname)
      }
    }
  }

  private def  validateAndGenerateDataVisCode(mObject:ObjectWithAttributes, conditionals:List[Conditional], msg:DataVisCodeMessage) = {
    val validator = new ConstrainedDataVisValidator
    if (validator.validate(conditionals, mObject))
      generateAndPublish(msg, conditionals)
    else
      socket ! DataVisInvalidError(validator.errors.toList, msg.context)
  }

  private def generateAndPublish(msg:DataVisCodeMessage, conditionals:List[Conditional]) = {
    val fileName = generator.generate(instanceId, msg.context, conditionals)
    socket ! DiagramWSActor.PublishFile(msg.context,  ("/assets/" + fileName).replace(File.separator, "/"))
  }
}

object DataVisActor{
  def props(socket:ActorRef, instanceId:String, graphType:String) = Props(new DataVisActor(socket, instanceId, graphType))
}<commit_msg>Removed access to actor scope from outside<commit_after>package models

import java.io.File

import akka.actor.{Actor, ActorRef, Props}
import models.DataVisActor.{MetamodelFailure, MetamodelLoaded}
import models.DiagramWSActor.DataVisInvalidError
import modigen.util.MetamodelBuilder
import modigen.util.datavis.domain.Conditional
import modigen.util.datavis.generator.ListenersGenerator
import modigen.util.datavis.parser.DataVisParsers
import modigen.util.datavis.validator.ConstrainedDataVisValidator
import modigen.util.domain.{MClass, MReference, Metamodel, ObjectWithAttributes}
import play.api.Logger
import play.api.libs.json.{JsObject, Json}
import shared.DiagramWSMessage.{DataVisCodeMessage, DataVisScopeQuery}
import shared.DiagramWSOutMessage.DataVisScope

import scala.concurrent.ExecutionContext.Implicits.global

class DataVisActor(socket:ActorRef, instanceId:String, graphType:String) extends Actor with DataVisParsers{
  val log = Logger(this getClass() getName())
  var metamodel:Metamodel = null
  val generator = new ListenersGenerator

  MetaModelDatabase.loadModel(graphType) onComplete{
    case scala.util.Success(opt) => opt match {
      case None => self ! MetamodelFailure()
      case Some(mm) => self ! MetamodelLoaded(mm.model)
    }
    case scala.util.Failure(t) => self ! MetamodelFailure
  }

  override def receive = {
    case msg:DataVisCodeMessage => handleDataVisCode(msg)
    case DataVisScopeQuery(mClass) => handleScopeQuery(mClass)
    case MetamodelLoaded(code) => metamodel = MetamodelBuilder().fromJson(Json.parse(code).asInstanceOf[JsObject])
    case MetamodelFailure() => log.error("Unable to lead metamodel")
    case _ => log.error("Unknown message received")
  }

  private def handleDataVisCode(msg:DataVisCodeMessage) = {
    log.debug("DataVis Code for object " + msg.context + ": " + msg.code)

    metamodel.getObjectByName(msg.classname) match {
      case None => socket ! DiagramWSActor.DataVisInvalidError(List("Unable to load class " + msg.classname + " from metamodel."), msg.context)
      case Some(mObject) => parseAll(script, msg.code) match {
        case NoSuccess(error, _) => socket ! DiagramWSActor.DataVisParseError("Could not parse code: " + error, msg.context)
        case Success(conditionals, _) => validateAndGenerateDataVisCode(mObject, conditionals, msg)
      }
    }
  }

  private def handleScopeQuery(classname:String) = {
    log.debug("Scope query for MObj" + classname)
    metamodel.getObjectByName(classname) match {
      case None => socket ! DiagramWSActor.DataVisInvalidError(List("Unable to load class " + classname + " from metamodel."), "")
      case Some(mObject) => mObject match {
        case mClass:MClass => socket ! DataVisScope(mClass.allAttributes.map(_.name), classname)
        case mReference:MReference => socket ! DataVisScope(mReference.attributes.map(_.name), classname)
      }
    }
  }

  private def  validateAndGenerateDataVisCode(mObject:ObjectWithAttributes, conditionals:List[Conditional], msg:DataVisCodeMessage) = {
    val validator = new ConstrainedDataVisValidator
    if (validator.validate(conditionals, mObject))
      generateAndPublish(msg, conditionals)
    else
      socket ! DataVisInvalidError(validator.errors.toList, msg.context)
  }

  private def generateAndPublish(msg:DataVisCodeMessage, conditionals:List[Conditional]) = {
    val fileName = generator.generate(instanceId, msg.context, conditionals)
    socket ! DiagramWSActor.PublishFile(msg.context,  ("/assets/" + fileName).replace(File.separator, "/"))
  }
}

object DataVisActor{
  def props(socket:ActorRef, instanceId:String, graphType:String) = Props(new DataVisActor(socket, instanceId, graphType))
  case class MetamodelLoaded(json:String)
  case class MetamodelFailure()
}
{'fraction_non_alphanumeric': 0.10485863299478451, 'fraction_numerical': 0.0, 'mean_word_length': 5.0674437968359705, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 35, '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': '6596261', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2162, 'n_words': 436}