config.ilp.solver="CPLEX"

xanthine = Graph.fromGMLString("""graph [
     node [ id 0 label "N" ]
     node [ id 1 label "C" ]
     node [ id 2 label "N" ]
     node [ id 3 label "C" ]
     node [ id 4 label "C" ]
     node [ id 5 label "C" ]
     node [ id 6 label "N" ]
     node [ id 7 label "C" ]
     node [ id 8 label "N" ]
     node [ id 9 label "O" ]                               
     node [ id 10 label "O" ]      
     node [ id 11 label "H" ]  
     node [ id 12 label "H" ]
     node [ id 13 label "H" ] 
     node [ id 14 label "H" ]                                              
     edge [ source 1 target 0 label "-" ]
     edge [ source 2 target 1 label "-" ]
     edge [ source 3 target 2 label "-" ]
     edge [ source 4 target 3 label "=" ]
     edge [ source 5 target 4 label "-" ]
     edge [ source 0 target 5 label "-" ]
     edge [ source 8 target 3 label "-" ]
     edge [ source 7 target 8 label "=" ]
     edge [ source 6 target 7 label "-" ]                               
     edge [ source 4 target 6 label "-" ]
     edge [ source 9 target 1 label "=" ]  
     edge [ source 10 target 5 label "=" ]
     edge [ source 11 target 0 label "-" ]
     edge [ source 12 target 2 label "-" ]
     edge [ source 13 target 6 label "-" ]
     edge [ source 14 target 7 label "-" ]                         
]""", name="Xanthine")

xanthosine = Graph.fromGMLString("""graph [
     node [ id 0 label "N" ]
     node [ id 1 label "C" ]
     node [ id 2 label "N" ]
     node [ id 3 label "C" ]
     node [ id 4 label "C" ]
     node [ id 5 label "C" ]
     node [ id 6 label "N" ]
     node [ id 7 label "C" ]
     node [ id 8 label "N" ]
     node [ id 9 label "O" ]                               
     node [ id 10 label "O" ]      
     node [ id 11 label "H" ]  
     node [ id 12 label "H" ]
     node [ id 13 label "H" ] 
								 
     node [ id 14 label "C" ]							 
     node [ id 15 label "H" ]
     node [ id 16 label "C" ]
     node [ id 17 label "H" ]
     node [ id 18 label "O" ]
     node [ id 19 label "H" ]
     node [ id 20 label "C" ]
     node [ id 21 label "H" ]
     node [ id 22 label "O" ]
     node [ id 23 label "H" ]
     node [ id 24 label "C" ]                               
     node [ id 25 label "O" ]      
     node [ id 26 label "H" ]  
     node [ id 27 label "C" ]
     node [ id 28 label "H" ] 
     node [ id 29 label "H" ]
     node [ id 30 label "O" ]	
     node [ id 31 label "H" ]	
								                                              
     edge [ source 1 target 0 label "-" ]
     edge [ source 2 target 1 label "-" ]
     edge [ source 3 target 2 label "-" ]
     edge [ source 4 target 3 label "=" ]
     edge [ source 5 target 4 label "-" ]
     edge [ source 0 target 5 label "-" ]
     edge [ source 8 target 3 label "-" ]
     edge [ source 7 target 8 label "-" ]
     edge [ source 6 target 7 label "=" ]                               
     edge [ source 4 target 6 label "-" ]
     edge [ source 9 target 1 label "=" ]  
     edge [ source 10 target 5 label "=" ]
     edge [ source 11 target 0 label "-" ]
     edge [ source 12 target 2 label "-" ]
     edge [ source 13 target 7 label "-" ]
	edge [ source 14 target 8 label "-" ]
     
	edge [ source 15 target 14 label "-" ]
     edge [ source 25 target 14 label "-" ]
     edge [ source 16 target 14 label "-" ]
     edge [ source 17 target 16 label "-" ]
     edge [ source 18 target 16 label "-" ]
	edge [ source 19 target 18 label "-" ]
     edge [ source 20 target 16 label "-" ]
     edge [ source 21 target 20 label "-" ]
     edge [ source 22 target 20 label "-" ]
	edge [ source 23 target 22 label "-" ]
     edge [ source 24 target 20 label "-" ]                               
     edge [ source 25 target 24 label "-" ]
     edge [ source 26 target 24 label "-" ]  
     edge [ source 27 target 24 label "-" ]
     edge [ source 28 target 27 label "-" ]
     edge [ source 29 target 27 label "-" ]
     edge [ source 30 target 27 label "-" ]
	edge [ source 31 target 30 label "-" ]                       
]""", name="Xanthosine")

caffeine = Graph.fromGMLString("""graph [
     node [ id 0 label "N" ]
     node [ id 1 label "C" ]
     node [ id 2 label "N" ]
     node [ id 3 label "C" ]
     node [ id 4 label "C" ]
     node [ id 5 label "C" ]
     node [ id 6 label "N" ]
     node [ id 7 label "C" ]
     node [ id 8 label "N" ]
     node [ id 9 label "O" ]                               
     node [ id 10 label "O" ]      
     node [ id 11 label "C" ]  
     node [ id 12 label "C" ]
     node [ id 13 label "C" ] 
     node [ id 14 label "H" ]
     node [ id 15 label "H" ]
     node [ id 16 label "H" ]
     node [ id 17 label "H" ]
	node [ id 18 label "H" ]
	node [ id 19 label "H" ]
     node [ id 20 label "H" ]
     node [ id 21 label "H" ]
	node [ id 22 label "H" ]
	node [ id 23 label "H" ]
     edge [ source 1 target 0 label "-" ]
     edge [ source 2 target 1 label "-" ]
     edge [ source 3 target 2 label "-" ]
     edge [ source 4 target 3 label "=" ]
     edge [ source 5 target 4 label "-" ]
     edge [ source 0 target 5 label "-" ]
     edge [ source 8 target 3 label "-" ]
     edge [ source 7 target 8 label "=" ]
     edge [ source 6 target 7 label "-" ]                               
     edge [ source 4 target 6 label "-" ]
     edge [ source 9 target 1 label "=" ]  
     edge [ source 10 target 5 label "=" ]
     edge [ source 11 target 0 label "-" ]
     edge [ source 12 target 2 label "-" ]
     edge [ source 13 target 6 label "-" ]
     edge [ source 14 target 7 label "-" ]     
	edge [ source 11 target 15 label "-" ]   
	edge [ source 11 target 16 label "-" ]
	edge [ source 11 target 17 label "-" ]   
	edge [ source 12 target 18 label "-" ]      
	edge [ source 12 target 19 label "-" ]   
	edge [ source 12 target 20 label "-" ]
	edge [ source 13 target 21 label "-" ]   
	edge [ source 13 target 22 label "-" ] 	
	edge [ source 13 target 23 label "-" ] 					               
]""", name="Caffeine")



#nmethylation = Rule.fromGMLString(
"""rule [
   left [
        node [ id 1 label "H" ]
        edge [ source 1 target 2 label "-" ]
   ]
   context [
        node [ id 2 label "N" ]
        node [ id 3 label "C"]
        node [ id 4 label "C"]
        edge [ source 3 target 2 label "-" ]
        edge [ source 4 target 2 label "-" ]
   ]
   right [
        node [ id 5 label "C" ]
        node [ id 6 label "H" ]
        node [ id 7 label "H" ]
        node [ id 8 label "H" ]  
        edge [ source 2 target 5 label "-" ]                                                                                                  
        edge [ source 5 target 6 label "-" ]
        edge [ source 5 target 7 label "-" ]
        edge [ source 5 target 8 label "-" ]                         
   ]
]"""
#)

n1methylation = Rule.fromGMLString(
"""rule [
   left [
        node [ id 1 label "H" ]
        edge [ source 1 target 2 label "-" ]
   ]
   context [
        node [ id 2 label "N" ]
        node [ id 3 label "C"]
        node [ id 4 label "C"]
        node [ id 5 label "O"]
        node [ id 6 label "O"]
        node [ id 7 label "N"]
	   node [ id 8 label "C"]
	   node [ id 9 label "H"]
	   node [ id 10 label "H"]
	   node [ id 11 label "H"]
        edge [ source 3 target 2 label "-" ]
        edge [ source 4 target 2 label "-" ]
        edge [ source 3 target 5 label "=" ]
	   edge [ source 4 target 6 label "=" ]
	   edge [ source 4 target 7 label "-" ]
	   edge [ source 8 target 7 label "-" ]
	   edge [ source 9 target 8 label "-" ]
	   edge [ source 10 target 8 label "-" ]
	   edge [ source 11 target 8 label "-" ]
   ]
   right [
        node [ id 12 label "C" ]
        node [ id 13 label "H" ]
        node [ id 14 label "H" ]
        node [ id 15 label "H" ]  
        edge [ source 2 target 12 label "-" ]                                                                                                  
        edge [ source 12 target 13 label "-" ]
        edge [ source 12 target 14 label "-" ]
        edge [ source 12 target 15 label "-" ]                         
   ]
]"""
)

n3methylation = Rule.fromGMLString(
"""rule [
   left [
        node [ id 1 label "H" ]
        edge [ source 1 target 2 label "-" ]
   ]
   context [
        node [ id 2 label "N" ]
        node [ id 3 label "C"]
        node [ id 4 label "C"]
        node [ id 5 label "O"]
        node [ id 6 label "C"]
	   node [ id 7 label "N"]
	   node [ id 8 label "C"]
	   node [ id 9 label "H" ] 
	   node [ id 10 label "H" ] 
	   node [ id 11 label "H" ] 
        edge [ source 3 target 2 label "-" ]
        edge [ source 4 target 2 label "-" ]
        edge [ source 3 target 5 label "=" ]
        edge [ source 4 target 6 label "=" ]
	   edge [ source 6 target 7 label "-" ]
	   edge [ source 7 target 8 label "-" ]
	   edge [ source 8 target 9 label "-" ]
	   edge [ source 8 target 10 label "-" ]
	   edge [ source 8 target 11 label "-" ]
	   
   ]
   right [
        node [ id 12 label "C" ]
        node [ id 13 label "H" ]
        node [ id 14 label "H" ]
        node [ id 15 label "H" ]
        edge [ source 2 target 12 label "-" ]                                                                                                  
        edge [ source 12 target 13 label "-" ]
        edge [ source 12 target 14 label "-" ]
        edge [ source 12 target 15 label "-" ]
   ]
]"""
)

n7methylation = Rule.fromGMLString(
"""rule [
   left [
        node [ id 1 label "H" ]
        edge [ source 1 target 2 label "-" ]
   ]
   context [
        node [ id 2 label "N" ]
        node [ id 3 label "C"]
        node [ id 4 label "C"]
	   node [ id 5 label "H"]
        edge [ source 3 target 2 label "-" ]
        edge [ source 4 target 2 label "-" ]
        edge [ source 3 target 5 label "-" ]
   ]
   right [
        node [ id 6 label "C" ]
        node [ id 7 label "H" ]
        node [ id 8 label "H" ]  
        node [ id 9 label "H" ]
        edge [ source 2 target 6 label "-" ]                                                                                                  
        edge [ source 6 target 7 label "-" ]
        edge [ source 6 target 8 label "-" ]      
	   edge [ source 6 target 9 label "-" ]                   
   ]
]"""
)

n1demethylation = Rule.fromGMLString(
"""rule [
   left [
        node [ id 1 label "C" ]
        node [ id 2 label "H" ]
        node [ id 3 label "H" ]
        node [ id 4 label "H" ]  
        edge [ source 5 target 1 label "-" ]                                                                                                  
        edge [ source 1 target 2 label "-" ]
        edge [ source 1 target 3 label "-" ]
        edge [ source 1 target 4 label "-" ] 
   ]
   context [
        node [ id 5 label "N" ]
        node [ id 6 label "C"]
        node [ id 7 label "C"]
        node [ id 8 label "O"]
	   node [ id 9 label "O"]
        edge [ source 6 target 5 label "-" ]
        edge [ source 7 target 5 label "-" ]
        edge [ source 6 target 8 label "=" ]
	   edge [ source 7 target 9 label "=" ]
   ]
   right [
        node [ id 10 label "H" ]
        edge [ source 10 target 5 label "-" ]                        
   ]
]"""
)

n3demethylation = Rule.fromGMLString(
"""rule [
   left [
        node [ id 1 label "C" ]
        node [ id 2 label "H" ]
        node [ id 3 label "H" ]
        node [ id 4 label "H" ]  
        edge [ source 5 target 1 label "-" ]                                                                                                  
        edge [ source 1 target 2 label "-" ]
        edge [ source 1 target 3 label "-" ]
        edge [ source 1 target 4 label "-" ] 
   ]
   context [
        node [ id 5 label "N" ]
        node [ id 6 label "C"]
        node [ id 7 label "C"]
        node [ id 8 label "O"]
	   node [ id 9 label "N"]
	   node [ id 10 label "H" ]
        edge [ source 6 target 5 label "-" ]
        edge [ source 7 target 5 label "-" ]
        edge [ source 6 target 8 label "=" ]
	   edge [ source 7 target 9 label "-" ]
	   edge [ source 9 target 10 label "-" ]
   ]
   right [
        node [ id 11 label "H" ]
        edge [ source 11 target 5 label "-" ]                        
   ]
]"""
)

n7demethylation = Rule.fromGMLString(
"""rule [
   left [
        node [ id 1 label "C" ]
        node [ id 2 label "H" ]
        node [ id 3 label "H" ]
        node [ id 4 label "H" ]  
        edge [ source 5 target 1 label "-" ]                                                                                                  
        edge [ source 1 target 2 label "-" ]
        edge [ source 1 target 3 label "-" ]
        edge [ source 1 target 4 label "-" ] 
   ]
   context [
        node [ id 5 label "N" ]
        node [ id 6 label "C"]
        node [ id 7 label "C"]
        node [ id 8 label "N"]
	   node [ id 9 label "H"]
        edge [ source 6 target 5 label "-" ]
        edge [ source 7 target 6 label "=" ]
        edge [ source 8 target 7 label "-" ]
	   edge [ source 9 target 8 label "-" ]
   ]
   right [
        node [ id 10 label "H" ]
        edge [ source 10 target 5 label "-" ]                        
   ]
]"""
)

flowPrinter = FlowPrinter()
flowPrinter.printUnfiltered = False

postSection("Loaded Graphs")
for a in inputGraphs:
	a.print()
postSection("Loaded Rules")
for a in inputRules:
	a.print()

dg = DG(graphDatabase=inputGraphs)
dg.build().execute(
	addSubset(inputGraphs)
	>> rightPredicate[
		lambda d: all(g.vLabelCount("C") <= 20 for g in d.right)
	](
		repeat(inputRules)
	)
)
dg.print()
postSection("Product Graphs")
for a in dg.vertices:
	a.graph.print()

flowAutocata = Flow(dg)
flowAutocata.overallAutocatalysis.enable()
for a in {xanthine}:
	flowAutocata.addSource(a)
flowAutocata.addSink(caffeine)
flowAutocata.findSolutions()
flowAutocata.solutions.list()
flowAutocata.solutions.print(flowPrinter)


sys.exit(0)

rc = rcEvaluator(inputRules)
for dRef in dg.derivations:
	der = dRef.derivation
	educt = rcId(der.left[0])
	for i in range(1, len(der.left)):
		educt = educt *rcParallel* rcId(der.left[i])
	product = rcId(der.right[0])
	for i in range(1, len(der.right)):
		product = product *rcParallel* rcId(der.right[i])
	rcExp = educt *rcSuper(allowPartial=False)* der.rule *rcSuper(allowPartial=False)* product
	res = rc.eval(rcExp)
	dRef.print()
	for a in res:
		a.print()
		a.printGML()