pykappa.grammar

Functions

parse_tree_to_expression(tree)

Convert a Lark ParseTree to an Expression object.

Classes

AgentBuilder(tree)

Builds Agent objects from Lark parse trees.

KappaParser()

Parser for Kappa language files and expressions.

LarkTreetoExpression([visit_tokens])

Transforms a Lark ParseTree into an Expression object.

PatternBuilder(tree)

Builds Pattern objects from Lark parse trees.

RuleBuilder(tree)

Builds Rule objects from Lark parse trees.

SiteBuilder(tree)

Builds Site objects from Lark parse trees.

class pykappa.grammar.AgentBuilder(tree)[source]

Builds Agent objects from Lark parse trees.

Parameters:

tree (Tree[Token])

parsed_type

Type name of the agent.

Type:

str

parsed_interface

List of sites belonging to the agent.

Type:

list[pykappa.pattern.Site]

agent_name(tree)[source]
Parameters:

tree (Tree[Token])

Return type:

None

property object: Agent
parsed_interface: list[Site]
parsed_type: str
site(tree)[source]
Parameters:

tree (Tree[Token])

Return type:

None

class pykappa.grammar.KappaParser[source]

Parser for Kappa language files and expressions.

Note

Don’t instantiate directly: use the global kappa_parser instance.

parse(text)[source]
Parameters:

text (str)

Return type:

Tree[Token]

parse_file(filepath)[source]
Parameters:

filepath (str)

Return type:

Tree[Token]

class pykappa.grammar.LarkTreetoExpression(visit_tokens=True)[source]

Transforms a Lark ParseTree into an Expression object.

Note

Uses a Transformer to preserve the tree structure of the original ParseTree. This doesn’t need to use Transformer_NonRecursive anymore due to grammar changes, but methods explicitly call transform on children.

Parameters:

visit_tokens (bool)

FALSE(token)[source]
SIGNED_FLOAT(token)[source]
SIGNED_INT(token)[source]
TRUE(token)[source]
algebraic_expression(children)[source]
binary_op(children)[source]
binary_op_expression(children)[source]
comparison(children)[source]
conditional_expression(children)[source]
declared_variable_name(children)[source]
defined_constant(children)[source]
list_op_expression(children)[source]
logical_and(children)[source]
logical_not(children)[source]
logical_or(children)[source]
parentheses(children)[source]
pattern(children)[source]
reserved_variable_name(children)[source]
unary_op(children)[source]
unary_op_expression(children)[source]
class pykappa.grammar.PatternBuilder(tree)[source]

Builds Pattern objects from Lark parse trees.

Parameters:

tree (Tree[Token])

parsed_agents

List of agents in the pattern.

Type:

list[pykappa.pattern.Agent]

agent(tree)[source]
Parameters:

tree (Tree[Token])

Return type:

None

property object: Pattern
parsed_agents: list[Agent]
class pykappa.grammar.RuleBuilder(tree)[source]

Builds Rule objects from Lark parse trees.

Parameters:

tree (Tree[Token])

parsed_label

Optional label for the rule.

Type:

str | None

left_agents

Agents on the left side of the rule.

Type:

list[pykappa.pattern.Agent | None]

right_agents

Agents on the right side of the rule.

Type:

list[pykappa.pattern.Agent | None]

parsed_rates

Rate expressions for the rule.

Type:

list[pykappa.algebra.Expression]

tree_data

Type of rule being built.

Type:

str

left_agents: list[Agent | None]
property objects: list[Rule]
parsed_label: str | None
parsed_rates: list[Expression]
rate(tree)[source]
Parameters:

tree (Tree[Token])

Return type:

None

rev_rule_expression(tree)[source]
Parameters:

tree (Tree[Token])

Return type:

None

right_agents: list[Agent | None]
rule_expression(tree)[source]
Parameters:

tree (Tree[Token])

Return type:

None

tree_data: str
class pykappa.grammar.SiteBuilder(tree)[source]

Builds Site objects from Lark parse trees.

Parameters:

tree (Tree[Token])

parsed_site_name

Name of the site being built.

Type:

str

parsed_state

Internal state of the site.

Type:

str

parsed_partner

Partner specification for the site.

Type:

str | pykappa.pattern.SiteType | int | pykappa.pattern.Site

property object: Site
parsed_agents: list[Agent]
parsed_partner: str | SiteType | int | Site
parsed_site_name: str
parsed_state: str
partner(tree)[source]
Parameters:

tree (Tree[Token])

Return type:

None

site_name(tree)[source]
Parameters:

tree (Tree[Token])

Return type:

None

state(tree)[source]
Parameters:

tree (Tree[Token])

Return type:

None

pykappa.grammar.parse_tree_to_expression(tree)[source]

Convert a Lark ParseTree to an Expression object.

Note

Since there isn’t extra logic when converting algebraic expressions, we can convert from the Lark representation in-place, without creating a new object, hence a Transformer instead of Visitor.

Parameters:

tree (Tree) – Lark ParseTree rooted at algebraic_expression.

Returns:

Expression object representing the parsed expression.

Return type:

Expression