Skip to content

Instantly share code, notes, and snippets.

@dhoelzgen
Created August 7, 2012 08:55
Show Gist options
  • Save dhoelzgen/3283467 to your computer and use it in GitHub Desktop.
Save dhoelzgen/3283467 to your computer and use it in GitHub Desktop.
Better Formatting for DLV / DLV Complex Answer Sets
#!/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..3].map {|a| "#{pred}#{a}"}.join(", ") + ", ..., #{pred}#{arg[-1]}"
end
end
puts ""
@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
test(a).
test(b).
numbers(1..10).
test(c) :- test(a), test(b).
test(d) :- test(a), not test(b).
fact.
ANSWERSET 1:
fact
numbers(1), numbers(2), numbers(3), numbers(4), ..., numbers(10)
test(a), test(b), test(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment