예제 #1
0
파일: generateRules.py 프로젝트: tarsqi/ttk
class Environment:

    def __init__(self,id=0):
        self.id = id
        self.isConsistent = 1
        self.NODES = ObjectList("Nodes")
        self.LINKS = ObjectList("TimeML_Links")
        self.POINTS = ObjectList("Points")
        self.PLINKS = ObjectList("Point_Links")
        self.AXIOMS = AXIOMS
        self.POINT_AXIOMS = POINT_AXIOMS
        self.settings = settings
        
    def addNode(self,node): self.NODES.add(node)
    def addLink(self,link): self.LINKS.add(link)
    def addPoint(self,point): self.POINTS.add(point)
    def addPLink(self,plink): self.PLINKS.add(plink)

    def copy(self):
        newEnv = Environment()
        newEnv.id = self.id
        newEnv.isConsistent = self.isConsistent
        for node in self.NODES: newEnv.NODES.add(node)
        for link in self.LINKS: newEnv.LINKS.add(link)
        for point in self.POINTS:
            newPoint = point.copy(newEnv)
        for plink in self.PLINKS: 
            p1Name = plink.begin.string
            p2Name = plink.end.string
            p1 = newEnv.findPointWithName(p1Name)
            p2 = newEnv.findPointWithName(p2Name)
            PLink(newEnv,p1,plink.relation,p2)
        return newEnv

    def close(self,closureType,debug=0):
        closedEnv = self.copy()
        CLOS = Closure(closedEnv,closureType)
        if debug: CLOS.debugOn()
        CLOS.computeClosure()
        closedEnv.isConsistent = CLOS.isConsistent
        return closedEnv

    def translateEnvironment(self,name1,name2):
        """Take the PLinks and translate them into a disjunction of
        TimeML relations."""
        timemlRels = []
        for reltype in LINK2PLINK.keys():
            consistent = 1
            env = self.copy()
            for plink in LINK2PLINK[reltype]:
                rel = plink[2]
                p1Name = translateBoundaries(name1,name2,plink[0],plink[1])
                p2Name = translateBoundaries(name1,name2,plink[3],plink[4])
                p1 = env.findPointWithName(p1Name)
                p2 = env.findPointWithName(p2Name)
                if env.hasConflictingLink(p1,rel,p2): 
                    consistent = 0
                    continue 
                if env.findPLink(p1,rel,p2):
                    continue
                PLink(env,p1,rel,p2)
                if rel == '=': PLink(env,p2,rel,p1)
            if consistent:
                closedEnv = env.close("points")
                if closedEnv.isConsistent: 
                    timemlRels.append(reltype)
        relstring = ''
        for rel in sort(timemlRels):
            relstring = relstring + rel + ' '
        return relstring
    
    def hasConflictingLink(self,p1,rel,p2):
        existingLink = self.findPLink(p1,rel,p2)
        if existingLink:
            #print "    found existing link"
            if existingLink.relation != rel: 
                #print "    found existing inconsistent link"
                return 1
        existingLink = self.findPLink(p2,rel,p1)
        if existingLink:
            #print "    found existing reversed link"
            if existingLink.relation == '<' or rel == '<': 
                #print "    found existing inconsistent reversed link"
                return 1
        return 0

    def findPLink(self,node1,rel,node2):
        """Quick and dirty, index links to speed this up."""
        for link in self.PLINKS.asList():
            if link.begin.string == node1.string and link.end.string == node2.string:
                return link
        return None

    def findNodeWithName(self,name):
        for n in self.NODES:
            if n.string == name: return n
        return None

    def findPointWithName(self,name):
        for point in self.POINTS:
            if point.string == name: return point
        return None

    def getNewPLinks(self,nodeName):
        """Get all links created to bypass nodeName. This only works as
        expected for the axiom compilation stage."""
        newPLinks = []
        for plink in self.PLINKS:
            if plink.isTrivial(): continue
            if plink.begin.interval.string == nodeName: continue
            if plink.end.interval.string == nodeName: continue
            newPLinks.append(plink)
        return newPLinks
            
    def asPLinkString(self):
        plinks = self.PLINKS.asList()
        plinks.sort()
        plStr = "["
        for plink in plinks:
            if plink.isTrivial(): continue
            plStr = "%s %s" % (plStr,plink.asPrettyString())
        plStr = plStr + " ]"
        return plStr
    
    def __str__(self):
        return "\nENVIRONMENT(%s,%s)\n" % (self.id,self.isConsistent) + \
                str(self.NODES) + "\n" + str(self.LINKS) + "\n" + \
                str(self.POINTS) + "\n" + str(self.PLINKS)

    def printPointEnv(self):
        print "\nENVIRONMENT(%s)\n" % (self.id) , \
                str(self.POINTS) , "\n" , str(self.PLINKS), "\n" 

    def printPLinks(self,fh=sys.stdout):
        fh.write("  %s\n" % self.asPLinkString())
예제 #2
0
class Environment(object):

    def __init__(self,id=0):
        self.id = id
        self.isConsistent = 1
        self.NODES = ObjectList("Nodes")
        self.LINKS = ObjectList("TimeML_Links")
        self.POINTS = ObjectList("Points")
        self.PLINKS = ObjectList("Point_Links")
        self.AXIOMS = AXIOMS
        self.POINT_AXIOMS = POINT_AXIOMS
        self.settings = settings
        
    def addNode(self,node): self.NODES.add(node)
    def addLink(self,link): self.LINKS.add(link)
    def addPoint(self,point): self.POINTS.add(point)
    def addPLink(self,plink): self.PLINKS.add(plink)

    def copy(self):
        newEnv = Environment()
        newEnv.id = self.id
        newEnv.isConsistent = self.isConsistent
        for node in self.NODES: newEnv.NODES.add(node)
        for link in self.LINKS: newEnv.LINKS.add(link)
        for point in self.POINTS:
            newPoint = point.copy(newEnv)
        for plink in self.PLINKS: 
            p1Name = plink.begin.string
            p2Name = plink.end.string
            p1 = newEnv.findPointWithName(p1Name)
            p2 = newEnv.findPointWithName(p2Name)
            PLink(newEnv,p1,plink.relation,p2)
        return newEnv

    def close(self,closureType,debug=0):
        closedEnv = self.copy()
        CLOS = Closure(closedEnv,closureType)
        if debug: CLOS.debugOn()
        CLOS.computeClosure()
        closedEnv.isConsistent = CLOS.isConsistent
        return closedEnv

    def translateEnvironment(self,name1,name2):
        """Take the PLinks and translate them into a disjunction of
        TimeML relations."""
        timemlRels = []
        for reltype in LINK2PLINK.keys():
            consistent = 1
            env = self.copy()
            for plink in LINK2PLINK[reltype]:
                rel = plink[2]
                p1Name = translateBoundaries(name1,name2,plink[0],plink[1])
                p2Name = translateBoundaries(name1,name2,plink[3],plink[4])
                p1 = env.findPointWithName(p1Name)
                p2 = env.findPointWithName(p2Name)
                if env.hasConflictingLink(p1,rel,p2): 
                    consistent = 0
                    continue 
                if env.findPLink(p1,rel,p2):
                    continue
                PLink(env,p1,rel,p2)
                if rel == '=': PLink(env,p2,rel,p1)
            if consistent:
                closedEnv = env.close("points")
                if closedEnv.isConsistent: 
                    timemlRels.append(reltype)
        relstring = ''
        for rel in sort(timemlRels):
            relstring = relstring + rel + ' '
        return relstring
    
    def hasConflictingLink(self,p1,rel,p2):
        existingLink = self.findPLink(p1,rel,p2)
        if existingLink:
            #print "    found existing link"
            if existingLink.relation != rel: 
                #print "    found existing inconsistent link"
                return 1
        existingLink = self.findPLink(p2,rel,p1)
        if existingLink:
            #print "    found existing reversed link"
            if existingLink.relation == '<' or rel == '<': 
                #print "    found existing inconsistent reversed link"
                return 1
        return 0

    def findPLink(self,node1,rel,node2):
        """Quick and dirty, index links to speed this up."""
        for link in self.PLINKS.asList():
            if link.begin.string == node1.string and link.end.string == node2.string:
                return link
        return None

    def findNodeWithName(self,name):
        for n in self.NODES:
            if n.string == name: return n
        return None

    def findPointWithName(self,name):
        for point in self.POINTS:
            if point.string == name: return point
        return None

    def getNewPLinks(self,nodeName):
        """Get all links created to bypass nodeName. This only works as
        expected for the axiom compilation stage."""
        newPLinks = []
        for plink in self.PLINKS:
            if plink.isTrivial(): continue
            if plink.begin.interval.string == nodeName: continue
            if plink.end.interval.string == nodeName: continue
            newPLinks.append(plink)
        return newPLinks
            
    def asPLinkString(self):
        plinks = self.PLINKS.asList()
        plinks.sort()
        plStr = "["
        for plink in plinks:
            if plink.isTrivial(): continue
            plStr = "%s %s" % (plStr,plink.asPrettyString())
        plStr = plStr + " ]"
        return plStr
    
    def __str__(self):
        return "\nENVIRONMENT(%s,%s)\n" % (self.id,self.isConsistent) + \
                str(self.NODES) + "\n" + str(self.LINKS) + "\n" + \
                str(self.POINTS) + "\n" + str(self.PLINKS)

    def printPointEnv(self):
        print("\nENVIRONMENT(%s)\n" % (self.id) , \
                str(self.POINTS) , "\n" , str(self.PLINKS), "\n") 

    def printPLinks(self,fh=sys.stdout):
        fh.write("  %s\n" % self.asPLinkString())