Skip to content

Commit

Permalink
feat: harmonise sc-element types
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Oct 16, 2024
1 parent fc647dc commit ea1220e
Show file tree
Hide file tree
Showing 10 changed files with 1,253 additions and 442 deletions.
94 changes: 47 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

The python implementation of the client for communication with
[the OSTIS Technology web-oriented platform](https://github.com/ostis-ai/ostis-web-platform/blob/develop/docs/main.pdf).
This library is compatible with `0.9.0` version of [sc-machine](https://github.com/ostis-ai/sc-machine).
This library is compatible with `0.10.0` version of [sc-machine](https://github.com/ostis-ai/sc-machine).

Low-level functionality implemented in **[py-sc-client](https://github.com/ostis-ai/py-sc-client)**:

Expand Down Expand Up @@ -157,25 +157,25 @@ It uses when new elements are created or if it's need to check right type.
It contains methods to check if it is node, connector or link, const or var, and so on.

If you paid attention, the class is in constants submodule.
They are already defined, and you can import them from file `sc_client.constants.sc_types`.
If you need bitmasks, they are in `sc_client.constants.sc_types.bitmasks`.
They are already defined, and you can import them from file `sc_client.constants.sc_type`.
If you need bitmasks, they are in `sc_client.constants.sc_type.bitmasks`.

```python
from sc_client.constants import sc_types
from sc_client.constants import sc_type

sc_type_struct = sc_types.NODE_CONST_STRUCT
sc_type_structure = sc_type.CONST_NODE_STRUCTURE

assert sc_type_struct.is_valid()
assert sc_type_structure.is_valid()

assert sc_type_struct.is_node()
assert not sc_type_struct.is_connector()
assert not sc_type_struct.is_link()
assert sc_type_structure.is_node()
assert not sc_type_structure.is_connector()
assert not sc_type_structure.is_link()

assert sc_type_struct.is_const()
assert not sc_type_struct.is_var()
assert sc_type_structure.is_const()
assert not sc_type_structure.is_var()

assert sc_type_struct.is_struct()
assert not sc_type_struct.is_tuple()
assert sc_type_structure.is_structure()
assert not sc_type_structure.is_tuple()
# And many more
```

Expand Down Expand Up @@ -206,18 +206,18 @@ It returns list of all elements by ScConstruction *constr*.

```python
from sc_client.client import generate_elements
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScConstruction
from sc_client.models import ScLinkContent, ScLinkContentType

construction = ScConstruction() # First you need initialize

construction.generate_node(sc_types.NODE_CONST, 'node') # Create node const
construction.generate_node(sc_type.CONST_NODE, 'node') # Create node const

link_content = ScLinkContent("Hello!", ScLinkContentType.STRING) # Create link content
construction.generate_link(sc_types.LINK_CONST, link_content, 'link') # Create link with that content
construction.generate_link(sc_type.CONST_NODE_LINK, link_content, 'link') # Create link with that content

construction.generate_connector(sc_types.EDGE_ACCESS_CONST_POS_PERM, 'node', 'link')
construction.generate_connector(sc_type.CONST_PERM_POS_ARC, 'node', 'link')
# Create unaliased connector between previous node

addrs = generate_elements(construction) # List of elements
Expand Down Expand Up @@ -259,7 +259,7 @@ Returns list of ScTemplateResult by *template*.

```python
from sc_client.client import search_by_template
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScTemplate, ScAddr

action_node: ScAddr
Expand All @@ -268,13 +268,13 @@ rrel_1: ScAddr
# Some ScAddrs for example

template = ScTemplate()
template.triple(action_node, sc_types.EDGE_ACCESS_VAR_POS_PERM, action_node >> "_action_node")
template.triple(action_node, sc_type.VAR_PERM_POS_ARC, action_node >> "_action_node")
# Triple `action_node-(*new)membership-arc-(*aliased with "_action_node")action_node`
template.quintuple(
"_action_node",
sc_types.EDGE_ACCESS_VAR_POS_TEMP,
sc_types.NODE_VAR >> "_arg_node",
sc_types.EDGE_ACCESS_VAR_POS_TEMP,
sc_type.VAR_TEMP_POS_ARC,
sc_type.VAR_NODE >> "_arg_node",
sc_type.VAR_TEMP_POS_ARC,
rrel_1,
)

Expand Down Expand Up @@ -324,7 +324,7 @@ Returns ScTemplateResult by *template*.

```python
from sc_client.client import generate_by_template
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScTemplate, ScAddr

main_node: ScAddr
Expand All @@ -335,15 +335,15 @@ link_node: ScAddr
template = ScTemplate()
template.quintuple(
main_node >> '_main_node',
sc_types.EDGE_D_COMMON_VAR,
sc_types.LINK_VAR >> '_link',
sc_types.EDGE_ACCESS_VAR_POS_PERM,
sc_type.VAR_COMMON_ARC,
sc_type.VAR_NODE_LINK >> '_link',
sc_type.VAR_PERM_POS_ARC,
relation_node,
)
template.triple(
'_main_node',
sc_types.EDGE_ACCESS_VAR_POS_TEMP,
(sc_types.NODE_VAR, '_var_node')
sc_type.VAR_TEMP_POS_ARC,
(sc_type.VAR_NODE, '_var_node')
)
gen_params = {'_link': link_node, '_var_node': 'node_idtf'}
gen_result = generate_by_template(template, gen_params)
Expand Down Expand Up @@ -402,12 +402,12 @@ Returns list of ScTypes for given elements.
from sc_client.client import get_elements_types

from sc_client.client import generate_elements
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScConstruction

construction = ScConstruction() # Create elements for example
construction.generate_node(sc_types.NODE_CONST)
construction.generate_node(sc_types.NODE_VAR)
construction.generate_node(sc_type.CONST_NODE)
construction.generate_node(sc_type.VAR_NODE)
elements = generate_elements(construction)

elements_types = get_elements_types(*elements)
Expand All @@ -432,11 +432,11 @@ assert results == [True, False] # Warning: it doesn't return False, it raised e

```python
from sc_client.client import generate_elements_by_scs, generate_elements
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import SCs, ScConstruction

construction = ScConstruction() # Create output_struct for example
construction.generate_node(sc_types.NODE_CONST)
construction.generate_node(sc_type.CONST_NODE)
output_struct = generate_elements(construction)[0]

results = generate_elements_by_scs([SCs("concept1 -> node1;;", output_struct), "concept1 -> node2;;"])
Expand All @@ -451,11 +451,11 @@ Delete *addrs* from the KB memory and returns boolean status.

```python
from sc_client.client import generate_elements, set_link_contents
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScConstruction, ScLinkContent, ScLinkContentType

construction = ScConstruction() # Create link for example
construction.generate_link(sc_types.LINK_CONST, ScLinkContent("One", ScLinkContentType.STRING))
construction.generate_link(sc_type.CONST_NODE_LINK, ScLinkContent("One", ScLinkContentType.STRING))
link = generate_elements(construction)[0]

link_content = ScLinkContent("Two", ScLinkContentType.STRING, link)
Expand All @@ -478,10 +478,10 @@ Typed-dict class that contains *idtf* and optional *type*

```python
from sc_client.client import resolve_keynodes
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScIdtfResolveParams

params = ScIdtfResolveParams(idtf='new_keynode_that_doesnt_exist', type=sc_types.NODE_CONST)
params = ScIdtfResolveParams(idtf='new_keynode_that_doesnt_exist', type=sc_type.CONST_NODE)
addrs = resolve_keynodes(params) # list with 1 new keynode addr

params = ScIdtfResolveParams(idtf='keynode_that_have_to_exist_but_doesnt', type=None)
Expand Down Expand Up @@ -522,12 +522,12 @@ Set the new content to corresponding links and return boolean status.

```python
from sc_client.client import set_link_contents, generate_elements
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScLinkContent, ScLinkContentType, ScConstruction

construction = ScConstruction() # Create link for example
link_content1 = ScLinkContent("One", ScLinkContentType.STRING)
construction.generate_link(sc_types.LINK_CONST, link_content1)
construction.generate_link(sc_type.CONST_NODE_LINK, link_content1)
link = generate_elements(construction)[0]

link_content2 = ScLinkContent("Two", ScLinkContentType.STRING, link)
Expand All @@ -543,12 +543,12 @@ Get list of contents of the given links.

```python
from sc_client.client import generate_elements, get_link_content
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScLinkContent, ScLinkContentType, ScConstruction

construction = ScConstruction() # Create link for example
link_content1 = ScLinkContent("One", ScLinkContentType.STRING)
construction.generate_link(sc_types.LINK_CONST, link_content1)
construction.generate_link(sc_type.CONST_NODE_LINK, link_content1)
link = generate_elements(construction)[0]

link_content = get_link_content(link)[0]
Expand All @@ -563,14 +563,14 @@ Returns list of lists of links for every content.

```python
from sc_client.client import generate_elements, search_links_by_contents
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScLinkContent, ScLinkContentType, ScConstruction

search_string = "search string"

construction = ScConstruction() # Create link with search string
link_content1 = ScLinkContent(search_string, ScLinkContentType.STRING)
construction.generate_link(sc_types.LINK_CONST, link_content1)
construction.generate_link(sc_type.CONST_NODE_LINK, link_content1)
link = generate_elements(construction)[0]

links = search_links_by_contents(search_string)[0]
Expand All @@ -585,14 +585,14 @@ Returns list of lists of links for every content substring.

```python
from sc_client.client import generate_elements, search_links_by_contents_substrings
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScLinkContent, ScLinkContentType, ScConstruction

search_string = "substring1 substring2"

construction = ScConstruction() # Create link with search string
link_content1 = ScLinkContent(search_string, ScLinkContentType.STRING)
construction.generate_link(sc_types.LINK_CONST, link_content1)
construction.generate_link(sc_type.CONST_NODE_LINK, link_content1)
link = generate_elements(construction)[0]

links_list = search_links_by_contents_substrings(*search_string.split(" "))
Expand All @@ -608,14 +608,14 @@ Returns list of lists of contents of the given content substrings.

```python
from sc_client.client import generate_elements, search_link_contents_by_content_substrings
from sc_client.constants import sc_types
from sc_client.constants import sc_type
from sc_client.models import ScLinkContent, ScLinkContentType, ScConstruction

search_string = "substring1 substring2"

construction = ScConstruction() # Create link with search string
link_content1 = ScLinkContent(search_string, ScLinkContentType.STRING)
construction.generate_link(sc_types.LINK_CONST, link_content1)
construction.generate_link(sc_type.CONST_NODE_LINK, link_content1)
link_addr = generate_elements(construction)[0]

links_list = search_link_contents_by_content_substrings(*search_string.split(" "))
Expand Down
127 changes: 127 additions & 0 deletions src/sc_client/constants/sc_type/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from sc_client.constants.sc_type import bitmasks as b
from sc_client.constants.sc_type.sc_type import ScType

UNKNOWN = ScType()

# sc-elements
NODE = ScType(b.SC_TYPE_NODE)
CONNECTOR = ScType(b.SC_TYPE_CONNECTOR)
COMMON_EDGE = ScType(b.SC_TYPE_COMMON_EDGE)
ARC = ScType(b.SC_TYPE_ARC)
COMMON_ARC = ScType(b.SC_TYPE_COMMON_ARC)
MEMBERSHIP_ARC = ScType(b.SC_TYPE_MEMBERSHIP_ARC)

# constancy
CONST = ScType(b.SC_TYPE_CONST)
VAR = ScType(b.SC_TYPE_VAR)

CONST_NODE = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE)
VAR_NODE = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE)
CONST_CONNECTOR = ScType(b.SC_TYPE_CONST | b.SC_TYPE_CONNECTOR)
VAR_CONNECTOR = ScType(b.SC_TYPE_VAR | b.SC_TYPE_CONNECTOR)
CONST_COMMON_EDGE = ScType(b.SC_TYPE_CONST | b.SC_TYPE_COMMON_EDGE)
VAR_COMMON_EDGE = ScType(b.SC_TYPE_VAR | b.SC_TYPE_COMMON_EDGE)
CONST_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_ARC)
VAR_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_ARC)
CONST_COMMON_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_COMMON_ARC)
VAR_COMMON_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_COMMON_ARC)
CONST_MEMBERSHIP_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_MEMBERSHIP_ARC)
VAR_MEMBERSHIP_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_MEMBERSHIP_ARC)

# permanency
PERM_ARC = ScType(b.SC_TYPE_PERM_ARC)
TEMP_ARC = ScType(b.SC_TYPE_TEMP_ARC)

CONST_PERM_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_PERM_ARC)
VAR_PERM_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_PERM_ARC)
CONST_TEMP_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_TEMP_ARC)
VAR_TEMP_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_TEMP_ARC)

# actuality
ACTUAL_TEMP_ARC = ScType(b.SC_TYPE_ACTUAL_ARC | b.SC_TYPE_TEMP_ARC)
INACTUAL_TEMP_ARC = ScType(b.SC_TYPE_INACTUAL_ARC | b.SC_TYPE_TEMP_ARC)

CONST_ACTUAL_TEMP_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_ACTUAL_ARC | b.SC_TYPE_TEMP_ARC)
VAR_ACTUAL_TEMP_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_ACTUAL_ARC | b.SC_TYPE_TEMP_ARC)
CONST_INACTUAL_TEMP_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_INACTUAL_ARC | b.SC_TYPE_TEMP_ARC)
VAR_INACTUAL_TEMP_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_INACTUAL_ARC | b.SC_TYPE_TEMP_ARC)

# positivity
POS_ARC = ScType(b.SC_TYPE_POS_ARC)
NEG_ARC = ScType(b.SC_TYPE_NEG_ARC)

# fuzzy sc-arcs
FUZ_ARC = ScType(b.SC_TYPE_FUZ_ARC)

# positive sc-arcs
CONST_POS_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_POS_ARC)
VAR_POS_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_POS_ARC)

PERM_POS_ARC = ScType(b.SC_TYPE_PERM_ARC | b.SC_TYPE_POS_ARC)
TEMP_POS_ARC = ScType(b.SC_TYPE_TEMP_ARC | b.SC_TYPE_POS_ARC)
ACTUAL_TEMP_POS_ARC = ScType(b.SC_TYPE_ACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_POS_ARC)
INACTUAL_TEMP_POS_ARC = ScType(b.SC_TYPE_INACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_POS_ARC)

CONST_PERM_POS_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_PERM_ARC | b.SC_TYPE_POS_ARC)
CONST_TEMP_POS_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_POS_ARC)
CONST_ACTUAL_TEMP_POS_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_ACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_POS_ARC)
CONST_INACTUAL_TEMP_POS_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_INACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_POS_ARC)

VAR_PERM_POS_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_PERM_ARC | b.SC_TYPE_POS_ARC)
VAR_TEMP_POS_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_POS_ARC)
VAR_ACTUAL_TEMP_POS_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_ACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_POS_ARC)
VAR_INACTUAL_TEMP_POS_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_INACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_POS_ARC)

# negative sc-arcs
CONST_NEG_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NEG_ARC)
VAR_NEG_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NEG_ARC)

PERM_NEG_ARC = ScType(b.SC_TYPE_PERM_ARC | b.SC_TYPE_NEG_ARC)
TEMP_NEG_ARC = ScType(b.SC_TYPE_TEMP_ARC | b.SC_TYPE_NEG_ARC)
ACTUAL_TEMP_NEG_ARC = ScType(b.SC_TYPE_ACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_NEG_ARC)
INACTUAL_TEMP_NEG_ARC = ScType(b.SC_TYPE_INACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_NEG_ARC)

CONST_PERM_NEG_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_PERM_ARC | b.SC_TYPE_NEG_ARC)
CONST_TEMP_NEG_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_NEG_ARC)
CONST_ACTUAL_TEMP_NEG_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_ACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_NEG_ARC)
CONST_INACTUAL_TEMP_NEG_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_INACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_NEG_ARC)

VAR_PERM_NEG_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_PERM_ARC | b.SC_TYPE_NEG_ARC)
VAR_TEMP_NEG_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_NEG_ARC)
VAR_ACTUAL_TEMP_NEG_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_ACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_NEG_ARC)
VAR_INACTUAL_TEMP_NEG_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_INACTUAL_ARC | b.SC_TYPE_TEMP_ARC | b.SC_TYPE_NEG_ARC)

# fuzzy sc-arcs
CONST_FUZ_ARC = ScType(b.SC_TYPE_CONST | b.SC_TYPE_FUZ_ARC)
VAR_FUZ_ARC = ScType(b.SC_TYPE_VAR | b.SC_TYPE_FUZ_ARC)

# semantic sc-node types
NODE_LINK = ScType(b.SC_TYPE_NODE_LINK)
NODE_LINK_CLASS = ScType(b.SC_TYPE_NODE_LINK | b.SC_TYPE_NODE_CLASS)
NODE_TUPLE = ScType(b.SC_TYPE_NODE_TUPLE)
NODE_STRUCTURE = ScType(b.SC_TYPE_NODE_STRUCTURE)
NODE_ROLE = ScType(b.SC_TYPE_NODE_ROLE)
NODE_NO_ROLE = ScType(b.SC_TYPE_NODE_NO_ROLE)
NODE_CLASS = ScType(b.SC_TYPE_NODE_CLASS)
NODE_SUPERCLASS = ScType(b.SC_TYPE_NODE_SUPERCLASS)
NODE_MATERIAL = ScType(b.SC_TYPE_NODE_MATERIAL)

CONST_NODE_LINK = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE_LINK)
CONST_NODE_LINK_CLASS = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE_LINK | b.SC_TYPE_NODE_CLASS)
CONST_NODE_TUPLE = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE_TUPLE)
CONST_NODE_STRUCTURE = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE_STRUCTURE)
CONST_NODE_ROLE = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE_ROLE)
CONST_NODE_NO_ROLE = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE_NO_ROLE)
CONST_NODE_CLASS = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE_CLASS)
CONST_NODE_SUPERCLASS = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE_SUPERCLASS)
CONST_NODE_MATERIAL = ScType(b.SC_TYPE_CONST | b.SC_TYPE_NODE_MATERIAL)

VAR_NODE_LINK = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE_LINK)
VAR_NODE_LINK_CLASS = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE_LINK | b.SC_TYPE_NODE_CLASS)
VAR_NODE_TUPLE = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE_TUPLE)
VAR_NODE_STRUCTURE = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE_STRUCTURE)
VAR_NODE_ROLE = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE_ROLE)
VAR_NODE_NO_ROLE = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE_NO_ROLE)
VAR_NODE_CLASS = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE_CLASS)
VAR_NODE_SUPERCLASS = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE_SUPERCLASS)
VAR_NODE_MATERIAL = ScType(b.SC_TYPE_VAR | b.SC_TYPE_NODE_MATERIAL)
Loading

0 comments on commit ea1220e

Please sign in to comment.