Class: SolverFactory
- Inherits:
-
Object
- Object
- SolverFactory
- Defined in:
- lib/solver-lib.rb
Instance Attribute Summary collapse
-
#z3path ⇒ Object
readonly
Returns the value of attribute z3path.
-
#z3version ⇒ Object
readonly
Returns the value of attribute z3version.
Instance Method Summary collapse
- #check_type(type, value, xpath) ⇒ Object
- #convert_solver_value(type, datatype, value, xpath) ⇒ Object
- #display_assertion(assertion, solver) ⇒ Object
-
#initialize(progress, z3path) ⇒ SolverFactory
constructor
A new instance of SolverFactory.
- #new_solver ⇒ Object
- #parse_unsat_core(solver, unsat_core) ⇒ Object
- #query(options, &block) ⇒ Object
-
#query_model(options, &block) ⇒ Object
main entry point!.
Constructor Details
#initialize(progress, z3path) ⇒ SolverFactory
Returns a new instance of SolverFactory.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/solver-lib.rb', line 176 def initialize(progress, z3path) @progress = progress z3version = nil exitstatus = nil begin z3version = `#{z3path} --version`.chomp() exitstatus = $?.exitstatus rescue Errno::ENOENT end if exitstatus == 0 && z3version != nil then progress.print_line "INFO: Using #{z3version} found in z3path=#{z3path}" @z3path = z3path @z3version = z3version else progress.print_line "WARN: could not find z3 in z3path=#{z3path.inspect}" `which z3` if $?.exitstatus == 0 then z3version = `z3 --version`.chomp() progress.print_line "However, #{z3version} is availlable on the PATH. Using this one." @z3path = 'z3' @z3version = z3version else raise RuntimeError.new("ERROR: unable to find Z3 solver. Exiting! (Please provide its location in the config file using the key :z3path)") end end end |
Instance Attribute Details
#z3path ⇒ Object (readonly)
Returns the value of attribute z3path.
174 175 176 |
# File 'lib/solver-lib.rb', line 174 def z3path @z3path end |
#z3version ⇒ Object (readonly)
Returns the value of attribute z3version.
174 175 176 |
# File 'lib/solver-lib.rb', line 174 def z3version @z3version end |
Instance Method Details
#check_type(type, value, xpath) ⇒ Object
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/solver-lib.rb', line 414 def check_type(type, value, xpath) if type == "String" then unless value =~ /\".*\"/ then raise RuntimeError.new("inconsistent model value: model value for #{xpath} should be a String, but was #{value}.") end elsif type == "Bool" unless value == "true" or value == "false" then raise RuntimeError.new("inconsistent model value for #{xpath} should be a Bool, but was #{value.inspect}.") end elsif type == "Int" unless value =~ /\d+/ then raise RuntimeError.new("inconsistent model value: model value for #{xpath} should be a Int, but was #{value}.") end else raise RuntimeError.new("encountered unknown solver type: type of solver-var for #{xpath} is #{info[:type]}.") end end |
#convert_solver_value(type, datatype, value, xpath) ⇒ Object
403 404 405 406 407 408 409 410 411 412 |
# File 'lib/solver-lib.rb', line 403 def convert_solver_value(type, datatype, value, xpath) check_type(type, value, xpath) if datatype == :date then return (Time.at(0).to_date + Integer(value)).to_s elsif datatype == :timestamp then return Time.at(Integer(value)).to_s else return value end end |
#display_assertion(assertion, solver) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/solver-lib.rb', line 234 def display_assertion(assertion, solver) if assertion =~ /validation_rule_(.*)_instance_(.*)/ then return "Validation rule #{$1} of structure #{$2.gsub("_", "/")}" else info = solver.getAssociationForAssertionID(assertion) if info then if info[:assertion_type] == :doc_field_filled then return "Field #{info[:xpath]} contains value #{info[:value].inspect} in document" elsif info[:assertion_type] == :doc_field_empty then return "Field #{info[:xpath]} is empty in document" elsif info[:assertion_type] == :doc_structure_exists then return "Structure #{info[:xpath]} exists in document" elsif info[:assertion_type] == :doc_structure_not_exists then return "Structure #{info[:xpath]} does not exist in document" elsif info[:assertion_type] == :constraint then if info[:constraint_type] == :min then return "Field #{info[:xpath]} has a minimum value of #{info[:value]}." elsif info[:constraint_type] == :max then return "Field #{info[:xpath]} has a maximum value of #{info[:value]}." elsif info[:constraint_type] == :required then return "Field #{info[:xpath]} is required." elsif info[:constraint_type] == :values then return "Field #{info[:xpath]} is an enum field and can only contain the values #{info[:values].inspect}." else p info raise RuntimeError.new("encountered unknown :constraint_type #{info[:constraint_type].inspect} for assertionID #{assertion.inspect}") end else raise RuntimeError.new("encountered unknown :assertion_type #{info[:assertion_type].inspect} for assertionID #{assertion.inspect}") end else return assertion end end end |
#new_solver ⇒ Object
203 204 205 |
# File 'lib/solver-lib.rb', line 203 def new_solver() Z3Solver.new(@progress, @z3path) end |
#parse_unsat_core(solver, unsat_core) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/solver-lib.rb', line 270 def parse_unsat_core(solver, unsat_core) # TODO: implement unless unsat_core =~ /\((.*)\)/ then raise RuntimeError.new("could not parse unsat core: #{unsat_core.inspect}") end list = $1.split(" ") result = "EXPLANATION:\n" result += "The following values\n" list.filter{|a| a.start_with?("doc-")}.each do |assertion| result += "- #{display_assertion(assertion, solver)} (#{assertion})\n" end result += "\nviolate the following constraints\n" list.filter{|a| !a.start_with?("doc-")}.each do |assertion| result += "- #{display_assertion(assertion, solver)} (#{assertion})\n" end return result end |
#query(options, &block) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/solver-lib.rb', line 207 def query(, &block) unless [:solverLog] raise RuntimeError.new("No solverLog specified, but the option :solverLog is required!") end @progress.print_line("calling solver (solver-log is written to #{[:solverLog]})") session = SolverSession.new(self, [:solverLog]) block.call(session) session.to_solver(@progress, "(check-sat)") result = session.from_solver(@progress) explanation = true if result == "sat" # model is not contradictory -> continue elsif result == "unsat" # model is contradictory -> add a message session.to_solver(@progress, "(get-unsat-core)") unsat_core = session.from_solver(@progress) explanation = session.parse_unsat_core(unsat_core) else ModelUtils.handleSolverError(@progress, , result) end return explanation end |
#query_model(options, &block) ⇒ Object
main entry point!
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/solver-lib.rb', line 291 def query_model(, &block) unless [:solverLog] raise RuntimeError.new("No solverLog specified, but the option :solverLog is required!") end @progress.print_line("calling solver (solver-log is written to #{[:solverLog]})") session = SolverSession.new(self, [:solverLog]) block.call(session) session.to_solver(@progress, "(check-sat)") result = session.from_solver(@progress) if result.chomp == "sat" # model is not contradictory -> extract model const_list = [] const_info = {} session.for_each_const do |name, info| const_list << name const_info[name] = info end session.to_solver(@progress, "(get-value (#{const_list.join(" ")}))") model_str = session.read_multiline_from_solver() model = {} ModelUtils.foreach_field_in_model_str(model_str, const_info) do |varname, value, info| unless info then @progress.print_line "WARN: no info availlable for varname=#{varname.inspect}, value=#{value.inspect}" end xpath = info[:xpath] if model.has_key?(xpath) then if varname.end_with?("-filled") then unless model[xpath][:type] == :field then raise RuntimeError.new("inconsistent model: xpath #{xpath} should be a field, not a #{model[xpath][:type]}.") end unless value == "true" or value == "false" then raise RuntimeError.new("inconsistent model value: filled-value of #{xpath} should be a Bool, but was #{value.inspect}.") end if value == "true" then model[xpath][:filled] = true else model[xpath][:filled] = false end elsif varname.end_with?("-value") then unless model[xpath][:type] == :field || model[xpath][:type] == :list then raise RuntimeError.new("inconsistent model: xpath #{xpath} should be a field or a list, not a #{model[xpath][:type]}.") end if model[xpath][:type] == :field then model[xpath][:value] = convert_solver_value(info[:type], info[:datatype], value, xpath) elsif model[xpath][:type] == :list then unless varname =~ /-index-(\d+)-value$/ raise RuntimeError.new("inconsistent model: xpath #{xpath} is a list value and hence its varname should end like -index-X-value, not #{varname.inspect}.") end index = Integer($1) model[xpath][:xpath_element] = info[:xpath_element] if model[xpath].has_key?(:value) then val = convert_solver_value(info[:type], info[:datatype], value, xpath) model[xpath][:value].insert(index, val) else val = convert_solver_value(info[:type], info[:datatype], value, xpath) model[xpath][:value] = [val] end end elsif varname.end_with?("-size") then unless model[xpath][:type] == :list || model[xpath][:type] == :field then raise RuntimeError.new("inconsistent model: xpath #{xpath} should be a list or a field, not a #{model[xpath][:type].inspect}.") end if model[xpath][:type] == :field then model[xpath][:type] = :list end model[xpath][:size] = Integer(value) elsif varname.start_with?("struct-") && varname.end_with?("-exists") then unless model[xpath][:type] == :struct then raise RuntimeError.new("inconsistent model: xpath #{xpath} should be a struct, not a #{model[xpath][:type]}.") end model[xpath][:exists] = value end else if varname.end_with?("-filled") then unless value == "true" or value == "false" then raise RuntimeError.new("inconsistent model value: filled-value of #{xpath} should be a Bool, but was #{value.inspect}.") end if value == "true" then model[xpath] = { :type => :field, :filled => true } else model[xpath] = { :type => :field, :filled => false } end elsif varname.end_with?("-value") then check_type(info[:type], value, xpath) model[xpath] = { :type => :field, :value => value } elsif varname.start_with?("struct-") && varname.end_with?("-exists") then unless value == "true" or value == "false" then raise RuntimeError.new("inconsistent model value: exists-value of struct #{xpath} should be a Bool, but was #{value.inspect}.") end model[xpath] = { :type => :struct, :exists => value } elsif varname.end_with?("-size") then model[xpath] = { :type => :list, :size => value } end end end return model elsif result.chomp == "unsat" # model is contradictory -> add a message session.to_solver(@progress, "(get-unsat-core)") unsat_core = session.from_solver(@progress) session.writeProtocolToFile(@progress, [:solverLog]) @progress.print_line "Solver reported a contradiction! please see #{[:solverLog]} for further information." return nil else ModelUtils.handleSolverError(@progress, session, , result) end return nil end |