Skip to content

Instantly share code, notes, and snippets.

@dhoelzgen
Created August 7, 2012 08:55

Revisions

  1. dhoelzgen revised this gist Aug 7, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion output.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ANSWERSET 1:
    fact
    numbers(1), numbers(2), numbers(3), numbers(4), numbers(5), ...
    numbers(1), numbers(2), numbers(3), numbers(4), ..., numbers(10)
    test(a), test(b), test(c)
  2. dhoelzgen revised this gist Aug 7, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dlv_formatter.rb
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ def printCurrent(number)
    elsif arg.size < 6
    puts " " + arg.map {|a| "#{pred}#{a}"}.join(", ")
    else
    puts " " + arg[0..4].map {|a| "#{pred}#{a}"}.join(", ") + ", ..."
    puts " " + arg[0..3].map {|a| "#{pred}#{a}"}.join(", ") + ", ..., #{pred}#{arg[-1]}"
    end
    end

  3. dhoelzgen revised this gist Aug 7, 2012. 2 changed files with 13 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions example.dlv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    test(a).
    test(b).

    numbers(1..10).

    test(c) :- test(a), test(b).
    test(d) :- test(a), not test(b).

    fact.
    4 changes: 4 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    ANSWERSET 1:
    fact
    numbers(1), numbers(2), numbers(3), numbers(4), numbers(5), ...
    test(a), test(b), test(c)
  4. dhoelzgen revised this gist Aug 7, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions dlv_formatter.rb
    Original file line number Diff line number Diff line change
    @@ -35,8 +35,6 @@ def printCurrent(number)

    puts ""

    puts @answerset.map {|a,b| b.count}.inject{|sum,x| sum + x }

    @answerset = Hash.new
    end

  5. dhoelzgen created this gist Aug 7, 2012.
    78 changes: 78 additions & 0 deletions dlv_formatter.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    #!/usr/bin/env ruby

    output = %x(/usr/local/bin/dlv-complex -silent #{ARGV[0]})

    output = output.scan(/[[:print:]]/).join

    @answerset = Hash.new

    def reset
    @bracket_level = 0
    @current_pred = ""
    @current_arg = ""
    end

    def addLiteral
    return unless @current_pred && @current_pred.any?

    @answerset[@current_pred] ||= Array.new
    @answerset[@current_pred] << @current_arg
    reset
    end

    def printCurrent(number)
    puts "ANSWERSET #{number}:"

    @answerset.sort {|a,b| a[0] <=> b[0]}.each do |pred, arg|
    if arg.size < 2
    puts " #{pred}#{arg}"
    elsif arg.size < 6
    puts " " + arg.map {|a| "#{pred}#{a}"}.join(", ")
    else
    puts " " + arg[0..4].map {|a| "#{pred}#{a}"}.join(", ") + ", ..."
    end
    end

    puts ""

    puts @answerset.map {|a,b| b.count}.inject{|sum,x| sum + x }

    @answerset = Hash.new
    end

    curlyLevel = 0; i = 0

    output.each_char do |c|

    # Several answersets
    if c == '{' && curlyLevel == 0
    reset
    curlyLevel = 1
    elsif c == '}' && curlyLevel == 1
    curlyLevel = 0
    addLiteral
    printCurrent(i+=1)
    else

    # Within answerset
    next if c == ' ' && @bracket_level == 0

    if c == ',' && @bracket_level == 0
    addLiteral
    else

    curlyLevel += 1 if c == '{'
    curlyLevel -= 1 if c == '}'

    @bracket_level += 1 if c == '('

    if @bracket_level == 0
    @current_pred += c
    else
    @current_arg += c
    end

    @bracket_level -= 1 if c == ')'
    end
    end
    end