Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replication rules #107

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Testing/parsing/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,22 @@ def test_bidirectional():

rule_expr = "#! rules\nK(S{u}).B()::cyt => K(S{p})::cyt + B()::cyt @ 3*[K()::cyt]/2*v_1 | 2*[K()::cyt]/3*v_1"
assert not objects.rules_parser.parse(rule_expr).success


def test_replication():
rule_expr = "X()::rep =*> X()::rep + X()::rep"
assert objects.rule_parser.parse(rule_expr).success
# assert objects.rule_parser.parse(rule_expr).data[1] == objects.r1
xtrojak marked this conversation as resolved.
Show resolved Hide resolved

rule_expr = "X()::rep =*> X()::rep + X()::rep @ 3*[X()::rep]/2*v_1"
assert objects.rule_parser.parse(rule_expr).success

rule_expr = "X()::rep =*> X()::rep + X()::rep + X()::rep"
output = objects.rule_parser.parse(rule_expr)
assert output.success

rule_expr = "X()::rep + Y()::rep =*> X()::rep + X()::rep"
assert not objects.rule_parser.parse(rule_expr).success

rule_expr = "X()::rep =*> X()::rep + X()::rep + Y()::rep"
assert not objects.rule_parser.parse(rule_expr).success
28 changes: 16 additions & 12 deletions eBCSgen/Parsing/ParseBCSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from numpy import inf
from copy import deepcopy
from lark import Lark, Token, Transformer, Tree
from lark import Lark, Transformer, Tree
from lark import UnexpectedCharacters, UnexpectedToken, UnexpectedEOF
from lark.load_grammar import _TERMINAL_NAMES
import regex
Expand Down Expand Up @@ -116,7 +116,7 @@ def to_side(self):

init: const? rate_complex
definition: def_param "=" number
rule: ((label)? side ARROW side ("@" rate)? (";" variable)?) | ((label)? side BI_ARROW side ("@" rate "|" rate )? (";" variable)?)
rule: ((label)? side ARROW side ("@" rate)? (";" variable)?) | ((label)? side BI_ARROW side ("@" rate "|" rate )? (";" variable)?) | ((label)? side REPLICATION_ARROW side ("@" rate)? (";" variable)?)
xtrojak marked this conversation as resolved.
Show resolved Hide resolved
cmplx_dfn: cmplx_name "=" value

side: (const? complex "+")* (const? complex)?
Expand All @@ -133,6 +133,7 @@ def to_side(self):
POW: "**"
ARROW: "=>"
BI_ARROW: "<=>"
REPLICATION_ARROW: "=*>"
RULES_START: "#! rules"
INITS_START: "#! inits"
DEFNS_START: "#! definitions"
Expand Down Expand Up @@ -647,18 +648,21 @@ def rule(self, matches):
)
)
pairs = [(i, i + lhs.counter) for i in range(min(lhs.counter, rhs.counter))]
if lhs.counter > rhs.counter:
if arrow == "=*>":
if lhs.counter >= rhs.counter or lhs.counter != 1 or rhs.counter <= 1:
raise UnspecifiedParsingError("Rule does not contain replication")

for i in range(lhs.counter, rhs.counter):
if lhs.seq[pairs[-1][0]] == rhs.seq[pairs[-1][1] - lhs.counter]:
if rhs.seq[pairs[-1][1] - lhs.counter] == rhs.seq[i]:
pairs += [(pairs[-1][0], i + lhs.counter)]
else:
raise UnspecifiedParsingError("Rule does not contain replication")

elif lhs.counter > rhs.counter:
pairs += [(i, None) for i in range(rhs.counter, lhs.counter)]
elif lhs.counter < rhs.counter:
for i in range(lhs.counter, rhs.counter):
replication = False
if lhs.counter == 1 and rhs.counter > 1:
if lhs.seq[pairs[-1][0]] == rhs.seq[pairs[-1][1] - lhs.counter]:
if rhs.seq[pairs[-1][1] - lhs.counter] == rhs.seq[i]:
pairs += [(pairs[-1][0], i + lhs.counter)]
replication = True
if not replication:
pairs += [(None, i + lhs.counter)]
pairs += [(None, i + lhs.counter) for i in range(lhs.counter, rhs.counter)]

reversible = False
if arrow == "<=>":
Expand Down
Loading