<commit_before>/**
 * Copyright (C) 2011 SINTEF <<EMAIL>>
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June 2007;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.thingml.cgenerator

import org.sintef.thingml.constraints.ThingMLHelpers
import org.thingml.cgenerator.CGenerator._
import org.thingml.model.scalaimpl.ThingMLScalaImpl._
import org.sintef.thingml.resource.thingml.analysis.helper.CharacterEscaper
import scala.collection.JavaConversions._
import sun.applet.resources.MsgAppletViewer
import org.eclipse.emf.ecore.xml.`type`.internal.RegEx.Match
import com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile
import javax.xml.transform.Result
import java.util.{Hashtable, ArrayList}
import scala.util.parsing.input.StreamReader
import java.io._
import scala.actors._
import scala.actors.Actor._

import io.Source
import java.lang.{Boolean, StringBuilder}
import org.sintef.thingml._
import org.thingml.model.scalaimpl.aspects.MergedConfigurationCache

object SimpleCopyTemplate {

    def copyFromClassPath(path : String) : String = {
      Source.fromInputStream(this.getClass.getClassLoader.getResourceAsStream(path),"utf-8").getLines().mkString("\n")
    }
}

object CGenerator {

  def compileAndRunArduino(model : ThingMLModel, arduinoDir : String, libdir : String) {
    // First look for a configuration in the model
    model.getConfigs.filter{ c => !c.isFragment }.headOption match {
      case Some (c) => compileAndRunArduino(c, arduinoDir, libdir)
      case None =>
        // look in all configs
      model.allConfigurations.filter{ c => !c.isFragment }.headOption match {
        case Some (c) => compileAndRunArduino(c, arduinoDir, libdir)
        case None => {}
      }
    }
  }

  def compileAndRunArduino(cfg : Configuration, arduinoDir : String, libdir : String) {

    // Create a temp folder
    var folder = File.createTempFile(cfg.getName, null);
    folder.delete
    folder.mkdirs
    folder.deleteOnExit

    // Create a folder having the name of the config
    folder = new File(folder, cfg.getName);
    folder.mkdirs

    // Compile the configuration:
    var pde_code =  CGenerator.compile(cfg)

    // Write the code in a pde file
    var pde_file = new File(folder, cfg.getName + ".pde")
    var w: PrintWriter = new PrintWriter(new FileWriter(pde_file))
    w.print(pde_code)
    w.close

    // Open the arduino environment on the generated file
    openArduinoIDE(pde_file.getAbsolutePath, arduinoDir, libdir)

  }

  private val console_out = actor {
           loopWhile(true){
              react {
                  case TIMEOUT =>
                      //caller ! "react timeout"
                  case proc:Process =>
                      println("[PROC] " + proc)
                      val out = new BufferedReader( new InputStreamReader(proc.getInputStream))

                      var line:String = null
                      while({line = out.readLine; line != null}){
                          println("["+ proc + " OUT] " + line)
                      }

                      out.close
              }
        }
    }

    private val console_err = actor {
           loopWhile(true){
              react {
                  case TIMEOUT =>
                      //caller ! "react timeout"
                  case proc:Process =>
                      println("[PROC] " + proc)

                      val err = new BufferedReader( new InputStreamReader(proc.getErrorStream))
                      var line:String = null

                      while({line = err.readLine; line != null}){
                           println("["+ proc + " ERR] " + line)
                      }
                      err.close

              }
        }
    }

  def openArduinoIDE(pde_file : String, arduinoDir : String, arduinolibdir : String)  {


    var arduino = new File(arduinoDir)

    var classpath : String = System.getProperty("java.class.path")

    var libpath : String = System.getProperty("java.library.path")

    if (libpath.length() > 0) {
      libpath = File.pathSeparator + libpath
    }
    libpath = arduinolibdir + libpath



    if (!arduino.exists() || !arduino.isDirectory) {
      System.err.println("ERROR: Arduino installation directory " + arduinoDir + " does not exist.")
      return;
    }

    var libdir = new File(arduino, "lib")
    if (!libdir.exists() || !libdir.isDirectory) {
      System.err.println("ERROR: Could not find lib directory in arduino installation at " + arduinoDir + ".")
      return;
    }

    libdir.listFiles().foreach{ f =>
      if (f.getName.endsWith(".jar")) {
        var fname = f.getAbsolutePath
        if (fname.contains(" ")) fname = "\"" + fname + "\""
        if (classpath.length() > 0) classpath = fname + File.pathSeparator + classpath
        else classpath = fname
      }
    }

    var pb: ProcessBuilder = new ProcessBuilder("java")

    pb.command().add("-Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
    pb.command().add("-Djava.library.path=" + libpath)
    pb.command().add("-Djava.class.path=" + classpath)
    pb.command().add("processing.app.Base")
    pb.command().add(pde_file)

    println("EXEC : " + pb.command().toString)

    var env = pb.environment
<commit_msg>Fix for paths with spaces<commit_after>/**
 * Copyright (C) 2011 SINTEF <<EMAIL>>
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June 2007;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.thingml.cgenerator

import org.sintef.thingml.constraints.ThingMLHelpers
import org.thingml.cgenerator.CGenerator._
import org.thingml.model.scalaimpl.ThingMLScalaImpl._
import org.sintef.thingml.resource.thingml.analysis.helper.CharacterEscaper
import scala.collection.JavaConversions._
import sun.applet.resources.MsgAppletViewer
import org.eclipse.emf.ecore.xml.`type`.internal.RegEx.Match
import com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile
import javax.xml.transform.Result
import java.util.{Hashtable, ArrayList}
import scala.util.parsing.input.StreamReader
import java.io._
import scala.actors._
import scala.actors.Actor._

import io.Source
import java.lang.{Boolean, StringBuilder}
import org.sintef.thingml._
import org.thingml.model.scalaimpl.aspects.MergedConfigurationCache

object SimpleCopyTemplate {

    def copyFromClassPath(path : String) : String = {
      Source.fromInputStream(this.getClass.getClassLoader.getResourceAsStream(path),"utf-8").getLines().mkString("\n")
    }
}

object CGenerator {

  def compileAndRunArduino(model : ThingMLModel, arduinoDir : String, libdir : String) {
    // First look for a configuration in the model
    model.getConfigs.filter{ c => !c.isFragment }.headOption match {
      case Some (c) => compileAndRunArduino(c, arduinoDir, libdir)
      case None =>
        // look in all configs
      model.allConfigurations.filter{ c => !c.isFragment }.headOption match {
        case Some (c) => compileAndRunArduino(c, arduinoDir, libdir)
        case None => {}
      }
    }
  }

  def compileAndRunArduino(cfg : Configuration, arduinoDir : String, libdir : String) {

    // Create a temp folder
    var folder = File.createTempFile(cfg.getName, null);
    folder.delete
    folder.mkdirs
    folder.deleteOnExit

    // Create a folder having the name of the config
    folder = new File(folder, cfg.getName);
    folder.mkdirs

    // Compile the configuration:
    var pde_code =  CGenerator.compile(cfg)

    // Write the code in a pde file
    var pde_file = new File(folder, cfg.getName + ".pde")
    var w: PrintWriter = new PrintWriter(new FileWriter(pde_file))
    w.print(pde_code)
    w.close

    // Open the arduino environment on the generated file
    openArduinoIDE(pde_file.getAbsolutePath, arduinoDir, libdir)

  }

  private val console_out = actor {
           loopWhile(true){
              react {
                  case TIMEOUT =>
                      //caller ! "react timeout"
                  case proc:Process =>
                      println("[PROC] " + proc)
                      val out = new BufferedReader( new InputStreamReader(proc.getInputStream))

                      var line:String = null
                      while({line = out.readLine; line != null}){
                          println("["+ proc + " OUT] " + line)
                      }

                      out.close
              }
        }
    }

    private val console_err = actor {
           loopWhile(true){
              react {
                  case TIMEOUT =>
                      //caller ! "react timeout"
                  case proc:Process =>
                      println("[PROC] " + proc)

                      val err = new BufferedReader( new InputStreamReader(proc.getErrorStream))
                      var line:String = null

                      while({line = err.readLine; line != null}){
                           println("["+ proc + " ERR] " + line)
                      }
                      err.close

              }
        }
    }

  def openArduinoIDE(pde_file : String, arduinoDir : String, arduinolibdir : String)  {


    var arduino = new File(arduinoDir)

    var classpath : String = System.getProperty("java.class.path")

    var libpath : String = System.getProperty("java.library.path")

    if (libpath.length() > 0) {
      libpath = File.pathSeparator + libpath
    }
    libpath = arduinolibdir + libpath



    if (!arduino.exists() || !arduino.isDirectory) {
      System.err.println("ERROR: Arduino installation directory " + arduinoDir + " does not exist.")
      return;
    }

    var libdir = new File(arduino, "lib")
    if (!libdir.exists() || !libdir.isDirectory) {
      System.err.println("ERROR: Could not find lib directory in arduino installation at " + arduinoDir + ".")
      return;
    }

    libdir.listFiles().foreach{ f =>
      if (f.getName.endsWith(".jar")) {
        var fname = f.getAbsolutePath
        if (fname.indexOf(' ') > 0) fname = "\"" + fname + "\""
        if (classpath.length() > 0) classpath = fname + File.pathSeparator + classpath
        else classpath = fname
      }
    }

    var pb: ProcessBuilder = new ProcessBuilder("java")

    pb.command().add("-Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
    pb.command().add("-Djava.library.path=" + libpath)
    pb.command().add("-Djava.class.path=" + classpath)
    pb.command().add("processing.app.Base")
    pb.command().add(pde_file)

    println("EXEC : " + pb.command().toString)

    var env = pb.environment

{'fraction_non_alphanumeric': 0.10871660096364433, 'fraction_numerical': 0.0028909329829172143, 'mean_word_length': 2.4210368594546, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '1375891', 'n_tokens_mistral': 3450, 'n_tokens_neox': 3286, 'n_words': 837}