}
}
private RuleGraph createRoot(Rule r, Data data) {
RuleGraph result = null;
{
Agent aL = null;
Agent bondA = null;
Agent bondB = null;
Site siteA = null;
Site siteB = null;
Iterator<Agent> rhsIt = data.rhs.iterator();
Iterator<Agent> lhsIt = data.lhs.iterator();
boolean reverse=false;
while(lhsIt.hasNext() && rhsIt.hasNext()) {
Agent a = lhsIt.next();
Agent b = rhsIt.next();
Iterator<Site> asIt = a.getSites().iterator();
Iterator<Site> bsIt = b.getSites().iterator();
while(asIt.hasNext() && bsIt.hasNext()){
Site s = asIt.next();
Site bs = bsIt.next();
String m = s.getBindingMark();
String n = bs.getBindingMark();
if (m != null && !"?".equals(m) && !"_".equals(m)){
if (data.first.containsKey(m))
data.second.put(m, a);
else
data.first.put(m, a);
}
if ((m == null && n != null) || (n == null && m != null)){
if (bondA != null){
if(bondB != null)
throw new IllegalArgumentException("Rule not supported: More than one bond formation dissallowed.");
bondB = a;
siteB = bs;
if (m != null){
data.first.remove(m);
data.second.remove(m);
reverse = true;
}
} else {
bondA = a;
siteA = bs;
}
}
if (s.getState() != null && !s.getState().equals(bs.getState())){
if (aL != null)
throw new IllegalArgumentException("Rule not supported: More than one flip dissallowed.");
aL = a;
}
}
}
if (aL == null && bondA == null)
throw new IllegalArgumentException("At least one site should differ");
if (aL != null && bondA != null)
throw new IllegalArgumentException("Rule not supported: Either flip or bond, not both.");
if (bondA == null){
int indexOf = data.lhs.indexOf(aL);
Agent aR = data.rhs.get(indexOf);
result = data.rgArrays[indexOf] = new RuleGraph(new SiteNode(aL, aR));
data.q.add(pairOf(aL, data.rgArrays[indexOf]));
} else {
result = new RuleGraph(new BondNode());
int i=0;
i = data.lhs.indexOf(bondA);
RuleGraph r1 = data.rgArrays[i] = new RuleGraph(new Node(reverse ? data.rhs.get(i) : bondA));
result.addChild(new Link("1",siteA.getName()), r1);
i = data.lhs.indexOf(bondB);
RuleGraph r2 = data.rgArrays[i] = new RuleGraph(new Node(reverse ? data.rhs.get(i) : bondB));
result.addChild(new Link("2",siteB.getName()), r2);
data.q.add(pairOf(bondA, r1));
data.q.add(pairOf(bondB, r2));
}