<commit_before>package uk.co.turingatemyhamster.shortbol
package client

import java.io.StringWriter

import fastparse.core.Parsed.Success
import org.widok._
import org.widok.html._
import pl.metastack.metarx.Buffer
import uk.co.turingatemyhamster.datatree
import uk.co.turingatemyhamster.datatree.io.RdfIo
import uk.co.turingatemyhamster.shortbol.ast.SBFile
import uk.co.turingatemyhamster.shortbol.ops.Eval.EvalOps
import uk.co.turingatemyhamster.shortbol.ops.ShortbolParser.POps
import uk.co.turingatemyhamster.shortbol.ops.{Exporter, LogMessage, PrettyPrinter, ShortbolParser}

import scala.scalajs.js


/**
  *
  *
  * @author <NAME>
  */
object Sandbox extends PageApplication {
  implicit val ec = scala.scalajs.concurrent.JSExecutionContext.queue

  lazy val logMsgs: Buffer[LogMessage] = Buffer()

  override def view() = div(
    h1("Shortbol sandpit"),
    div(
      """@import stdlib:sbol
        |
        |@prefix test <http://me.name/test#>
        |@defaultPrefix test
        |
        |seq : DnaSequence("agct")
        |
        |cmp : DnaComponent
        |  sequence = seq
        |
        |prom : Promoter
        |  displayId = "p1"
        |  name = "Promoter 1"
        |  description = "The first promoter"
        |""".stripMargin).id("shortbol_editor").css("shortbol_ace"),
    div().id("longbol").css("shortbol_ace"),
    div().id("xml-rdf").css("shortbol_ace"),
    div(logMsgs map { m => div(m.pretty).css(m.level.pretty) }).id("log")
    )

  override def ready(): Unit = {
    lazy val ace = js.Dynamic.global.ace
    lazy val rdf = js.Dynamic.global.rdf
    lazy val rdf_xml_parser = js.Dynamic.global.rdf

    println("Loading editor")
    val editor = ace.edit("shortbol_editor")
    println("Editor created")
    editor.setTheme("ace/theme/solarized_light")
    println("Set theme")
    editor.getSession().setMode("ace/mode/sbolshorthand2")
    println("Set mode")

    val result = ace.edit("longbol")
    result.setTheme("ace/theme/solarized_light")
    result.getSession().setMode("ace/mode/sbolshorthand2")
    result.setReadOnly(true)

    val xmlRdf = js.Dynamic.global.ace.edit("xml-rdf")
    xmlRdf.setTheme("ace/theme/solarized_light")
    xmlRdf.getSession().setMode("ace/mode/xml")
    xmlRdf.setReadOnly(true)

    editor.getSession().on("change", (e: AnyVal) => swallowExceptions(processShortbol()))
    processShortbol()

    def processShortbol(): Unit = {
      val txt = editor.getSession().getValue().asInstanceOf[String]

      ShortbolParser.SBFile.withPositions(ast.LocalName("shortbol_editor"), txt) match {
        case s: Success[SBFile] =>
          val (c, v) = s.value.eval.run(Fixture.configuredContext)

//          println("Setting log messages")
          logMsgs.clear()
          logMsgs ++= c.logms

          val sw = new StringWriter()
          PrettyPrinter(sw)(ast.SBFile(v))
          sw.append("\n")
//          println("Evaluated to:")
//          println(ast.SBFile(v))
//          println("Pretty printed as:")
//          println(sw)

          result.getSession().setValue(sw.toString)

//          println("Exporting to datatree")
          val doc = Exporter[datatree.ast.AstDatatree](c).apply(v)
//          println("Exporting to xml-rdf")
          val rdfIo = RdfIo.rdfIo[datatree.ast.AstDatatree]
          val xml = RdfIo.write[datatree.ast.AstDatatree](doc)
          val xmlText = xml.render(2)

          xmlRdf.getSession().setValue(xmlText)
<commit_msg>Updated the sandbox to track the current API.<commit_after>package uk.co.turingatemyhamster.shortbol
package client

import java.io.StringWriter

import fastparse.core.Parsed.Success
import org.widok._
import org.widok.html._
import pl.metastack.metarx.Buffer
import uk.co.turingatemyhamster.datatree
import uk.co.turingatemyhamster.datatree.io.RdfIo
import uk.co.turingatemyhamster.shortbol.ast.SBFile
import uk.co.turingatemyhamster.shortbol.ops.Eval.EvalOps
import uk.co.turingatemyhamster.shortbol.ops.ShortbolParser.POps
import uk.co.turingatemyhamster.shortbol.ops.{Exporter, LogMessage, PrettyPrinter, ShortbolParser}

import scala.scalajs.js


/**
  *
  *
  * @author <NAME>
  */
object Sandbox extends PageApplication {
  implicit val ec = scala.scalajs.concurrent.JSExecutionContext.queue

  lazy val logMsgs: Buffer[LogMessage] = Buffer()

  override def view() = div(
    h1("Shortbol sandpit"),
    div(
      """@import stdlib:sbol
        |
        |@prefix test <http://me.name/test#>
        |@defaultPrefix test
        |
        |seq : DnaSequence("agct")
        |
        |cmp : DnaComponent
        |  sequence = seq
        |
        |prom : Promoter
        |  displayId = "p1"
        |  name = "Promoter 1"
        |  description = "The first promoter"
        |""".stripMargin).id("shortbol_editor").css("shortbol_ace"),
    div().id("longbol").css("shortbol_ace"),
    div().id("xml-rdf").css("shortbol_ace"),
    div(logMsgs map { m => div(m.pretty).css(m.level.pretty) }).id("log")
    )

  override def ready(): Unit = {
    lazy val ace = js.Dynamic.global.ace
    lazy val rdf = js.Dynamic.global.rdf
    lazy val rdf_xml_parser = js.Dynamic.global.rdf

    println("Loading editor")
    val editor = ace.edit("shortbol_editor")
    println("Editor created")
    editor.setTheme("ace/theme/solarized_light")
    println("Set theme")
    editor.getSession().setMode("ace/mode/sbolshorthand2")
    println("Set mode")

    val result = ace.edit("longbol")
    result.setTheme("ace/theme/solarized_light")
    result.getSession().setMode("ace/mode/sbolshorthand2")
    result.setReadOnly(true)

    val xmlRdf = js.Dynamic.global.ace.edit("xml-rdf")
    xmlRdf.setTheme("ace/theme/solarized_light")
    xmlRdf.getSession().setMode("ace/mode/xml")
    xmlRdf.setReadOnly(true)

    editor.getSession().on("change", (e: AnyVal) => swallowExceptions(processShortbol()))
    processShortbol()

    def processShortbol(): Unit = {
      val txt = editor.getSession().getValue().asInstanceOf[String]

      ShortbolParser.SBFile.withPositions(ast.LocalName("shortbol_editor"), txt) match {
        case s: Success[SBFile] =>
          val (c, v) = s.value.eval.run(Fixture.configuredContext)

//          println("Setting log messages")
          logMsgs.clear()
          logMsgs ++= c.logms

          val sw = new StringWriter()
          PrettyPrinter(sw)(v)
          sw.append("\n")
//          println("Evaluated to:")
//          println(ast.SBFile(v))
//          println("Pretty printed as:")
//          println(sw)

          result.getSession().setValue(sw.toString)

//          println("Exporting to datatree")
          val doc = Exporter[datatree.ast.AstDatatree](c).apply(v)
//          println("Exporting to xml-rdf")
          val rdfIo = RdfIo.rdfIo[datatree.ast.AstDatatree]
          val xml = RdfIo.write[datatree.ast.AstDatatree](doc)
          val xmlText = xml.render(2)

          xmlRdf.getSession().setValue(xmlText)

{'fraction_non_alphanumeric': 0.13811037272464605, 'fraction_numerical': 0.0017336030049118752, 'mean_word_length': 2.9741676234213545, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, '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': '4234689', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2297, 'n_words': 379}