From 951794a7acc59229bdc3aced51cb871a1b42b90c Mon Sep 17 00:00:00 2001 From: ccremers Date: Sun, 4 Dec 2005 14:46:37 +0000 Subject: [PATCH] - Added spdl generator initials. --- scripts/if2spdl/If.py | 68 +++++++++++++++++++++++++------------ scripts/if2spdl/Ifparser.py | 9 ++--- scripts/if2spdl/Spdl.py | 35 +++++++++++++++++++ scripts/if2spdl/if2spdl.py | 13 ++++--- 4 files changed, 92 insertions(+), 33 deletions(-) create mode 100644 scripts/if2spdl/Spdl.py diff --git a/scripts/if2spdl/If.py b/scripts/if2spdl/If.py index e7e8335..2e7cd22 100644 --- a/scripts/if2spdl/If.py +++ b/scripts/if2spdl/If.py @@ -4,10 +4,9 @@ # # Objects and stuff for the intermediate format # -firstone = True - class Message(object): - pass + def __cmp__(self,other): + return cmp(str(self),str(other)) class Constant(Message): def __init__ (self,type,s,optprime=""): @@ -61,33 +60,46 @@ class Fact(list): def __repr__(self): return "Fact<" + list.__repr__(self) + ">" + def getActor(self): + return None + class GoalFact(Fact): def __repr__(self): return "Goal " + Fact.__repr__(self) class PrincipalFact(Fact): + def __init__(self,t): + self.step = t[0] + self.readnextfrom = t[1] + self.actor = t[2] + self.runknowledge = t[3] + self.knowledge = t[4] + self.bool = t[5] + self.session = t[6] + def __str__(self): res = "Principal Fact:" - res += "\nStep " + str(self[0]) - res += "\nReadNextFrom " + str(self[1]) - res += "\nActor " + str(self[2]) - res += "\nRunKnowledge " + str(self[3]) - res += "\nKnowledge " + str(self[4]) - #res += "\nBool " + str(self[5]) - res += "\nSession " + str(self[6]) + res += "\nStep " + str(self.step) + res += "\nReadNextFrom " + str(self.readnextfrom) + res += "\nActor " + str(self.actor) + res += "\nRunKnowledge " + str(self.runknowledge) + res += "\nKnowledge " + str(self.knowledge) + #res += "\nBool " + str(self.bool) + res += "\nSession " + str(self.session) return res + "\n" def __repr__(self): return str(self) + def getActor(self): + return self.actor + class TimeFact(Fact): def __repr__(self): return "Time " + Fact.__repr__(self) class MessageFact(Fact): def __init__(self,t): - global firstone - self.step = t[0] self.realsender = t[1] self.claimsender = t[2] @@ -95,12 +107,6 @@ class MessageFact(Fact): self.message = t[4] self.session = t[5] - ### TEST - if firstone: - print self.spdl() - #firstone = False - - def __str__(self): res = "Message Fact:" res += "\nStep " + str(self.step) @@ -139,10 +145,11 @@ class Label(object): return str(self) class Rule(object): - def __init__(self,left=None,right=None): + def __init__(self,left=[],right=[]): self.left = left self.right = right self.label = None + self.actor = None def setLabel(self,label): self.label = label @@ -152,10 +159,10 @@ class Rule(object): if self.label != None: res += " (" + str(self.label) +")" res += "\n" - if self.left != None: + if len(self.left) > 0: res += str(self.left) + "\n" - if self.right != None: - if self.left != None: + if len(self.right) > 0: + if len(self.left) > 0: res += "=>\n" res += str(self.right) + "\n" res += ".\n" @@ -164,15 +171,32 @@ class Rule(object): def __repr__(self): return str(self) + def getActor(self): + return None + + class InitialRule(Rule): + def __str__(self): return "Initial " + Rule.__str__(self) + class MessageRule(Rule): + def __init__(self,left=[],right=[]): + Rule.__init__(self,left,right) + self.actor = None + for fact in left + right: + actor = fact.getActor() + if actor != None: + self.actor = actor + def __str__(self): return "Message " + Rule.__str__(self) + def getActor(self): + return self.actor + class GoalRule(Rule): def __str__(self): return "Goal " + Rule.__str__(self) diff --git a/scripts/if2spdl/Ifparser.py b/scripts/if2spdl/Ifparser.py index 9edb68e..7134c98 100755 --- a/scripts/if2spdl/Ifparser.py +++ b/scripts/if2spdl/Ifparser.py @@ -297,15 +297,16 @@ def linesParse(lines): parser = ifParser() result = parser.parseString("".join( lines[1:])) - ### TEST - #print result + return result # Main code def main(): + print "Testing Ifparser module" + print file = open("NSPK_LOWE.if", "r") - linesParse(file.readlines()) + rulelist = linesParse(file.readlines()) file.close() - + print Spdl.generator(rulelist) if __name__ == '__main__': main() diff --git a/scripts/if2spdl/Spdl.py b/scripts/if2spdl/Spdl.py new file mode 100644 index 0000000..025faf9 --- /dev/null +++ b/scripts/if2spdl/Spdl.py @@ -0,0 +1,35 @@ +#!/usr/bin/python +# +# Spdl generator +# +import If + +def processRole(rulelist, role): + + print "Role", role + for rule in rulelist: + if rule.getActor() == role: + print rule + + print + return "" + + +def getRoles(rulelist): + roles = [] + for rule in rulelist: + actor = rule.getActor() + if actor != None: + if actor not in roles: + roles.append(actor) + return roles + +def generator(rulelist): + roles = getRoles(rulelist) + print "Found",len(rulelist),"rules." + print "Roles:", roles + res = "" + for role in roles: + res += processRole(rulelist,role) + return res + diff --git a/scripts/if2spdl/if2spdl.py b/scripts/if2spdl/if2spdl.py index c7c80fa..35ca54c 100755 --- a/scripts/if2spdl/if2spdl.py +++ b/scripts/if2spdl/if2spdl.py @@ -1,15 +1,14 @@ #!/usr/bin/python -from parser import * -from generator import * -import pprint +import If +import Ifparser +import Spdl def main(): file = open("NSPK_LOWE.if", "r") - res = ifParse ("".join(file.readlines() ) ) - generateSpdl(res) - #pprint.pprint (res.asList()) - + rulelist = Ifparser.linesParse(file.readlines()) + file.close() + print Spdl.generator(rulelist) if __name__ == "__main__": main()