text:
layout: post

#event information
title:  "Future Is Here Festival"
cover: "/images/6890345708_26c9d6e745_k.jpg"
attribution:
  name: "Futuristic Minimalist Kite"
  author: "Ryan Somma"
  license: "CC BY 2.0"
  name_url: "https://www.flickr.com/photos/ideonexus/6890345708"
  license_url: "https://creativecommons.org/licenses/by/2.0"
date:   "2016-04-23 00:00"
endDate:   "2016-04-24 23:59"
creation: "2016-04-01"
location:
  name: "Shakespeare Theatre - Sydney Harman Hall"
  address:
    - "610 F Street NW"
  city: Washington
  state: DC
  zip: 20004
tags: [ 'Festivals' ]
excerpt: >
  Smithsonian magazine's *The Future Is Here Festival*
  brings together the world's most renowned experts in science, technology,
  engineering, culture, the arts, inner and outer space exploration and
  other innovative subjects. Attend the free festival on Saturday
  April 23 and Sunday April 24.

---

Smithsonian magazine launched *The Future Is Here Festival* in 2013
to bring together the world's most renowned experts in science, technology,
engineering, culture, the arts, inner and outer space exploration and
other innovative subjects. This annual festival features dynamic presentations,
demonstrations and performances and inspires the kind of curiosity and
questions that drive our society forward.

The event is free but [you must register](https://www.eventbrite.com/e/smithsonian-magazines-2016-the-future-is-here-festivaltm-registration-21495104436)
to attend the event.

### Saturday, April 23

#### Toni Antonelli
 Chief Technologist, Exploration Systems for the Lockheed Martin Civil Space line of business

Tony Antonelli is Chief Technologist, Exploration Systems for the Lockheed
Martin Civil Space line of business. In this role, he is responsible for helping to
expedite the human exploration strategy through the development and integration
of both science objectives and human exploration goals. His efforts contribute
to shaping a definitive path for sustainable human missions to Mars. Tony is
a retired Navy Captain and former NASA Astronaut who has accumulated over
4,700 flight hours in over 40 different kinds of aircraft and served as
the pilot for two Space Shuttle missions. While serving at NASA, Tony's
leadership roles within the Astronaut Office included the Space Launch System,
Commercial Crew, Capsule Communicator (CAPCOM), and Space Shuttle Propulsion.

#### ArcAttack
 Tesla Coil Band

Creators of the original Singing Tesla Coil, the crew of ArcAttack use
their high tech wizardry to generate a truly 'electrifying' performance.
The show perfectly combines ArcAttack's love of rock music and their love of
science, creating a show you will never forget!

#### Sheldon Brown
 Director of the Arthur C. Clarke Center for Human Imagination

Sheldon Brown combines computer science research with vanguard cultural
production in the production of virtual reality installations that are
exhibited around the world. He holds the MacArthur Foundation Endowed Chair
of Digital Media and Learning at UCSD where he is the Director of the
Arthur C. Clarke Center for Human Imagination. His work is supported in
part by the National Science Foundation, the National Endowment for the
Arts, IBM, Intel, Sony and Google.

#### Vint Cerf
 Vice President and Chief Internet Evangelist for Google and Arthur C. Clarke Panelist

Vinton G. Cerf co-designed the TCP/IP protocols and the architecture of the
Internet and is Chief Internet Evangelist for Google. He is a member of the
National Science Board and National Academy of Engineering and Fellow of
ACM, IEEE, AAAS, and BCS. Cerf received the US Presidential Medal of Freedom,
US National Medal of Technology, Queen Elizabeth Prize for Engineering,
Prince of Asturias Award, Japan Prize, ACM Turing Award, Legion d'Honneur
and 25 honorary degrees.

#### Alexandra Cousteau
 Explorer, Storyteller, Environmental Advocate

A National Geographic Emerging Explorer, filmmaker and globally recognized
advocate on water issues, Alexandra Cousteau continues the work of her
renowned grandfather Jacques-Yves Cousteau and her father Philippe
Cousteau, Sr. She has mastered the remarkable storytelling tradition handed
down to her and has the unique ability to inspire audiences on the weighty
issues of policy, politics and action. Alexandra is dedicated to advocating
the importance of conservation and sustainable management of water in order
to preserve a healthy planet. Her global initiatives seek to inspire and
empower individuals to protect not only the ocean and its inhabitants,
but also the human communities that rely on freshwater resources.

#### Céline S. Cousteau
 Explorer, Filmmaker and Diver

Founder and executive director of CauseCentric Productions, a non-profit
organization that creates cause focused multi-media content. Céline holds
a degree in Psychology, a Masters in International and Intercultural Management
and is fluent in three languages. Traveling as a public speaker, Céline is a
member of the World Economic Forum Council on Oceans, collaborating to
provide recommendations to the WEF about ocean issues. Celine is the daughter
of ocean explorer and filmmaker Jean-Michel Cousteau and granddaughter of the
legendary Jacques Yves Cousteau.

#### Anthony S. Fauci, M.D.
 Director, National Institute of Allergy and Infectious Diseases at National Institutes of Health

Since his appointment as NIAID director in 1984, Dr. Fauci has overseen an
extensive research portfolio devoted to preventing, diagnosing, and treating
infectious and immune-mediated diseases. He is also Chief of the NIAID
Laboratory of Immunoregulation, where he has made numerous important discoveries
related to HIV/AIDS and is one of the most-cited scientists in the field.
Dr. Fauci serves as one of the key advisors to the White House and
Department of Health and Human Services on global AIDS issues, and on
initiatives to bolster medical and public health preparedness against emerging
infectious disease threats such as Ebola and pandemic influenza. He was one
of the principal architects of the President's Emergency Plan for AIDS Relief
(PEPFAR), which has already been responsible for saving millions of lives
throughout the developing world.

#### Kirk Johnson
 Director of Smithsonian National Museum of Natural History (NMNH)

Kirk Johnson is a paleobotanist who spent most of his career at the Denver
Museum of Nature & Science, where he led expeditions that resulted in the
discovery of more than 1,400 fossil sites on all continents. His research
focuses on fossil plants, the extinction of the dinosaurs, and methods for
dating rocks and fossils. He led the Snowmastodon Project, the excavation
of an amazing ice age site discovered by construction workers in Snowmass
Village, in Colorado, on October 14, 2010. This dig was featured in the
NOVA documentary, "Ice Age Death Trap," and in his book, Digging Snowmastodon,
Discovering an Ice Age World in the Colorado Rockies.

#### Tim Miller
 Co-Founder and Executive Creative Director of Blur Studio

Tim Miller is a director, visual effects artist and co-founder of Blur Studio
in Los Angeles. He made his feature film directing debut with Deadpool,
top grossing movie of 2016. He was nominated for the Academy Award for
Best Animated Short Film as co-story writer and executive producer of
the short animated film Gopher Broke (2004). Miller also designed the
opening sequences of The Girl with the Dragon Tattoo and Thor: The Dark World.

#### Nicholas Negroponte
 Co-founder of the MIT Media Lab, Founder of One Laptop Per Child Project

Nicholas Negroponte is the co-founder (with Jerome B. Wiesner) of the MIT
Media Lab (1985), which he directed for its first 20 years. A graduate of
MIT, Negroponte was a pioneer in the field of computer-aided design and
has been a member of the MIT faculty since 1966. He gave the first TED
talk in 1984, as well as 13 since. He is author of the 1995 best seller,
Being Digital, which has been translated into more than 40 languages.
In 2005 he founded the non-profit One Laptop per Child, which deployed
$1 billion of laptops for primary education in the developing world. In the
private sector, Negroponte served on the board of directors of Motorola
(for 15 years) and was general partner in a venture capital firm specializing
in digital technologies for information and entertainment. He has personally
provided start-up funds for more than 40 companies, including Zagats and Wired magazine.

#### Fiona Raby
 Professor of Design and Emerging Technology at Parsons
 Arthur C. Clarke Panelist

Fiona Raby is Professor of Design and Emerging Technology at Parsons,
The New School for Design, and a partner in the design studio Dunne & Raby.
She is also professor of Industrial Design (studio-id2) at the University of
Applied Arts in Vienna (2011-2016). Between 2011-2015 she was Reader in Design
Interactions at the Royal College of Art in London and taught there from 1995
in Architecture, Computer Related Design and Design Interactions. Dunne & Raby
use design as a medium to stimulate discussion and debate amongst designers,
industry and the public about the social, cultural and ethical implications
of existing and emerging technologies. She is co-author, with Anthony Dunne,
of Design Noir (2001) and Speculative Everything (2013). Projects include
Technological Dream Series, No 1: Robots (2007), Designs For An Over
Populated Planet: Foragers (2010), The United Micro Kingdoms (2013),
and The School of Constructed Realities(2015). Their work has been exhibited
at MoMA in New York, the Pompidou Centre in Paris, and the Design Museum in
London, and is in several permanent collections including MoMA,
the Victoria and Albert Museum, and the Austrian Museum of Applied Arts.
Dunne & Raby received the inaugural MIT Media Lab Award in 2015.

#### Martine Rothblatt
 Transhumanist and Founder of Sirius Radio, Geostar and United Therapeutics

Martine Rothblatt is Chairman & Co-CEO of United Therapeutics (UTHR),
and President & CEO of its Lung Biotechnology public benefit company.
She previously created and led Sirius XM as its Chairman & CEO,
after pioneering the civilian satellite navigation industry. She has JD
and MBA degrees from UCLA, and a Ph.D. in Medical Ethics from the Royal
London School of Medicine & Dentistry. Her patented inventions are in
satellites, pharmaceuticals and cognitive software. Rothblatt's recent
books are on manufactured organs (Your Life or Mine, Ashgate) and
cyber ethics (Virtually Human, St. Martin's).

#### Adam Steltzner
 NASA's "rock and roll engineer"

Adam D. Steltzner is an Engineering Fellow at Jet Propulsion Laboratory
and is leading the development of the Sampling System for the Mars2020
project. He most recently led the team that developed the Curiosity Rover's
landing system. Adam joined the JPL in 1991 and has worked on various
projects including Galileo, Cassini, Mars Pathfinder, Champollion, and
Comet Nucleus Sample Return, Mars Exploration Rovers and the Mars
Science Laboratory.

#### Bruce Sterling
 Two-Time Hugo Award-Winning Sci-Fi Author and Cyberpunk

A futurist, journalist, science-fiction author and design critic,
Bruce Sterling is best known for his novels and his seminal work on
the Mirrorshades anthology, which defined the cyberpunk genre. He is
currently the Curator of the Casa Jasmina project at the Turin Fab Lab.
Nonfiction works include The Hacker Crackdown: Law and Disorder on the
Electronic Frontier; Tomorrow Now: Envisioning the Next Fifty Years;
and Shaping Things. He is a contributing editor of Wired magazine,
and also writes a weblog. Sterling was formerly the "visionary in
residence" at Art Center College of Design in Pasadena and the guest
curator for the Share Festival of Digital Art and Culture in Torino,
Italy, as well as the visionary in residence at the Sandberg Instituut
in Amsterdam. In 2011, he returned to Art Center as visionary in
residence to run a special project on augmented reality. Bruce Sterling
has been featured in many prestigious venues including Nightline, The
Late Show and Time, Newsweek and The Wall Street Journal among others.

#### Aditya Sood
 President of Simon Kinberg's production company, Genre Films

Aditya Sood is the President of Simon Kinberg's production company,
Genre Films. He is a producer of the Golden Globe winning and Oscar
nominated film, THE MARTIAN, and an Executive Producer of the
2016 film DEADPOOL. Prior to working at Genre, he ran Parkes/MacDonald
at DreamWorks and was a Vice President of Production at Warner Bros.
Pictures. Aditya was born in England, and grew up in Seattle, Washington.
He graduated from Pomona College with a degree in Politics, Philosophy
and Economics. He lives in Los Angeles, California with his wife
and their 9 month old son.

#### Andy Weir
 Author, The Martian

Andy Weir was first hired as a programmer for a national laboratory at
age fifteen and has been working as a software engineer ever since.
He is also a lifelong space nerd and a devoted hobbyist of subjects
like relativistic physics, orbital mechanics, and the history of
manned spaceflight. The Martian is his first novel.

### Saturday, April 23

#### Parker Abercrombie
 Software Engineer at NASA's Jet Propulsion Laboratory

Parker Abercrombie is a software engineer at NASA's Jet Propulsion
Laboratory, where he investigates ways that software can transform
how we explore and understand the universe around us. Prior to
joining JPL, Parker worked with teams at NASA and the U.S. Department
of Energy on software to visualize geographic data. Parker holds
an M.A. in Geography from Boston University.

#### Jim Green
 NASA Scientist

Dr. Green became the Director of the Planetary Science Division at
NASA Headquarters in 2006. He received his Ph.D. in Space Physics
from the University of Iowa in 1979 and began working in the
Magnetospheric Physics Branch at NASA's Marshall Space Flight Center
(MSFC) in 1980. At Marshall, Dr. Green developed and managed the Space
Physics Analysis Network that provided scientists all over the world
with rapid access to data, to other scientists, and to specific NASA
computer and information resources. From 1985 to 1992 he was the head
of the National Space Science Data Center (NSSDC) at Goddard Space Flight
Center (GSFC). In 1992, he became the Chief of the Space Science Data
Operations Office until 2005, when he became the Chief of the Science
Proposal Support Office. While at GSFC, Dr. Green was a co-investigator
and the Deputy Project Scientist on the Imager for Magnetopause-to-Aurora
Global Exploration (IMAGE) mission.

#### Alex Menzies
 Software Lead for Augmented and Virtual Reality Development at NASA

Alexander Menzies builds holograms of Mars. Alex serves as the software
lead for augmented and virtual reality development at NASA's Jet Propulsion
Laboratory. His recent work includes an immersive tool used to operate
the Curiosity Mars Rover called OnSight and a head mounted assistant
for astronauts onboard the international space station called SideKick.
Alex is also the principle inventor of the NASA Photogrammetry Pipeline
which is responsible for generating the most accurate 3D models of the
Martian surface ever made.

#### Rebecca Newberger Goldstein
 Philosopher / Author

Rebecca Newberger Goldstein is a philosopher who is also a novelist and
public intellectual. She is the author of ten books, many of which cross
the divide between fiction and non-fiction. Her latest book is Plato
at the Googleplex: Why Philosophy Won't Go Away. She has won numerous awards
for her fiction and scholarship, including a MacArthur fellowship, popularly
known as the "genius prize," and the National Humanities Medal, awarded
by President Barack Obama.

#### Dava Newman
 NASA Deputy Administrator

Along with NASA Administrator Charles Bolden, Newman is responsible for
providing overall leadership, planning, and policy direction for NASA.
Newman performs the duties and exercises the powers delegated by the
administrator, assists the administrator in making final agency decisions,
and acts for the administrator in his absence by performing all necessary
functions to govern NASA operations and exercises the powers vested in
the agency by law. Newman also is responsible for articulating the
agency's vision and representing NASA to the Executive Office of the
President, Congress, heads of federal and other appropriate government
agencies, international organizations, and external organizations and
communities. Newman's research studies were carried out through space
flight experiments, ground-based simulations, and mathematical modeling.
Her latest research efforts included: advanced space suit design,
dynamics and control of astronaut motion, mission analysis, and engineering
systems design and policy analysis. She also had ongoing efforts in assistive
technologies to augment human locomotion here on Earth.

#### Sara Seager
 Planetary Scientist and Astrophysicist

Professor Sara Seager is a planetary scientist and astrophysicist.
She has been a pioneer in the vast and unknown world of exoplanets,
planets that orbit stars other than the sun. Her ground-breaking research
ranges from the detection of exoplanet atmospheres to innovative theories
about life on other worlds to development of novel space mission concepts.
Now, dubbed an "astronomical Indiana Jones", she on a quest after the field's
holy grail, the discovery of a true Earth twin. Dr. Seager earned her PhD
from Harvard University and is now the Class of 1941 Profesor of Planetary
Science and Professor of Physics at the Massachusetts Institute of Technology.
Professor Seager is a 2013 MacArthur Fellow and was named in Time
Magazine's 25 Most Influential in Space in 2012.

#### Seth Shostak
 Director of Search for Extraterrestrial Intelligence (SETI)

Senior Astronomer at the SETI Institute, in Mountain View, California,
Seth has an undergraduate degree in physics from Princeton University,
and a doctorate in astronomy from the California Institute of Technology.
Seth has conducted radio astronomy research on galaxies, and has published
papers in professional journals and more than four hundred articles on
various topics including astronomy and technology. He lectures at Stanford
and was a Distinguished Speaker for the American Institute of Aeronautics
and Astronautics and was Chair of the International Academy of Astronautics'
SETI Permanent Committee. Seth's most recent tome is Confessions of an
Alien Hunter: A Scientist's Search for Extraterrestrial Intelligence
(National Geographic).

#### Aditya Sood
 President of Simon Kinberg's production company, Genre Films

Aditya Sood is the President of Simon Kinberg's production company,
Genre Films. He is a producer of the Golden Globe winning and Oscar
nominated film, THE MARTIAN, and an Executive Producer of the 2016 film
DEADPOOL. Prior to working at Genre, he ran Parkes/MacDonald at DreamWorks
and was a Vice President of Production at Warner Bros. Pictures. Aditya was
born in England, and grew up in Seattle, Washington. He graduated from Pomona
College with a degree in Politics, Philosophy and Economics. He lives in
Los Angeles, California with his wife and their 9 month old son.

#### Marco Tempest
 Cyber Illusionist

Marco Tempest is a cyber illusionist, combining magic and technology to
produce astonishing illusions. He began his performing career as a stage
magician and manipulator, winning many awards and establishing an international
reputation as one of the world¹s most unique performers. His interest in
computer generated imagery led him to incorporating video and digital
technology in his work and the development of a new form of contemporary
illusion. The expansion of the internet and social media provided more
opportunities for digital illusions and ways of interacting with audiences
and creating magically augmented realities. Marco is a keen advocate of the
open source community, working with artists, writers and technologists to
create new experiences and research the practical uses of the technology
of illusion. Marco continues to perform around the world, is a media
consultant on the subject of magic and illusion and lectures at
international conferences on the psychology of deception and creative
thinking. Marco is a Directors Fellow at the MIT Media Lab

#### Tony Valderramma
 Software Engineer

Tony Valderrama is a software engineer at NASA's Jet Propulsion Laboratory,
where he develops science and operations software for robotic missions.
Tony holds a B.S. in Computer Science from the Massachusetts Institute of Technology.

#### Frans de Waal
 Professor of Primate Behavior, Emory University / Author

Frans de Waal has been named one of Time magazine's 100 Most Influential
People. The author of Our Inner Ape, among many other works, he is the
C. H. Candler Professor in Emory University's Psychology Department and
director of the Living Links Center at the Yerkes National Primate
Research Center. He lives in Atlanta, Georgia.


text:
package com.shocktrade.server.trading

import java.util.Date

import com.ldaniels528.commons.helpers.OptionHelper._
import com.ldaniels528.tabular.Tabular
import com.ldaniels528.tabular.formatters.FormatHandler
import com.shocktrade.models.contest.OrderTerms.OrderTerm
import com.shocktrade.models.contest.PriceTypes.PriceType
import com.shocktrade.models.contest._
import com.shocktrade.models.profile.{UserProfileDAO, UserProfiles}
import com.shocktrade.server.trading.Outcome.{Failed, Succeeded}
import com.shocktrade.services.util.DateUtil._
import com.shocktrade.services.yahoofinance.YFIntraDayQuotesService.YFIntraDayQuote
import com.shocktrade.services.yahoofinance.{YFIntraDayQuotesService, YFStockQuoteService}
import com.shocktrade.util.{ConcurrentCache, DateUtil}
import org.joda.time.DateTime
import play.api.Logger
import play.libs.Akka
import reactivemongo.bson.BSONObjectID

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.language.postfixOps

/**
 * Trading Order Processor
 * @author lawrence.daniels@gmail.com
 */
object OrderProcessor {
  private val tabular = new Tabular().add(BSONObjectIDHandler)
  private val daysCloseLabels = Map(false -> "Open", true -> "Closed")
  private val quoteCache = ConcurrentCache[String, Seq[YFIntraDayQuote]](2.hours)
  private implicit val ec = Akka.system().dispatcher

  /**
   * Processes the given contest
   */
  def processContest(c: Contest, asOfDate: Date)(implicit ec: ExecutionContext): Future[Int] = {
    // compute the market close time (+20 minutes for margin of error)
    val tradingClose = new DateTime(getTradeStopTime()).plusMinutes(20).toDate

    // gather the outcomes
    val allOutcomes = for {
    // unwrap the claiming outcomes
      claimOutcomes <- processOrders(c, asOfDate, isDaysClose = isTradingActive(asOfDate))

      // close any expired orders
      closeOrders <- ContestDAO.closeExpiredOrders(c, asOfDate)

      // compute the updated count
      updatedCount = (claimOutcomes map { case (_, outcome) => if (outcome.isSuccess) 1 else 0 } sum) + closeOrders

    } yield (claimOutcomes, closeOrders, updatedCount)

    // is this contest expired?
    if (c.expirationTime.exists(expTime => tradingClose >= expTime)) {
      closeContest(c, asOfDate)
    }

    // if successful, display the summary,
    // and return the update count
    allOutcomes map {
      case (claimOutcomes, closeOrders, updatedCount) =>
        showProcessingSummary(c, claimOutcomes, closeOrders, updatedCount)
        updatedCount
    }
  }

  def closeContest(c: Contest, asOfDate: Date)(implicit ec: ExecutionContext) = {
    Logger.info(s"Closing contest '${c.name}' ...")

    val outcome = for {
      closeOrders <- closeAllOpenOrders(c)
      prices <- priceAllHeldSecurities(c)
      sellOff <- liquidateAllHeldSecurities(c, prices, asOfDate)
      closedContest <- ContestDAO.closeContest(c) map (_ orDie "Contest could not be closed")
      refunds <- refundTheProceedsToParticipants(closedContest)
      awards <- applyAwards(closedContest)
    } yield (sellOff, closedContest)

    // display the results
    outcome.map { case (sellOff, _) =>
      Logger.info(s"Contest '${c.name}' is closed.")
      tabular.transform(sellOff) foreach (info(c, _))
    }
    outcome
  }

  private def applyAwards(c: Contest)(implicit ec: ExecutionContext) = {
    Future.sequence {
      AwardsProcessor.qualifyAwards(c) map { case (participant, awards) =>
        UserProfileDAO.applyAwards(participant.id, awards)
      }
    }
  }

  private def closeAllOpenOrders(c: Contest)(implicit ec: ExecutionContext) = {
    info(c, "[1] Close all active orders")
    Future.sequence(c.participants.flatMap(participant => participant.orders map (order => ContestDAO.closeOrder(c.id, participant.id, order.id))))
  }

  private def priceAllHeldSecurities(c: Contest)(implicit ec: ExecutionContext) = {
    info(c, "[2] Price all currently held securities")
    val parameters = YFStockQuoteService.getParams("symbol", "exchange", "lastTrade", "tradeDate")
    val tickers = c.participants.flatMap(_.positions.map(_.symbol)).distinct
    Future.sequence(tickers.sliding(32, 32) map { symbols =>
      val quotes = YFStockQuoteService.getQuotes(symbols, parameters)
      quotes.map(_ map (q => Pricing(q.symbol, q.exchange, q.lastTrade, q.tradeDate)))
    } toSeq) map (_.flatten)
  }

  private def liquidateAllHeldSecurities(c: Contest, prices: Seq[Pricing], asOfDate: Date)(implicit ec: ExecutionContext) = {
    info(c, "[3] Liquidate all currently held securities")

    // build the mapping of all prices
    val quotes = Map(prices.map(q => (q.symbol, q)): _*)
    //tabular.transform(prices) foreach (info(c, _))

    // sell-off all positions
    Future.sequence(c.participants flatMap { participant =>
      participant.positions map { pos =>
        // create a fake work-order
        val workOrder = WorkOrder(
          id = pos.id,
          participant = participant,
          accountType = pos.accountType,
          symbol = pos.symbol,
          exchange = pos.exchange,
          orderTime = asOfDate,
          orderTerm = OrderTerms.GOOD_FOR_DAY,
          orderType = OrderTypes.SELL,
          price = for {q <- quotes.get(pos.symbol); p <- q.lastTrade} yield p,
          priceType = PriceTypes.MARKET,
          quantity = pos.quantity,
          commission = Commissions.getCommission(PriceTypes.MARKET),
          emailNotify = true,
          partialFulfillment = false,
          marginAccount = participant.marginAccount
        )

        // create a fake claim
        val claim = Claim(
          symbol = pos.symbol,
          exchange = pos.exchange,
          price = pos.pricePaid,
          quantity = pos.quantity,
          commission = Commissions.getCommission(PriceTypes.MARKET),
          purchaseTime = asOfDate,
          workOrder = workOrder)

        // liquidate the asset
        ContestDAO.reducePosition(c, claim, asOfDate) map { outcome =>
          Liquidation(participant.name, pos.symbol, pos.pricePaid, pos.quantity, workOrder.price, outcome)
        }
      }
    })
  }

  private def refundTheProceedsToParticipants(c: Contest)(implicit ec: ExecutionContext) = {
    Future.sequence(c.participants map { participant =>
      val totalCash = participant.cashAccount.cashFunds + participant.marginAccount.map(_.cashFunds).getOrElse(BigDecimal(0.0))
      UserProfiles.deductFunds(participant.id, -totalCash)
    })
  }

  /**
   * Processes the given orders
   */
  private def processOrders(c: Contest, asOfDate: Date, isDaysClose: Boolean)(implicit ec: ExecutionContext) = {
    info(c, s"Processing Orders as of ${new DateTime(asOfDate).toString("MM/dd/yyyy hh:mm:ss")} [Market ${daysCloseLabels(isDaysClose)}]")

    // if it's day's close, grab all orders; otherwise, all non-Market Close orders
    val orders = ContestDAO.getOpenWorkOrders(c, asOfDate)
    if (orders.nonEmpty) {
      info(c, s"${orders.size} eligible order(s) found")
      showOrders(c, orders)

      // get the common asset quotes
      val quotes = getEligibleStockQuotes(c, orders, asOfDate)

      // process the market close orders
      val eligibleClaims = orders flatMap { o =>
        getEligibleClaim(c, o, asOfDate, quotes.filter(_.symbol == o.symbol))
      }

      // display the eligible claims
      info(c, s"Attempting fulfillment on ${eligibleClaims.size} eligible claim(s)")
      showEligibleClaims(c, eligibleClaims)

      // update positions for processed orders
      val outcomes = fulfillOrders(c, asOfDate, eligibleClaims)
      showOrderFulfillment(c, outcomes)
      outcomes
    }
    else Future.successful(Nil)
  }

  private def showOrders(c: Contest, orders: Seq[WorkOrder]): Unit = {
    tabular.transform(orders map { o =>
      DisplayOrder(o.participant.name, o.symbol, o.priceType, o.price, o.quantity, o.orderTime, o.orderTerm)
    }) foreach (info(c, _))
  }

  case class DisplayOrder(player: String, symbol: String, priceType: PriceType, price: Option[BigDecimal], quantity: BigDecimal, time: Date, term: OrderTerm)

  private def showEligibleClaims(c: Contest, eligibleClaims: Seq[Claim]) {
    tabular.transform(eligibleClaims.map { ec =>
      DisplayEligibleClaim(ec.workOrder.participant.name, ec.symbol, ec.price, ec.quantity, ec.purchaseTime, ec.workOrder.orderTerm)
    }) foreach (info(c, _))
  }

  case class DisplayEligibleClaim(player: String, symbol: String, price: BigDecimal, quantity: BigDecimal, time: Date, term: OrderTerm)

  private def showOrderFulfillment(c: Contest, result: Future[Seq[(Claim, Outcome)]]) {
    result.map {
      _ map { case (claim, o) =>
        DisplayClaimOutcome(claim.workOrder.participant.name, claim.symbol, claim.price, claim.quantity, claim.purchaseTime, o match {
          case Succeeded(n) => "Claimed"
          case Failed(message, _) => message
        })
      }
    } foreach { outcomes =>
      tabular.transform(outcomes) foreach (info(c, _))
    }
  }

  case class DisplayClaimOutcome(player: String, symbol: String, price: BigDecimal, quantity: BigDecimal, time: Date, status: String)

  private def getEligibleClaim(c: Contest, wo: WorkOrder, asOfDate: Date, quotes: Seq[WorkQuote]): Option[Claim] = {
    var qualifications: List[DisplayQualification] = Nil

    // generate the list of potential claims
    val potentials = quotes.foldLeft[List[Claim]](Nil) { (list, q) =>
      // is it a valid claim?
      val (isGoodTime, time) = isEligibleTime(c, wo, q, asOfDate)
      val (isGoodVolume, quantity) = isEligibleVolume(c, wo, q)
      val (isGoodPrice, price) = isEligiblePrice(c, wo, q)

      qualifications = DisplayQualification(wo.symbol, wo.priceType, time, isGoodTime, price, isGoodPrice, quantity, isGoodVolume) :: qualifications

      // if the required volume is satisfied, and
      // the price type is either not LIMIT or the price is satisfied, then claim it
      if (isGoodTime && isGoodVolume && isGoodPrice)
        Claim(
          symbol = wo.symbol,
          exchange = wo.exchange,
          price = price,
          quantity = quantity,
          commission = wo.commission,
          purchaseTime = time,
          workOrder = wo) :: list
      else
        list
    }

    //info(c, "Qualifications:")
    //tabular.transform(qualifications) foreach(info(c, _))

    // one claim per work order ID,
    // produce the lowest price or the highest quantity
    val claim =
      if (wo.partialFulfillment)
        potentials.sortBy(-_.quantity).headOption
      else
        potentials.sortBy(_.price).headOption

    // if we can claim a higher quantity ...
    // remember we'll have to dollar-cost average the prices
    if (potentials.nonEmpty && (wo.quantity >= potentials.map(_.quantity).sum))
      dollarCostAverage(potentials)
    else
      claim
  }

  case class DisplayQualification(symbol: String, priceType: PriceType, time: Date, isGoodTime: Boolean, price: Double, isGoodPrice: Boolean, quantity: Long, isGoodVolume: Boolean)

  private def dollarCostAverage(potentials: Seq[Claim]) = {
    val totalQuantity = potentials.map(_.quantity).sum
    val totalCost = potentials.map(c => c.price * c.quantity.toDouble).sum
    val averagePrice = totalCost / totalQuantity.toDouble
    potentials.headOption.map(_.copy(price = averagePrice, quantity = totalQuantity))
  }

  private def showProcessingSummary(c: Contest, outcomes: Seq[(Claim, Outcome)], closedOrders: Int, updatedCount: Int) = {
    // display the summary information
    info(c, s"$updatedCount claimed of ${outcomes.length} qualified order(s)")

    // close orders
    if (closedOrders > 0) info(c, s"$closedOrders order(s) closed")

    // display the failures
    outcomes foreach {
      case (claim, outcome) =>
        info(c, s"Order #${claim.workOrder.id}: outcome = $outcome")
    }
  }

  private def isEligiblePrice(c: Contest, wo: WorkOrder, q: WorkQuote): (Boolean, Double) = {
    val isGood = wo.priceType match {
      case PriceTypes.LIMIT =>
        wo.orderType match {
          case OrderTypes.BUY => wo.price exists (q.price <= _)
          case OrderTypes.SELL => wo.price exists (q.price >= _)
        }
      case PriceTypes.STOP_LIMIT =>
        wo.orderType match {
          case OrderTypes.BUY => wo.price exists (q.price <= _)
          case OrderTypes.SELL => wo.price exists (q.price >= _)
        }
      case _ => true
    }
    ((q.price > 0.00d) && isGood, q.price)
  }

  private def isEligibleTime(c: Contest, wo: WorkOrder, q: WorkQuote, asOfDate: Date): (Boolean, Date) = {
    val isGood = wo.priceType match {
      case PriceTypes.MARKET_ON_CLOSE => !TradingClock.isTradingActive(asOfDate) || asOfDate > DateUtil.getTradeStopTime(asOfDate)
      case _ => wo.orderTime <= q.tradeDateTime
    }
    (isGood, q.tradeDateTime)
  }

  private def isEligibleVolume(c: Contest, wo: WorkOrder, q: WorkQuote): (Boolean, Long) = {
    if (q.totalVolume >= wo.quantity) (true, wo.quantity) else (wo.partialFulfillment && q.totalVolume > 0, q.totalVolume)
  }

  private def getEligibleStockQuotes(c: Contest, orders: Seq[WorkOrder], asOfDate: Date): Seq[WorkQuote] = {
    // get the distinct set of symbols we need
    val symbols = orders.map(_.symbol).distinct
    info(c, s"Retrieving quotes for symbols: ${symbols.mkString(",")}")

    // attempt to retrieve as many quotes from cache as we can
    val cachedQuotes = Map((for {
      symbol <- symbols
      prices <- quoteCache.get(symbol)
    } yield (symbol, prices)): _*)

    info(c, s"${cachedQuotes.size} quote(s) retrieved from cache")

    // retrieve all remaining pricing quotes from the service
    val svcQuotes = Map(symbols.filterNot(cachedQuotes.contains) map { symbol =>
      (symbol, YFIntraDayQuotesService.getQuotes(symbol))
    }: _*)

    info(c, s"${svcQuotes.size} quote(s) retrieved from the service layer")

    // store the service quote in the cache
    svcQuotes.foreach { case (symbol, prices) => quoteCache.put(symbol, prices) }
    //tabular.transform(svcQuotes.values.toSeq.flatten) foreach (info(c, _))

    // combine the service and cached quotes
    val quotes = svcQuotes ++ cachedQuotes

    // build the collection of stock quotes for claiming
    val stockQuotes = (orders flatMap { o =>
      quotes.get(o.symbol) map { prices =>
        val totalVolume = prices.map(_.volume).sum
        prices map (p => WorkQuote(o.symbol, o.exchange, p.close, p.timestamp, p.volume, totalVolume))
      }
    }).flatten

    // display the stock quotes
    stockQuotes
  }

  private def fulfillOrders(c: Contest, asOfDate: Date, claims: Seq[Claim])(implicit ec: ExecutionContext) = {
    Future.sequence(claims map { claim =>
      for {
      // perform the BUY or SELL
        outcome <- claim.workOrder.orderType match {
          case OrderTypes.BUY =>
            info(c, s"[${claim.workOrder.playerId}] Increasing position of ${claim.symbol} x ${claim.quantity}")
            ContestDAO.increasePosition(c, claim, asOfDate) map {
              case o@Failed(error, _) if error.contains("qualifying position") =>
                if (!claim.workOrder.participant.perks.contains(PerkTypes.PRFCTIMG)) {
                  ContestDAO.failOrder(c, claim.workOrder, error, asOfDate)
                }
                o
              case outcome => outcome
            }
          case OrderTypes.SELL =>
            info(c, s"[${claim.workOrder.playerId}] Reducing position of ${claim.symbol} x ${claim.quantity}")
            ContestDAO.reducePosition(c, claim, asOfDate) map {
              case o@Failed(error, _) if error.contains("qualifying position") =>
                if (!claim.workOrder.participant.perks.contains(PerkTypes.PRCHEMNT)) {
                  ContestDAO.failOrder(c, claim.workOrder, error, asOfDate)
                }
                o
              case outcome => outcome
            }
        }
      } yield (claim, outcome)
    })
  }

  private def isTradingActive(asOfDate: Date) = DateUtil.isTradingActive(asOfDate)

  private def info(c: Contest, message: String) = Logger.info(s"${c.name}: $message")

  private def error(c: Contest, message: String, e: Throwable = null) = Logger.error(s"${c.name}: $message", e)

  /**
   * BSON Object ID Handler for Tabular
   * @author lawrence.daniels@gmail.com
   */
  object BSONObjectIDHandler extends FormatHandler {
    override def handles(value: Any) = value match {
      case id: BSONObjectID => true
      case _ => false
    }

    override def format(value: Any): Option[String] = value match {
      case _id: BSONObjectID => Some(_id.stringify)
      case _ => None
    }
  }

  case class Liquidation(player: String, symbol: String, pricePaid: BigDecimal, quantity: Long, marketPrice: Option[BigDecimal], outcome: Outcome)

  case class Pricing(symbol: String, exchange: Option[String], lastTrade: Option[Double], tradeDate: Option[Date])

  /**
   * Generically represents the common elements of a stock quote
   * @author lawrence.daniels@gmail.com
   */
  case class WorkQuote(symbol: String,
                       exchange: String,
                       price: Double,
                       tradeDateTime: Date,
                       volume: Long,
                       totalVolume: Long)

}

redpajama_set_name: RedPajamaGithub


text:
package com.microsoft.bingads.examples;

import java.rmi.*;
import java.util.ArrayList;

import com.microsoft.bingads.*;
import com.microsoft.bingads.campaignmanagement.*;

public class CreateKeywordsAds {

    static AuthorizationData authorizationData;
    static ServiceClient<ICampaignManagementService> CampaignService;

    private static java.lang.String UserName = "<UserNameGoesHere>";
    private static java.lang.String Password = "<PasswordGoesHere>";
    private static java.lang.String DeveloperToken = "<DeveloperTokenGoesHere>";
    private static long CustomerId = <CustomerIdGoesHere>;
    private static long AccountId = <AccountIdGoesHere>;

    public static void main(java.lang.String[] args) {

        try
        {
            authorizationData = new AuthorizationData();
            authorizationData.setDeveloperToken(DeveloperToken);
            authorizationData.setAuthentication(new PasswordAuthentication(UserName, Password));
            authorizationData.setCustomerId(CustomerId);
            authorizationData.setAccountId(AccountId);

            CampaignService = new ServiceClient<ICampaignManagementService>(
                    	authorizationData,
                        ICampaignManagementService.class);

             // Specify a campaign.

             Campaign campaign = new Campaign();
             campaign.setName("Winter Clothing " + System.currentTimeMillis());
             campaign.setDescription("Winter clothing line.");
             campaign.setBudgetType(BudgetLimitType.MONTHLY_BUDGET_SPEND_UNTIL_DEPLETED);
             campaign.setMonthlyBudget(1000.00);
             campaign.setTimeZone("PacificTimeUSCanadaTijuana");
             campaign.setDaylightSaving(true);

             // Specify an ad group.

             AdGroup adGroup = new AdGroup();
             adGroup.setName("Women's Heated Ski Glove Sale");
             ArrayList<AdDistribution> adDistribution = new ArrayList<AdDistribution>();
             adDistribution.add(AdDistribution.SEARCH);
             adGroup.setAdDistribution(adDistribution);
             adGroup.setBiddingModel(BiddingModel.KEYWORD);
             adGroup.setPricingModel(PricingModel.CPC);
             adGroup.setStartDate(null);
             adGroup.setEndDate(new com.microsoft.bingads.campaignmanagement.Date());
             adGroup.getEndDate().setDay(31);
             adGroup.getEndDate().setMonth(12);
             adGroup.getEndDate().setYear(2015);
             Bid exactMatchBid = new Bid();
             exactMatchBid.setAmount(0.09);
             adGroup.setExactMatchBid(exactMatchBid);
             Bid phraseMatchBid = new Bid();
             phraseMatchBid.setAmount(0.07);
             adGroup.setPhraseMatchBid(phraseMatchBid);
             adGroup.setLanguage("English");

             // In this example only the second keyword should succeed. The Text of the first keyword exceeds the limit,
             // and the third keyword is a duplicate of the second keyword.

             ArrayOfKeyword keywords = new ArrayOfKeyword();

             Keyword keyword1 = new Keyword();
             keyword1.setBid(new Bid());
             keyword1.getBid().setAmount(0.47);
             keyword1.setParam2("10% Off");
             keyword1.setMatchType(MatchType.BROAD);
             keyword1.setText("Brand-A Gloves Brand-A Gloves Brand-A Gloves Brand-A Gloves Brand-A Gloves " +
                                 "Brand-A Gloves Brand-A Gloves Brand-A Gloves Brand-A Gloves Brand-A Gloves " +
                                 "Brand-A Gloves Brand-A Gloves Brand-A Gloves Brand-A Gloves Brand-A Gloves");
             keywords.getKeywords().add(keyword1);

             Keyword keyword2 = new Keyword();
             keyword2.setBid(new Bid());
             keyword2.getBid().setAmount(0.47);
             keyword2.setParam2("10% Off");
             keyword2.setMatchType(MatchType.PHRASE);
             keyword2.setText("Brand-A Gloves");
             keywords.getKeywords().add(keyword2);

             Keyword keyword3 = new Keyword();
             keyword3.setBid(new Bid());
             keyword3.getBid().setAmount(0.47);
             keyword3.setParam2("10% Off");
             keyword3.setMatchType(MatchType.PHRASE);
             keyword3.setText("Brand-A Gloves");
             keywords.getKeywords().add(keyword3);

             // In this example only the second ad should succeed. The Title of the first ad is empty and not valid,
             // and the third ad is a duplicate of the second ad.

             ArrayOfAd ads = new ArrayOfAd();

             TextAd ad1 = new TextAd();
             ad1.setDestinationUrl("http://www.alpineskihouse.com/winterglovesale");
             ad1.setDisplayUrl("AlipineSkiHouse.com");
             ad1.setText("Huge Savings on heated gloves.");
             ad1.setTitle("");
             ads.getAds().add(ad1);

             TextAd ad2 = new TextAd();
             ad2.setDestinationUrl("http://www.alpineskihouse.com/winterglovesale");
             ad2.setDisplayUrl("AlipineSkiHouse.com");
             ad2.setText("Huge Savings on heated gloves.");
             ad2.setTitle("Winter Glove Sale");
             ads.getAds().add(ad2);

             TextAd ad3 = new TextAd();
             ad3.setDestinationUrl("http://www.alpineskihouse.com/winterglovesale");
             ad3.setDisplayUrl("AlipineSkiHouse.com");
             ad3.setText("Huge Savings on heated gloves.");
             ad3.setTitle("Winter Glove Sale");
             ads.getAds().add(ad3);

             // Add the campaign, ad group, keywords, and ads

             ArrayOfCampaign campaigns = new ArrayOfCampaign();
             campaigns.getCampaigns().add(campaign);
             ArrayOflong campaignIds = addCampaigns(AccountId, campaigns);

             ArrayOfAdGroup adGroups = new ArrayOfAdGroup();
             adGroups.getAdGroups().add(adGroup);
             ArrayOflong adGroupIds = addAdGroups(campaignIds.getLongs().get(0), adGroups);

             AddKeywordsResponse addKeywordsResponse = addKeywords(adGroupIds.getLongs().get(0), keywords);
             ArrayOfNullableOflong keywordIds = addKeywordsResponse.getKeywordIds();
             ArrayOfBatchError keywordErrors = addKeywordsResponse.getPartialErrors();

             AddAdsResponse addAdsResponse = addAds(adGroupIds.getLongs().get(0), ads);
             ArrayOfNullableOflong adIds = addAdsResponse.getAdIds();
             ArrayOfBatchError adErrors = addAdsResponse.getPartialErrors();

             // Print the new assigned campaign and ad group identifiers

             printCampaignIdentifiers(campaignIds);
             printAdGroupIdentifiers(adGroupIds);

             // Print the new assigned keyword and ad identifiers, as well as any partial errors

             printKeywordResults(keywords, keywordIds, keywordErrors);
             printAdResults(ads, adIds, adErrors);

             // Delete the campaign, ad group, keyword, and ad that were previously added.
             // You should remove this line if you want to view the added entities in the
             // Bing Ads web application or another tool.

             deleteCampaigns(AccountId, campaignIds);
             System.out.printf("Deleted CampaignId %d\n", campaignIds.getLongs().get(0));

         // Campaign Management service operations can throw AdApiFaultDetail.
         } catch (AdApiFaultDetail_Exception fault) {
             System.out.println("The operation failed with the following faults:\n");

             for (AdApiError error : fault.getFaultInfo().getErrors().getAdApiErrors())
             {
                 System.out.printf("AdApiError\n");
                 System.out.printf("Code: %d\nError Code: %s\nMessage: %s\n\n", error.getCode(), error.getErrorCode(), error.getMessage());
             }

         // Campaign Management service operations can throw ApiFaultDetail.
         } catch (ApiFaultDetail_Exception ex) {
             System.out.println("The operation failed with the following faults:\n");

             for (BatchError error : ex.getFaultInfo().getBatchErrors().getBatchErrors())
             {
                 System.out.printf("BatchError at Index: %d\n", error.getIndex());
                 System.out.printf("Code: %d\nMessage: %s\n\n", error.getCode(), error.getMessage());
             }

             for (OperationError error : ex.getFaultInfo().getOperationErrors().getOperationErrors())
             {
                 System.out.printf("OperationError\n");
                 System.out.printf("Code: %d\nMessage: %s\n\n", error.getCode(), error.getMessage());
             }
         } catch (RemoteException ex) {
             System.out.println("Service communication error encountered: ");
             System.out.println(ex.getMessage());
             ex.printStackTrace();
         } catch (Exception ex) {
             System.out.println("Error encountered: ");
             System.out.println(ex.getMessage());
             ex.printStackTrace();
         }
     }

     // Adds one or more campaigns to the specified account.

     static ArrayOflong addCampaigns(long accountId, ArrayOfCampaign campaigns) throws RemoteException, Exception
     {
         AddCampaignsRequest request = new AddCampaignsRequest();

         // Set the request information.

         request.setAccountId(accountId);
         request.setCampaigns(campaigns);

         return CampaignService.getService().addCampaigns(request).getCampaignIds();
     }

     // Deletes one or more campaigns from the specified account.

     static void deleteCampaigns(long accountId, ArrayOflong campaignIds) throws RemoteException, Exception
     {
    	 DeleteCampaignsRequest request = new DeleteCampaignsRequest();

         // Set the request information.

         request.setAccountId(accountId);
         request.setCampaignIds(campaignIds);

         CampaignService.getService().deleteCampaigns(request);
     }

     // Adds one or more ad groups to the specified campaign.

     static ArrayOflong addAdGroups(long campaignId, ArrayOfAdGroup adGroups) throws RemoteException, Exception
     {
         AddAdGroupsRequest request = new AddAdGroupsRequest();

         // Set the request information.

         request.setCampaignId(campaignId);
         request.setAdGroups(adGroups);

         return CampaignService.getService().addAdGroups(request).getAdGroupIds();
     }

     // Adds one or more keywords to the specified ad group.

     static AddKeywordsResponse addKeywords(long adGroupId, ArrayOfKeyword keywords) throws RemoteException, Exception
     {
         AddKeywordsRequest request = new AddKeywordsRequest();

         // Set the request information.

         request.setAdGroupId(adGroupId);
         request.setKeywords(keywords);

         return CampaignService.getService().addKeywords(request);
     }

     // Adds one or more ads to the specified ad group.

     static AddAdsResponse addAds(long adGroupId, ArrayOfAd ads) throws RemoteException, Exception
     {
         AddAdsRequest request = new AddAdsRequest();

         // Set the request information.

         request.setAdGroupId(adGroupId);
         request.setAds(ads);

         return CampaignService.getService().addAds(request);
     }

     // Prints the campaign identifiers for each campaign added.

     static void printCampaignIdentifiers(ArrayOflong campaignIds)
     {
         if (campaignIds == null)
         {
             return;
         }

         for (long id : campaignIds.getLongs())
         {
             System.out.printf("Campaign successfully added and assigned CampaignId %d\n\n", id);
         }
     }

     // Prints the ad group identifiers for each ad group added.

     static void printAdGroupIdentifiers(ArrayOflong adGroupIds)
     {
         if (adGroupIds == null)
         {
             return;
         }

         for (long id : adGroupIds.getLongs())
         {
             System.out.printf("AdGroup successfully added and assigned AdGroupId %d\n\n", id);
         }
     }

     // Prints the keyword identifiers, as well as any partial errors

     static void printKeywordResults(ArrayOfKeyword keywords, ArrayOfNullableOflong keywordIds, ArrayOfBatchError partialErrors)
     {
         if (keywordIds == null)
         {
             return;
         }

         // Print the identifier of each successfully added keyword.

         for (int index = 0; index < keywords.getKeywords().size(); index++ )
         {
             // The array of keyword identifiers equals the size of the attempted keywords. If the element
             // is not null, the keyword at that index was added successfully and has a keyword identifer.

             if (keywordIds.getLongs().get(index) != null)
             {
                 System.out.printf("Keyword[%d] (Text:%s) successfully added and assigned KeywordId %d\n",
                     index,
                     keywords.getKeywords().get(index).getText(),
                     keywordIds.getLongs().get(index));
             }
         }

         // Print the error details for any keyword not successfully added.
         // Note also that multiple error reasons may exist for the same attempted keyword.

         for (BatchError error : partialErrors.getBatchErrors())
         {
             // The index of the partial errors is equal to the index of the list
             // specified in the call to AddKeywords.

             System.out.printf("\nKeyword[%d] (Text:%s) not added due to the following error:\n",
            		 error.getIndex(), keywords.getKeywords().get(error.getIndex()).getText());

             System.out.printf("\tIndex: %d\n", error.getIndex());
             System.out.printf("\tCode: %d\n", error.getCode());
             System.out.printf("\tErrorCode: %s\n", error.getErrorCode());
             System.out.printf("\tMessage: %s\n", error.getMessage());

             // In the case of an EditorialError, more details are available
             if (error.getType().equals("EditorialError") && error.getErrorCode().equals("CampaignServiceEditorialValidationError"))
             {
                 System.out.printf("\tDisapprovedText: %s\n", ((EditorialError)(error)).getDisapprovedText());
                 System.out.printf("\tLocation: %s\n", ((EditorialError)(error)).getLocation());
                 System.out.printf("\tPublisherCountry: %s\n", ((EditorialError)(error)).getPublisherCountry());
                 System.out.printf("\tReasonCode: %d\n", ((EditorialError)(error)).getReasonCode());
             }
         }

         System.out.println("");
     }

     // Prints the ad identifiers, as well as any partial errors

     static void printAdResults(ArrayOfAd ads, ArrayOfNullableOflong adIds, ArrayOfBatchError partialErrors)
     {
         if (adIds == null)
         {
             return;
         }

         java.lang.String[] attributeValues = new java.lang.String[ads.getAds().size()];

         for (int index = 0; index < ads.getAds().size(); index++ )
         {
             // Determine the type of ad. Prepare the corresponding attribute value to be printed,
             // both for successful new ads and partial errors.

             if (ads.getAds().get(index) instanceof TextAd)
             {
                 attributeValues[index] = "Title:" + ((TextAd)ads.getAds().get(index)).getTitle();
             }
             else if (ads.getAds().get(index) instanceof MobileAd)
             {
                 attributeValues[index] = "Title:" + ((MobileAd)ads.getAds().get(index)).getTitle();
             }
             else if (ads.getAds().get(index) instanceof ProductAd)
             {
                 attributeValues[index] = "PromotionalText:" + ((ProductAd)ads.getAds().get(index)).getPromotionalText();
             }
             else
             {
                 attributeValues[index] = "Unknown Ad Type";
             }

             // The array of ad identifiers equals the size of the attempted ads. If the element
             // is not null, the ad at that index was added successfully and has an ad identifer.

             if (adIds.getLongs().get(index) != null)
             {
                 System.out.printf("Ad[%d] (%s) successfully added and assigned AdId %d\n",
                     index,
                     attributeValues[index],
                     adIds.getLongs().get(index));
             }
         }

         // Print the error details for any ad not successfully added.
         // Note also that multiple error reasons may exist for the same attempted ad.

         for (BatchError error : partialErrors.getBatchErrors())
         {
             // The index of the partial errors is equal to the index of the list
             // specified in the call to AddAds.

             System.out.printf("\nAd[%d] (%s) not added due to the following error:\n", error.getIndex(), attributeValues[error.getIndex()]);

             System.out.printf("\tIndex: %d\n", error.getIndex());
             System.out.printf("\tCode: %d\n", error.getCode());
             System.out.printf("\tErrorCode: %s\n", error.getErrorCode());
             System.out.printf("\tMessage: %s\n", error.getMessage());

             // In the case of an EditorialError, more details are available
             if (error.getType().equals("EditorialError") && error.getErrorCode().equals("CampaignServiceEditorialValidationError"))
             {
                 System.out.printf("\tDisapprovedText: %s\n", ((EditorialError)(error)).getDisapprovedText());
                 System.out.printf("\tLocation: %s\n", ((EditorialError)(error)).getLocation());
                 System.out.printf("\tPublisherCountry: %s\n", ((EditorialError)(error)).getPublisherCountry());
                 System.out.printf("\tReasonCode: %d\n", ((EditorialError)(error)).getReasonCode());
             }
         }

         System.out.println("");
     }
 }


text:
import React from 'react'

// const timeline = [{
//   time: 0,
//   fact: ''
// }]
//

export default function Voyeur() {
  return (

  )
}


text:
<?php
/**
* Maths functions
*/

abstract class Maths
{
    /**
    * Find out if the number is of the power of two, for calculating cup competitions.
    *
    * @param mixed $intNum
    */
    public static function isPowerOfTwo($intNum)
    {
        return ($intNum & ($intNum - 1)) == 0;
    }
}
?>


text:
"""Tests for Estimator."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import functools
import itertools
import json
import os
import tempfile

import numpy as np
import six
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf

from tensorflow.contrib.learn.python.learn import metric_spec
from tensorflow.contrib.learn.python.learn.estimators import _sklearn
from tensorflow.contrib.learn.python.learn.estimators import estimator
from tensorflow.contrib.learn.python.learn.estimators import model_fn
from tensorflow.contrib.learn.python.learn.utils import input_fn_utils
from tensorflow.python.framework import ops
from tensorflow.python.saved_model import loader
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.util import compat

_BOSTON_INPUT_DIM = 13
_IRIS_INPUT_DIM = 4

def boston_input_fn(num_epochs=None):
  boston = tf.contrib.learn.datasets.load_boston()
  features = tf.train.limit_epochs(
      tf.reshape(tf.constant(boston.data), [-1, _BOSTON_INPUT_DIM]),
      num_epochs=num_epochs)
  labels = tf.reshape(tf.constant(boston.target), [-1, 1])
  return features, labels

def boston_input_fn_with_queue(num_epochs=None):
  features, labels = boston_input_fn(num_epochs=num_epochs)

  # Create a minimal queue runner.
  fake_queue = tf.FIFOQueue(30, tf.int32)
  queue_runner = tf.train.QueueRunner(fake_queue, [tf.constant(0)])
  tf.train.add_queue_runner(queue_runner)

  return features, labels

def iris_input_fn():
  iris = tf.contrib.learn.datasets.load_iris()
  features = tf.reshape(tf.constant(iris.data), [-1, _IRIS_INPUT_DIM])
  labels = tf.reshape(tf.constant(iris.target), [-1])
  return features, labels

def iris_input_fn_labels_dict():
  iris = tf.contrib.learn.datasets.load_iris()
  features = tf.reshape(tf.constant(iris.data), [-1, _IRIS_INPUT_DIM])
  labels = {
      'labels': tf.reshape(tf.constant(iris.target), [-1])
  }
  return features, labels

def boston_eval_fn():
  boston = tf.contrib.learn.datasets.load_boston()
  n_examples = len(boston.target)
  features = tf.reshape(
      tf.constant(boston.data), [n_examples, _BOSTON_INPUT_DIM])
  labels = tf.reshape(tf.constant(boston.target), [n_examples, 1])
  return tf.concat_v2([features, features], 0), tf.concat_v2([labels, labels],
                                                             0)

def extract(data, key):
  if isinstance(data, dict):
    assert key in data
    return data[key]
  else:
    return data

def linear_model_params_fn(features, labels, mode, params):
  features = extract(features, 'input')
  labels = extract(labels, 'labels')

  assert mode in (
      tf.contrib.learn.ModeKeys.TRAIN,
      tf.contrib.learn.ModeKeys.EVAL,
      tf.contrib.learn.ModeKeys.INFER)
  prediction, loss = (
      tf.contrib.learn.models.linear_regression_zero_init(features, labels)
  )
  train_op = tf.contrib.layers.optimize_loss(
      loss, tf.contrib.framework.get_global_step(), optimizer='Adagrad',
      learning_rate=params['learning_rate'])
  return prediction, loss, train_op

def linear_model_fn(features, labels, mode):
  features = extract(features, 'input')
  labels = extract(labels, 'labels')
  assert mode in (
      tf.contrib.learn.ModeKeys.TRAIN,
      tf.contrib.learn.ModeKeys.EVAL,
      tf.contrib.learn.ModeKeys.INFER)
  if isinstance(features, dict):
    (_, features), = features.items()
  prediction, loss = (
      tf.contrib.learn.models.linear_regression_zero_init(features, labels)
  )
  train_op = tf.contrib.layers.optimize_loss(
      loss, tf.contrib.framework.get_global_step(), optimizer='Adagrad',
      learning_rate=0.1)
  return prediction, loss, train_op

def linear_model_fn_with_model_fn_ops(features, labels, mode):
  """Same as linear_model_fn, but returns `ModelFnOps`."""
  assert mode in (
      tf.contrib.learn.ModeKeys.TRAIN,
      tf.contrib.learn.ModeKeys.EVAL,
      tf.contrib.learn.ModeKeys.INFER)
  prediction, loss = (
      tf.contrib.learn.models.linear_regression_zero_init(features, labels)
  )
  train_op = tf.contrib.layers.optimize_loss(
      loss, tf.contrib.framework.get_global_step(), optimizer='Adagrad',
      learning_rate=0.1)
  return model_fn.ModelFnOps(mode=mode,
                             predictions=prediction,
                             loss=loss,
                             train_op=train_op)

def logistic_model_no_mode_fn(features, labels):
  features = extract(features, 'input')
  labels = extract(labels, 'labels')
  labels = tf.one_hot(labels, 3, 1, 0)
  prediction, loss = (
      tf.contrib.learn.models.logistic_regression_zero_init(features, labels)
  )
  train_op = tf.contrib.layers.optimize_loss(
      loss, tf.contrib.framework.get_global_step(), optimizer='Adagrad',
      learning_rate=0.1)
  return {'class': tf.argmax(prediction, 1), 'prob': prediction}, loss, train_op

VOCAB_FILE_CONTENT = 'emerson\nlake\npalmer\n'
EXTRA_FILE_CONTENT = 'kermit\npiggy\nralph\n'

def _build_estimator_for_export_tests(tmpdir):
  def _input_fn():
    iris = tf.contrib.learn.datasets.load_iris()
    return {
        'feature': tf.constant(iris.data, dtype=tf.float32)
    }, tf.constant(iris.target, shape=[150], dtype=tf.int32)

  feature_columns = [tf.contrib.layers.real_valued_column('feature',
                                                          dimension=4)]

  est = tf.contrib.learn.LinearRegressor(feature_columns)
  est.fit(input_fn=_input_fn, steps=20)

  feature_spec = tf.contrib.layers.create_feature_spec_for_parsing(
      feature_columns)
  export_input_fn = input_fn_utils.build_parsing_serving_input_fn(feature_spec)

  # hack in an op that uses an asset, in order to test asset export.
  # this is not actually valid, of course.
  def export_input_fn_with_asset():
    features, labels, inputs = export_input_fn()

    vocab_file_name = os.path.join(tmpdir, 'my_vocab_file')
    vocab_file = tf.gfile.GFile(vocab_file_name, mode='w')
    vocab_file.write(VOCAB_FILE_CONTENT)
    vocab_file.close()
    hashtable = tf.contrib.lookup.HashTable(
        tf.contrib.lookup.TextFileStringTableInitializer(vocab_file_name), 'x')
    features['bogus_lookup'] = hashtable.lookup(
        tf.to_int64(features['feature']))

    return input_fn_utils.InputFnOps(features, labels, inputs)

  return est, export_input_fn_with_asset

class CheckCallsMonitor(tf.contrib.learn.monitors.BaseMonitor):

  def __init__(self, expect_calls):
    super(CheckCallsMonitor, self).__init__()
    self.begin_calls = None
    self.end_calls = None
    self.expect_calls = expect_calls

  def begin(self, max_steps):
    self.begin_calls = 0
    self.end_calls = 0

  def step_begin(self, step):
    self.begin_calls += 1
    return {}

  def step_end(self, step, outputs):
    self.end_calls += 1
    return False

  def end(self):
    assert (self.end_calls == self.expect_calls and
            self.begin_calls == self.expect_calls)

class EstimatorTest(tf.test.TestCase):

  def testModelFnArgs(self):
    expected_param = {'some_param': 'some_value'}
    expected_config = tf.contrib.learn.RunConfig()
    expected_config.i_am_test = True
    def _argument_checker(features, labels, mode, params, config):
      _, _ = features, labels
      self.assertEqual(tf.contrib.learn.ModeKeys.TRAIN, mode)
      self.assertEqual(expected_param, params)
      self.assertTrue(config.i_am_test)
      return tf.constant(0.), tf.constant(0.), tf.constant(0.)
    est = tf.contrib.learn.Estimator(model_fn=_argument_checker,
                                     params=expected_param,
                                     config=expected_config)
    est.fit(input_fn=boston_input_fn, steps=1)

  def testInvalidModelFn_no_train_op(self):
    def _invalid_model_fn(features, labels):
      # pylint: disable=unused-argument
      w = tf.Variable(42.0, 'weight')
      loss = 100.0 - w
      return None, loss, None
    est = tf.contrib.learn.Estimator(model_fn=_invalid_model_fn)
    with self.assertRaisesRegexp(ValueError, 'Missing training_op'):
      est.fit(input_fn=boston_input_fn, steps=1)

  def testInvalidModelFn_no_loss(self):
    def _invalid_model_fn(features, labels, mode):
      # pylint: disable=unused-argument
      w = tf.Variable(42.0, 'weight')
      loss = 100.0 - w
      train_op = w.assign_add(loss / 100.0)
      predictions = loss
      if mode == tf.contrib.learn.ModeKeys.EVAL:
        loss = None
      return predictions, loss, train_op
    est = tf.contrib.learn.Estimator(model_fn=_invalid_model_fn)
    est.fit(input_fn=boston_input_fn, steps=1)
    with self.assertRaisesRegexp(ValueError, 'Missing loss'):
      est.evaluate(input_fn=boston_eval_fn, steps=1)

  def testInvalidModelFn_no_prediction(self):
    def _invalid_model_fn(features, labels):
      # pylint: disable=unused-argument
      w = tf.Variable(42.0, 'weight')
      loss = 100.0 - w
      train_op = w.assign_add(loss / 100.0)
      return None, loss, train_op
    est = tf.contrib.learn.Estimator(model_fn=_invalid_model_fn)
    est.fit(input_fn=boston_input_fn, steps=1)
    with self.assertRaisesRegexp(ValueError, 'Missing prediction'):
      est.evaluate(input_fn=boston_eval_fn, steps=1)
    with self.assertRaisesRegexp(ValueError, 'Missing prediction'):
      est.predict(input_fn=boston_input_fn)
    with self.assertRaisesRegexp(ValueError, 'Missing prediction'):
      est.predict(
          input_fn=functools.partial(boston_input_fn, num_epochs=1),
          as_iterable=True)

  def testCustomConfig(self):
    test_random_seed = 5783452

    class TestInput(object):

      def __init__(self):
        self.random_seed = 0

      def config_test_input_fn(self):
        self.random_seed = tf.get_default_graph().seed
        return tf.constant([[1.]]), tf.constant([1.])

    config = tf.contrib.learn.RunConfig(tf_random_seed=test_random_seed)
    test_input = TestInput()
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn, config=config)
    est.fit(input_fn=test_input.config_test_input_fn, steps=1)
    # If input_fn ran, it will have given us the random seed set on the graph.
    self.assertEquals(test_random_seed, test_input.random_seed)

  def testCheckInputs(self):
    est = tf.contrib.learn.SKCompat(
        tf.contrib.learn.Estimator(model_fn=linear_model_fn))
    # Lambdas so we have to different objects to compare
    right_features = lambda: np.ones(shape=[7, 8], dtype=np.float32)
    right_labels = lambda: np.ones(shape=[7, 10], dtype=np.int32)
    est.fit(right_features(), right_labels(), steps=1)
    # TODO(wicke): This does not fail for np.int32 because of data_feeder magic.
    wrong_type_features = np.ones(shape=[7., 8.], dtype=np.int64)
    wrong_size_features = np.ones(shape=[7, 10])
    wrong_type_labels = np.ones(shape=[7., 10.], dtype=np.float32)
    wrong_size_labels = np.ones(shape=[7, 11])
    est.fit(x=right_features(), y=right_labels(), steps=1)
    with self.assertRaises(ValueError):
      est.fit(x=wrong_type_features, y=right_labels(), steps=1)
    with self.assertRaises(ValueError):
      est.fit(x=wrong_size_features, y=right_labels(), steps=1)
    with self.assertRaises(ValueError):
      est.fit(x=right_features(), y=wrong_type_labels, steps=1)
    with self.assertRaises(ValueError):
      est.fit(x=right_features(), y=wrong_size_labels, steps=1)

  def testBadInput(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    self.assertRaisesRegexp(ValueError,
                            'Either x or input_fn must be provided.',
                            est.fit, x=None, input_fn=None)
    self.assertRaisesRegexp(ValueError,
                            'Can not provide both input_fn and x or y',
                            est.fit, x='X', input_fn=iris_input_fn)
    self.assertRaisesRegexp(ValueError,
                            'Can not provide both input_fn and x or y',
                            est.fit, y='Y', input_fn=iris_input_fn)
    self.assertRaisesRegexp(ValueError,
                            'Can not provide both input_fn and batch_size',
                            est.fit, input_fn=iris_input_fn, batch_size=100)
    self.assertRaisesRegexp(
        ValueError, 'Inputs cannot be tensors. Please provide input_fn.',
        est.fit, x=tf.constant(1.))

  def testUntrained(self):
    boston = tf.contrib.learn.datasets.load_boston()
    est = tf.contrib.learn.SKCompat(
        tf.contrib.learn.Estimator(model_fn=linear_model_fn))
    with self.assertRaises(tf.contrib.learn.NotFittedError):
      _ = est.score(
          x=boston.data,
          y=boston.target.astype(np.float64))
    with self.assertRaises(tf.contrib.learn.NotFittedError):
      est.predict(x=boston.data)

  def testContinueTrainingDictionaryInput(self):
    boston = tf.contrib.learn.datasets.load_boston()
    output_dir = tempfile.mkdtemp()
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn,
                                     model_dir=output_dir)
    boston_input = {'input': boston.data}
    float64_target = {'labels': boston.target.astype(np.float64)}
    est.fit(x=boston_input, y=float64_target, steps=50)
    scores = est.evaluate(
      x=boston_input,
      y=float64_target,
      metrics={'MSE': tf.contrib.metrics.streaming_mean_squared_error})
    del est
    # Create another estimator object with the same output dir.
    est2 = tf.contrib.learn.Estimator(model_fn=linear_model_fn,
                                      model_dir=output_dir)

    # Check we can evaluate and predict.
    scores2 = est2.evaluate(
      x=boston_input,
      y=float64_target,
      metrics={'MSE': tf.contrib.metrics.streaming_mean_squared_error})
    self.assertAllClose(scores2['MSE'],
                        scores['MSE'])
    predictions = np.array(list(est2.predict(x=boston_input)))
    other_score = _sklearn.mean_squared_error(predictions, float64_target['labels'])
    self.assertAllClose(other_score, scores['MSE'])

  def testContinueTraining(self):
    boston = tf.contrib.learn.datasets.load_boston()
    output_dir = tempfile.mkdtemp()
    est = tf.contrib.learn.SKCompat(
        tf.contrib.learn.Estimator(model_fn=linear_model_fn,
                                   model_dir=output_dir))
    float64_labels = boston.target.astype(np.float64)
    est.fit(x=boston.data, y=float64_labels, steps=50)
    scores = est.score(
        x=boston.data,
        y=float64_labels,
        metrics={'MSE': tf.contrib.metrics.streaming_mean_squared_error})
    del est
    # Create another estimator object with the same output dir.
    est2 = tf.contrib.learn.SKCompat(
        tf.contrib.learn.Estimator(model_fn=linear_model_fn,
                                   model_dir=output_dir))

    # Check we can evaluate and predict.
    scores2 = est2.score(
        x=boston.data,
        y=float64_labels,
        metrics={'MSE': tf.contrib.metrics.streaming_mean_squared_error})
    self.assertAllClose(scores['MSE'], scores2['MSE'])
    predictions = np.array(list(est2.predict(x=boston.data)))
    other_score = _sklearn.mean_squared_error(predictions, float64_labels)
    self.assertAllClose(scores['MSE'], other_score)

    # Check we can keep training.
    est2.fit(x=boston.data, y=float64_labels, steps=100)
    scores3 = est2.score(
        x=boston.data,
        y=float64_labels,
        metrics={'MSE': tf.contrib.metrics.streaming_mean_squared_error})
    self.assertLess(scores3['MSE'], scores['MSE'])

  def testEstimatorParams(self):
    boston = tf.contrib.learn.datasets.load_boston()
    est = tf.contrib.learn.SKCompat(
        tf.contrib.learn.Estimator(model_fn=linear_model_params_fn,
                                   params={'learning_rate': 0.01}))
    est.fit(x=boston.data, y=boston.target, steps=100)

  def testBostonAll(self):
    boston = tf.contrib.learn.datasets.load_boston()
    est = tf.contrib.learn.SKCompat(
        tf.contrib.learn.Estimator(model_fn=linear_model_fn))
    float64_labels = boston.target.astype(np.float64)
    est.fit(x=boston.data, y=float64_labels, steps=100)
    scores = est.score(
        x=boston.data,
        y=float64_labels,
        metrics={'MSE': tf.contrib.metrics.streaming_mean_squared_error})
    predictions = np.array(list(est.predict(x=boston.data)))
    other_score = _sklearn.mean_squared_error(predictions, boston.target)
    self.assertAllClose(scores['MSE'], other_score)
    self.assertTrue('global_step' in scores)
    self.assertEqual(100, scores['global_step'])

  def testBostonAllDictionaryInput(self):
    boston = tf.contrib.learn.datasets.load_boston()
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    boston_input = {'input': boston.data}
    float64_target = {'labels': boston.target.astype(np.float64)}
    est.fit(x=boston_input, y=float64_target, steps=100)
    scores = est.evaluate(
      x=boston_input,
      y=float64_target,
      metrics={'MSE': tf.contrib.metrics.streaming_mean_squared_error})
    predictions = np.array(list(est.predict(x=boston_input)))
    other_score = _sklearn.mean_squared_error(predictions, boston.target)
    self.assertAllClose(other_score, scores['MSE'])
    self.assertTrue('global_step' in scores)
    self.assertEqual(scores['global_step'], 100)

  def testIrisAll(self):
    iris = tf.contrib.learn.datasets.load_iris()
    est = tf.contrib.learn.SKCompat(
        tf.contrib.learn.Estimator(model_fn=logistic_model_no_mode_fn))
    est.fit(iris.data, iris.target, steps=100)
    scores = est.score(
        x=iris.data,
        y=iris.target,
        metrics={('accuracy', 'class'): tf.contrib.metrics.streaming_accuracy})
    predictions = est.predict(x=iris.data)
    predictions_class = est.predict(x=iris.data, outputs=['class'])['class']
    self.assertEqual(predictions['prob'].shape[0], iris.target.shape[0])
    self.assertAllClose(
        predictions['class'],
        predictions_class)
    self.assertAllClose(
        predictions['class'],
        np.argmax(predictions['prob'], axis=1))
    other_score = _sklearn.accuracy_score(iris.target, predictions['class'])
    self.assertAllClose(scores['accuracy'], other_score)
    self.assertTrue('global_step' in scores)
    self.assertEqual(100, scores['global_step'])

  def testIrisAllDictionaryInput(self):
    iris = tf.contrib.learn.datasets.load_iris()
    est = tf.contrib.learn.Estimator(model_fn=logistic_model_no_mode_fn)
    iris_data = {'input': iris.data}
    iris_target = {'labels': iris.target}
    est.fit(iris_data, iris_target, steps=100)
    scores = est.evaluate(
      x=iris_data,
      y=iris_target,
      metrics={('accuracy', 'class'): tf.contrib.metrics.streaming_accuracy})
    predictions = list(est.predict(x=iris_data))
    predictions_class = list(est.predict(x=iris_data, outputs=['class']))
    self.assertEqual(len(predictions), iris.target.shape[0])
    classes_batch = np.array([p['class'] for p in predictions])
    self.assertAllClose(
      classes_batch,
      np.array([p['class'] for p in predictions_class]))
    self.assertAllClose(
      classes_batch,
      np.argmax(np.array([p['prob'] for p in predictions]), axis=1))
    other_score = _sklearn.accuracy_score(iris.target, classes_batch)
    self.assertAllClose(other_score, scores['accuracy'])
    self.assertTrue('global_step' in scores)
    self.assertEqual(scores['global_step'], 100)

  def testIrisInputFn(self):
    iris = tf.contrib.learn.datasets.load_iris()
    est = tf.contrib.learn.Estimator(model_fn=logistic_model_no_mode_fn)
    est.fit(input_fn=iris_input_fn, steps=100)
    _ = est.evaluate(input_fn=iris_input_fn, steps=1)
    predictions = list(est.predict(x=iris.data))
    self.assertEqual(len(predictions), iris.target.shape[0])

  def testIrisInputFnLabelsDict(self):
    iris = tf.contrib.learn.datasets.load_iris()
    est = tf.contrib.learn.Estimator(model_fn=logistic_model_no_mode_fn)
    est.fit(input_fn=iris_input_fn_labels_dict, steps=100)
    _ = est.evaluate(
        input_fn=iris_input_fn_labels_dict,
        steps=1,
        metrics={
            'accuracy':
                metric_spec.MetricSpec(
                    metric_fn=tf.contrib.metrics.streaming_accuracy,
                    prediction_key='class',
                    label_key='labels')
        })
    predictions = list(est.predict(x=iris.data))
    self.assertEqual(len(predictions), iris.target.shape[0])

  def testIrisIterator(self):
    iris = tf.contrib.learn.datasets.load_iris()
    est = tf.contrib.learn.Estimator(model_fn=logistic_model_no_mode_fn)
    x_iter = itertools.islice(iris.data, 100)
    y_iter = itertools.islice(iris.target, 100)
    est.fit(x_iter, y_iter, steps=100)
    _ = est.evaluate(input_fn=iris_input_fn, steps=1)
    predictions = list(est.predict(x=iris.data))
    self.assertEqual(len(predictions), iris.target.shape[0])

  def testIrisIteratorArray(self):
    iris = tf.contrib.learn.datasets.load_iris()
    est = tf.contrib.learn.Estimator(model_fn=logistic_model_no_mode_fn)
    x_iter = itertools.islice(iris.data, 100)
    y_iter = (np.array(x) for x in iris.target)
    est.fit(x_iter, y_iter, steps=100)
    _ = est.evaluate(input_fn=iris_input_fn, steps=1)
    _ = six.next(est.predict(x=iris.data))['class']

  def testIrisIteratorPlainInt(self):
    iris = tf.contrib.learn.datasets.load_iris()
    est = tf.contrib.learn.Estimator(model_fn=logistic_model_no_mode_fn)
    x_iter = itertools.islice(iris.data, 100)
    y_iter = (v for v in iris.target)
    est.fit(x_iter, y_iter, steps=100)
    _ = est.evaluate(input_fn=iris_input_fn, steps=1)
    _ = six.next(est.predict(x=iris.data))['class']

  def testIrisTruncatedIterator(self):
    iris = tf.contrib.learn.datasets.load_iris()
    est = tf.contrib.learn.Estimator(model_fn=logistic_model_no_mode_fn)
    x_iter = itertools.islice(iris.data, 50)
    y_iter = ([np.int32(v)] for v in iris.target)
    est.fit(x_iter, y_iter, steps=100)

  def testTrainInputFn(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    est.fit(input_fn=boston_input_fn, steps=1)
    _ = est.evaluate(input_fn=boston_eval_fn, steps=1)

  def testTrainStepsIsIncremental(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    est.fit(input_fn=boston_input_fn, steps=10)
    self.assertEqual(10, est.get_variable_value('global_step'))
    est.fit(input_fn=boston_input_fn, steps=15)
    self.assertEqual(25, est.get_variable_value('global_step'))

  def testTrainMaxStepsIsNotIncremental(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    est.fit(input_fn=boston_input_fn, max_steps=10)
    self.assertEqual(10, est.get_variable_value('global_step'))
    est.fit(input_fn=boston_input_fn, max_steps=15)
    self.assertEqual(15, est.get_variable_value('global_step'))

  def testPredict(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    boston = tf.contrib.learn.datasets.load_boston()
    est.fit(input_fn=boston_input_fn, steps=1)
    output = list(est.predict(x=boston.data, batch_size=10))
    self.assertEqual(len(output), boston.target.shape[0])

  def testPredictInputFn(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    boston = tf.contrib.learn.datasets.load_boston()
    est.fit(input_fn=boston_input_fn, steps=1)
    input_fn = functools.partial(boston_input_fn, num_epochs=1)
    output = list(est.predict(input_fn=input_fn))
    self.assertEqual(len(output), boston.target.shape[0])

  def testPredictInputFnWithQueue(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    boston = tf.contrib.learn.datasets.load_boston()
    est.fit(input_fn=boston_input_fn, steps=1)
    input_fn = functools.partial(boston_input_fn_with_queue, num_epochs=2)
    output = list(est.predict(input_fn=input_fn))
    self.assertEqual(len(output), boston.target.shape[0]*2)

  def testPredictConstInputFn(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    boston = tf.contrib.learn.datasets.load_boston()
    est.fit(input_fn=boston_input_fn, steps=1)
    def input_fn():
      features = tf.reshape(tf.constant(boston.data), [-1, _BOSTON_INPUT_DIM])
      labels = tf.reshape(tf.constant(boston.target), [-1, 1])
      return features, labels
    output = list(est.predict(input_fn=input_fn))
    self.assertEqual(len(output), boston.target.shape[0])

  def testWithModelFnOps(self):
    """Test for model_fn that returns `ModelFnOps`."""
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn_with_model_fn_ops)
    boston = tf.contrib.learn.datasets.load_boston()
    est.fit(input_fn=boston_input_fn, steps=1)
    input_fn = functools.partial(boston_input_fn, num_epochs=1)
    scores = est.evaluate(input_fn=input_fn, steps=1)
    self.assertIn('loss', scores.keys())
    output = list(est.predict(input_fn=input_fn))
    self.assertEqual(len(output), boston.target.shape[0])

  def testWrongInput(self):
    def other_input_fn():
      return {'other': tf.constant([0, 0, 0])}, tf.constant([0, 0, 0])
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    est.fit(input_fn=boston_input_fn, steps=1)
    with self.assertRaises(ValueError):
      est.fit(input_fn=other_input_fn, steps=1)

  def testMonitors(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    est.fit(input_fn=boston_input_fn,
            steps=21,
            monitors=[CheckCallsMonitor(expect_calls=21)])

  def testSummaryWriting(self):
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    est.fit(input_fn=boston_input_fn, steps=200)
    est.evaluate(input_fn=boston_input_fn, steps=200)
    loss_summary = tf.contrib.testing.simple_values_from_events(
        tf.contrib.testing.latest_events(est.model_dir), ['OptimizeLoss/loss'])
    self.assertEqual(1, len(loss_summary))

  def testLossInGraphCollection(self):

    class _LossCheckerHook(tf.train.SessionRunHook):

      def begin(self):
        self.loss_collection = tf.get_collection(tf.GraphKeys.LOSSES)

    hook = _LossCheckerHook()
    est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
    est.fit(input_fn=boston_input_fn, steps=200, monitors=[hook])
    self.assertTrue(hook.loss_collection)

  def test_export_returns_exported_dirname(self):
    expected = '/path/to/some_dir'
    with tf.test.mock.patch.object(estimator, 'export') as mock_export_module:
      mock_export_module._export_estimator.return_value = expected

      est = tf.contrib.learn.Estimator(model_fn=linear_model_fn)
      actual = est.export('/path/to')

    self.assertEquals(expected, actual)

  def test_export_savedmodel(self):
    tmpdir = tempfile.mkdtemp()
    est, export_input_fn = _build_estimator_for_export_tests(tmpdir)

    extra_file_name = os.path.join(compat.as_bytes(tmpdir),
                                   compat.as_bytes('my_extra_file'))
    extra_file = tf.gfile.GFile(extra_file_name, mode='w')
    extra_file.write(EXTRA_FILE_CONTENT)
    extra_file.close()
    assets_extra = {'some/sub/directory/my_extra_file': extra_file_name}

    export_dir_base = os.path.join(compat.as_bytes(tmpdir),
                                   compat.as_bytes('export'))
    export_dir = est.export_savedmodel(export_dir_base, export_input_fn,
                                       assets_extra=assets_extra)

    self.assertTrue(tf.gfile.Exists(export_dir_base))
    self.assertTrue(tf.gfile.Exists(export_dir))
    self.assertTrue(tf.gfile.Exists(
        os.path.join(compat.as_bytes(export_dir),
                     compat.as_bytes('saved_model.pb'))))
    self.assertTrue(tf.gfile.Exists(
        os.path.join(compat.as_bytes(export_dir),
                     compat.as_bytes('variables'))))
    self.assertTrue(tf.gfile.Exists(
        os.path.join(compat.as_bytes(export_dir),
                     compat.as_bytes('variables/variables.index'))))
    self.assertTrue(tf.gfile.Exists(os.path.join(
        compat.as_bytes(export_dir),
        compat.as_bytes('variables/variables.data-00000-of-00001'))))

    self.assertTrue(tf.gfile.Exists(
        os.path.join(compat.as_bytes(export_dir), compat.as_bytes('assets'))))
    self.assertTrue(tf.gfile.Exists(
        os.path.join(compat.as_bytes(export_dir),
                     compat.as_bytes('assets/my_vocab_file'))))
    self.assertEqual(
        compat.as_bytes(VOCAB_FILE_CONTENT),
        compat.as_bytes(tf.gfile.GFile(
            os.path.join(compat.as_bytes(export_dir),
                         compat.as_bytes('assets/my_vocab_file'))).read()))

    expected_extra_path = os.path.join(
        compat.as_bytes(export_dir),
        compat.as_bytes('assets.extra/some/sub/directory/my_extra_file'))
    self.assertTrue(tf.gfile.Exists(
        os.path.join(compat.as_bytes(export_dir),
                     compat.as_bytes('assets.extra'))))
    self.assertTrue(tf.gfile.Exists(expected_extra_path))
    self.assertEqual(
        compat.as_bytes(EXTRA_FILE_CONTENT),
        compat.as_bytes(tf.gfile.GFile(expected_extra_path).read()))

    expected_vocab_file = os.path.join(compat.as_bytes(tmpdir),
                                       compat.as_bytes('my_vocab_file'))
    # Restore, to validate that the export was well-formed.
    with tf.Graph().as_default() as graph:
      with tf.Session(graph=graph) as sess:
        loader.load(sess, [tag_constants.SERVING], export_dir)
        assets = [x.eval()
                  for x in graph.get_collection(ops.GraphKeys.ASSET_FILEPATHS)]
        self.assertItemsEqual([expected_vocab_file], assets)
        graph_ops = [x.name for x in graph.get_operations()]
        self.assertTrue('input_example_tensor' in graph_ops)
        self.assertTrue('ParseExample/ParseExample' in graph_ops)
        self.assertTrue('linear/linear/feature/matmul' in graph_ops)

    # cleanup
    tf.gfile.DeleteRecursively(tmpdir)

class InferRealValuedColumnsTest(tf.test.TestCase):

  def testInvalidArgs(self):
    with self.assertRaisesRegexp(ValueError, 'x or input_fn must be provided'):
      tf.contrib.learn.infer_real_valued_columns_from_input(None)

    with self.assertRaisesRegexp(ValueError, 'cannot be tensors'):
      tf.contrib.learn.infer_real_valued_columns_from_input(tf.constant(1.0))

  def _assert_single_feature_column(
      self, expected_shape, expected_dtype, feature_columns):
    self.assertEqual(1, len(feature_columns))
    feature_column = feature_columns[0]
    self.assertEqual('', feature_column.name)
    self.assertEqual({
        '': tf.FixedLenFeature(shape=expected_shape, dtype=expected_dtype)
    }, feature_column.config)

  def testInt32Input(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(
        np.ones(shape=[7, 8], dtype=np.int32))
    self._assert_single_feature_column([8], tf.int32, feature_columns)

  def testInt32InputFn(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input_fn(
        lambda: (tf.ones(shape=[7, 8], dtype=tf.int32), None))
    self._assert_single_feature_column([8], tf.int32, feature_columns)

  def testInt64Input(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(
        np.ones(shape=[7, 8], dtype=np.int64))
    self._assert_single_feature_column([8], tf.int64, feature_columns)

  def testInt64InputFn(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input_fn(
        lambda: (tf.ones(shape=[7, 8], dtype=tf.int64), None))
    self._assert_single_feature_column([8], tf.int64, feature_columns)

  def testFloat32Input(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(
        np.ones(shape=[7, 8], dtype=np.float32))
    self._assert_single_feature_column([8], tf.float32, feature_columns)

  def testFloat32InputFn(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input_fn(
        lambda: (tf.ones(shape=[7, 8], dtype=tf.float32), None))
    self._assert_single_feature_column([8], tf.float32, feature_columns)

  def testFloat64Input(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(
        np.ones(shape=[7, 8], dtype=np.float64))
    self._assert_single_feature_column([8], tf.float64, feature_columns)

  def testFloat64InputFn(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input_fn(
        lambda: (tf.ones(shape=[7, 8], dtype=tf.float64), None))
    self._assert_single_feature_column([8], tf.float64, feature_columns)

  def testBoolInput(self):
    with self.assertRaisesRegexp(
        ValueError, 'on integer or non floating types are not supported'):
      tf.contrib.learn.infer_real_valued_columns_from_input(
          np.array([[False for _ in xrange(8)] for _ in xrange(7)]))

  def testBoolInputFn(self):
    with self.assertRaisesRegexp(
        ValueError, 'on integer or non floating types are not supported'):
      # pylint: disable=g-long-lambda
      tf.contrib.learn.infer_real_valued_columns_from_input_fn(
          lambda: (tf.constant(False, shape=[7, 8], dtype=tf.bool), None))

  def testStringInput(self):
    with self.assertRaisesRegexp(
        ValueError, 'on integer or non floating types are not supported'):
      # pylint: disable=g-long-lambda
      tf.contrib.learn.infer_real_valued_columns_from_input(
          np.array([['%d.0' % i for i in xrange(8)] for _ in xrange(7)]))

  def testStringInputFn(self):
    with self.assertRaisesRegexp(
        ValueError, 'on integer or non floating types are not supported'):
      # pylint: disable=g-long-lambda
      tf.contrib.learn.infer_real_valued_columns_from_input_fn(
          lambda: (
              tf.constant([['%d.0' % i for i in xrange(8)] for _ in xrange(7)]),
              None))

  def testBostonInputFn(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input_fn(
        boston_input_fn)
    self._assert_single_feature_column(
        [_BOSTON_INPUT_DIM], tf.float64, feature_columns)

  def testIrisInputFn(self):
    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input_fn(
        iris_input_fn)
    self._assert_single_feature_column(
        [_IRIS_INPUT_DIM], tf.float64, feature_columns)

class ReplicaDeviceSetterTest(tf.test.TestCase):

  def testVariablesAreOnPs(self):
    tf_config = {'cluster': {tf.contrib.learn.TaskType.PS: ['fake_ps_0']}}
    with tf.test.mock.patch.dict('os.environ',
                                 {'TF_CONFIG': json.dumps(tf_config)}):
      config = tf.contrib.learn.RunConfig()

    with tf.device(estimator._get_replica_device_setter(config)):
      v = tf.Variable([1, 2])
      w = tf.Variable([2, 1])
      a = v + w
    self.assertDeviceEqual('/job:ps/task:0', v.device)
    self.assertDeviceEqual('/job:ps/task:0', v.initializer.device)
    self.assertDeviceEqual('/job:ps/task:0', w.device)
    self.assertDeviceEqual('/job:ps/task:0', w.initializer.device)
    self.assertDeviceEqual('/job:worker', a.device)

  def testVariablesAreLocal(self):
    with tf.device(estimator._get_replica_device_setter(
        tf.contrib.learn.RunConfig())):
      v = tf.Variable([1, 2])
      w = tf.Variable([2, 1])
      a = v + w
    self.assertDeviceEqual('', v.device)
    self.assertDeviceEqual('', v.initializer.device)
    self.assertDeviceEqual('', w.device)
    self.assertDeviceEqual('', w.initializer.device)
    self.assertDeviceEqual('', a.device)

  def testMutableHashTableIsOnPs(self):
    tf_config = {'cluster': {tf.contrib.learn.TaskType.PS: ['fake_ps_0']}}
    with tf.test.mock.patch.dict('os.environ',
                                 {'TF_CONFIG': json.dumps(tf_config)}):
      config = tf.contrib.learn.RunConfig()

    with tf.device(estimator._get_replica_device_setter(config)):
      default_val = tf.constant([-1, -1], tf.int64)
      table = tf.contrib.lookup.MutableHashTable(tf.string,
                                                 tf.int64,
                                                 default_val)
      input_string = tf.constant(['brain', 'salad', 'tank'])
      output = table.lookup(input_string)
    self.assertDeviceEqual('/job:ps/task:0', table._table_ref.device)
    self.assertDeviceEqual('/job:ps/task:0', output.device)

  def testMutableHashTableIsLocal(self):
    with tf.device(estimator._get_replica_device_setter(
        tf.contrib.learn.RunConfig())):
      default_val = tf.constant([-1, -1], tf.int64)
      table = tf.contrib.lookup.MutableHashTable(tf.string,
                                                 tf.int64,
                                                 default_val)
      input_string = tf.constant(['brain', 'salad', 'tank'])
      output = table.lookup(input_string)
    self.assertDeviceEqual('', table._table_ref.device)
    self.assertDeviceEqual('', output.device)

  def testTaskIsSetOnWorkerWhenJobNameIsSet(self):
    tf_config = {
        'cluster': {
            tf.contrib.learn.TaskType.PS: ['fake_ps_0']
        },
        'task': {
            'type': tf.contrib.learn.TaskType.WORKER,
            'index': 3
        }
    }
    with tf.test.mock.patch.dict('os.environ',
                                 {'TF_CONFIG': json.dumps(tf_config)}):
      config = tf.contrib.learn.RunConfig()

    with tf.device(estimator._get_replica_device_setter(config)):
      v = tf.Variable([1, 2])
      w = tf.Variable([2, 1])
      a = v + w
    self.assertDeviceEqual('/job:ps/task:0', v.device)
    self.assertDeviceEqual('/job:ps/task:0', v.initializer.device)
    self.assertDeviceEqual('/job:ps/task:0', w.device)
    self.assertDeviceEqual('/job:ps/task:0', w.initializer.device)
    self.assertDeviceEqual('/job:worker/task:3', a.device)

if __name__ == '__main__':
  tf.test.main()


text:
package org.codelibs.elasticsearch.sstmpl.action;

import static org.elasticsearch.action.ValidateActions.addValidationError;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.CompositeIndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

public class MultiSearchScriptTemplateRequest extends ActionRequest implements CompositeIndicesRequest {

    private int maxConcurrentSearchRequests = 0;
    private List<SearchScriptTemplateRequest> requests = new ArrayList<>();

    private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed();

    public MultiSearchScriptTemplateRequest() {
    }

    /**
     * Add a search template request to execute. Note, the order is important, the search response will be returned in the
     * same order as the search requests.
     */
    public MultiSearchScriptTemplateRequest add(final SearchScriptTemplateRequestBuilder request) {
        requests.add(request.request());
        return this;
    }

    /**
     * Add a search template request to execute. Note, the order is important, the search response will be returned in the
     * same order as the search requests.
     */
    public MultiSearchScriptTemplateRequest add(final SearchScriptTemplateRequest request) {
        requests.add(request);
        return this;
    }

    /**
     * Returns the amount of search requests specified in this multi search requests are allowed to be ran concurrently.
     */
    public int maxConcurrentSearchRequests() {
        return maxConcurrentSearchRequests;
    }

    /**
     * Sets how many search requests specified in this multi search requests are allowed to be ran concurrently.
     */
    public MultiSearchScriptTemplateRequest maxConcurrentSearchRequests(final int maxConcurrentSearchRequests) {
        if (maxConcurrentSearchRequests < 1) {
            throw new IllegalArgumentException("maxConcurrentSearchRequests must be positive");
        }

        this.maxConcurrentSearchRequests = maxConcurrentSearchRequests;
        return this;
    }

    public List<SearchScriptTemplateRequest> requests() {
        return this.requests;
    }

    @Override
    public ActionRequestValidationException validate() {
        ActionRequestValidationException validationException = null;
        if (requests.isEmpty()) {
            validationException = addValidationError("no requests added", validationException);
        }
        for (final SearchScriptTemplateRequest request : requests) {
            final ActionRequestValidationException ex = request.validate();
            if (ex != null) {
                if (validationException == null) {
                    validationException = new ActionRequestValidationException();
                }
                validationException.addValidationErrors(ex.validationErrors());
            }
        }
        return validationException;
    }

    public IndicesOptions indicesOptions() {
        return indicesOptions;
    }

    public MultiSearchScriptTemplateRequest indicesOptions(final IndicesOptions indicesOptions) {
        this.indicesOptions = indicesOptions;
        return this;
    }

    public MultiSearchScriptTemplateRequest(final StreamInput in)
            throws IOException {
        super(in);
        maxConcurrentSearchRequests = in.readVInt();
        requests = in.readList(SearchScriptTemplateRequest::new);
    }

    @Override
    public void writeTo(final StreamOutput out) throws IOException {
        super.writeTo(out);
        out.writeVInt(maxConcurrentSearchRequests);
        out.writeList(requests);
    }
}