#' ### CODE OWNERS: Shea Parkes, Kyle Baird
#'
#' ### OBJECTIVE:
#'   * UI side of Epic FHIR Shiny App.
#'
#' ### DEVELOPER NOTES:
#'   * None

require(shiny)

#' ### LIBRARIES, LOCATIONS, LITERALS, ETC. GO ABOVE HERE

shinyUI(fluidPage(

  titlePanel("Demonstrating the value of INPC"),

  sidebarLayout(
    sidebarPanel(
      tabsetPanel(
        tabPanel(
          'FHIR Data'
          ,uiOutput('ui_name')
          ,p()
          ,h4('Patient Info')
          ,textOutput('patient_dob')
          ,textOutput('patient_address')
          ,p()
          ,uiOutput('ui_loinc')
          ,uiOutput('ui_fhir')
          #,verbatimTextOutput("trace_fhir")
        )
        ,tabPanel(
          'Model Params'
          ,numericInput("scale_input", label = h3("CTS Scale"), value = 1)
          ,numericInput("order_input", label = h3("CTS Order"), value = 1)
        )
      )
    ),

    mainPanel(
      tabsetPanel(
       tabPanel('OldFaith', plotOutput("distPlot"))
       ,tabPanel('Data Table', dataTableOutput("labsTable"))
      )
    )
  )
))

Fix UI to match server.

#' ### CODE OWNERS: Shea Parkes, Kyle Baird
#'
#' ### OBJECTIVE:
#'   * UI side of Epic FHIR Shiny App.
#'
#' ### DEVELOPER NOTES:
#'   * None

require(shiny)

#' ### LIBRARIES, LOCATIONS, LITERALS, ETC. GO ABOVE HERE

shinyUI(fluidPage(

  titlePanel("Demonstrating the value of INPC"),

  sidebarLayout(
    sidebarPanel(
      tabsetPanel(
        tabPanel(
          'FHIR Data'
          ,uiOutput('ui_name')
          ,p()
          ,h4('Patient Info')
          ,textOutput('patient_dob')
          ,textOutput('patient_address')
          ,p()
          ,uiOutput('ui_loinc')
          ,uiOutput('ui_fhir')
          #,verbatimTextOutput("trace_fhir")
        )
        ,tabPanel(
          'Model Params'
          ,numericInput("scale_input", label = h3("CTS Scale"), value = 1)
          ,numericInput("order_input", label = h3("CTS Order"), value = 1)
        )
      )
    ),

    mainPanel(
      tabsetPanel(
       tabPanel('Plot', plotOutput("labsPlot"))
       ,tabPanel('Data Table', dataTableOutput("labsTable"))
      )
    )
  )
))


.onAttach <- function(...) {
  if (!isTRUE(getOption("syberia.silent"))) {
    packageStartupMessage(paste0("Loading ", crayon::red("Syberia"), "...\n"))
  }

  # We want to initialize a Syberia project in the current working directory
  # because 9 times out of 10 this is what the user wants.
  #
  # However, this hook doesn't work on install, because install is done from
  # the working directory of the package, not the user.
  #
  # But when the user calls library(syberia), this will work.
  try({
    syberia_engine()
    packageStartupMessage(crayon::yellow(
      "Loaded syberia project ", sQuote(active_project()$root()), "...\n"
    ))
  }, silent = TRUE)
}

.onDetach <- function(...) {
  # TODO: (RK) Detach any environments attached to the search path
  # by the active project.
}

Install bettertrace on startup if not present.

.onAttach <- function(...) {
  if (!isTRUE(getOption("syberia.silent"))) {
    packageStartupMessage(paste0("Loading ", crayon::red("Syberia"), "...\n"))
  }

  # Load better trace.
  if (isTRUE(getOption("syberia.autoload_bettertrace", TRUE))) {
    if (!is.element("devtools", utils::installed.packages()[, 1])) {
      packageStartupMessage(crayon::yellow("   ...Installing devtools\n"))
      utils::install.packages("devtools")
    }

    if (!requireNamespace("bettertrace", quietly = TRUE)) {
      packageStartupMessage(crayon::yellow("   ...Installing github.com/robertzk/bettertrace\n"))
      devtools::install_github("robertzk/bettertrace")
    }
    library(bettertrace)
  }

  # We want to initialize a Syberia project in the current working directory
  # because 9 times out of 10 this is what the user wants.
  #
  # However, this hook doesn't work on install, because install is done from
  # the working directory of the package, not the user.
  #
  # But when the user calls library(syberia), this will work.
  try({
    syberia_engine()
    packageStartupMessage(crayon::yellow(
      "Loaded syberia project ", sQuote(active_project()$root()), "...\n"
    ))
  }, silent = TRUE)
}

.onDetach <- function(...) {
  # TODO: (RK) Detach any environments attached to the search path
  # by the active project.
}


#Load Packages

library(XML)
library(tidyr)
library(stringr)
library(magrittr)
library(dplyr)
library(RWeka)

#load OCR'd ICC Deceisions data into R
icc_dir <- "text"
files <- dir(icc_dir, "*.txt")
raw <- file.path(icc_dir, files) %>%
  lapply(., scan, "character", sep = "\n")
names(raw) <- files
icc_texts <- lapply(raw, paste, collapse = " ") %>%
  lapply(., tolower) %>%
  lapply(., WordTokenizer) %>%
  lapply(., paste, collapse = " ")

#Create an N-gram maker with Rweka's function
ngrammify <- function(data, n) {
  NGramTokenizer(data, Weka_control(min = n, max = n))
}

#Turn text list into N-grams, in this case 5-grams
icc_grams <- lapply(icc_texts, ngrammify, 5)
every_grams <- icc_grams %>% unlist() %>% unique()

#Remove non-words from the raw icc texts
filter_unreasonable_ngrams <- function(ngrams) {
  require(stringr)
  ngrams[!str_detect(ngrams, "[^a-z ]")]
}

#Run the filter_unreasonable_ngrams function
fix_grams <- filter_unreasonable_ngrams(every_grams)
head(fix_grams)

Save the data to a CSV

#Load Packages

library(XML)
library(tidyr)
library(stringr)
library(magrittr)
library(dplyr)
library(RWeka)

#load OCR'd ICC Deceisions data into R
icc_dir <- "text"
files <- dir(icc_dir, "*.txt")
raw <- file.path(icc_dir, files) %>%
  lapply(., scan, "character", sep = "\n")
names(raw) <- files
icc_texts <- lapply(raw, paste, collapse = " ") %>%
  lapply(., tolower) %>%
  lapply(., WordTokenizer) %>%
  lapply(., paste, collapse = " ")

#Create an N-gram maker with Rweka's function
ngrammify <- function(data, n) {
  NGramTokenizer(data, Weka_control(min = n, max = n))
}

#Turn text list into N-grams, in this case 5-grams
icc_grams <- lapply(icc_texts, ngrammify, 5)
every_grams <- icc_grams %>% unlist() %>% unique()

#Remove non-words from the raw icc texts
filter_unreasonable_ngrams <- function(ngrams) {
  require(stringr)
  ngrams[!str_detect(ngrams, "[^a-z ]")]
}

#Run the filter_unreasonable_ngrams function
fix_grams <- filter_unreasonable_ngrams(every_grams)
head(fix_grams)

#Save the data
write.csv(fix_grams, file = "out/every_ngram_fix.csv")


#' ## Code Owners: Kyle Baird
#' ### OWNERS ATTEST TO THE FOLLOWING:
#'   * The `master` branch will meet Milliman QRM standards at all times.
#'   * Deliveries will only be made from code in the `master` branch.
#'   * Review/Collaboration notes will be captured in Pull Requests (prior to merging).
#'
#'
#' ### Objective:
#'   * Load the data for analytics into native R data structures so it is easy to work with
#'
#' ### Developer Notes:
#'   * <none>

path.dir.source <- paste0(
  Sys.getenv("USERPROFILE")
  ,"/repos/epic-fhir/data/"
  )

#' ## LIBRARIES, LOCATIONS, LITERALS, ETC. GO ABOVE HERE

df.labs <- read.csv(
  paste0(path.dir.source, "labs.csv")
  ,header = TRUE
  ,stringsAsFactors = FALSE
  )
# df.results <- read.csv(
#   paste0(path.dir.source, "results.csv")
#   ,header = TRUE
#   ,stringsAsFactors = FALSE
#   )
df.patients <- read.csv(
  paste0(path.dir.source, "patients.csv")
  ,header = TRUE
  ,stringsAsFactors = FALSE
  )
df.patients$dob.r <- as.Date(df.patients$dob, "%Y-%m-%dT%H:%M:%SZ")

#'
#' _ _ _
#' ## R SPECIFIC FOOTER
sessionInfo()
Sys.time()
proc.time()

Adjust R importing to be relative.

#' ## Code Owners: Kyle Baird, Shea Parkes
#'
#' ### Objective:
#'   * Load the data for analytics into native R data structures so it is easy to work with
#'
#' ### Developer Notes:
#'   * <none>

path.dir.source <- '../data/'

#' ## LIBRARIES, LOCATIONS, LITERALS, ETC. GO ABOVE HERE

df.labs <- read.csv(
  paste0(path.dir.source, "labs.csv")
  ,header = TRUE
  ,stringsAsFactors = FALSE
  )
# df.results <- read.csv(
#   paste0(path.dir.source, "results.csv")
#   ,header = TRUE
#   ,stringsAsFactors = FALSE
#   )
df.patients <- read.csv(
  paste0(path.dir.source, "patients.csv")
  ,header = TRUE
  ,stringsAsFactors = FALSE
  )
df.patients$dob.r <- as.Date(df.patients$dob, "%Y-%m-%dT%H:%M:%SZ")

#'
#' _ _ _
#' ## R SPECIFIC FOOTER
sessionInfo()
Sys.time()
proc.time()


decorate = modules::import('decorate', attach = TRUE)
modules::import('ebits/base', attach = c('closure', 'match_call_defaults'))

# FIXME: Doesn’t work with recursive functions
# Reproduce: fib = .cache %@% function (n) if (n < 2) 1 else fib(n - 1) + fib(n - 2)
# Suspicion: somehow, the state of the function is shared, and `n` is
# continuously decreased. But only sometimes.
cache = decorator %@% function (f) {
    cache = list()
    g = function (...) {
        call = match_call_defaults()
        args = call[-1]
        args_hash = if (is.null(args)) '.' else
            paste(sapply(lapply(args, eval.parent), deparse), collapse = ', ')
        if (exists(args_hash, cache))
            cache[[args_hash]]
        else {
            call[[1]] = f
            result = eval.parent(call)
            cache[[args_hash]] <<- result
            result
        }
    }
    closure(formals(f), body(g), environment())
}

`%@%` = decorate$`%@%`

Use hashing for more efficient lookup

decorate = modules::import('decorate', attach = TRUE)
modules::import('ebits/base', attach = c('closure', 'match_call_defaults'))

# FIXME: Doesn’t work with recursive functions
# Reproduce: fib = .cache %@% function (n) if (n < 2) 1 else fib(n - 1) + fib(n - 2)
# Suspicion: somehow, the state of the function is shared, and `n` is
# continuously decreased. But only sometimes.
cache = decorator %@% function (f) {
    cache = new.env()
    g = function (...) {
        call = match_call_defaults()
        args = call[-1]
        args_hash = if (is.null(args)) '.' else
            paste(sapply(lapply(args, eval.parent), deparse), collapse = ', ')
        if (exists(args_hash, cache))
            cache[[args_hash]]
        else {
            call[[1]] = f
            result = eval.parent(call)
            cache[[args_hash]] <<- result
            result
        }
    }
    closure(formals(f), body(g), environment())
}

`%@%` = decorate$`%@%`


fram.db.name <- "./fram db/Final pre and post databases/FramVS2-PSC-Coho-PostSeason.mdb"
fram.run.name <- "bc-bkCoho2015 Final"

run.year <- 2015

Add sample configs to export past FRAM years

###########   2015 Post Season Catch #################
fram.db.name <- "./fram db/Final pre and post databases/FramVS2-PSC-Coho-PostSeason.mdb"
fram.run.name <- "bc-bkCoho2015 Final"
run.year <- 2015

###########   2014 Post Season Catch #################
#fram.db.name <- "./fram db/FramVS2-PSC-Coho-Backwards-for 2013 and 2014.mdb"
#fram.run.name <- "bc-BK2014 w TAMM inputs final#2"
#run.year <- 2014

###########   2013 Post Season Catch #################
#fram.db.name <- "./fram db/FramVS2-PSC-Coho-Backwards-for 2013 and 2014.mdb"
#fram.run.name <- "bk 2013 Final ver3 Feb 11 2015"
#run.year <- 2013

###########   2012 Post Season Catch #################
#fram.db.name <- "./fram db/FramVS2-PSC-Coho-Backwards-2013.mdb"
#fram.run.name <- "Coho2012Post_PSC SSNPx2 Q 2014"
#run.year <- 2012

###########   2011 Post Season Catch #################
#fram.db.name <- "./fram db/FramVS2-PSC-Coho-Backwards-2012 Feb 12.mdb"
#fram.run.name <- "Coho2011Post_PSC 2013"
#run.year <- 2011

###########   2010 Post Season Catch #################
#fram.db.name <- "./fram db/FramVS2-PSC-Coho-Backwards-2012 Feb 12.mdb"
#fram.run.name <- "bk10PSCFeb14"
#run.year <- 2010


source('topics.r')
##source('stm.r')

args <- commandArgs(trailingOnly = TRUE)

dtm_path <- args[1]

if( ! grepl( '.rdata', dtm_path ) ) {
   dtm_path <- paste( dtm, 'dtm.rdata', sep='' )
}

load( dtm_path )

k <- as.integer( args[2] )

model <- create_model( dtm , k )

path <- paste( dtm_path , 'topic-', args[2], '.rdata' , sep = '' )

print( path )

save( model , file = path )

Fix path handling when not appending dtm...

source('topics.r')
##source('stm.r')

args <- commandArgs(trailingOnly = TRUE)

dtm_path <- args[1]

if( ! grepl( '.rdata', dtm_path ) ) {
   dtm_path <- paste( dtm_path, '/dtm.rdata', sep='' )
}

load( dtm_path )

k <- as.integer( args[2] )

model <- create_model( dtm , k )

path <- paste( dtm_path , 'topic-', args[2], '.rdata' , sep = '' )

print( path )

save( model , file = path )


Add script for plotting number of quadwords.

library("ggplot2")
setwd("~/dev/BitFunnel/src/Scripts")

png(filename="qwords.png",width=1600,height=1200)
queries <- read.csv(header=TRUE, file="/tmp/QueryPipelineStatistics.csv")
pos = seq(1, length(queries$quadwords))
df <- data.frame(pos, queries$quadwords)

ggplot(df, aes(x=pos,y=queries.quadwords)) +
theme_bw() +
geom_point(alpha=1/100) +
theme(axis.text = element_text(size=40),
      axis.title = element_text(size=40),
      legend.title=element_text(size=40),
      legend.text=element_text(size=40))
dev.off()

png(filename="qwords-solid.png",width=1600,height=1200)
ggplot(df, aes(x=pos,y=queries.quadwords)) +
theme_bw() +
geom_point(alpha=1) +
theme(axis.text = element_text(size=40),
      axis.title = element_text(size=40),
      legend.title=element_text(size=40),
      legend.text=element_text(size=40))
dev.off()


Add R script to plot RMSE. (Not yet working).

#!/usr/bin/env Rscript
args <- commandArgs(trailingOnly = TRUE)
file = args[1]

x <- read.csv(file, header=T)

library(ggplot2)
library(methods)

ggplot(x, aes(x = filename, fill = variable)) +
  geom_bar(stat="identity", ymin=0, aes(y=value, ymax=value), position="dodge") +
  geom_text(aes(x=filename, y=value, ymax=value, label=value,
                hjust=ifelse(sign(value)>0, 1, 0)),
            position = position_dodge(width=1)) +
  scale_y_continuous(labels = percent_format()) +
  coord_flip()

print()


# Predict the worst FVC measurements that a person will have base on
# demographic data.

library(sclero)
library(ggplot2)
library(reshape2)
library(plyr)

data(patient)
data(pft)

patient.worst <- ddply(pft, ~ patient.id + test.type, summarize, lifetime.worst = lifetime_worst(perc.of.predicted))
patient.worst <- dcast(patient.worst, patient.id ~ test.type, value.var = "lifetime.worst")

data <- merge(patient, patient.worst, by = "patient.id")

years_bw <- function(d1, d2) as.numeric(as.Date(d2) - as.Date(d1)) / 365

data <- transform(data, age = years_bw(date.of.birth, date.of.first.symptom))

sex.str <- c("male", "female")
aa.str <- c("no", "yes")

data <- transform(data, african.american = ifelse(race1 == 2, 1, 0))
data <- data[, c("patient.id", "age", "sex", "african.american", "fvc", "dlco")]
data <- na.omit(data)
data <- transform(data,
                  sex = as.factor(sex.str[sex + 1]),
                  african.american = as.factor(aa.str[african.american + 1]))

lm.fit <- lm(fvc ~ 1 + age + sex + african.american, data = data)


pbcopy = function (object, transform = NULL) {
    if (! is.null(transform))
        object = capture.output(transform(object))
    on.exit(close(f))
    f = pipe('pbcopy', 'w')
    writeLines(object, f)
}

pbpaste = function () {
    on.exit(close(f))
    f = pipe('pbpaste')
    readLines(f)
}


#!/usr/bin/Rscript

library(rphast)

tree <- read.tree('mcc.nh')
load('regDNA.RData')

locMsa <- msa(regDNA, names(regDNA), alphabet='ACTGN')
treeChar <- write.tree(tree)

mod <- phyloFit(locMsa, tree=treeChar, subst.mod='UNREST', no.opt='branches', ninf.sites=1)
mod$tree <- treeChar

mod2 <- phyloFit(locMsa, init.mod=mod, subst.mod='UNREST', no.opt='branches', ninf.sites=1)

pdf('unrest-rate-estimates.pdf')
plot(mod2)
dev.off()


Add script that summarizes experimental record

#!/usr/bin/env Rscript

queryCol = "query"
timeoutCol = "didTimeOut"

args <- commandArgs(TRUE)
requiredArgCount = 1
if(length(args) < requiredArgCount) {
	cat(sprintf("Expected %d arguments!\n", requiredArgCount))
	q(status=1)
}

X <- read.csv(args[1])

nRecords = length(X[[queryCol]])
nTimeouts = sum(X[[timeoutCol]])

cat(args[1], "\n")
cat(sprintf("#records: %d, #timeouts: %d, %%timeouts: %f\n", nRecords, nTimeouts, nTimeouts / nRecords * 100))


options(stringsAsFactors = FALSE)
library(plyr)
library(reshape2)

source("inst/R/sclerodata-path.r")
clinic.csv <- file.path(sclerodata.path, "tVisit.csv")
clinic.rdata <- file.path("data", "clinic.rdata")

clinic.raw <- read.csv(clinic.csv)

keep.columns <- c(
    "PtID", "Visit.Date", "Total.Skin.Score",
    "Skin.Sev.Score", "RP.Sev.Score", "GI.Sev.Score"
    )

clinic.raw <- clinic.raw[, keep.columns]

new.names <- c(
    "patient.id", "date", "total.skin",
    "skin.severity", "rp.severity", "gi.severity"
    )

names(clinic.raw) <- new.names

clinic <- melt(clinic.raw, measure.vars = c("total.skin", "skin.severity", "rp.severity", "gi.severity"),
               variable.name = "score.type", value.name = "score.value")

clinic <- arrange(clinic, patient.id, date, score.type)

save(clinic, file = clinic.rdata)


realpath = function (path) {
    if (.Platform$OS.type == 'unix')
        system(paste('realpath -m -s', shQuote(path.expand(path))), intern = TRUE)
    else
        normalizePath(path, mustWork = FALSE)
}

expect_paths_equal = function (actual, expected) {
    actual_norm = realpath(merge_path(actual))
    expected_norm = realpath(expected)
    expect_equal(actual_norm, expected_norm, label = deparse(substitute(actual)))
}

expect_is_cwd = function (actual)
    eval.parent(bquote(expect_paths_equal(.(actual), '.')))


x <- 1
y <- 1

theta <- 2/5
n <- 100
plot(c(-2,2),c(-2,2),type="n")
abline(v=0)
abline(h=0)

for(i in 0:n) {
  angle <- complex(real = cos(2*pi*theta*i), imaginary = sin(2*pi*theta*i))
  point <- complex(real = x, imaginary = y)
  point0 <- point * angle
  x1 <- Re(point0)
  y1 <- Im(point0)
  segments(x1,y1,-y1,x1,col=(i %% 5)+1,lwd = 2)
  segments(-y1,x1,-x1,-y1,col=(i %% 5)+1,lwd = 2)
  segments(-x1,-y1,y1,-x1,col=(i %% 5)+1,lwd = 2)
  segments(y1,-x1,x1,y1,col=(i %% 5)+1,lwd = 2)
}


#!/usr/bin/env Rscript

source("/tmp/class-libs.R")

class_name = "Stat 131a"
class_libs = c(
    "learnr", "0.9.2"
)

class_libs_install_version(class_name, class_libs)

devtools::install_github('DataComputing/DataComputing', ref='d5cebba', upgrade_dependencies = FALSE)


Add script to tidy heatmap.r nested structure into dfs by station

# heatmap-tidy.r: take the nested list structure of station_set
# (from heatmap.r) and convert it to a tidy dataframe of all obs and
# all stations. then chop it up by station nand year and export to csv
# so that main_static.r can use it.

library(tidyverse)
library(purrr)
select = dplyr::select
filter = dplyr::filter

# skip this if running directly after heatmap.r!
station_set = readRDS('heatmap-data.rds')

# get the nested list structure into a nested data frame
tidy_data =
  station_set %>%
  {
    data_frame(
    id   = map_chr(., pluck, 'id'),
    # name = map_chr(., pluck, 'name'),
    percentiles =
      map(., pluck, 'percentileHeatmap_array') %>%
      map(as_tibble) %>%
      map(set_names, 1:12) %>%
      map(rownames_to_column, 'day') %>%
      map(gather, key = 'month', value = 'percentile', -day),
    categories =
      map(., pluck, 'categoryHeatmap_array') %>%
      map(as_tibble) %>%
      map(set_names, 1:12) %>%
      map(rownames_to_column, 'day') %>%
      map(gather, key = 'month', value = 'category', -day))
  } %>%
  # now unnest, drop the duplicated columns and then nest by station
  unnest() %>%
  mutate(date = as.Date(paste('2018', month, day, sep = '-'))) %>%
  select(id, date, percentile) %>%
  nest(-id)

# write 'em out to disk
walk2(tidy_data$data, tidy_data$id,
  ~ write_csv(.x, paste0('databackup/', .y, '-2018.csv')))


library("ggplot2")
setwd("~/dev/BitFunnel/src/Scripts")
df <- read.csv(header=TRUE, file="history.csv")

png(filename="bitfunnel-progress.png",width=1600,height=1200)
df$date <- as.Date(df$date)
ggplot(df, aes(date, wc)) + geom_line(size=3) + scale_x_date() +
    labs(x = "Date", y = "Lines of code") +
    stat_smooth(se=FALSE) +
    theme_bw() +
    theme(axis.text = element_text(size=40),
          axis.title = element_text(size=40))
dev.off()

endDate <- as.Date("2017/07/01")
png(filename="bitfunnel-extrapolation.png",width=1600,height=1200)
ggplot(df, aes(date, wc)) + geom_line(size=3) +
    scale_x_date(limits=c(df$date[1],endDate)) +
    labs(x = "Date", y = "Lines of code") +
    stat_smooth(method="lm",fullrange=TRUE,se=FALSE) +
    geom_hline(yintercept=72000,color="green") +
    geom_hline(yintercept=144000,color="red") +
    theme_bw() +
    theme(axis.text = element_text(size=40),
          axis.title = element_text(size=40))
dev.off()


LoadVideoFeatures <- function(csv_file){
  options( stringsAsFactors=F )
  df = read.csv(csv_file)
  data_to_numeric_list = function(col) lapply(col, function(x) as.numeric(strsplit(x, "\\s+")[[1]]))
  return(apply(df, 2, data_to_numeric_list))
}


Add R script for plotting smoothed Precision/Recall curves.

#!/usr/bin/env Rscript

library(data.table)
library(ggplot2)

irPlot <- function(df, type="Precision", n=10, selection="") {

    # filter out NaN
    df <- df[complete.cases(df), ]

    # add grp for indexing
    df$grp <- paste(df$Recommender,df$KTL, df$Similarity)
    # set index
    df <- data.table(df, key='grp')

    # filter out topN values
    top <- df[,sum(get(type)), by=grp]
    top <- top[order(V1, decreasing=T)]
    top <- head(top, n)
    top <- df[top$grp]

    if(nchar(selection) > 0) {
        data <- df[c(selection)]
    }
    else {
        data <- df[top]
    }

    plot <- ggplot(data, aes_string(x="TopN",y=type, group="grp", shape="grp", color="grp"))
    plot <- plot + geom_point()
    plot <- plot + geom_line(size=0.2)
    plot <- plot + stat_smooth(method = "loess",formula = y ~ x, size = 1, se=T, level=0.95)
    plot <- plot + labs(shape="Configurations")

    return(plot)
}

args <- commandArgs(trailingOnly = TRUE)

# read file
# 2013-11-17-163634-VTT36k-memory-based-unclustered.csv
df <- fread("~/projects/goldfish/results/2013-11-17-163634-VTT36k-memory-based-unclustered.csv")
#df <- fread("~/projects/goldfish/results/2013-11-20-010054-VTT36k-memory-based-clustered.csv")
#df <- fread("~/projects/goldfish/results/2013-11-27-154121-VTT36k.csv")

quartz()

irPlot(df, type="Precision")


# Polynomial regression for machine learning.
#
# polynomial regression is a form of regression analysis in which the
# relationship between the independent variable x and the dependent variable y is
# modelled as an nth degree polynomial in x. Polynomial regression fits a
# nonlinear relationship between the value of x and the corresponding conditional
# mean of y, denoted E(y |x)Although polynomial regression fits a nonlinear model
# to the data, as a statistical estimation problem it is linear, in the sense
# that the regression function E(y | x) is linear in the unknown parameters that
# are estimated from the data. For this reason, polynomial regression is
# considered to be a special case of multiple linear regression.

# Importing the data set
dataset = read.csv('Position_Salaries.csv')
dataset = dataset[2:3]

# no need to split
# # Splitting the Dataset into a Training set and a Test set
# # install.packages('caTools')
# # library(caTools)
# set.seed(123) # choose random number, only same number for debugging
# split = sample.split(dataset$Salary, SplitRatio = 0.8)
# training_set = subset(dataset, split==TRUE)
# test_set = subset(dataset, split==FALSE)

# Fit Polynomial regression to the dataset
dataset$Level2 = dataset$Level^2
dataset$Level3 = dataset$Level^3
dataset$Level4 = dataset$Level^4
dataset$Level5 = dataset$Level^5
poly_reg = lm(formula = Salary ~ ., data = dataset)
summary(poly_reg)

# Visualize graphic result of Polynomial regression
ggplot() +
  geom_point(aes(x = dataset$Level, y = dataset$Salary),
             colour='red') +
  geom_line(aes(x = dataset$Level, y = predict(poly_reg, newdata = dataset)),
            colour='blue') +
  xlab('Level') + ylab('Salary')

# Predict a new result with Polynomial regression
y_pred = predict(poly_reg, newdata = data.frame(Level = 6.5,
                                                Level2 = 6.5^2,
                                                Level3 = 6.5^3,
                                                Level4 = 6.5^4,
                                                Level5 = 6.5^5))


Add a testing file for wrapping variadic functions

REBOL []

recycle/torture

libc: switch fourth system/version [
	3 [
		make library! %msvcrt.dll
	]
	4 [
		make library! %libc.so.6
	]
]

printf: make routine! [
	[
		"An example of wrapping variadic functions"
		fmt [pointer] "fixed"
		... "variadic"
		return: [int32]
	]
	libc "printf"
]

sprintf: make routine! [
	[
		"An example of wrapping variadic functions"
		buf [pointer] "destination buffer, must be big enough"
		fmt [pointer] "fixed"
		... "variadic"
		return: [int32]
	]
	libc "sprintf"
]

i: 1000
j: 0.0
printf reduce [
	join "i: %d, %f" newline
	i [int32]
	j [float]
]

printf compose [
	"hello %p%c"
	;10.0
	"ffi" [pointer]
	;"ffi"
	(to integer! newline) [int8]
]

printf compose [
	"hello %s%c"
	"world" [pointer]
	(to integer! newline) [int8]
]

printf compose [
	"hello %s%c"
	"ffi" [pointer]
	(to integer! newline) [int8]
]

h: make struct! [
	uint8 [128] a
]
len: sprintf reduce [
	addr-of h
	join "hello %s" newline
	"world" [pointer]
]
prin ["h:" copy/part to string! values-of h len]


input <- file('stdin', 'r')

a <- as.integer(readLines(input, n=1))
b <- as.integer(readLines(input, n=1))

write(paste("PROD =", a * b), '')


# Decision Tree regression for machine learning.
#
# Decision tree builds regression or classification models in the form of a tree
# structure. It brakes down a dataset into smaller and smaller subsets while at
# the same time an associated decision tree is incrementally developed. The final
# result is a tree with decision nodes and leaf nodes.

# Importing the data set
dataset = read.csv('Position_Salaries.csv')
dataset = dataset[2:3]

# Splitting the Dataset into a Training set and a Test set
# install.packages('caTools')
# library(caTools)
# set.seed(123) # choose random number, only same number for debugging
# split = sample.split(dataset$Salary, SplitRatio = 0.8)
# training_set = subset(dataset, split==TRUE)
# test_set = subset(dataset, split==FALSE)

# Feature Scaling
# training_set = scale(training_set)
# test_set = scale(test_set)

# Fit Decision Tree Regression to the dataset
# install.packages('rpart')
# library(rpart)
regressor = rpart(formula = dataset$Salary ~ .,
                  data = dataset,
                  control = rpart.control(minsplit = 1))

# Predict a new result with Decision Tree Regression model
y_pred = predict(regressor, newdata = data.frame(Level = 6.5))

# HIGH RESOLUTION: Visualize graphic result of the Decision Tree Regression model
# install.packages('ggplot2')
# library(ggplot2)
x_grid = seq(min(dataset$Level), max(dataset$Level), 0.01)
ggplot() +
  geom_point(aes(x = dataset$Level, y = dataset$Salary),
             colour='red') +
  geom_line(aes(x = x_grid, y = predict(regressor, newdata = data.frame(Level=x_grid))),
            colour='blue') +
  ggtitle('Truth or Bluff (Decision Tree Regression)') +
  xlab('Level') + ylab('Salary')


Fix interactive legacy data.frame output in R

print.data.frame = function (x, ..., digits = NULL, quote = FALSE, right = TRUE,
                             row.names = TRUE)
{
    if (! isTRUE(all.equal(rownames(x), as.character(seq_len(nrow(x))))))
        x = tibble::rownames_to_column(x)
    print(dplyr::tbl_df(x))
}


# Support Vector regression for machine learning.
#
# Support Vector Machine can also be used as a regression method, maintaining all
# the main features that characterize the algorithm (maximal margin). The Support
# Vector Regression (SVR) uses the same principles as the SVM for classification,
# with only a few minor differences. First of all, because output is a real
# number it becomes very difficult to predict the information at hand, which has
# infinite possibilities. In the case of regression, a margin of tolerance is set
# in approximation to the SVM which would have already requested from the
# problem. But besides this fact, there is also a more complicated reason, the
# algorithm is more complicated therefore to be taken in consideration. However,
# the main idea is always the same: to minimize error, individualizing the
# hyperplane which maximizes the margin, keeping in mind that part of the error
# is tolerated.

# Importing the data set
dataset = read.csv('Position_Salaries.csv')
dataset = dataset[2:3]

# Splitting the Dataset into a Training set and a Test set
# install.packages('caTools')
# library(caTools)
# set.seed(123) # choose random number, only same number for debugging
# split = sample.split(dataset$Salary, SplitRatio = 0.8)
# training_set = subset(dataset, split==TRUE)
# test_set = subset(dataset, split==FALSE)

# Feature Scaling
# training_set = scale(training_set)
# test_set = scale(test_set)

# Fit SVR to the dataset
# install.packages('e1071')
# library(e1071)
regressor = svm(formula = dataset$Salary ~ .,
                data = dataset,
                type = 'eps-regression')

# Predict a new result with SVR model
y_pred = predict(regressor, newdata = data.frame(Level = 6.5))

# Visualize graphic result of the SVR model
# install.packages('ggplot2')
# library(ggplot2)
ggplot() +
  geom_point(aes(x = dataset$Level, y = dataset$Salary),
             colour='red') +
  geom_line(aes(x = dataset$Level, y = predict(regressor, newdata = dataset)),
            colour='blue') +
  ggtitle('Truth or Bluff (SVR)') +
  xlab('Level') + ylab('Salary')


Create script to repair (as best as possible) faulty encodings

# This script repairs broken encodings in the base database.  These
# flawed encodings are produced by non-utf changelogs and news files.
library(plyr)
source("db.r")

update_sql <- function(table, values) {
  id <- values$id
  changes <- paste(names(values[, -1]), "= ?", collapse = ", ")

  paste(
    "UPDATE ", table, "\n",
    "SET ", changes, "\n",
    "WHERE id = ", id, ";",
    sep=""
  )
}

update_row <- function(table, values) {
  message("Updating row: ", values$id)
  values <- as.data.frame(values)
  res <- dbSendPreparedQuery(db, update_sql(table, values),
    bind.data = values[, -1])
  dbClearResult(res)
}

insert_version <- function(pkg) {
  pkg$package_id <- package_id(pkg)
  pkg$created_at <- date()
}

fix_table <- function(name) {

  fixup <- function(x) ifelse(is.na(x), NA, iconv(x, sub = "?"))

  df <- dbReadTable(db, name)
  char <- sapply(df, is.character)
  df[char] <- lapply(df[char], fixup)

  a_ply(df, 1, update_row, table = name)
}

# to text: description, license, depends, suggests, author


Create a quick analysis file to load and peak at FRC data

#Install dependencies
install.packages("reshape2")
install.package("ggplot2")

#Requires
require("reshape2")
require("ggplot2")

#Import the data
teams <- read.csv("./the-blue-alliance-data-master/the-blue-alliance-data-master/teams/teams.csv", header=FALSE)
names(teams) <- c("number", "name", "sponsors", "location", "website", "rookie_year")

#Let's parese the location further down into state
teams <- cbind(teams,colsplit(teams$location, pattern = ",", names = c("city","state","country")))

#Let's take a look ate how many teams join each year
png("new-teams-by-year.png")
hist(teams$rookie_year)
dev.off()

#Let's look at how many teams are in each sate
#TODO
png("team-count-by-state.png")
hist(sum(teams$location))
plot(table(teams$state))
dev.off()


Add faster implementation of frequent_words

source("computing_frequencies.r")
source("number_to_pattern.r")

faster_frequent_words <- function(text, k) {
  frequent_patterns <- {}
  frequency_array <- computing_frequencies(text, k)
  max_count <- max(frequency_array)
  for (i in 0:(4^k-1)) {
    if (frequency_array[i + 1] == max_count) {
      pattern <- number_to_pattern(i, k)
      frequent_patterns <- append(frequent_patterns, pattern)
    }
  }
  return(frequent_patterns)
}

message("Text")
text <- scan("stdin", what="character", nlines=1)
message("K")
k <- scan("stdin", nlines=1)

print(faster_frequent_words(text, k))


input <- file('stdin', 'r')

a <- readLines(input, n=1)
b <- as.integer(readLines(input, n=1))
c <- as.double(readLines(input, n=1))

write(sprintf("TOTAL = R$ %.2f", b + (c * 0.15)), "")


# Introduction to data structures in R

# atomic vector
# -------------
v <- c(1,2.0,3.5)
v1 <- c('foo', 'bar')

# always flat
c(1, c(2, c(3,4)))

# homogenous -- all elements need to be of one type
# values will be coerced to more flexible type
c("a", 1)

# list
# ----
# heterogenous
x <- list(1:3, "a", c(TRUE, FALSE, TRUE), c(2.3, 5.9))
# list can contain lists
y <- list(list(1,2), c(3,4))

# factors
# -------
# vector containing only predefined values, used for categorical data
f <- factor(c("a", "b", "b", "a"))
f[1]
f[1] <- "c"
# arithmetics on factors usually does not make sense
f[1] + f[2]
# but
c(f)
# use case
sex_char <- c("m", "m", "m")
sex_factor <- factor(sex_char, levels = c("m", "f"))

# be carefull !!
# most data loading functions in R automatically convert character vectors to factors

# matrices and arrays
# -------------------

# Two scalar arguments to specify rows and columns
a <- matrix(1:6, ncol = 3, nrow = 2)
# One vector argument to describe all dimensions
b <- array(1:12, c(2, 3, 2))

# You can also modify an object in place by setting dim()
c <- 1:6
dim(c) <- c(3, 2)
dim(c) <- c(2, 3)
length(c)
ncol(c)
nrow(c)
dim(c)

# dataframes
# ----------
# for storing and easy maniputation of tabular data
df <- data.frame(x = 1:3, y = c("a", "b", "c"))
names(df)
colnames(df)
rownames(df)
length(df)
nrow(df)
ncol(df)

# merging dataframes together
cbind(df, data.frame(z = 3:1))
rbind(df, data.frame(x = 10, y = "z"))


#!/usr/bin/env Rscript
# From https://github.com/berkeley-dsep-infra/datahub/issues/814

devtools::install_github('cran/stargazer', ref='5.2.2', upgrade_dependencies=FALSE)
devtools::install_github('cran/lm.beta', ref='1.5-1', upgrade_dependencies=FALSE)
devtools::install_github('cran/multcomp', ref='1.4-8', upgrade_dependencies=FALSE)
devtools::install_github('cran/lfe', ref='2.8-2', upgrade_dependencies=FALSE)


input <- file('stdin', 'r')

a <- as.integer(readLines(input, n=1))
b <- as.integer(readLines(input, n=1))

write(paste("SOMA =", a + b), '')


Add an R script to fit the model, and compare with survival::clogit estimates

## example conditional logistic regression using Stan
## David C Muller

library(survival)
library(rstan)

set.seed(77834)

## use the infertility data from the survival package
datlist <- list(N=nrow(infert),
                n_grp=max(infert[, "stratum"]),
                n_coef=2,
                x=infert[,c("spontaneous", "induced")],
                y=infert[, "case"],
                grp=infert[, "stratum"])

## fit using default parameters
clogit_stan <- stan("clogit.stan", data=datlist)
clogit_stan

## fit using survival::clogit for comparison
clogit_survival <- clogit(case ~ spontaneous + induced + strata(stratum),
                          data=infert)
summary(clogit_survival)


# Multiple linear regression for machine learning.
#
# A linear regression model that contains more than one predictor variable is
# called a multiple linear regression model. It is basically the same as Simple
# Linear regression, but with more predictor variables (features). The idea is
# that linearly related predictor variables can approximate the labels with a
# 'best fitted' hyperplane or surface.

# Importing the data set
dataset = read.csv('50_Startups.csv')

# Encode categorical data and make them into numeric categories.
dataset$State = factor(dataset$State,
                       levels = c('New York', 'California', 'Florida'),
                       labels = c(1,2,3))

# Splitting the Dataset into a Training set and a Test set
# install.packages('caTools')
# library(caTools)
set.seed(123) # choose random number, only same number for debugging
split = sample.split(dataset$Profit, SplitRatio = 0.8)
training_set = subset(dataset, split==TRUE)
test_set = subset(dataset, split==FALSE)

# Don't need Feature Scaling

# Fit the training set with the mutliple linear regression model
regressor = lm(formula = Profit ~ ., data = training_set)
summary(regressor)
# And from it we can see
regressor = lm(formula = Profit ~ R.D.Spend, data = training_set)

# Predict Test set results
label_pred = predict(regressor, newdata = test_set)


#!/usr/bin/env Rscript

source("/tmp/class-libs.R")

class_name = "2020 Spring Env Econ C118"
class_libs = c(
  "margins", "0.3.23"
)
class_libs_install_version(class_name, class_libs)


input <- file('stdin', 'r')

a <- as.integer(readLines(input, n=1))
b <- as.integer(readLines(input, n=1))

soma = a + b

write(paste("X =", soma), '')


REBOL []

msvcrt: make library! %msvcrt.dll
getdrives: make routine! compose/deep [
	[return: [uint32]]
	(msvcrt) "_getdrives"
]

maps: getdrives
i: 0
while [i < 26] [
	unless zero? maps and shift 1 i [
		print rejoin [to char! (to integer! #"A") + i ":"]
	]
	++ i
]
close msvcrt


context("Converting rectypes")

test_that("Converting rectypes works.",
          expect_equal(
            ripums:::convert_rectype(
              c(`1` = "H", `2` = "P", `3` = "I"),
              c(1, 3, 3, 2, 1)
            ),
            c("H", "I", "I", "P", "H")
          )
)


input <- file('stdin', 'r')

a <- as.double(readLines(input, n=1))
b <- as.double(readLines(input, n=1))
c <- as.double(readLines(input, n=1))

result = sprintf("MEDIA = %.1f", (a * 2.0 + b * 3.0 + c * 5.0) / 10.0)
write(result, "")


#!/usr/bin/Rscript
model <- read("model.txt")
layer.size <- model$layer.size
neuron.size <- model$neuron.size
synapse <- model.synapse

pf.in <- file("test-images", "rb")
pf.out <- file("test-labels", "rb")

#check magic
magic <-readBin(pf.in, integer(), 1, endian = "big")
stopifnot(magic == 2051)
magic <-readBin(pf.out, integer(), 1, endian = "big")
stopifnot(magic == 2049)

#read and check train data numbers
n <- readBin(pf.in, integer(), 1, endian = "big")[1]
stopifnot(n == readBin(pf.out, integer(), 1, endian = "big"))

# read number of rows and columns
m <- readBin(pf.in, integer(), 1, endian = "big")[1] * readBin(pf.in, integer(), 1, endian = "big")[1]

# number of right answers
right <- 0

for (ii in 1:n) {
  vec.in <- readBin(pf.in, integer(), n = m, size = 1)
  vec.in <- (vec.in + 256) %% 256 / 256.0
  vec.in <- c(vec.in, 1)
  ac <- list(t(as.matrix(vec.in)))
  for (i in 2:layer.size) {
    ac.next <- ac[[i - 1]] %*% synapse[[i - 1]]
    #active function : x > 0 ? x : 0
    rectifier <- function(x) { (abs(x) + x) * 0.5}
    ac.next <- rectifier(ac.next)
    ac.next[neuron.size[i]] <- 1
    ac <- c(ac, list(ac.next))
  }

  #do softmax for output layer
  result <- exp(ac[[layer.size]][1:10])
  normalizer <- function(x) { x / sum(x) }
  result <- normalizer(result)

  class <- 1
  for (i in 2:10) {
      if (result[class] < result[i]) class <- i
  }

  label <- readBin(pf.out, integer(), size = 1) + 1
  if (class == label) right <- right + 1
}

print(right)


context("fake")

# We want to make sure these installed.
library(lubridate)
library(stringr)

test_that("returns 3", {
  expect_equal(3, three())
})

test_that("is_three recognizes 3", {
  expect_that(is_three(3), is_true())
  expect_that(is_three(5), not(is_true()))
  expect_that(is_three(5), is_false())
})

Remove a currently broken test.

context("fake")

test_that("returns 3", {
  expect_equal(3, three())
})

test_that("is_three recognizes 3", {
  expect_that(is_three(3), is_true())
  expect_that(is_three(5), not(is_true()))
  expect_that(is_three(5), is_false())
})


# Random Forest Regression for machine learning.
#
# Random forest algorithm is a supervised classification algorithm. As the name
# suggest, this algorithm creates the forest with a number of decision trees.
#
# In general, the more trees in the forest the more robust the forest looks like.
# In the same way in the random forest classifier, the higher the number of trees
# in the forest gives the high accuracy results.
# Importing the data set
dataset = read.csv('Position_Salaries.csv')
dataset = dataset[2:3]

# Fit Random Forest Regression to the dataset
# install.packages("randomForest")
# library(randomForest)
set.seed(1234) # For debuging
regressor = randomForest(x = dataset[1], y = dataset$Salary,
                         ntree = 100)

# Predict a new result with Random Forest Regression model
y_pred = predict(regressor, newdata = data.frame(Level = 6.5))

# HIGH RESOLUTION: Visualize graphic result of the Random Forest Regression model
# install.packages('ggplot2')
# library(ggplot2)
x_grid = seq(min(dataset$Level), max(dataset$Level), 0.01)
ggplot() +
  geom_point(aes(x = dataset$Level, y = dataset$Salary),
             colour='red') +
  geom_line(aes(x = x_grid, y = predict(regressor, newdata = data.frame(Level=x_grid))),
            colour='blue') +
  ggtitle('Truth or Bluff (Random Forest Regression)') +
  xlab('Level') + ylab('Salary')


Implement doc parsing and display for modules

parse_documentation = function (module) {
    module_path = module_path(module)
    parsed = list(env = module,
                  blocks = roxygen2:::parse_file(module_path, module))
    rdfiles = roxygen2:::roc_process(rd_roclet(), parsed, dirname(module_path))
    rdcontents = lapply(rdfiles, roxygen2:::format.rd_file, wrap = FALSE)
    setNames(rdcontents, sub('\\.Rd$', '', names(rdcontents)))
}

module_help = function (topic, verbose = getOption('verbose'),
                        help_type = getOption('help_type', 'text')) {
    if (help_type != 'text')
        warning('Only help_type == ', sQuote('text'), ' supported for now.')

    topic = substitute(topic)

    if (! is_module_help_topic(topic, parent.frame()))
        stop(sQuote(deparse(topic)), ' is not a valid module help topic',
             call. = FALSE)

    module_name = as.character(topic[[2]])
    module = get(module_name, parent.frame())
    object = as.character(topic[[3]])

    doc = attr(module, 'doc')[[object]]
    if (is.null(doc))
        stop('No documentation available for ', sQuote(object),
             ' in module ', sQuote(module_name), call. = FALSE)

    rd = tools::parse_Rd(textConnection(doc))

    # Taken from utils:::print.help_files_with_topic
    temp = tools::Rd2txt(rd, out = tempfile('Rtxt'), package = module_name)

    # Patch header line.
    doc_text = readLines(temp)
    doc_text[1] = sub('package:', ' module:', doc_text[1])
    writeLines(doc_text, temp)

    file.show(temp,
              title = gettextf('R Help on %s', sQuote(as.character(topic))),
              delete.file = TRUE)
}

is_module_help_topic = function (topic, parent)
    is.call(topic) && topic[[1]] == '$' &&
    exists(as.character(topic[[2]]), parent) &&
    ! is.null(module_name(get(as.character(topic[[2]]), parent)))

`?` = function (e1, e2) {
    topic = substitute(e1)
    if (missing(e2) && is_module_help_topic(topic, parent.frame()))
        eval(call('module_help', topic))
    else
        eval(`[[<-`(match.call(), 1, utils::`?`))
}

help = function (topic, ...) {
    topic = substitute(topic)
    delegate = if (is_module_help_topic(topic, parent.frame()))
        module_help
    else
        utils::help
    eval(`[[<-`(match.call(), 1, delegate))
}


REBOL [
	System: "REBOL [R3] Language Interpreter and Run-time Environment"
	Title: "REBOL 3 Mezzanine: Control"
	Rights: {
		Copyright 2012 REBOL Technologies
		REBOL is a trademark of REBOL Technologies
	}
	License: {
		Licensed under the Apache License, Version 2.0
		See: http://www.apache.org/licenses/LICENSE-2.0
	}
]

launch: func [
	{Runs a script as a separate process; return immediately.}
	script [file! string! none!] "The name of the script"
	/args arg [string! block! none!] "Arguments to the script"
	/wait "Wait for the process to terminate"
	/local exe
][
	if file? script [script: to-local-file clean-path script]
	exe: to-local-file system/options/boot

	; Quote everything, just in case it has spaces:
	args: to-string reduce [{"} exe {" "} script {" }]
	if arg [append args arg]
	either wait [call/wait args] [call args]
]

wrap: func [
	"Evaluates a block, wrapping all set-words as locals."
	body [block!] "Block to evaluate"
][
	do bind/copy/set body make object! 0
]

Fix LAUNCH to properly work with argv-based CALL

REBOL [
	System: "REBOL [R3] Language Interpreter and Run-time Environment"
	Title: "REBOL 3 Mezzanine: Control"
	Rights: {
		Copyright 2012 REBOL Technologies
		REBOL is a trademark of REBOL Technologies
	}
	License: {
		Licensed under the Apache License, Version 2.0
		See: http://www.apache.org/licenses/LICENSE-2.0
	}
]

launch: func [
	{Runs a script as a separate process; return immediately.}
	script [file! string! none!] "The name of the script"
	/args arg [string! block! none!] "Arguments to the script"
	/wait "Wait for the process to terminate"
][
	if file? script [script: to-local-file clean-path script]
	args: reduce [to-local-file system/options/boot script]
	if arg [append args arg]
	either wait [call/wait args] [call args]
]

wrap: func [
	"Evaluates a block, wrapping all set-words as locals."
	body [block!] "Block to evaluate"
][
	do bind/copy/set body make object! 0
]


context('Find module path relative files')

test_that('module_file works in global namespace', {
    expect_that(module_file(), equals(getwd()))
    expect_true(nchar(module_file('run-all.r')) > 0)
    throws_error(module_file('XXX-does-not-exist', mustWork = TRUE),
                 'no file found')
})

test_that('module_file works for module', {
    a = import('a')
    expect_true(grepl('/b$', module_file('b', module = a)))
    expect_true(grepl('/c\\.r$', module_file('c.r', module = a)))
    expect_that(length(module_file(c('b', 'c.r'), module = a)), equals(2))
})


#!/usr/bin/Rscript

library(ape)

tree <- read.nexus('mcc.tree')
nms <- tree$tip.label
abs <- sapply(strsplit(nms, '_'), '[[', 1)
ind <- match(abs, state.abb)
reg <- state.region[ind]

regDNA <- factor(reg,
                 levels=c("Northeast", "South", "North Central", "West"),
                 labels=c('A', 'T', 'C', 'G'))
regDNA <- as.character(regDNA)
regDNA[is.na(regDNA)] <- 'N'
regDNA[abs=='Mexico'] <- 'A'
## let mexico have northeast label since no northeast sequences
regDNA <- as.list(regDNA)
names(regDNA) <- nms
write.dna(regDNA, file='loc.aln', format='fasta')
write.tree(tree, file='mcc.nh')


Create R script for plotting results related to the master thesis.

#!/usr/bin/env Rscript

source('functions.r')

results_path <- '~/Projects/goldfish/results'
graphs_path <- '~/Projects/goldfish/graphs'

# list of data set files to plot
vtt <- list()

vtt$cluster7 <- "2013-11-28-142459-VTT36k-7 clusters.csv"

vtt <- lapply(vtt, get_dataset, folder=results_path)

#Threshold 0.4 TanimotoCoefficient
#irPlot(vtt$cluster7, "Precision_Recall", 10, 1)
#irPlot(vtt$cluster7, "Recall", 10)
#irPlot(vtt$cluster7, "Precision", 10)
#irPlot(vtt$unclustered, "Precision_Recall", 0)
#irPlot(vtt$unclustered, "Recall", 10)

irPlot(vtt$cluster7, type="Precision_Recall", k=10, legendPos='bottomLeft', save_path=graphs_path)

#comparePlot("Threshold 0.3 TanimotoCoefficient", "Precision_Recall", vtt)
#comparePlot("Threshold 0.35 TanimotoCoefficient", "Precision_Recall", vtt)
#comparePlot("2NN TanimotoCoefficient", "Precision_Recall", vtt)
#comparePlot("3NN TanimotoCoefficient", "Precision_Recall", vtt)


Install script for most common/useful packages

###############################################
### COMMONLY USED PACKAGES IN AIM R SCRIPTS ###
###############################################

#### DATA WRANGLING ####
install.packages(
  c(
    "dplyr", ## Notably useful for data frame manipulation with group_by(), summarize(), and mutate() and the piping operator %>%. Do you have a moment to hear the good news of our savior, piping?
    "tidyr", ## Lots of useful things, but specifically gather() and spread() for converting wide data frames into long ones and vice versa
    "stringr", ## All about character strings. Great for str_split() in particular, but the whole str_ family of functions are A+
    "RODBC", ## Allows R to pass SQL queries to Access databases and pull out the results
    "XLSX", ## Read in modern Excel workbooks and spreadsheets
    "broom" ## Get stats objects into tidy data frames. Not as common
  )
)

#### SPATIAL DATA ####
install.packages(
  c(
    "sp", ## Functions for manipulating spatial objects. If you want a Spatial _____ Data Frame, this is part of the deal
    "spsurvey", ## Contains plenty, but the most important to AIM is the GRTS function
    "rgeos", ## Additional spatial object functions
    "rgdal", ## Contains the mission critical readOGR() function that we read shapefiles in with
    "raster" ## Everything you didn't know you needed for dealing with rasters
  )
)

#### DATA VISUALIZATION ####
install.packages(
  c(
    "ggplot2", ## The go-to for figure generation. Most R-using scientists AND Nate Silver use it, so you should too
    "ggthemes", ## Quick themes to painlessly apply to figures from ggplot
    "ggmap", ## Mapping support for ggplot
  )
)

#### MISCELLANEOUS PACKAGES ####
install.packages("arcgisbinding") ## Young and finicky, but once you have it all installed (an ordeal) you should be able to read from and write to file geodatabases from R
install.packages("gridExtra") ## Lets you make grid objects that you can place ggplot figures into. May occasionally be preferable to faceting in ggplot, but rarely
install.packages("shiny") ## Required for working with Shiny tools in any form. Can be maddening
install.packages("purrr") ## Really, really useful for writing functions, particularly those that fail gracefully


input <- file('stdin', 'r')

a <- as.double(readLines(input, n=1))
b <- as.double(readLines(input, n=1))

result = sprintf("MEDIA = %.5f", (a * 3.5 + b * 7.5) / 11.0)
write(result, "")


# Regression template for machine learning.

# Importing the data set
dataset = read.csv('Position_Salaries.csv')
dataset = dataset[2:3]

# Splitting the Dataset into a Training set and a Test set
# install.packages('caTools')
# library(caTools)
set.seed(123) # choose random number, only same number for debugging
split = sample.split(dataset$Salary, SplitRatio = 0.8)
training_set = subset(dataset, split==TRUE)
test_set = subset(dataset, split==FALSE)

# Feature Scaling
training_set = scale(training_set)
test_set = scale(test_set)

# Fit regression to the dataset
# Create regressor

# Predict a new result with regression model
y_pred = predict(regressor, newdata = data.frame(Level = 6.5))

# Visualize graphic result of the regression model
# install.packages('ggplot2')
# library(ggplot2)
ggplot() +
  geom_point(aes(x = dataset$Level, y = dataset$Salary),
             colour='red') +
  geom_line(aes(x = dataset$Level, y = predict(regressor, newdata = dataset)),
            colour='blue') +
  ggtitle('Truth or Bluff (Regression Model)') +
  xlab('Level') + ylab('Salary')

# HIGH RESOLUTION: Visualize graphic result of the regression model
# install.packages('ggplot2')
# library(ggplot2)
x_grid = seq(min(dataset$Level), max(dataset$Level), 0.1)
ggplot() +
  geom_point(aes(x = dataset$Level, y = dataset$Salary),
             colour='red') +
  geom_line(aes(x = x_grid, y = predict(regressor, newdata = data.frame(Level=x_grid))),
            colour='blue') +
  ggtitle('Truth or Bluff (Regression Model)') +
  xlab('Level') + ylab('Salary')


Add (R) script to generate histograms of projects wto. number of commits

setwd("/home/brito/salt/JSRepairClass/input/applications/")
data <- read.csv("repositories_metrics.csv")
hist(data$TotalCommits, main = paste("Histogram for ", nrow(data), "node.js applications"), xlab = "Total number of commits")

setwd("/home/brito/salt/JSRepairClass/input/modules/")
data <- read.csv("repositories_metrics.csv")
hist(data$TotalCommits, main = paste("Histogram of", nrow(data), "top rated node.js modules (from npmjs.com)"), xlab = "Total number of commits")


Add test for bikalisting show_more button

*** Settings ***

Library         BuiltIn
Library         Selenium2Library  timeout=5  implicit_wait=0.2
Library         String
Resource        keywords.txt
Resource        plone/app/robotframework/selenium.robot
Library         Remote  ${PLONEURL}/RobotRemote
Variables       plone/app/testing/interfaces.py
Variables       bika/lims/tests/variables.py

Suite Setup     Start browser
Suite Teardown  Close All Browsers

Library          DebugLibrary

*** Variables ***

*** Test Cases ***

Test show_more button
    Enable autologin as  LabManager
    set autologin username  test_labmanager
    Create Object   clients  Client  c01   title=Client01
    Create Object   clients  Client  c02   title=Client02
    Create Object   clients  Client  c03   title=Client03
    Create Object   clients  Client  c04   title=Client04
    Create Object   clients  Client  c05   title=Client05
    Create Object   clients  Client  c06   title=Client06
    Create Object   clients  Client  c07   title=Client07
    Create Object   clients  Client  c08   title=Client08
    Create Object   clients  Client  c09   title=Client09
    Create Object   clients  Client  c10   title=Client10
    go to    ${PLONEURL}/clients?list_pagesize=5
    page should not contain      Client10
    click element      css=.bika_listing_show_more
    wait until page contains     Client10

*** Keywords ***

Start browser
    Open browser                        ${PLONEURL}  chrome
    Set selenium speed                  ${SELENIUM_SPEED}


Add tests for st2 api key load command which veriy the command is idempotent.

*** Variables ***

*** Test Cases ***
TEST:Verify load command works and is idempotent
    # Create two api keys
    ${result}=           Run  st2 apikey create
    Log To Console       \nCREATE APIKEY:\n
    Log To Console       \nRESULT:\n: ${result}
    Should Contain       ${result}  key
    Should Contain       ${result}  created_at

    ${result}=           Run  st2 apikey create
    Log To Console       \nCREATE APIKEY:\n
    Log To Console       \nRESULT:\n: ${result}
    Should Contain       ${result}  key
    Should Contain       ${result}  created_at

    # Dump the keys and load them twice - operation should be idempotent and existing keys should be updated
    ${result}=           Run  st2 apikey list -d --show-secrets -j > /tmp/apikeys.json

    ${result}=           Run  st2 apikey list | wc -l
    Set Suite Variable   ${APIKEY COUNT}  ${result}
    Log To Console       \nKEY LIST COUNT:\n
    Log To Console       \nRESULT:\n
    Log To Console       \n${result}

    ${result}=           Run  st2 apikey load /tmp/apikeys.json
    Log To Console       \nRUN st2 apikey load:\n
    Log To Console       \nRESULT:\n
    Log To Console       \n${result}

    # Verify count is correct / same after load
    ${result}=           Run  st2 apikey list | wc -l
    Should Be Equal As Integers  ${result}  ${APIKEY COUNT}

    ${result}=           Run  st2 apikey load /tmp/apikeys.json
    Log To Console       \nRUN st2 apikey load:\n
    Log To Console       \nRESULT:\n
    Log To Console       \n${result}

    # Verify count is correct / same after load
    ${result}=           Run  st2 apikey list | wc -l
    Should Be Equal As Integers  ${result}  ${APIKEY COUNT}

*** Settings ***
Library         Process
Library         String
Library         OperatingSystem
Resource        ../common/keywords.robot


*** Settings ***

Documentation  Test OpenBMC GUI "Server power operations" sub-menu of "Server control".

Resource        ../../lib/resource.robot

Suite Setup     Launch Browser And Login GUI
Suite Teardown  Close Browser
Test Setup      Test Setup Execution

*** Variables ***

*** Test Cases ***

Verify Existence Of All Sections In Server Power Operations Page
    [Documentation]  Verify existence of all sections in Server Power Operations page.
    [Tags]  Verify_Existence_Of_All_Sections_In_Server_Power_Operations_Page

    Page Should Contain  Current status
    Page Should Contain  Host OS boot settings
    Page Should Contain  Operations

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Click Element  ${xpath_control_menu}
    Click Element  ${xpath_server_power_operations_sub_menu}
    Wait Until Keyword Succeeds  30 sec  10 sec  Location Should Contain  server-power-operations

Add BMC GUI test cases for bmc power operation

*** Settings ***

Documentation  Test OpenBMC GUI "Server power operations" sub-menu of "Server control".

Resource        ../../lib/resource.robot

Suite Setup     Launch Browser And Login GUI
Suite Teardown  Close Browser
Test Setup      Test Setup Execution

*** Variables ***

${xpath_enable_onetime_boot_checkbox}      //*[contains(@class,'custom-checkbox')]
${xpath_boot_option_select}                //*[@id='boot-option']

*** Test Cases ***

Verify Existence Of All Input Boxes In Host Os Boot Settings
    [Documentation]  Verify existence of all input boxes in host os boot settings.
    [Tags]  Verify_Existence_Of_Input_Boxes_In_Host_Os_Boot_Settings

    Page Should Contain Element  ${xpath_enable_onetime_boot_checkbox}
    Page Should Contain Element  ${xpath_boot_option_select}

Verify Existence Of All Sections In Host Os Boot Settings
    [Documentation]  Verify existence of all sections in host os boot settings.
    [Tags]  Verify_Existence_Of_All_Sections_In_Host_Os_Boot_Settings

    Page Should Contain  Boot settings override
    Page Should Contain  TPM required policy

Verify Existence Of All Sections In Server Power Operations Page
    [Documentation]  Verify existence of all sections in Server Power Operations page.
    [Tags]  Verify_Existence_Of_All_Sections_In_Server_Power_Operations_Page

    Page Should Contain  Current status
    Page Should Contain  Host OS boot settings
    Page Should Contain  Operations

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Click Element  ${xpath_control_menu}
    Click Element  ${xpath_server_power_operations_sub_menu}
    Wait Until Keyword Succeeds  30 sec  10 sec  Location Should Contain  server-power-operations


*** Settings ***
Library    WhiteLibrary
Library    Process
Test Setup    Start Test Application
Test Teardown    Terminate Process    ${handle}    kill=true
Resource    resource.robot

*** Test Cases ***
Attach To A Running Application By Name
    Attach Application By Name    ${TEST APPLICATION NAME}
    Application Should Be Attached

Attach To A Running Application By Id
    Attach Application By Id    ${test application id}
    Application Should Be Attached

Attach Window Successfully
    Attach Application By Name    ${TEST APPLICATION NAME}
    Attach Main Window

Attach Nonexisting Window
    Attach Application By Id    ${test application id}
    Set White Find Window Timeout    2 s
    ${status}    ${error_msg}    Run Keyword And Ignore Error    Attach Window    Bogus Window
    Should Contain    ${error_msg}    after waiting for 2 seconds
    Set White Find Window Timeout    30 s

Close Nonexisting Window
    Attach Application By Id    ${test application id}
    Set White Find Window Timeout    2 s
    Attach Main Window
    ${status}    ${error_msg}    Run Keyword And Ignore Error    Close Window    Bogus Window
    Should Contain    ${error_msg}    after waiting for 2 seconds
    Set White Find Window Timeout    30 s

*** Keywords ***
Start Test Application
    ${handle} =    Start Process    ${TEST APPLICATION}
    Set Test Variable    ${handle}
    ${test application id} =    Get Process Id    ${handle}
    Set Test Variable    ${test application id}

Application Should Be Attached
    Attach Window    UI Automation Test Window
    Verify Label    lblA    Value 1

Fix test: time format in expected error message from int to float

*** Settings ***
Library    WhiteLibrary
Library    Process
Test Setup    Start Test Application
Test Teardown    Terminate Process    ${handle}    kill=true
Resource    resource.robot

*** Test Cases ***
Attach To A Running Application By Name
    Attach Application By Name    ${TEST APPLICATION NAME}
    Application Should Be Attached

Attach To A Running Application By Id
    Attach Application By Id    ${test application id}
    Application Should Be Attached

Attach Window Successfully
    Attach Application By Name    ${TEST APPLICATION NAME}
    Attach Main Window

Attach Nonexisting Window
    Attach Application By Id    ${test application id}
    Set White Find Window Timeout    2 s
    ${status}    ${error_msg}    Run Keyword And Ignore Error    Attach Window    Bogus Window
    Should Contain    ${error_msg}    after waiting for 2.0 seconds
    Set White Find Window Timeout    30 s

Close Nonexisting Window
    Attach Application By Id    ${test application id}
    Set White Find Window Timeout    2 s
    Attach Main Window
    ${status}    ${error_msg}    Run Keyword And Ignore Error    Close Window    Bogus Window
    Should Contain    ${error_msg}    after waiting for 2.0 seconds
    Set White Find Window Timeout    30 s

*** Keywords ***
Start Test Application
    ${handle} =    Start Process    ${TEST APPLICATION}
    Set Test Variable    ${handle}
    ${test application id} =    Get Process Id    ${handle}
    Set Test Variable    ${test application id}

Application Should Be Attached
    Attach Window    UI Automation Test Window
    Verify Label    lblA    Value 1


*** Settings ***

Documentation  Test OpenBMC GUI "Certificate management" sub-menu of
...            "Server configuration".

Resource        ../../lib/resource.robot

Suite Setup     Launch Browser And Login OpenBMC GUI
Suite Teardown  Close Browser
Test Setup      Test Setup Execution

*** Variables ***

${xpath_select_certificate_management}  //a[@href='#/configuration/certificate']
${xpath_select_server_configuration}    //*[@id="nav__top-level"]/li[4]/button
${xpath_add_certificate_button}         //button[text() ="Add new certificate"]

*** Test Cases ***

Verify Existence Of All Sections In Certificate Page
    [Documentation]  Verify existence of all sections in certificate page.
    [Tags]  Verify_Existence_Of_All_Sections_In_Certificate_Page

    Page should contain  Certificate
    Page should contain  Valid from
    Page should contain  Valid until

Verify Existence Of Add Certificate Button
    [Documentation]  Verify existence of add certificate button.
    [Tags]  Verify_Existence_Of_Add_Certificate_Button

    Page Should Contain Element  ${xpath_add_certificate_button}

*** Keywords ***

Test Setup Execution
   [Documentation]  Do test case setup tasks.

    Wait Until Page Does Not Contain Element  ${xpath_refresh_circle}
    Click Element  ${xpath_select_server_configuration}
    Wait Until Page Does Not Contain Element  ${xpath_refresh_circle}
    Click Element  ${xpath_select_certificate_management}
    Wait Until Page Contains  SSL certificates

Fix for GUI certificate test failure

*** Settings ***

Documentation  Test OpenBMC GUI "Certificate management" sub-menu of
...            "Server configuration".

Resource        ../../lib/resource.robot

Suite Setup     Launch Browser And Login OpenBMC GUI
Suite Teardown  Close Browser
Test Setup      Test Setup Execution

*** Variables ***

${xpath_select_certificate_management}  //a[@href='#/access-control/ssl-certificates']
${xpath_select_access_control}          //*[@id="nav__top-level"]/li[5]/button
${xpath_add_certificate_button}         //*[contains(text(), "Add new certificate")]

*** Test Cases ***

Verify Existence Of All Sections In Certificate Page
    [Documentation]  Verify existence of all sections in certificate page.
    [Tags]  Verify_Existence_Of_All_Sections_In_Certificate_Page

    Page should contain  Certificate
    Page should contain  Valid from
    Page should contain  Valid until

Verify Existence Of Add Certificate Button
    [Documentation]  Verify existence of add certificate button.
    [Tags]  Verify_Existence_Of_Add_Certificate_Button

    Page Should Contain Element  ${xpath_add_certificate_button}

*** Keywords ***

Test Setup Execution
   [Documentation]  Do test case setup tasks.

    Wait Until Page Does Not Contain Element  ${xpath_refresh_circle}
    Click Element  ${xpath_select_access_control}
    Wait Until Page Does Not Contain Element  ${xpath_refresh_circle}
    Click Element  ${xpath_select_certificate_management}
    Wait Until Page Contains  SSL certificates


*** Settings ***
Documentation   This suite is for disable field mode if enabled.

Resource        ../lib/code_update_utils.robot

*** Test Cases ***

Verify Field Mode Is Disable
    [Documentation]  Disable software manager field mode.

    Disable Field Mode And Verify Unmount

Add field mode check and log collection on failure

*** Settings ***
Documentation   This suite is for disable field mode if enabled.

Resource        ../lib/code_update_utils.robot
Resource        ../lib/openbmc_ffdc.robot

Test Teardown   FFDC On Test Case Fail

*** Test Cases ***

Verify Field Mode Is Disable
    [Documentation]  Disable software manager field mode.

    # Field mode is enabled before running CT.
    # It is to ensure that the setting is not changed during CT
    Field Mode Should Be Enabled
    Disable Field Mode And Verify Unmount


*** Settings ***
Resource         ../../lib/resource.txt
Resource         ../../lib/bmc_redfish_resource.robot
Resource         ../../lib/common_utils.robot

Suite Teardown   redfish.Logout

*** Test Cases ***

Verify BMC Firmware Version
    [Documentation]  Get firmware version from BMC manager.
    [Tags]  Verify_BMC_Firmware_Version

    redfish.Login
    ${resp}=  redfish.Get  Managers/bmc
    Should Be Equal As Strings  ${resp.status}  ${HTTP_OK}
    ${bmc_version}=  Get BMC Version
    Should Be Equal As Strings
    ...  ${resp.dict["FirmwareVersion"]}  ${bmc_version.strip('"')}
    redfish.Logout

Verify BMC Manager Properties
    [Documentation]  Verify BMC managers resource properties.
    [Tags]  Verify_BMC_Manager_Properties

    redfish.Login
    ${resp}=  redfish.Get  Managers/bmc
    Should Be Equal As Strings  ${resp.status}  ${HTTP_OK}
    # Example:
    #  "Description": "Baseboard Management Controller"
    #  "Id": "bmc"
    #  "Model": "OpenBmc",
    #  "Name": "OpenBmc Manager",
    #  "UUID": "xxxxxxxx-xxx-xxx-xxx-xxxxxxxxxxxx"
    #  "PowerState": "On"

    Should Be Equal As Strings
    ...  ${resp.dict["Description"]}  Baseboard Management Controller
    Should Be Equal As Strings  ${resp.dict["Id"]}  bmc
    Should Be Equal As Strings  ${resp.dict["Model"]}  OpenBmc
    Should Be Equal As Strings  ${resp.dict["Name"]}  OpenBmc Manager
    Should Not Be Empty  ${resp.dict["UUID"]}
    Should Be Equal As Strings  ${resp.dict["PowerState"]}  On
    redfish.Logout

Test BMC Manager GracefulRestart
    [Documentation]  BMC graceful restart.
    [Tags]  Test_BMC_Manager_GracefulRestart

    # Example:
    # "Actions": {
    # "#Manager.Reset": {
    #  "ResetType@Redfish.AllowableValues": [
    #    "GracefulRestart"
    #  ],
    #  "target": "/redfish/v1/Managers/bmc/Actions/Manager.Reset"
    # }

    redfish.Login
    ${payload}=  Create Dictionary  ResetType=GracefulRestart
    ${resp}=  redfish.Post  Managers/bmc/Actions/Manager.Reset  body=&{payload}
    Should Be Equal As Strings  ${resp.status}  ${HTTP_OK}

    # TODO: Add logic to ping and check BMC online state


# Copyright 2022 Nathan Sommer and Ben Coleman
#
# 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/>.

*** Settings ***
Resource    resources/setup.robot
Test Setup    Setup Server and Client Accounts
Force Tags    gkeepd_launch

*** Keywords ***

Setup Server and Client Accounts
    Launch Gkeepd And Configure Admin Account on Client
    Add Faculty and Configure Accounts on Client    faculty1
    Establish Course  faculty1    cs1     student1
    Create Accounts On Client    student1
    Create Git Config    student1

*** Test Cases ***

Student Submits to Docker Assignment
    [Tags]    happy_path
    Add Assignment to Client  faculty1  good_docker
    Gkeep Upload Succeeds   faculty1   cs1    good_docker
    Gkeep Publish Succeeds  faculty1    cs1     good_docker
    Clone Assignment  student1  faculty1    cs1     good_docker
    Student Submits    student1    faculty1    cs1    good_docker    correct_solution
    Submission Test Results Email Exists    student1    cs1    good_docker    "In Docker"

# Docker container does not exist
# malformed yaml file
# missing image field
# missing type field
# type is not "docker"


Add test case for testing st2 execution tail command.

*** Variables ***
${EXECUTION RUN}    st2 run
${EXECUTION TAIL}   st2 execution tail

${CHILD EXECUTION 3 STARTED}    (?ims).*Child execution \\(task=task3\\) .*? has started\..*
${CHILD EXECUTION 3 FINISHED}   (?ims).*Child execution \\(task=task3\\) .*? has finished \\(status=succeeded\\)\.
${CHILD EXECUTION 10 STARTED}   (?ims).*Child execution \\(task=task10\\) .*? has started\..*
${CHILD EXECUTION 10 FINISHED}  (?ims).*Child execution \\(task=task10\\) .*? has finished \\(status=succeeded\\)\.
${PARENT EXECUTION FINISHED}    (?ims).*Execution .*? has completed \\(status=succeeded\\).

*** Test Cases ***
TEST:Verify st2 execution tail command works correctly
    ${result}=           Run  ${EXECUTION RUN} examples.action_chain_streaming_demo -a
    @{Execution ID}      Split String  ${result}
    Set Suite Variable   @{Execution ID}
    Log To Console       \nRUN EXECUTION:\n
    Log To Console       EXECUTION ID: @{Execution ID}[-1]\n

    ${tail_output}=       Run  ${EXECUTION TAIL} @{Execution ID}[-1]
    Log To Console       \nRUN EXECUTION TAIL (might take a while):\n
    Log To Console       \nRESULT:\n
    Log To Console       \n${tail_output}
    Should Match Regexp  ${tail_output}  ${CHILD EXECUTION 3 STARTED}
    Should Match Regexp  ${tail_output}  ${CHILD EXECUTION 3 FINISHED}
    Should Match Regexp  ${tail_output}  ${CHILD EXECUTION 10 STARTED}
    Should Match Regexp  ${tail_output}  ${CHILD EXECUTION 10 FINISHED}
    Should Match Regexp  ${tail_output}  ${PARENT EXECUTION FINISHED}

*** Settings ***
Library         Process
Library         String
Library         OperatingSystem
Resource        ../common/keywords.robot
#Suite Setup     SETUP:Copy and Load Examples Pack
#Suite Teardown  TEARDOWN:Uninstall Examples Pack


*** Variables ***
${NeutronNorthbound}    /controller/nb/v2/neutron
${NetworkNorthbound}    ${NeutronNorthbound}/networks
${SubnetNorthbound}     ${NeutronNorthbound}/subnets
${PortNorthbound}       ${NeutronNorthbound}/ports

*** Settings ***
Suite Setup       Create Session    ODL    http://${CONTROLLER}:${PORT}    headers=${HEADERS}    auth=${AUTH}
Suite Teardown    Delete All Sessions
Library           RequestsLibrary
Variables         ../../../variables/Variables.py

*** Test Cases ***
Get the complete list of networks
    [Tags]    reachability
    ${resp}   get request    ODL    ${NetworkNorthbound}
    Should be Equal As Strings    ${resp.status_code}    200

Get the complete list of subnets
    [Tags]    reachability
    ${resp}   get request    ODL    ${SubnetNorthbound}
    Should be Equal As Strings    ${resp.status_code}    200

Get the complete list of ports
    [Tags]    reachability
    ${resp}   get request    ODL    ${PortNorthbound}
    Should be Equal As Strings    ${resp.status_code}    200


Add tests for CLI include_attributes usage.

*** Variables ***
${EXECUTION LIST}         st2 execution list
${ACTION LIST}            st2 action list

*** Test Cases ***
TEST:Verify include attributes works as expected for st2 execution list
    ${result}=           Run  ${EXECUTION LIST} --attr id
    Log To Console       \nRESULT:\n${result}
    Should Contain       ${result}  id
    Should Not Contain   ${result}  status
    Should Not Contain   ${result}  context
    Should Not Contain   ${result}  start_timestamp
    Should Not Contain   ${result}  end_timestamp
    Should Not Contain   ${result}  action.ref

    ${result}=        Run  ${EXECUTION LIST} --attr doesntexist
    Log To Console   \nRESULT:\n${result}
    Should Contain   ${result}  Invalid or unsupported include attribute specified

TEST:Verify include attributes works as expected for st2 action list
    ${result}=           Run  ${ACTION LIST} --attr name
    Log To Console       \nRESULT:\n${result}
    Should Contain       ${result}  name
    Should Not Contain   ${result}  pack
    Should Not Contain   ${result}  description

    ${result}=        Run  ${ACTION LIST} --attr doesntexist
    Log To Console   \nRESULT:\n${result}
    Should Contain   ${result}  Invalid or unsupported include attribute specified

*** Settings ***
Library            Process
Library            OperatingSystem
Resource           ../common/keywords.robot


*** Settings ***

Documentation  Test OpenBMC GUI "Memory" sub-menu of "Resource Management".

Resource        ../../lib/gui_resource.robot
Suite Setup     Suite Setup Execution
Suite Teardown  Close Browser

*** Variables ***

${xpath_memory_header}           //h1[text()="Memory"]
${xpath_memory_sub_menu}         //*[@data-test-id='nav-item-memory']

*** Test Cases ***

Verify Navigate To Memory Page
    [Documentation]  Login to GUI and perform page navigation to memory page
    ...  and verify it loads successfully.
    [Tags]  Verify_Navigate_To_Memory_Page

    Page Should Contain Element  ${xpath_memory_header}

Verify Existence Of All Sections In Memory Page
    [Documentation]  Login to GUI and perform page navigation to memory page and
    ...  check if all visible sections is actually loaded successfully.
    [Tags]  Verify_Existence_Of_All_Sections_In_Memory_Page

    Page Should Contain  Quick links
    Page Should Contain  Logical memory block size
    Page Should Contain  System memory page setup
    Page Should Contain  I/O Adapter enlarged capacity
    Page Should Contain  Active memory mirroring

*** Keywords ***

Suite Setup Execution
    [Documentation]  Do suite setup tasks.

    Launch Browser And Login GUI
    Click Element  ${xpath_resource_management_menu}
    Click Element  ${xpath_memory_sub_menu}
    Wait Until Keyword Succeeds  30 sec  10 sec  Location Should Contain  memory


*** Settings ***

Library        Collections
Library        String
Library        SeleniumLibrary  implicit_wait=${IMPLICIT_WAIT}  timeout=${TIMEOUT}
Library        cumulusci.robotframework.CumulusCI  ${ORG}
Library        cumulusci.robotframework.Salesforce  debug=${DEBUG}

*** Variables ***
${BROWSER}          chrome
${BROWSER_VERSION}  63
${DEBUG}            ${false}
${IMPLICIT_WAIT}    5.0
${TIMEOUT}          5.0

*** Keywords ***

Open Test Browser
    ${login_url} =  Login Url
    Open Browser  ${login_url}  ${BROWSER}  desired_capabilities=version:${BROWSER_VERSION}
    Sleep  2
    #Run Keyword If  '${BROWSER}' == 'chrome'  Open Test Browser Chrome
    #...    ELSE IF  '${BROWSER}' == 'firefox'  Open Test Browser Firefox
    #Go To  ${LOGIN_URL}

Open Test Browser Chrome
    ${chrome_options} =  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys
    Call Method  ${chrome_options}  add_argument  --disable-notifications
    Create Webdriver  Chrome  timeout=${IMPLICIT_WAIT}  chrome_options=${chrome_options}

Open Test Browser Firefox
    Create Webdriver  Firefox

Increase implicit wait to 7 seconds to be more conservative

*** Settings ***

Library        Collections
Library        String
Library        SeleniumLibrary  implicit_wait=${IMPLICIT_WAIT}  timeout=${TIMEOUT}
Library        cumulusci.robotframework.CumulusCI  ${ORG}
Library        cumulusci.robotframework.Salesforce  debug=${DEBUG}

*** Variables ***
${BROWSER}          chrome
${BROWSER_VERSION}  63
${DEBUG}            ${false}
${IMPLICIT_WAIT}    7.0
${TIMEOUT}          7.0

*** Keywords ***

Open Test Browser
    ${login_url} =  Login Url
    Open Browser  ${login_url}  ${BROWSER}  desired_capabilities=version:${BROWSER_VERSION}
    Sleep  2
    #Run Keyword If  '${BROWSER}' == 'chrome'  Open Test Browser Chrome
    #...    ELSE IF  '${BROWSER}' == 'firefox'  Open Test Browser Firefox
    #Go To  ${LOGIN_URL}

Open Test Browser Chrome
    ${chrome_options} =  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys
    Call Method  ${chrome_options}  add_argument  --disable-notifications
    Create Webdriver  Chrome  timeout=${IMPLICIT_WAIT}  chrome_options=${chrome_options}

Open Test Browser Firefox
    Create Webdriver  Firefox


*** Settings ***

Resource  cumulusci/robotframework/CumulusCI.robot
Resource        robot/Cumulus/resources/NPSP.robot
Suite Setup       Run Task Class   tasks.generate_bdi_data.GenerateBDIData
...            num_records=20  mapping_yaml=testdata/bdi_benchmark/mapping.yml

*** Test Cases ***

Import a data batch via the API
    Batch Data Import

Correct number for test again.

*** Settings ***

Resource  cumulusci/robotframework/CumulusCI.robot
Resource        robot/Cumulus/resources/NPSP.robot
Suite Setup       Run Task Class   tasks.generate_bdi_data.GenerateBDIData
...            num_records=5000  mapping_yaml=testdata/bdi_benchmark/mapping.yml

*** Test Cases ***

Import a data batch via the API
    Batch Data Import


Add initial rest test cases Support to test Null properties

*** Settings ***
Documentation		This suite will verifiy all OpenBMC rest interfaces
...					Details of valid interfaces can be found here...
...					https://github.com/openbmc/docs/blob/master/rest-api.md

Resource		../lib/rest_client.robot

*** Variables ***

*** Test Cases ***
Good connection for testing
	${resp} =	Read Properties	/
	${jdata}=	To Json 	${resp.content}
	${c}= 		get from List 	${jdata} 	0
	Should Contain		"/org"	${c}

Get an object with no properties
	${resp} =	Read Properties	/org/openbmc/inventory
	Should Be Empty	${resp.content}

Get a Property
	${resp} =	Read attribute	/org/openbmc/inventory/system/chassis/motherboard/cpu0	is_fru
	Should Contain		1	${resp}

Get a null Property
	${resp} =	Read attribute	/org/openbmc/inventory	is_fru
	Should Contain		1	${resp}

Enumeration object

List object

Invoke a method without properties

Invoke a method with properties

Issue a POST

Issue a PUT

*** Keywords ***
Read Properties
	[arguments]	${uri}
	${resp} =	OpenBMC Get Request	${uri}
	[return]	${resp}

Read attribute
	[arguments]	${uri}	${attr}
	${resp} =	OpenBMC Get Request	${uri}/attr/${attr}
	[return]	${resp.content}


*** Settings ***
Library    Selenium2Library
Suite Teardown    Close browser

*** Variables ***
${BROWSER}    ff

*** Test Cases ***
Open main page
    Open browser    http://www.ruokakori.net/    browser=${BROWSER}
    Title should be    Ruokakori.net: Löydä alueesi halvin ruokakori - Pääsivu


*** Settings ***
Documentation    This suite tests Redfish Host power operations.

Resource         ../../lib/boot_utils.robot
Resource         ../../lib/common_utils.robot
Resource         ../../lib/open_power_utils.robot

Test Setup       Test Setup Execution
Test Teardown    Test Teardown Execution

*** Test Cases ***

Verify Redfish Host GracefulShutdown
    [Documentation]  Verify Redfish host graceful shutdown operation.
    [Tags]  Verify_Redfish_Host_GracefulShutdown

    Redfish Power Off

Verify Redfish BMC PowerOn
    [Documentation]  Verify Redfish host power on operation.
    [Tags]  Verify_Redfish_Host_PowerOn

    Redfish Power On

    # TODO: Replace OCC state check with redfish property when available.
    Verify OCC State

Verify Redfish BMC GracefulRestart
    [Documentation]  Verify Redfish host graceful restart operation.
    [Tags]  Verify_Redfish_Host_GracefulRestart

    Redfish Host Reboot

Verify Redfish BMC PowerOff
    [Documentation]  Verify Redfish host power off operation.
    [Tags]  Verify_Redfish_Host_PowerOff

    Redfish Hard Power Off

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Start SOL Console Logging

Test Teardown Execution
    [Documentation]  Collect FFDC and SOL log.

    FFDC On Test Case Fail
    ${sol_log}=    Stop SOL Console Logging
    Log   ${sol_log}
    Set Auto Reboot  ${1}

Add power sensor check post power on

*** Settings ***
Documentation    This suite tests Redfish Host power operations.

Resource         ../../lib/boot_utils.robot
Resource         ../../lib/common_utils.robot
Resource         ../../lib/open_power_utils.robot

Test Setup       Test Setup Execution
Test Teardown    Test Teardown Execution

*** Test Cases ***

Verify Redfish Host GracefulShutdown
    [Documentation]  Verify Redfish host graceful shutdown operation.
    [Tags]  Verify_Redfish_Host_GracefulShutdown

    Redfish Power Off

Verify Redfish BMC PowerOn
    [Documentation]  Verify Redfish host power on operation.
    [Tags]  Verify_Redfish_Host_PowerOn

    Redfish Power On

    # TODO: Replace OCC state check with redfish property when available.
    Verify OCC State

    Redfish.Login
    ${power_control}=  Redfish.Get Attribute  ${REDFISH_CHASSIS_POWER_URI}  PowerControl
    Rprint Vars   power_control
    Valid Dict  power_control[${0}]  ['PowerConsumedWatts']

Verify Redfish BMC GracefulRestart
    [Documentation]  Verify Redfish host graceful restart operation.
    [Tags]  Verify_Redfish_Host_GracefulRestart

    Redfish Host Reboot

Verify Redfish BMC PowerOff
    [Documentation]  Verify Redfish host power off operation.
    [Tags]  Verify_Redfish_Host_PowerOff

    Redfish Hard Power Off

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Printn
    Start SOL Console Logging

Test Teardown Execution
    [Documentation]  Collect FFDC and SOL log.

    FFDC On Test Case Fail
    ${sol_log}=    Stop SOL Console Logging
    Log   ${sol_log}
    Set Auto Reboot  ${1}


*** Settings ***
Library         Selenium2Library
Test Setup      Go to homepage
Test Teardown   Close Browser

*** Variables ***
${HOMEPAGE}    http://www.google.com
${BROWSER}     chrome

*** Test Cases ***
Visit my personal site and check content
    Go to my personal site and check results    http://www.kanouivirach.com    Barcamp

*** Keywords ***
Go to my personal site and check results
  [Arguments]    ${site}    ${content}
  Go To    ${site}
  Wait Until Page Contains    ${content}

Go to homepage
    Open Browser    ${HOMEPAGE}    ${BROWSER}


*** Settings ***
Documentation    This suite tests Redfish Host power operations.

Resource         ../../lib/boot_utils.robot
Resource         ../../lib/common_utils.robot

Test Setup       Test Setup Execution
Test Teardown    Test Teardown Execution

*** Test Cases ***

Verify Redfish Host GracefulShutdown
    [Documentation]  Verify Redfish host graceful shutdown operation.
    [Tags]  Verify_Redfish_Host_GracefulShutdown

    Redfish Power Off

Verify Redfish BMC PowerOn
    [Documentation]  Verify Redfish host power on operation.
    [Tags]  Verify_Redfish_Host_PowerOn

    Redfish Power On

Verify Redfish BMC GracefulRestart
    [Documentation]  Verify Redfish host graceful restart operation.
    [Tags]  Verify_Redfish_Host_GracefulRestart

    Redfish Host Reboot

Verify Redfish BMC PowerOff
    [Documentation]  Verify Redfish host power off operation.
    [Tags]  Verify_Redfish_Host_PowerOff

    Redfish Hard Power Off

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Start SOL Console Logging

Test Teardown Execution
    [Documentation]  Collect FFDC and SOL log.

    FFDC On Test Case Fail
    ${sol_log}=    Stop SOL Console Logging
    Log   ${sol_log}
    Set Auto Reboot  ${1}

Enable OCC active state check post power on

*** Settings ***
Documentation    This suite tests Redfish Host power operations.

Resource         ../../lib/boot_utils.robot
Resource         ../../lib/common_utils.robot
Resource         ../../lib/open_power_utils.robot

Test Setup       Test Setup Execution
Test Teardown    Test Teardown Execution

*** Test Cases ***

Verify Redfish Host GracefulShutdown
    [Documentation]  Verify Redfish host graceful shutdown operation.
    [Tags]  Verify_Redfish_Host_GracefulShutdown

    Redfish Power Off

Verify Redfish BMC PowerOn
    [Documentation]  Verify Redfish host power on operation.
    [Tags]  Verify_Redfish_Host_PowerOn

    Redfish Power On

    # TODO: Replace OCC state check with redfish property when available.
    Verify OCC State

Verify Redfish BMC GracefulRestart
    [Documentation]  Verify Redfish host graceful restart operation.
    [Tags]  Verify_Redfish_Host_GracefulRestart

    Redfish Host Reboot

Verify Redfish BMC PowerOff
    [Documentation]  Verify Redfish host power off operation.
    [Tags]  Verify_Redfish_Host_PowerOff

    Redfish Hard Power Off

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Start SOL Console Logging

Test Teardown Execution
    [Documentation]  Collect FFDC and SOL log.

    FFDC On Test Case Fail
    ${sol_log}=    Stop SOL Console Logging
    Log   ${sol_log}
    Set Auto Reboot  ${1}


*** Settings ***
Documentation    Module to test IPMI cold and warm reset functionalities.

Resource         ../lib/ipmi_client.robot
Resource         ../lib/openbmc_ffdc.robot

Test Teardown    FFDC On Test Case Fail

*** Variables ***

# User may pass LOOP_COUNT.
${LOOP_COUNT}  ${1}

*** Test Cases ***

Test IPMI Warm Reset
    [Documentation]  Check IPMI warm reset and wait for BMC to become online.
    [Tags]  Test_IPMI_Warm_Reset
    Repeat Keyword  ${LOOP_COUNT} times  IPMI MC Reset Warm (off)

Test IPMI Cold Reset
    [Documentation]  Check IPMI cold reset and wait for BMC to become online.
    [Tags]  Test_IPMI_Cold_Reset

    Repeat Keyword  ${LOOP_COUNT} times  IPMI MC Reset Cold (run)

Verify BMC Power Cycle via IPMI
    [Documentation]  Verify IPMI power cycle command works fine.
    [Tags]  Verify_BMC_Power_Cycle_via_IPMI

    Repeat Keyword  ${LOOP_COUNT} times  IPMI Power Cycle

Add test case to verify power reset via IPMI

*** Settings ***
Documentation    Module to test IPMI cold and warm reset functionalities.

Resource         ../lib/ipmi_client.robot
Resource         ../lib/openbmc_ffdc.robot

Test Teardown    FFDC On Test Case Fail

*** Variables ***

# User may pass LOOP_COUNT.
${LOOP_COUNT}  ${1}

*** Test Cases ***

Test IPMI Warm Reset
    [Documentation]  Check IPMI warm reset and wait for BMC to become online.
    [Tags]  Test_IPMI_Warm_Reset
    Repeat Keyword  ${LOOP_COUNT} times  IPMI MC Reset Warm (off)

Test IPMI Cold Reset
    [Documentation]  Check IPMI cold reset and wait for BMC to become online.
    [Tags]  Test_IPMI_Cold_Reset

    Repeat Keyword  ${LOOP_COUNT} times  IPMI MC Reset Cold (run)

Verify BMC Power Cycle via IPMI
    [Documentation]  Verify IPMI power cycle command works fine.
    [Tags]  Verify_BMC_Power_Cycle_via_IPMI

    Repeat Keyword  ${LOOP_COUNT} times  IPMI Power Cycle

Verify Power Reset via IPMI
    [Documentation]  Verify IPMI power reset command works fine.
    [Tags]  Verify_Power_Reset_via_IPMI

    Repeat Keyword  ${LOOP_COUNT} times  IPMI Power Reset


*** Settings ***
Library           OperatingSystem

*** Variables ***
${WINDOWS}        ${/ != '/'}

*** Test Cases ***
Tilde in path
    ${path} =    Normalize Path    ~/foo
    ${home} =    Get Home
    Should Be Equal    ${path}    ${home}${/}foo
    Directory Should Exist    ~

Tilde and username in path
    ${user} =    Get User
    ${path} =    Normalize Path    ~${user}/foo
    ${home} =    Get Home
    Should Be Equal    ${path}    ${home}${/}foo
    Directory Should Exist    ~${user}

*** Keywords ***
Get Home
    ${home} =    Run Keyword If    ${WINDOWS}
    ...    Get Windows Home
    ...    ELSE
    ...    Get Posix Home
    ${home} =    Normalize Path    ${home}
    [Return]    ${home}

Get Windows Home
    [Return]    %{HOMEDRIVE}%{HOMEPATH}

Get Posix Home
    [Return]    %{HOME}

Get User
    ${user} =    Run Keyword If    ${WINDOWS}
    ...    Get Windows User
    ...    ELSE
    ...    Get Posix User
    [Return]    ${user}

Get Windows User
    [Return]    %{USERNAME}

Get Posix User
    [Return]    %{USER}

Make path expansion tests more resilient to env differences

*** Settings ***
Library           OperatingSystem

*** Variables ***
${WINDOWS}        ${/ != '/'}

*** Test Cases ***
Tilde in path
    ${path} =    Normalize Path    ~/foo
    ${home} =    Get Home
    Should Be Equal    ${path}    ${home}${/}foo
    Directory Should Exist    ~

Tilde and username in path
    ${user} =    Get User
    ${path} =    Normalize Path    ~${user}/foo
    ${home} =    Get Home
    Should Be Equal    ${path}    ${home}${/}foo
    Directory Should Exist    ~${user}

*** Keywords ***
Get Home
    ${home} =    Run Keyword If    ${WINDOWS}
    ...    Get Windows Home
    ...    ELSE
    ...    Get Posix Home
    ${home} =    Normalize Path    ${home}
    [Return]    ${home}

Get Windows Home
    ${home} =    Get Environment Variable    USERPROFILE    %{HOMEDRIVE}%{HOMEPATH}
    [Return]    ${home}

Get Posix Home
    [Return]    %{HOME}

Get User
    ${user} =    Run Keyword If    ${WINDOWS}
    ...    Get Windows User
    ...    ELSE
    ...    Get Posix User
    [Return]    ${user}

Get Windows User
    [Return]    %{USERNAME}

Get Posix User
    [Return]    %{USER}


*** Settings ***

Documentation   Test OpenBMC GUI "Hardware Status" sub-menu of "Health" menu.

Resource        ../../lib/resource.robot

Suite Setup     Launch Browser And Login GUI
Suite Teardown  Close Browser
Test Setup      Test Setup Execution

*** Variables ***

${xpath_hardware_status_heading}  //h1[text()="Hardware status"]

*** Test Cases ***

Verify Navigation To Hardware Status Page
    [Documentation]  Verify navigation to hardware status page.
    [Tags]  Verify_Navigation_To_Hardware_Status_Page

    Page Should Contain Element  ${xpath_hardware_status_heading}

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Click Element  ${xpath_health_menu}
    Click Element  ${xpath_hardware_status_sub_menu}
    Wait Until Keyword Succeeds  30 sec  5 sec  Location Should Contain  hardware-status

Verify Components On Hardware Status Page

*** Settings ***

Documentation   Test OpenBMC GUI "Hardware Status" sub-menu of "Health" menu.

Resource        ../../lib/resource.robot

Suite Setup     Launch Browser And Login GUI
Suite Teardown  Close Browser
Test Setup      Test Setup Execution

*** Variables ***

${xpath_hardware_status_heading}  //h1[text()="Hardware status"]

*** Test Cases ***

Verify Navigation To Hardware Status Page
    [Documentation]  Verify navigation to hardware status page.
    [Tags]  Verify_Navigation_To_Hardware_Status_Page

    Page Should Contain Element  ${xpath_hardware_status_heading}

Verify Components On Hardware Status Page
    [Documentation]  Verify whether required components are displayed hardware status page.
    [Tags]  Verify_Components_On_Hardware_Status_Page

    Page Should Contain  System
    Page Should Contain  BMC manager
    Page Should Contain  Chassis
    Page Should Contain  DIMM slot
    Page Should Contain  Fans
    Page Should Contain  Power supplies
    Page Should Contain  Processors

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Click Element  ${xpath_health_menu}
    Click Element  ${xpath_hardware_status_sub_menu}
    Wait Until Keyword Succeeds  30 sec  5 sec  Location Should Contain  hardware-status


*** Settings ***

Documentation  Test OpenBMC GUI "Serial over LAN Console" sub-menu of "Control".

Resource        ../../lib/gui_resource.robot

Suite Setup     Launch Browser And Login GUI
Suite Teardown  Close Browser
Test Setup      Test Setup Execution

*** Variables ***

${xpath_open_in_new_tab_button}  //button[contains(text(),'Open in new tab')]

*** Test Cases ***

Verify Navigation To SOL Console Page
    [Documentation]  Verify navigation to SOL console page.
    [Tags]  Verify_Navigation_To_SOL_Console_Page

    Page Should Contain Element  ${xpath_sol_console_heading}

Verify Existence Of All Sections In SOL Console Page
    [Documentation]  Verify existence of all sections in SOL console page.
    [Tags]  Verify_Existence_Of_All_Sections_In_SOL_Console_Page

    Page Should Contain  SOL console redirects server's serial port output to this window

Verify Existence Of All Buttons In SOL Console Page
    [Documentation]  Verify existence of all buttons in SOL console page.
    [Tags]  Verify_Existence_Of_All_Buttons_In_SOL_Console_Page

    Page Should Contain Element  ${xpath_open_in_new_tab_button}

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Click Element  ${xpath_control_menu}
    Click Element  ${xpath_sol_sub_menu}
    Wait Until Keyword Succeeds  30 sec  10 sec  Location Should Contain  serial-over-lan

Fix for GUI SOL page text change

*** Settings ***

Documentation  Test OpenBMC GUI "Serial over LAN Console" sub-menu of "Control".

Resource        ../../lib/gui_resource.robot

Suite Setup     Launch Browser And Login GUI
Suite Teardown  Close Browser
Test Setup      Test Setup Execution

*** Variables ***

${xpath_open_in_new_tab_button}  //button[contains(text(),'Open in new tab')]

*** Test Cases ***

Verify Navigation To SOL Console Page
    [Documentation]  Verify navigation to SOL console page.
    [Tags]  Verify_Navigation_To_SOL_Console_Page

    Page Should Contain Element  ${xpath_sol_console_heading}

Verify Existence Of All Sections In SOL Console Page
    [Documentation]  Verify existence of all sections in SOL console page.
    [Tags]  Verify_Existence_Of_All_Sections_In_SOL_Console_Page

    Page Should Contain  SOL console redirects the server's serial port output to this window

Verify Existence Of All Buttons In SOL Console Page
    [Documentation]  Verify existence of all buttons in SOL console page.
    [Tags]  Verify_Existence_Of_All_Buttons_In_SOL_Console_Page

    Page Should Contain Element  ${xpath_open_in_new_tab_button}

*** Keywords ***

Test Setup Execution
    [Documentation]  Do test case setup tasks.

    Click Element  ${xpath_control_menu}
    Click Element  ${xpath_sol_sub_menu}
    Wait Until Keyword Succeeds  30 sec  10 sec  Location Should Contain  serial-over-lan


*** Settings ***
Library           OperatingSystem

*** Variables ***
${WINDOWS}        ${/ != '/'}

*** Test Cases ***
Tilde in path
    ${path} =    Normalize Path    ~/foo
    ${home} =    Get Home
    Should Be Equal    ${path}    ${home}${/}foo
    Directory Should Exist    ~

Tilde and username in path
    ${user} =    Get User
    ${path} =    Normalize Path    ~${user}/foo
    ${home} =    Get Home
    Should Be Equal    ${path}    ${home}${/}foo
    Directory Should Exist    ~${user}

*** Keywords ***
Get Home
    ${home} =    Run Keyword If    ${WINDOWS}
    ...    Get Windows Home
    ...    ELSE
    ...    Get Posix Home
    ${home} =    Normalize Path    ${home}
    [Return]    ${home}

Get Windows Home
    ${home} =    Get Environment Variable    USERPROFILE    %{HOMEDRIVE}%{HOMEPATH}
    [Return]    ${home}

Get Posix Home
    [Return]    %{HOME}

Get User
    ${user} =    Run Keyword If    ${WINDOWS}
    ...    Get Windows User
    ...    ELSE
    ...    Get Posix User
    [Return]    ${user}

Get Windows User
    [Return]    %{USERNAME}

Get Posix User
    [Return]    %{USER}

Make path expansion tests even more resilient to env differences.

*** Settings ***
Library           OperatingSystem

*** Variables ***
${WINDOWS}        ${/ != '/'}

*** Test Cases ***
Tilde in path
    ${path} =    Normalize Path    ~/foo
    ${home} =    Get Home
    Should Be Equal    ${path}    ${home}${/}foo
    Directory Should Exist    ~

Tilde and username in path
    ${user} =    Get User
    ${path} =    Normalize Path    ~${user}/foo    case_normalize=True
    ${home} =    Get Home    case_normalize=True
    Should Be Equal    ${path}    ${home}${/}foo
    Directory Should Exist    ~${user}

*** Keywords ***
Get Home
    [Arguments]    ${case_normalize}=False
    ${home} =    Run Keyword If    ${WINDOWS}
    ...    Get Windows Home
    ...    ELSE
    ...    Get Posix Home
    ${home} =    Normalize Path    ${home}    ${case_normalize}
    [Return]    ${home}

Get Windows Home
    ${home} =    Get Environment Variable    USERPROFILE    %{HOMEDRIVE}%{HOMEPATH}
    [Return]    ${home}

Get Posix Home
    [Return]    %{HOME}

Get User
    ${user} =    Run Keyword If    ${WINDOWS}
    ...    Get Windows User
    ...    ELSE
    ...    Get Posix User
    [Return]    ${user}

Get Windows User
    [Return]    %{USERNAME}

Get Posix User
    [Return]    %{USER}


Add a stub Robot script for Renode testing

*** Settings ***
Suite Setup                   Setup
Suite Teardown                Teardown
Test Setup                    Reset Emulation
Test Teardown                 Test Teardown
Resource                      ${RENODEKEYWORDS}

*** Test Cases ***
Should Walk The Menu
    Execute Command          include @${CURDIR}/litex-vexriscv-tflite.resc
    Create Terminal Tester   sysbus.uart

    Start Emulation

    Wait For Line On Uart    CFU Playground
    Wait For Prompt On Uart  main>
    Write Line To Uart       3
    Wait For Line On Uart    Project Menu
    Write Line To Uart       h
    Wait For Line On Uart    Hello, World!