Example #1
0
def erule(rule, domain):
  def makeDiffusions(expression, dst):
    for agent in expression:
      if(interfaceHasSite(agent["interface"], "%")):
        diffuseAgent(agent["interface"], dst)

  def encloseAndSuffix(prefix, suffix):
    return "(" + prefix + ") " + suffix

  def mixRuleWithLoc(loc):
    newRule = copy.deepcopy(rule)
    newRule["label"] = newRule["label"].replace("%loc",loc)
    checkForAlgebraicExpressions(newRule["lhs"],["%loc"],[locations[loc]],str(0))
    checkForAlgebraicExpressions(newRule["rhs"],["%loc"],[locations[loc]],str(0))
    solveAlgebraicExpressions(newRule["lhs"])
    solveAlgebraicExpressions(newRule["rhs"])
    newRule["lhs"] = checkForChains(newRule["lhs"])
    newRule["rhs"] = checkForChains(newRule["rhs"])
    insertLocInExpression(newRule["lhs"],loc)
    insertLocInExpression(newRule["rhs"],loc)
    newRule["rate"] = substituteDataArrayElementInString("%loc", newRule["rate"], locations[loc])
    newRule["rate"] = newRule["rate"].replace("%loc", loc)
    #if (isBimol(rule["lhs"])):
    #  newRule["rate"] = encloseAndSuffix(newRule["rate"], "/ " + str(locations[loc]))
    return newRule
  
  def mixRuleWithOrgDst(matrixCell):
    (org, dst, r) = matrixCell
    if (options.fullMatrix == False and r == 0):
      return nullInstruction
    if (org != dst):
      newRule = copy.deepcopy(rule)
      newRule["label"] = newRule["label"].replace("%org",org)
      newRule["label"] = newRule["label"].replace("%dst",dst)
      checkForAlgebraicExpressions(newRule["lhs"],[org,dst],[locations[org],locations[dst]], str(r))
      checkForAlgebraicExpressions(newRule["rhs"],[org,dst],[locations[org],locations[dst]], str(r))
      solveAlgebraicExpressions(newRule["lhs"])
      solveAlgebraicExpressions(newRule["rhs"])
      newRule["lhs"] = checkForChains(newRule["lhs"])
      newRule["rhs"] = checkForChains(newRule["rhs"])
      insertOrgInExpression(newRule["lhs"],org)
      insertOrgInExpression(newRule["rhs"],org)
      insertDstInExpression(newRule["lhs"],dst)
      insertDstInExpression(newRule["rhs"],dst)
      newRule["rate"] = substituteDataArrayElementInString("%org", newRule["rate"], locations[org])
      newRule["rate"] = substituteDataArrayElementInString("%dst", newRule["rate"], locations[dst])
      newRule["rate"] = newRule["rate"].replace("%cell", str(r))
      newRule["rate"] = newRule["rate"].replace("%org", org)
      newRule["rate"] = newRule["rate"].replace("%dst", dst)
      #if (isBimol(rule["lhs"])):
      #  newRule["rate"] = encloseAndSuffix(newRule["rate"], "/ " + str(locations[org]))
      makeDiffusions(newRule["rhs"], dst)
      return newRule
    else:
      return nullInstruction

  ruleBuilder = None
  if (domain[0].__class__ == "".__class__): #if it is string, it is just loc
    ruleBuilder = mixRuleWithLoc
  else: #the type is tuple, it is a matrix
    ruleBuilder = mixRuleWithOrgDst
  for r in map(ruleBuilder, domain):
    printing.printRule(r)