- Added spdl generator initials.
This commit is contained in:
parent
8dd9dfb901
commit
951794a7ac
@ -4,10 +4,9 @@
|
|||||||
#
|
#
|
||||||
# Objects and stuff for the intermediate format
|
# Objects and stuff for the intermediate format
|
||||||
#
|
#
|
||||||
firstone = True
|
|
||||||
|
|
||||||
class Message(object):
|
class Message(object):
|
||||||
pass
|
def __cmp__(self,other):
|
||||||
|
return cmp(str(self),str(other))
|
||||||
|
|
||||||
class Constant(Message):
|
class Constant(Message):
|
||||||
def __init__ (self,type,s,optprime=""):
|
def __init__ (self,type,s,optprime=""):
|
||||||
@ -61,33 +60,46 @@ class Fact(list):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Fact<" + list.__repr__(self) + ">"
|
return "Fact<" + list.__repr__(self) + ">"
|
||||||
|
|
||||||
|
def getActor(self):
|
||||||
|
return None
|
||||||
|
|
||||||
class GoalFact(Fact):
|
class GoalFact(Fact):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Goal " + Fact.__repr__(self)
|
return "Goal " + Fact.__repr__(self)
|
||||||
|
|
||||||
class PrincipalFact(Fact):
|
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):
|
def __str__(self):
|
||||||
res = "Principal Fact:"
|
res = "Principal Fact:"
|
||||||
res += "\nStep " + str(self[0])
|
res += "\nStep " + str(self.step)
|
||||||
res += "\nReadNextFrom " + str(self[1])
|
res += "\nReadNextFrom " + str(self.readnextfrom)
|
||||||
res += "\nActor " + str(self[2])
|
res += "\nActor " + str(self.actor)
|
||||||
res += "\nRunKnowledge " + str(self[3])
|
res += "\nRunKnowledge " + str(self.runknowledge)
|
||||||
res += "\nKnowledge " + str(self[4])
|
res += "\nKnowledge " + str(self.knowledge)
|
||||||
#res += "\nBool " + str(self[5])
|
#res += "\nBool " + str(self.bool)
|
||||||
res += "\nSession " + str(self[6])
|
res += "\nSession " + str(self.session)
|
||||||
return res + "\n"
|
return res + "\n"
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self)
|
return str(self)
|
||||||
|
|
||||||
|
def getActor(self):
|
||||||
|
return self.actor
|
||||||
|
|
||||||
class TimeFact(Fact):
|
class TimeFact(Fact):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Time " + Fact.__repr__(self)
|
return "Time " + Fact.__repr__(self)
|
||||||
|
|
||||||
class MessageFact(Fact):
|
class MessageFact(Fact):
|
||||||
def __init__(self,t):
|
def __init__(self,t):
|
||||||
global firstone
|
|
||||||
|
|
||||||
self.step = t[0]
|
self.step = t[0]
|
||||||
self.realsender = t[1]
|
self.realsender = t[1]
|
||||||
self.claimsender = t[2]
|
self.claimsender = t[2]
|
||||||
@ -95,12 +107,6 @@ class MessageFact(Fact):
|
|||||||
self.message = t[4]
|
self.message = t[4]
|
||||||
self.session = t[5]
|
self.session = t[5]
|
||||||
|
|
||||||
### TEST
|
|
||||||
if firstone:
|
|
||||||
print self.spdl()
|
|
||||||
#firstone = False
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
res = "Message Fact:"
|
res = "Message Fact:"
|
||||||
res += "\nStep " + str(self.step)
|
res += "\nStep " + str(self.step)
|
||||||
@ -139,10 +145,11 @@ class Label(object):
|
|||||||
return str(self)
|
return str(self)
|
||||||
|
|
||||||
class Rule(object):
|
class Rule(object):
|
||||||
def __init__(self,left=None,right=None):
|
def __init__(self,left=[],right=[]):
|
||||||
self.left = left
|
self.left = left
|
||||||
self.right = right
|
self.right = right
|
||||||
self.label = None
|
self.label = None
|
||||||
|
self.actor = None
|
||||||
|
|
||||||
def setLabel(self,label):
|
def setLabel(self,label):
|
||||||
self.label = label
|
self.label = label
|
||||||
@ -152,10 +159,10 @@ class Rule(object):
|
|||||||
if self.label != None:
|
if self.label != None:
|
||||||
res += " (" + str(self.label) +")"
|
res += " (" + str(self.label) +")"
|
||||||
res += "\n"
|
res += "\n"
|
||||||
if self.left != None:
|
if len(self.left) > 0:
|
||||||
res += str(self.left) + "\n"
|
res += str(self.left) + "\n"
|
||||||
if self.right != None:
|
if len(self.right) > 0:
|
||||||
if self.left != None:
|
if len(self.left) > 0:
|
||||||
res += "=>\n"
|
res += "=>\n"
|
||||||
res += str(self.right) + "\n"
|
res += str(self.right) + "\n"
|
||||||
res += ".\n"
|
res += ".\n"
|
||||||
@ -164,15 +171,32 @@ class Rule(object):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self)
|
return str(self)
|
||||||
|
|
||||||
|
def getActor(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class InitialRule(Rule):
|
class InitialRule(Rule):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Initial " + Rule.__str__(self)
|
return "Initial " + Rule.__str__(self)
|
||||||
|
|
||||||
|
|
||||||
class MessageRule(Rule):
|
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):
|
def __str__(self):
|
||||||
return "Message " + Rule.__str__(self)
|
return "Message " + Rule.__str__(self)
|
||||||
|
|
||||||
|
def getActor(self):
|
||||||
|
return self.actor
|
||||||
|
|
||||||
class GoalRule(Rule):
|
class GoalRule(Rule):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Goal " + Rule.__str__(self)
|
return "Goal " + Rule.__str__(self)
|
||||||
|
@ -297,15 +297,16 @@ def linesParse(lines):
|
|||||||
parser = ifParser()
|
parser = ifParser()
|
||||||
result = parser.parseString("".join( lines[1:]))
|
result = parser.parseString("".join( lines[1:]))
|
||||||
|
|
||||||
### TEST
|
return result
|
||||||
#print result
|
|
||||||
|
|
||||||
# Main code
|
# Main code
|
||||||
def main():
|
def main():
|
||||||
|
print "Testing Ifparser module"
|
||||||
|
print
|
||||||
file = open("NSPK_LOWE.if", "r")
|
file = open("NSPK_LOWE.if", "r")
|
||||||
linesParse(file.readlines())
|
rulelist = linesParse(file.readlines())
|
||||||
file.close()
|
file.close()
|
||||||
|
print Spdl.generator(rulelist)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
35
scripts/if2spdl/Spdl.py
Normal file
35
scripts/if2spdl/Spdl.py
Normal file
@ -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
|
||||||
|
|
@ -1,15 +1,14 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
from parser import *
|
import If
|
||||||
from generator import *
|
import Ifparser
|
||||||
import pprint
|
import Spdl
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
file = open("NSPK_LOWE.if", "r")
|
file = open("NSPK_LOWE.if", "r")
|
||||||
res = ifParse ("".join(file.readlines() ) )
|
rulelist = Ifparser.linesParse(file.readlines())
|
||||||
generateSpdl(res)
|
file.close()
|
||||||
#pprint.pprint (res.asList())
|
print Spdl.generator(rulelist)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user