Skip to content

Commit

Permalink
Refactor PFDL code (#60)
Browse files Browse the repository at this point in the history
- Make code more modular to enable the reuse of the code (e.g.. make
more functions and methods single purpose)
- Fix various bugs in the petri net generation
- Renaming
- Format code and add docstrings
- Allow a PFDL program to be passed over to the scheduler as a string
(currently, only a file path was allowed)
- Add various quality of live features/fixes
- Allow the free configuration of the "start task" (former
productionTask)
- Allow integers as loop limits and negative numbers

---------

Co-authored-by: Oliver Stolz <[email protected]>
  • Loading branch information
maxhoerstr and OliverStolzBO authored Aug 23, 2024
1 parent a8d9393 commit 74ac2f6
Show file tree
Hide file tree
Showing 25 changed files with 1,532 additions and 1,409 deletions.
40 changes: 17 additions & 23 deletions pfdl_grammar/PFDLLexer.g4
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
lexer grammar PFDLLexer;

// DenterHelper for generating INDENT AND DEDENT tokens to realize
// a python-like grammar with Indentations
// DenterHelper for generating INDENT AND DEDENT tokens to realize a python-like grammar with
// Indentations

tokens { INDENT, DEDENT }
tokens {
INDENT,
DEDENT
}

@lexer::header{
@lexer::header {
from antlr_denter.DenterHelper import DenterHelper
from PFDLParser import PFDLParser
}
Expand Down Expand Up @@ -56,7 +59,7 @@ QUOTE: '"';
ARRAY_LEFT: '[';
ARRAY_RIGHT: ']';

COMMENT : '#' ~[\n]+ -> skip;
COMMENT: '#' ~[\n]* -> skip;
WHITESPACE: [ \t]+ -> skip;
NL: ('\r'? '\n' ' '*);

Expand All @@ -76,48 +79,39 @@ SLASH: '/';
MINUS: '-';
PLUS: '+';


INTEGER: [0-9]+;
FLOAT: INTEGER '.'INTEGER;
FLOAT: INTEGER '.' INTEGER;

STRING: '"' ('\\"'|.)*? '"';
STRING: '"' ('\\"' | .)*? '"';
STARTS_WITH_LOWER_C_STR: [a-z][a-zA-Z0-9_]*;
STARTS_WITH_UPPER_C_STR: [A-Z][a-zA-Z0-9_]*;

// JSON Grammar
mode JSON;

JSON_STRING: '"' ('\\"'|.)*? '"';
JSON_STRING: '"' ('\\"' | .)*? '"';
JSON_TRUE: 'true';
JSON_FALSE: 'false';

JSON_COLON: ':';
JSON_QUOTE: '"';

JSON_COMMENT : '#' ~[\n]+ -> skip;
JSON_COMMENT: '#' ~[\n]+ -> skip;

JSON_ARRAY_LEFT: '[';
JSON_ARRAY_RIGHT: ']';

JSON_COMMA: ',';

NUMBER
: '-'? INT ('.' [0-9] +)? EXP?
;
NUMBER: '-'? INT ('.' [0-9]+)? EXP?;

fragment INT
: '0' | [1-9] [0-9]*
;
fragment INT: '0' | [1-9] [0-9]*;

// no leading zeros

fragment EXP
: [Ee] [+\-]? INT
;
fragment EXP: [Ee] [+\-]? INT;

WS
: [ \t\n\r] + -> skip
;
WS: [ \t\n\r]+ -> skip;

JSON_OPEN_2: '{' -> pushMode(JSON);
JSON_CLOSE: '}' -> popMode;
JSON_CLOSE: '}' -> popMode;
33 changes: 16 additions & 17 deletions pfdl_grammar/PFDLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ while_loop:
LOOP WHILE expression INDENT statement+ DEDENT;

counting_loop:
PARALLEL? LOOP STARTS_WITH_LOWER_C_STR TO attribute_access INDENT statement+ DEDENT;
PARALLEL? LOOP STARTS_WITH_LOWER_C_STR TO (attribute_access | INTEGER) INDENT statement+ DEDENT;

condition:
CONDITION INDENT expression NL+ DEDENT condition_passed condition_failed?;
Expand All @@ -60,15 +60,16 @@ condition_failed:
FAILED INDENT statement+ DEDENT;

parameter:
STARTS_WITH_LOWER_C_STR
| attribute_access;
STARTS_WITH_LOWER_C_STR | attribute_access;

struct_initialization:
STARTS_WITH_UPPER_C_STR INDENT json_object NL+ DEDENT
| STARTS_WITH_UPPER_C_STR NL* json_object NL*;

variable_definition:
STARTS_WITH_LOWER_C_STR COLON primitive array?;
STARTS_WITH_LOWER_C_STR COLON variable_type;

variable_type: primitive array?;

primitive:
NUMBER_P
Expand All @@ -82,9 +83,7 @@ attribute_access:
array:
ARRAY_LEFT (INTEGER | STARTS_WITH_LOWER_C_STR)? ARRAY_RIGHT;

number:
INTEGER
| FLOAT;
number: MINUS? (INTEGER | FLOAT);

value:
TRUE
Expand Down Expand Up @@ -128,13 +127,13 @@ json_open_bracket:
JSON_OPEN | JSON_OPEN_2;

json_value:
JSON_STRING
| JSON_TRUE
| JSON_FALSE
| NUMBER
| json_object
| json_array;

json_array
: JSON_ARRAY_LEFT json_value (JSON_COMMA json_value)* JSON_ARRAY_RIGHT
| JSON_ARRAY_LEFT JSON_ARRAY_RIGHT;
JSON_STRING
| JSON_TRUE
| JSON_FALSE
| NUMBER
| json_object
| json_array;

json_array:
JSON_ARRAY_LEFT json_value (JSON_COMMA json_value)* JSON_ARRAY_RIGHT
| JSON_ARRAY_LEFT JSON_ARRAY_RIGHT;
10 changes: 9 additions & 1 deletion pfdl_scheduler/model/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ class Process:
Attributes:
structs: A dict for mapping the Struct names to the Struct objects.
task: A dict for mapping the Task names to the Task objects.
start_task_name: the name of the start task of the PFDL program (typically "productionTask").
"""

def __init__(self, structs: Dict[str, Struct] = None, tasks: Dict[str, Task] = None) -> None:
def __init__(
self,
structs: Dict[str, Struct] = None,
tasks: Dict[str, Task] = None,
start_task_name: str = "productionTask",
) -> None:
"""Initialize the object.
Args:
structs: A dict for mapping the Struct names to the Struct objects.
tasks: A dict for mapping the Task names to the Task objects.
start_task_name: the name of the start task of the PFDL program (typically "productionTask").
"""
if structs:
self.structs: Dict[str, Struct] = structs
Expand All @@ -42,3 +49,4 @@ def __init__(self, structs: Dict[str, Struct] = None, tasks: Dict[str, Task] = N
self.tasks: Dict[str, Task] = tasks
else:
self.tasks: Dict[str, Task] = {}
self.start_task_name = start_task_name
Loading

0 comments on commit 74ac2f6

Please sign in to comment.