示例#1
0
 def initialize(self):
     (_, upper) = self.element.glCard
     self.numInstances = upper.value
     self.instances = SMTLib.SMT_IntVector(
         self.element.uid,
         self.numInstances,
         bits=self.getBits(self.parentInstances + 1))
     #gets the upper card bound of the parent clafer
     if not self.parentStack:
         self.parent = None
         self.parentInstances = 1
     else:
         self.parent = self.parentStack[-1]
         self.parentInstances = self.parent.numInstances
示例#2
0
    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])))