A Ruby implementation of Babelsberg, based on Topaz. Main repo at
Python Ruby C Other
Pull request Compare This branch is 1084 commits ahead of topazproject:master.
Latest commit ebd74d5 Dec 23, 2016 @timfel timfel bump z3 a little bit forward
Permalink
Failed to load latest commit information.
bench slightly change benchmark, so it runs on maglev, too, for comparison Jun 20, 2012
bin fix untag script to find our tags file Feb 20, 2013
dependencies bump z3 a little bit forward Dec 23, 2016
docs update actual publication date Jul 14, 2013
lib-ruby add forgotten attr_accessor, required now Apr 5, 2014
lib-topaz Merge remote-tracking branch 'topaz/tim/fix-translation' into interac… Dec 22, 2016
spec Merge remote-tracking branch 'topaz/tim/fix-translation' into interac… Dec 22, 2016
tasks Merge remote-tracking branch 'topaz/tim/fix-translation' into interac… Dec 22, 2016
tests Merge remote-tracking branch 'topaz/tim/fix-translation' into interac… Dec 22, 2016
topaz copy the relevant functions to our own misc module for now Dec 23, 2016
.gitignore ignore ffi test artifacts Sep 20, 2013
.gitmodules add z3 fork from github Mar 11, 2014
.travis.yml Merge remote-tracking branch 'topaz/tim/fix-translation' into interac… Dec 22, 2016
AUTHORS.rst include myself in authors file Oct 13, 2013
CONTRIBUTING.rst no https Feb 10, 2013
LICENSE Update LICENSE Mar 4, 2014
README.md Update README.md Aug 3, 2014
TODO-constraints.txt add a note that we will have to switch back to eager assignment :( Aug 13, 2013
echo_constraint_demo.rb start with limited-domain constraint satisfaction example Dec 14, 2012
libprime.rb example with prime numbers Nov 21, 2012
rectangle_constraint_demo.rb rectangle demo Dec 31, 2012
requirements.txt Don't qualify Invoke requirement Mar 18, 2013
targettopaz.py Merge branch 'master' into fibers Mar 18, 2013
test.rb assignment works now Jan 8, 2013
topaz.mspec Fixed several typos Jul 25, 2014
tox.ini fix a flake complaint and ignore two others Jul 6, 2016
worker_constraint_demo.rb rectangle demo Dec 31, 2012

README.md

Babelsberg/R

Build Status

A Topaz-based implementation of Babelsberg, licensed under 3-clause BSD

See also Babelsberg/JS

It allows you to solve, for example, the send+more=money puzzle like this:

require "libz3"
require "libarraysolver"

class Array
  def ins(range)
    return true if self.empty?
    self[1..-1].ins(range) &&
      self[0] >= range.first &&
      self[0] <= range.last
  end
end

# initialize each variable to an integer so that the solver knows its type
s,e,n,d,m,o,r,y = [0]*8

# each digit is between 0 and 9
always { [s,e,n,d,m,o,r,y].ins(0..9) } 
# all digits
always { [s,e,n,d,m,o,r,y].alldifferent? }

always do
            s*1000 + e*100 + n*10 + d +
            m*1000 + o*100 + r*10 + e ==
  m*10000 + o*1000 + n*100 + e*10 + y
end

# the leading digits can't be 0
always { s > 0 }
always { m > 0 }

puts ("solution: [s,e,n,d,m,o,r,y] = " + [s,e,n,d,m,o,r,y].to_s)

But it is also dynamic, so you can do:

require "libcassowary"
require "iosolver" # not included in standard library

class VideoEncoder
  attr_accessor :quality
  def initialize(settings_file)
    quality_setting = FloatRWIO.new(settings_file)
    always { self.quality > 0 && self.quality < 100 }
    always { self.quality == quality_setting }
    # ...
  end

  # ... rest of the impl
end

settings_file = File.open("./settings", "w")
settings_file << 50
e = VideoEncoder.new(settings_file)
puts e.quality # 50

settings_file.rewind
settings_file << 25
puts e.quality # 25

settings_file.rewind
settings_file << 0
puts e.quality # 1 -- the constraint requires quality to be >0
settings_file.rewind
puts settings_file.read # 1 -- the constraint is bi-directional, so the file was updated

Basically, you can write constraints using the always primitive that you always want to be true, using existing object-oriented abstractions (I am just using the accessor method for quality above), and the system will maintain them. The extent to which the system is able to keep constraints satisfied depends on the solver that is used. This implementation provides Z3, DeltaBlue, and Cassowary.

We have used this to implement electrical simulations, a video streaming application, puzzle solvers, and a simulation of the Lively Engine.

Papers about this implementation are forthcoming and a freely accessible technical report will be published shortly.

Created under a grant from Hasso Plattner Institute HPI Logo