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

Implement scs parser #19

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,10 @@ dmypy.json
.pyre/

.DS_Store
config.ini

*.ini

.antlr
antlr*.jar

*.code-workspace
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Prerequest

- Install dependencies:

```sh
python3 install -r packages.txt
```

# Neo4j client library

Whole infrastructure to develop client library is placed in the `db` folder. This folder contains [README](db/README.md) file with more information.

# Web

14 changes: 14 additions & 0 deletions db/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Development

- Run neo4j is a requirement to run code in development process. [Run official docker](https://neo4j.com/developer/docker/) is easiest way to do that.

- Create configuration file `config.ini`. See [config.ini](db/config.ini)

## Codestlye

- Please style your code aacording to [PEP8](https://www.python.org/dev/peps/pep-0008/) rules

## Regenerate ANTLR parser

- [Download](https://www.antlr.org/download.html) antlr generator.
- Run generation script `./scripts/generate_antlr.sh antlr-4.9.2-complete.jar`
1 change: 1 addition & 0 deletions db/sc/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from sc.memory import Memory
from sc.scs import SCsParser
38 changes: 37 additions & 1 deletion db/sc/core/keynodes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
class Keynodes:
class KeynodeNames:
SC_NODE = "sc_node"
SC_LINK = "sc_link"
SC_EDGE = "sc_edge"
SC_ARC = "sc_arc"

CORE_TYPES = [SC_NODE, SC_LINK, SC_EDGE, SC_ARC]

SC_CONST = "sc_const"
SC_VAR = "sc_var"

CONST_TYPES = [SC_CONST, SC_VAR]

SC_NODE_STRUCT = "sc_node_struct"
SC_NODE_TUPLE = "sc_node_tuple"
SC_NODE_ROLE = "sc_node_role"
SC_NODE_NO_ROLE = "sc_node_no_role"
SC_NODE_CLASS = "sc_node_class"
SC_NODE_ABSTRACT = "sc_node_abstract"
SC_NODE_MATERIAL = "sc_node_material"

NODE_TYPES = [
SC_NODE_STRUCT, SC_NODE_TUPLE, SC_NODE_ROLE,
SC_NODE_NO_ROLE, SC_NODE_CLASS, SC_NODE_ABSTRACT,
SC_NODE_MATERIAL]

SC_ARC_PERM = "sc_arc_perm"
SC_ARC_TEMP = "sc_arc_temp"

ARC_PERM_TYPES = [SC_ARC_PERM, SC_ARC_TEMP]

SC_ARC_POS = "sc_arc_pos"
SC_ARC_NEG = "sc_arc_neg"
SC_ARC_FUZ = "sc_arc_fuz"

ARC_POS_TYPES = [SC_ARC_POS, SC_ARC_NEG, SC_ARC_FUZ]

# common keynodes
NREL_SYS_IDTF = "nrel_system_identifier"
10 changes: 5 additions & 5 deletions db/sc/core/transaction/names.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sc.core.element import Element
from sc.core.keynodes import Keynodes
from sc.core.keynodes import KeynodeNames
from sc.core.keywords import Labels, TypeAttrs
from sc.core.transaction.utils import _parse_output_element, _get_label_from_type

Expand Down Expand Up @@ -32,7 +32,7 @@ class TransactionNamesWrite:

def __init__(self,
driver: neo4j.Driver,
nrel_sys_idtf: str = Keynodes.NREL_SYS_IDTF) -> None:
nrel_sys_idtf: str = KeynodeNames.NREL_SYS_IDTF) -> None:
self._driver = driver
self._sys_idtfs = set()
self._tasks = []
Expand All @@ -58,7 +58,7 @@ def _is_empty(self) -> bool:
return len(self._sys_idtfs) == 0

def _make_query(self) -> str:
query = (f"MATCH (l:{Labels.SC_LINK} {{content: '{Keynodes.NREL_SYS_IDTF}', {_const_attr()} }})"
query = (f"MATCH (l:{Labels.SC_LINK} {{content: '{KeynodeNames.NREL_SYS_IDTF}', {_const_attr()} }})"
f"<-[__idtf_edge:{Labels.SC_ARC} {{ {TypeAttrs.CONST}: 'CONST' }}]"
f"-(__sys_idtf:{Labels.SC_NODE}), \n"
f"(:{Labels.SC_EDGE_SOCK} {{edge_id: id(__idtf_edge)}})"
Expand Down Expand Up @@ -136,7 +136,7 @@ class TransactionNamesRead:

def __init__(self,
driver: neo4j.Driver,
nrel_sys_idtf: str = Keynodes.NREL_SYS_IDTF) -> None:
nrel_sys_idtf: str = KeynodeNames.NREL_SYS_IDTF) -> None:
self._driver = driver
self._sys_idtfs = set()

Expand All @@ -161,7 +161,7 @@ def _is_empty(self) -> bool:

def _make_query(self) -> str:

query = (f"MATCH (l:{Labels.SC_LINK} {{ content: '{Keynodes.NREL_SYS_IDTF}', {_const_attr()} }})"
query = (f"MATCH (l:{Labels.SC_LINK} {{ content: '{KeynodeNames.NREL_SYS_IDTF}', {_const_attr()} }})"
f"<-[edge:{Labels.SC_ARC} {{ {_const_attr()} }}]"
f"-(__sys_idtf:{Labels.SC_NODE}), \n"
f"(edge_sock:{Labels.SC_EDGE_SOCK} {{edge_id: id(edge)}})"
Expand Down
5 changes: 3 additions & 2 deletions db/sc/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
from sc.core.transaction import TransactionWrite, TransactionRead, TransactionNamesWrite, TransactionNamesRead

import neo4j
import logging


class Memory:

def __init__(self, config_path: str):
self._config = Config(config_path)

print(f"Connecting to {self._config.db_uri()}")
logging.info(f"Connecting to {self._config.db_uri()}")
self._client = Client(
self._config.db_uri(),
self._config.db_user(),
self._config.db_password())

def close(self):
print(f"Close connection to {self._config.db_uri()}")
logging.info(f"Close connection to {self._config.db_uri()}")
self._client.close()

@property
Expand Down
1 change: 1 addition & 0 deletions db/sc/scs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .parser import SCsParser
2 changes: 2 additions & 0 deletions db/sc/scs/antlr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .scsLexer import scsLexer as SCsLexerAntlr
from .scsParser import scsParser as SCsParserAntlr
155 changes: 155 additions & 0 deletions db/sc/scs/antlr/scs.interp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
token literal names:
null
'_'
'<>'
'<=>'
'_<>'
'_<=>'
'>'
'<'
'=>'
'<='
'_=>'
'_<='
'..>'
'<..'
'->'
'<-'
'-|>'
'<|-'
'-/>'
'</-'
'~>'
'<~'
'~|>'
'<|~'
'~/>'
'</~'
'_..>'
'_<..'
'_->'
'_<-'
'_-|>'
'_<|-'
'_-/>'
'_</-'
'_~>'
'_<~'
'_~|>'
'_<|~'
'_~/>'
'_</~'
'...'
'='
'('
')'
';'
'{'
'}'
'(*'
'*)'
null
null
'[*'
'*]'
null
null
null
null
null
null
null
';;'

token symbolic names:
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
ID_SYSTEM
ALIAS_SYMBOLS
CONTOUR_BEGIN
CONTOUR_END
CONTENT_BODY
LINK
EDGE_ATTR
LINE_TERMINATOR
LINE_COMMENT
MULTINE_COMMENT
WS
SENTENCE_SEP

rule names:
content
contour
connector_edge
connector_arc
connector
syntax
sentence_wrap
sentence
ifdf_alias
idtf_system
sentence_assign
idtf_edge
idtf_set_item
idtf_set_item_list
idtf_set
idtf_atomic
idtf_url
idtf_common
idtf_list
internal_sentence
internal_sentence_list
sentence_lvl_4_list_item
sentence_lvl_common
attr_list


atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 62, 226, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4, 18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23, 9, 23, 4, 24, 9, 24, 4, 25, 9, 25, 3, 2, 5, 2, 52, 10, 2, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 61, 10, 3, 12, 3, 14, 3, 64, 11, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 5, 6, 81, 10, 6, 3, 7, 7, 7, 84, 10, 7, 12, 7, 14, 7, 87, 11, 7, 3, 7, 3, 7, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 5, 9, 96, 10, 9, 3, 10, 3, 10, 3, 10, 3, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 13, 3, 13, 3, 13, 3, 13, 5, 13, 113, 10, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 14, 5, 14, 120, 10, 14, 3, 14, 3, 14, 3, 14, 3, 15, 3, 15, 3, 15, 7, 15, 128, 10, 15, 12, 15, 14, 15, 131, 11, 15, 3, 16, 3, 16, 3, 16, 3, 16, 3, 16, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 5, 17, 144, 10, 17, 3, 18, 3, 18, 3, 18, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 5, 19, 167, 10, 19, 3, 20, 3, 20, 3, 20, 5, 20, 172, 10, 20, 3, 20, 3, 20, 3, 20, 3, 20, 5, 20, 178, 10, 20, 7, 20, 180, 10, 20, 12, 20, 14, 20, 183, 11, 20, 3, 21, 3, 21, 5, 21, 187, 10, 21, 3, 21, 3, 21, 3, 21, 3, 22, 3, 22, 3, 22, 3, 22, 6, 22, 196, 10, 22, 13, 22, 14, 22, 197, 3, 22, 3, 22, 3, 23, 3, 23, 5, 23, 204, 10, 23, 3, 23, 3, 23, 3, 23, 3, 23, 3, 24, 3, 24, 3, 24, 3, 24, 7, 24, 214, 10, 24, 12, 24, 14, 24, 217, 11, 24, 3, 25, 3, 25, 3, 25, 6, 25, 222, 10, 25, 13, 25, 14, 25, 223, 3, 25, 2, 2, 26, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 2, 5, 3, 2, 4, 13, 3, 2, 14, 41, 4, 2, 42, 42, 51, 51, 2, 223, 2, 51, 3, 2, 2, 2, 4, 56, 3, 2, 2, 2, 6, 68, 3, 2, 2, 2, 8, 71, 3, 2, 2, 2, 10, 80, 3, 2, 2, 2, 12, 85, 3, 2, 2, 2, 14, 90, 3, 2, 2, 2, 16, 95, 3, 2, 2, 2, 18, 97, 3, 2, 2, 2, 20, 100, 3, 2, 2, 2, 22, 103, 3, 2, 2, 2, 24, 108, 3, 2, 2, 2, 26, 119, 3, 2, 2, 2, 28, 124, 3, 2, 2, 2, 30, 132, 3, 2, 2, 2, 32, 143, 3, 2, 2, 2, 34, 145, 3, 2, 2, 2, 36, 166, 3, 2, 2, 2, 38, 168, 3, 2, 2, 2, 40, 184, 3, 2, 2, 2, 42, 191, 3, 2, 2, 2, 44, 201, 3, 2, 2, 2, 46, 209, 3, 2, 2, 2, 48, 221, 3, 2, 2, 2, 50, 52, 7, 3, 2, 2, 51, 50, 3, 2, 2, 2, 51, 52, 3, 2, 2, 2, 52, 53, 3, 2, 2, 2, 53, 54, 7, 55, 2, 2, 54, 55, 8, 2, 1, 2, 55, 3, 3, 2, 2, 2, 56, 57, 7, 53, 2, 2, 57, 58, 6, 3, 2, 2, 58, 62, 8, 3, 1, 2, 59, 61, 5, 14, 8, 2, 60, 59, 3, 2, 2, 2, 61, 64, 3, 2, 2, 2, 62, 60, 3, 2, 2, 2, 62, 63, 3, 2, 2, 2, 63, 65, 3, 2, 2, 2, 64, 62, 3, 2, 2, 2, 65, 66, 7, 54, 2, 2, 66, 67, 8, 3, 1, 2, 67, 5, 3, 2, 2, 2, 68, 69, 9, 2, 2, 2, 69, 70, 8, 4, 1, 2, 70, 7, 3, 2, 2, 2, 71, 72, 9, 3, 2, 2, 72, 73, 8, 5, 1, 2, 73, 9, 3, 2, 2, 2, 74, 75, 5, 6, 4, 2, 75, 76, 8, 6, 1, 2, 76, 81, 3, 2, 2, 2, 77, 78, 5, 8, 5, 2, 78, 79, 8, 6, 1, 2, 79, 81, 3, 2, 2, 2, 80, 74, 3, 2, 2, 2, 80, 77, 3, 2, 2, 2, 81, 11, 3, 2, 2, 2, 82, 84, 5, 14, 8, 2, 83, 82, 3, 2, 2, 2, 84, 87, 3, 2, 2, 2, 85, 83, 3, 2, 2, 2, 85, 86, 3, 2, 2, 2, 86, 88, 3, 2, 2, 2, 87, 85, 3, 2, 2, 2, 88, 89, 7, 2, 2, 3, 89, 13, 3, 2, 2, 2, 90, 91, 5, 16, 9, 2, 91, 92, 7, 62, 2, 2, 92, 15, 3, 2, 2, 2, 93, 96, 5, 22, 12, 2, 94, 96, 5, 46, 24, 2, 95, 93, 3, 2, 2, 2, 95, 94, 3, 2, 2, 2, 96, 17, 3, 2, 2, 2, 97, 98, 7, 52, 2, 2, 98, 99, 8, 10, 1, 2, 99, 19, 3, 2, 2, 2, 100, 101, 9, 4, 2, 2, 101, 102, 8, 11, 1, 2, 102, 21, 3, 2, 2, 2, 103, 104, 7, 52, 2, 2, 104, 105, 7, 43, 2, 2, 105, 106, 5, 36, 19, 2, 106, 107, 8, 12, 1, 2, 107, 23, 3, 2, 2, 2, 108, 109, 7, 44, 2, 2, 109, 110, 5, 32, 17, 2, 110, 112, 5, 10, 6, 2, 111, 113, 5, 48, 25, 2, 112, 111, 3, 2, 2, 2, 112, 113, 3, 2, 2, 2, 113, 114, 3, 2, 2, 2, 114, 115, 5, 32, 17, 2, 115, 116, 7, 45, 2, 2, 116, 117, 8, 13, 1, 2, 117, 25, 3, 2, 2, 2, 118, 120, 5, 48, 25, 2, 119, 118, 3, 2, 2, 2, 119, 120, 3, 2, 2, 2, 120, 121, 3, 2, 2, 2, 121, 122, 5, 36, 19, 2, 122, 123, 8, 14, 1, 2, 123, 27, 3, 2, 2, 2, 124, 129, 5, 26, 14, 2, 125, 126, 7, 46, 2, 2, 126, 128, 5, 26, 14, 2, 127, 125, 3, 2, 2, 2, 128, 131, 3, 2, 2, 2, 129, 127, 3, 2, 2, 2, 129, 130, 3, 2, 2, 2, 130, 29, 3, 2, 2, 2, 131, 129, 3, 2, 2, 2, 132, 133, 7, 47, 2, 2, 133, 134, 8, 16, 1, 2, 134, 135, 5, 28, 15, 2, 135, 136, 7, 48, 2, 2, 136, 31, 3, 2, 2, 2, 137, 138, 5, 18, 10, 2, 138, 139, 8, 17, 1, 2, 139, 144, 3, 2, 2, 2, 140, 141, 5, 20, 11, 2, 141, 142, 8, 17, 1, 2, 142, 144, 3, 2, 2, 2, 143, 137, 3, 2, 2, 2, 143, 140, 3, 2, 2, 2, 144, 33, 3, 2, 2, 2, 145, 146, 7, 56, 2, 2, 146, 147, 8, 18, 1, 2, 147, 35, 3, 2, 2, 2, 148, 149, 5, 32, 17, 2, 149, 150, 8, 19, 1, 2, 150, 167, 3, 2, 2, 2, 151, 152, 5, 24, 13, 2, 152, 153, 8, 19, 1, 2, 153, 167, 3, 2, 2, 2, 154, 155, 5, 30, 16, 2, 155, 156, 8, 19, 1, 2, 156, 167, 3, 2, 2, 2, 157, 158, 5, 4, 3, 2, 158, 159, 8, 19, 1, 2, 159, 167, 3, 2, 2, 2, 160, 161, 5, 2, 2, 2, 161, 162, 8, 19, 1, 2, 162, 167, 3, 2, 2, 2, 163, 164, 5, 34, 18, 2, 164, 165, 8, 19, 1, 2, 165, 167, 3, 2, 2, 2, 166, 148, 3, 2, 2, 2, 166, 151, 3, 2, 2, 2, 166, 154, 3, 2, 2, 2, 166, 157, 3, 2, 2, 2, 166, 160, 3, 2, 2, 2, 166, 163, 3, 2, 2, 2, 167, 37, 3, 2, 2, 2, 168, 169, 5, 36, 19, 2, 169, 171, 8, 20, 1, 2, 170, 172, 5, 42, 22, 2, 171, 170, 3, 2, 2, 2, 171, 172, 3, 2, 2, 2, 172, 181, 3, 2, 2, 2, 173, 174, 7, 46, 2, 2, 174, 175, 5, 36, 19, 2, 175, 177, 8, 20, 1, 2, 176, 178, 5, 42, 22, 2, 177, 176, 3, 2, 2, 2, 177, 178, 3, 2, 2, 2, 178, 180, 3, 2, 2, 2, 179, 173, 3, 2, 2, 2, 180, 183, 3, 2, 2, 2, 181, 179, 3, 2, 2, 2, 181, 182, 3, 2, 2, 2, 182, 39, 3, 2, 2, 2, 183, 181, 3, 2, 2, 2, 184, 186, 5, 10, 6, 2, 185, 187, 5, 48, 25, 2, 186, 185, 3, 2, 2, 2, 186, 187, 3, 2, 2, 2, 187, 188, 3, 2, 2, 2, 188, 189, 5, 38, 20, 2, 189, 190, 8, 21, 1, 2, 190, 41, 3, 2, 2, 2, 191, 195, 7, 49, 2, 2, 192, 193, 5, 40, 21, 2, 193, 194, 7, 62, 2, 2, 194, 196, 3, 2, 2, 2, 195, 192, 3, 2, 2, 2, 196, 197, 3, 2, 2, 2, 197, 195, 3, 2, 2, 2, 197, 198, 3, 2, 2, 2, 198, 199, 3, 2, 2, 2, 199, 200, 7, 50, 2, 2, 200, 43, 3, 2, 2, 2, 201, 203, 5, 10, 6, 2, 202, 204, 5, 48, 25, 2, 203, 202, 3, 2, 2, 2, 203, 204, 3, 2, 2, 2, 204, 205, 3, 2, 2, 2, 205, 206, 5, 38, 20, 2, 206, 207, 3, 2, 2, 2, 207, 208, 8, 23, 1, 2, 208, 45, 3, 2, 2, 2, 209, 210, 5, 36, 19, 2, 210, 215, 5, 44, 23, 2, 211, 212, 7, 46, 2, 2, 212, 214, 5, 44, 23, 2, 213, 211, 3, 2, 2, 2, 214, 217, 3, 2, 2, 2, 215, 213, 3, 2, 2, 2, 215, 216, 3, 2, 2, 2, 216, 47, 3, 2, 2, 2, 217, 215, 3, 2, 2, 2, 218, 219, 7, 51, 2, 2, 219, 220, 7, 57, 2, 2, 220, 222, 8, 25, 1, 2, 221, 218, 3, 2, 2, 2, 222, 223, 3, 2, 2, 2, 223, 221, 3, 2, 2, 2, 223, 224, 3, 2, 2, 2, 224, 49, 3, 2, 2, 2, 20, 51, 62, 80, 85, 95, 112, 119, 129, 143, 166, 171, 177, 181, 186, 197, 203, 215, 223]
Loading