def standard_get_models(self, desired_number_of_models): result = [] count = 0 self.clock.tick("first model") while True: self.clock.tick("unsat") if (Options.MODE != Common.DEBUG and not(Options.PRODUCE_UNSAT_CORE)and count != desired_number_of_models and self.solver.check() == Common.SAT ) or \ (Options.MODE != Common.DEBUG and Options.PRODUCE_UNSAT_CORE and count != desired_number_of_models and self.solver.check(self.unsat_core_trackers) == Common.SAT ) or \ (Options.MODE == Common.DEBUG and count != desired_number_of_models and self.solver.check(self.unsat_core_trackers) == Common.SAT ): if count == 0: self.clock.tock("first model") m = self.solver.model() result.append(m) # Create a new constraint that blocks the current model if not Options.SUPPRESS_MODELS: self.printVars(m) preventSameModel(self, self.solver, m) count += 1 else: if count == 0 and Options.PRODUCE_UNSAT_CORE: self.clock.tock("unsat") debug_print("UNSAT") core = self.solver.unsat_core() debug_print(str(len(core)) + " constraints in unsat core: \n") for i in core: print(Constraints.interpretUnsatCore(self, str(i))) print() return result elif count == 0: standard_print("UNSAT") return result
def constraintVisit(self, element, previouslyBuilt = None): self.inConstraint = True debug_print(self.BRACKETEDCONSCOUNT) self.BRACKETEDCONSCOUNT = self.BRACKETEDCONSCOUNT + 1 if previouslyBuilt: print(previouslyBuilt.stringRep) self.currentConstraint = previouslyBuilt visitors.Visitor.visit(self, self.currentConstraint.element.exp) else: self.currentConstraint = BracketedConstraint.BracketedConstraint(self.cfr, element, self.claferStack) visitors.Visitor.visit(self, element.exp) self.currentConstraint.endProcessing() self.currentConstraint = None self.inConstraint = False
def constraintVisit(self, element, previouslyBuilt=None): self.inConstraint = True debug_print(self.BRACKETEDCONSCOUNT) self.BRACKETEDCONSCOUNT = self.BRACKETEDCONSCOUNT + 1 if previouslyBuilt: print(previouslyBuilt.stringRep) self.currentConstraint = previouslyBuilt visitors.Visitor.visit(self, self.currentConstraint.element.exp) else: self.currentConstraint = BracketedConstraint.BracketedConstraint( self.cfr, element, self.claferStack) visitors.Visitor.visit(self, element.exp) self.currentConstraint.endProcessing() self.currentConstraint = None self.inConstraint = False
def run(self): ''' :param module: The Clafer AST :type module: Module ''' if Options.MODE == Common.PRELOAD: return 0 if Options.MODE == Common.MODELSTATS: ModelStats.run(self, self.module) return 0 if Options.STRING_CONSTRAINTS: sys.exit("TODO string constraints") #Converters.printZ3StrConstraints(self) Z3Str.clafer_to_z3str("z3str_in") return 1 debug_print("Printing constraints.") self.printConstraints() self.clock.tick("Asserting Constraints") debug_print("Asserting constraints.") self.assertConstraints() self.clock.tock("Asserting Constraints") #print("Cache Hits: " + str(self.solver.converter.num_hit)) #print("Cache Misses: " + str(self.solver.converter.num_miss)) if Options.SOLVER == "smt2": self.solver.printConstraints() sys.exit() debug_print("Getting models.") self.clock.tick("Get Models") models = self.get_models(Options.NUM_INSTANCES) self.clock.tock("Get Models") print(self.clock) self.num_models = len(models) print(self.clock) if Options.LEARNING_ENVIRONMENT == "sharcnet": print(Options.SPLIT + str(Options.NUM_SPLIT)) sys.exit("TODO FIX SHARCNET") return self.num_models
def translate(self): ''' Converts Clafer constraints to Z3 constraints. ''' #TODO caching for ASIL subexpressions. Do set equals first. Cache nonsupered joins. try: self.clock.tick("translation") """ Create a ClaferSort associated with each Clafer. """ Visitor.visit(CreateSorts.CreateSorts(self), self.module) """ Resolve any 'parent' or 'this' ClaferID's. """ Visitor.visit(ResolveClaferIds.ResolveClaferIds(self), self.module) """ Add subclafers to the *fields* variable in the corresponding parent clafer. Also handles supers and refs. """ Visitor.visit(CreateHierarchy.CreateHierarchy(self), self.module) debug_print("Mapping colon clafers.") self.mapColonClafers() debug_print("Adjusting instances for scopes.") self.setScopes() """ Initializing ClaferSorts and their instances. """ #TODO Clean this up. Visitor.visit(Initialize.Initialize(self), self.module) self.fixSubSortIndices() self.createInstancesConstraintsAndFunctions() #for i in self.cfr_sorts.values(): # standard_print(str(i) + " : "+ str(i.numInstances)) debug_print("Creating cardinality constraints.") self.createCardinalityConstraints() debug_print("Creating ref constraints.") self.createRefConstraints() debug_print("Adding subsort constraints.") self.addSubSortConstraints() self.setTopLevelIndices() debug_print("Creating group cardinality constraints.") self.createGroupCardConstraints() #debug_print("Creating bracketed constraints.") #self.bracketedConstraintsPreview = CreateSimpleBracketedConstraints.CreateSimpleBracketedConstraints(self) #Visitor.visit(self.bracketedConstraintsPreview, self.module) debug_print("Creating bracketed constraints.") bcVisitor = CreateBracketedConstraints.CreateBracketedConstraints(self) #for i in self.bracketedConstraintsPreview.setEqualityConstraints: # bcVisitor.constraintVisit(None, i) #print("ABOVE") #for i in self.join_cache.keys(): # print(str(i))# + " : " + str(self.join_cache[i])) #print("BELOW") #bcVisitor.constraintVisit(None, self.bracketedConstraintsPreview.otherConstraints[3]) #sys.exit("ABOVE!!!!!!!!") #for i in self.bracketedConstraintsPreview.otherConstraints: #print(i.stringRep) #bcVisitor.constraintVisit(None, i) Visitor.visit(CreateBracketedConstraints.CreateBracketedConstraints(self), self.module) debug_print("Checking for goals.") if not Options.IGNORE_GOALS: Visitor.visit(CheckForGoals.CheckForGoals(self), self.module) self.clock.tock("translation") except UnusedAbstractException as e: print(str(e)) return 0