def isOn(self, index): ''' index is either an int or SMT-Int Returns a Boolean Constraint stating whether or not the instance at the given index is *on*. An instance is on if it is not set to self.parentInstances. ''' try: return SMTLib.SMT_NE(self.instances[index], SMTLib.SMT_IntConst(self.parentInstances)) except: return SMTLib.SMT_NE(index, SMTLib.SMT_IntConst(self.parentInstances))
def compute_int_set(instances): cons = [] for index in range(len(instances)): (i, c) = instances[index] cons.append( mAnd( c, *[ mOr(SMTLib.createNot(jc), SMTLib.SMT_NE(j, i)) for (j, jc) in instances[0:index] ])) return cons
def preventSameModel(cfr, solver, model): block = [] for i in cfr.cfr_sorts.values(): for j in i.instances: block.append( SMTLib.SMT_NE(j, SMTLib.SMT_IntConst(int(str(model[j.var]))))) if i.refs: for j in i.refs: try: val = model[j.var] except: #happens if a primitive ref is totally unrestricted continue if not val: continue else: block.append(SMTLib.SMT_NE(j, SMTLib.SMT_IntConst(val))) if block == []: solver.add(SMTLib.SMT_BoolConst(False)) else: solver.add(SMTLib.SMT_Or(*block))
def createInstancesConstraintsAndFunctions(self): for i in range(self.numInstances): (lower, upper, extraAbsenceConstraint) = self.instanceRanges[i] #lower == upper case (simpler) if lower == upper: constraint = SMTLib.SMT_EQ(self.instances[i], SMTLib.SMT_IntConst(upper)) if extraAbsenceConstraint: self.constraints.addInstanceConstraint( SMTLib.SMT_Or(self.isOff(i), constraint)) else: #TODO self.instances[i] = SMTLib.SMT_IntConst(lower) self.constraints.addInstanceConstraint(constraint) else: #parent pointer is >= lower self.constraints.addInstanceConstraint( SMTLib.SMT_GE(self.instances[i], SMTLib.SMT_IntConst(lower))) constraint = SMTLib.SMT_LE(self.instances[i], SMTLib.SMT_IntConst(upper)) if extraAbsenceConstraint: #parent pointer is <= upper , or equal to parentInstances self.constraints.addInstanceConstraint( SMTLib.SMT_Or(self.isOff(i), constraint)) else: #parent pointer is <= upper self.constraints.addInstanceConstraint(constraint) #sorted parent pointers (only consider things that are not part of an abstract) if not self.beneathAnAbstract: if i != self.numInstances - 1: self.constraints.addInstanceConstraint( SMTLib.SMT_LE(self.instances[i], self.instances[i + 1])) if not self.parent: return #if the parent is not live, then no child can point to it for i in range(self.parent.numInstances): for j in range(self.numInstances): self.constraints.addInstanceConstraint( SMTLib.SMT_Implies( self.parent.isOff(i), SMTLib.SMT_NE(self.instances[j], SMTLib.SMT_IntConst(i))), self.parent.known_polarity(i, local=True) != Common.DEFINITELY_ON)
def addRefConstraints(self): if not self.refSort: return elif isinstance(self.refSort, PrimitiveType) and self.refSort.type == "real": self.refs = SMTLib.SMT_RealVector(self.element.uid + "_ref", self.numInstances) elif isinstance(self.refSort, PrimitiveType): self.refs = SMTLib.SMT_IntVector(self.element.uid + "_ref", self.numInstances) else: self.refs = SMTLib.SMT_IntVector( self.element.uid + "_ref", self.numInstances, bits=self.getBits(self.refSort.parentInstances + 1)) if not isinstance(self.refSort, PrimitiveType): for i in range(self.numInstances): #refs pointer is >= 0 self.constraints.addRefConstraint( SMTLib.SMT_GE(self.refs[i], SMTLib.SMT_IntConst(0))) #ref pointer is <= upper card of ref parent self.constraints.addRefConstraint( SMTLib.SMT_LE( self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances))) #if integer refs, zero out refs that do not have live parents, #if clafer refs, set equal to ref.parentInstances if not live #reference symmetry breaking if not self.element.isAbstract: for i in range(self.numInstances - 1): for j in range(i + 1, self.numInstances): if isinstance(self.refSort, PrimitiveType): self.constraints.addRefConstraint( SMTLib.SMT_Implies( SMTLib.SMT_EQ(self.instances[i], self.instances[j]), SMTLib.SMT_LE(self.refs[i], self.refs[j]))) else: self.constraints.addRefConstraint( SMTLib.SMT_Implies( mAnd( SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst( self.refSort.numInstances)), SMTLib.SMT_EQ(self.instances[i], self.instances[j])), SMTLib.SMT_LE(self.refs[i], self.refs[j]))) for i in range(self.numInstances): if isinstance(self.refSort, PrimitiveType): if self.refSort == "integer": self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) elif self.refSort == "string": if Options.STRING_CONSTRAINTS: self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], self.cfr.EMPTYSTRING)), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: if self.known_polarity(i, local=True) != Common.DEFINITELY_ON: self.constraints.addRefConstraint( SMTLib.SMT_If( self.isOff(i), SMTLib.SMT_EQ( self.refs[i], SMTLib.SMT_IntConst( self.refSort.numInstances)), SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst( self.refSort.numInstances)))) else: self.constraints.addRefConstraint( SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances))) #if refsort.full does not exist, create it if not self.refSort.full: self.refSort.full = lambda x: mOr(*[ SMTLib.SMT_And( SMTLib.SMT_EQ(x, SMTLib.SMT_IntConst(i)), self.refSort.isOn(i)) for i in range(self.refSort.numInstances) ]) #the clafer that the reference points to must be "on" self.constraints.addRefConstraint( SMTLib.SMT_Implies( SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances)), self.refSort.full(self.refs[i])))