Skip to content

Commit

Permalink
dependencies (also on states of constraints, not just on constraint n…
Browse files Browse the repository at this point in the history
…ames) implemented and working perfectly for SDF, Rascal and DCG
  • Loading branch information
grammarware committed May 13, 2012
1 parent 5fc075e commit e17c2ad
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 139 deletions.
4 changes: 4 additions & 0 deletions shared/python/MBGF.py
Expand Up @@ -28,6 +28,10 @@ def parsebasic(self, xml):
self.depends = xml.attrib['depends']
else:
self.depends = ''
if 'blocks' in xml.attrib:
self.blocks = xml.attrib['blocks']
else:
self.blocks = ''
self.data = {}

class SrcSimpleModel (TopModel):
Expand Down
8 changes: 8 additions & 0 deletions topics/convergence/tri/m1
@@ -0,0 +1,8 @@
#!/bin/sh

# m1 file.mbgf one two
# $0 $1 $2 $3
./mbgf2xbgf.py $1 $2 $3 tmp.xbgf
xbgf tmp.xbgf `xpath $1 'mbgf:sequence/sources/src[@name="'$2'"]/text()' 2> /dev/null` tmp1.bgf
gdt tmp1.bgf `xpath $1 'mbgf:sequence/sources/src[@name="'$3'"]/text()' 2> /dev/null` || exit -1
rm tmp.xbgf tmp1.bgf
18 changes: 2 additions & 16 deletions topics/convergence/tri/m2
Expand Up @@ -2,19 +2,5 @@

# m2 file.mbgf one two
# $0 $1 $2 $3
./mbgf2xbgf.py $1 $2 $3 tmp.xbgf
xbgf tmp.xbgf `xpath $1 'mbgf:sequence/sources/src[@name="'$2'"]/text()' 2> /dev/null` tmp1.bgf
gdts tmp1.bgf `xpath $1 'mbgf:sequence/sources/src[@name="'$3'"]/text()' 2> /dev/null`
rm tmp.xbgf
./mbgf2xbgf.py $1 $3 $2 tmp.xbgf
xbgf tmp.xbgf `xpath $1 'mbgf:sequence/sources/src[@name="'$3'"]/text()' 2> /dev/null` tmp2.bgf
gdts tmp2.bgf `xpath $1 'mbgf:sequence/sources/src[@name="'$2'"]/text()' 2> /dev/null`
# m2 file.mbgf 1 one.bgf 2 two.bgf
# $0 $1 $2 $3 $4 $5
# ./mbgf2xbgf.py $1 $2 $4 tmp.xbgf
# xbgf tmp.xbgf $3 $3.tmp
# gdts $3.tmp $5
# rm tmp.xbgf
# ./mbgf2xbgf.py $1 $4 $2 tmp.xbgf
# xbgf tmp.xbgf $5 $5.tmp
# gdts $5.tmp $3
./m1 $1 $2 $3
./m1 $1 $3 $2
7 changes: 7 additions & 0 deletions topics/convergence/tri/m3
@@ -0,0 +1,7 @@
#!/bin/sh

# m3 file.mbgf one two three
# $0 $1 $2 $3 $4
./m2 $1 $2 $3
./m2 $1 $2 $4
./m2 $1 $3 $4
82 changes: 61 additions & 21 deletions topics/convergence/tri/mbgf2xbgf.py
Expand Up @@ -62,6 +62,7 @@ def wrapexp(exp,mod):
# executing
print('Inferring unidirectional grammar transformations from',inname,'to',outname,'...')
pbyid = {}
xbgfsbyid = {}
unscheduled = []
for p in predicates:
# print(p.who(),'#',p.id,'@',p.depends)
Expand All @@ -70,21 +71,10 @@ def wrapexp(exp,mod):
else:
unscheduled.append(p.id)
pbyid[p.id] = p
scheduled = []
while len(unscheduled)>0:
for i in range(0,len(unscheduled)):
candidate = pbyid[unscheduled[i]]
if not(candidate.depends) or candidate.depends in scheduled:
scheduled.append(unscheduled[i])
unscheduled.remove(unscheduled[i])
break
else:
print('Failed to schedule',unscheduled)
sys.exit(2)
print('Scheduled order:',scheduled)
for id in scheduled:
for id in unscheduled:
p = pbyid[id]
print('[MBGF]',p.who(),'(',p.getSpecifics(),',',p.getData(inname),',',p.getData(outname),')', end=' ::= ')
xbgfsbyid[id] = []
print('[MBGF] (#'+p.id+')',p.who(),'(',p.getSpecifics(),',',p.getData(inname),',',p.getData(outname),')', end=' ::= ')
if p.who() == 'NamingConvention':
if p.getData(inname) == p.getData(outname):
print('id')
Expand All @@ -103,7 +93,7 @@ def wrapexp(exp,mod):
w.addChild(XBGF3.Leaf('from',n1))
w.addChild(XBGF3.Leaf('to',n2))
ren.addParam(w)
xbgf.addStep(ren)
xbgfsbyid[id].append(ren)
elif p.who() == 'Width':
exp = p.expr
applynamemap(exp)
Expand Down Expand Up @@ -158,7 +148,7 @@ def wrapexp(exp,mod):
else:
print('ERROR: unknown out modifier!')
sys.exit(1)
xbgf.addStep(ren)
xbgfsbyid[id].append(ren)
elif p.who() == 'Unification':
n0 = p.nt
n1 = p.getNTs(inname)
Expand All @@ -173,7 +163,7 @@ def wrapexp(exp,mod):
ren = XBGF3.Step('unite')
ren.addParam(XBGF3.Leaf('add',n1[0]))
ren.addParam(XBGF3.Leaf('to',n3))
xbgf.addStep(ren)
xbgfsbyid[id].append(ren)
elif n2:
# print('[MBGF] unification('+n2+','+n0+') ::= split('+n3+','+n0+')')
print('split('+n3+','+n0+')')
Expand All @@ -185,7 +175,7 @@ def wrapexp(exp,mod):
ren.addParam(BGF3.LabelText(l.text))
# print('!!!',e.findall('add')[outnum-1].attrib)
# ren.addParam(XBGF3.Leaf('to',n3))
xbgf.addStep(ren)
xbgfsbyid[id].append(ren)
else:
# TODO: check for the situation when both n1 and n2 are not empty
# print('[MBGF] unification(∅,'+n2+') ::= id')
Expand Down Expand Up @@ -227,7 +217,7 @@ def wrapexp(exp,mod):
e.add(BGF3.Expression(e2))
p.setExpr(BGF3.Expression(e))
ren.addParam(p)
xbgf.addStep(ren)
xbgfsbyid[id].append(ren)
elif k1.endswith('assoc') and k2=='iterate':
if n in namemap:
n1 = namemap[n]
Expand Down Expand Up @@ -269,12 +259,62 @@ def wrapexp(exp,mod):
e.setExpr(BGF3.Expression(e2))
p.setExpr(BGF3.Expression(e))
ren.addParam(p)
xbgf.addStep(ren)
xbgfsbyid[id].append(ren)
else:
print('PROBLEM')
sys.exit(1)
else:
print('UNKNOWN COMMAND')
scheduled = []
depends = {}
impact = []
for i in unscheduled:
if len(xbgfsbyid[i]) == 0:
unscheduled.remove(i)
elif pbyid[i].depends:
step,state = pbyid[i].depends.split(':')
print('Step',i,'depends on state',state,'of step',step)
if xbgfsbyid[step][0].name == state:
# step should happen before i
depends[i] = step
else:
# i should happen before step
depends[step] = i
# impact.append(step)
print('Scheduling',unscheduled,'...')
if depends:
print('Dependencies:',depends)
while len(unscheduled)>0:
for i in range(0,len(unscheduled)):
candidate = pbyid[unscheduled[i]]
# if not candidate.blocks or candidate.blocks not in unscheduled:
# scheduled.append(unscheduled[i])
# unscheduled.remove(unscheduled[i])
# break
if unscheduled[i] not in depends or depends[unscheduled[i]] in scheduled:
scheduled.append(unscheduled[i])
unscheduled.remove(unscheduled[i])
break
#
# if not candidate.depends and unscheduled[i] not in impact:
# scheduled.append(unscheduled[i])
# unscheduled.remove(unscheduled[i])
# break
# elif candidate.depends:
# step,state = candidate.depends.split(':')
# print('Step',unscheduled[i],'depends on state',state,'of step',step)
# if step in unscheduled:
# print(xbgfsbyid[step][0].name)
# elif candidate.depends in scheduled:
# # TODO think of a better way of representing dependencies between predicates
# if
else:
print('Failed to schedule',unscheduled)
sys.exit(2)
print('Scheduled order:',scheduled)
for id in scheduled:
for cmd in xbgfsbyid[id]:
xbgf.addStep(cmd)
ET.ElementTree(xbgf.getXml()).write(sys.argv[4])
print('Success',predicates)
print('Success.')

101 changes: 0 additions & 101 deletions topics/convergence/tri/test.3bgf

This file was deleted.

2 changes: 1 addition & 1 deletion topics/convergence/tri/test.mbgf
Expand Up @@ -68,7 +68,7 @@
</in>
</src>
</unification>
<iteration depends="uni">
<iteration id="iter" depends="uni:unite">
<label>binary</label>
<name>expr</name>
<separator>ops</separator>
Expand Down

0 comments on commit e17c2ad

Please sign in to comment.