Wrapped classes

Global

  • getScConfigValue - function that returns value from a sc-memory config file.
    configValue = getScConfigValue('group', 'value')
    

ScAddr

IsValid()

if ScAddr is valid, then returns True; otherwise - False

ToInt()

convert addr value to integer (equal C++ ScAddr::Hash). Useful for debug purposes.

__eq__()

equal operator ==. Can be used like this:

if addr1 == addr2:
  pass # do something
__ne__()

not equal operator !=. Can be used like this:

if addr1 != addr2:
  pass # do something

ScType

This class equal to ScType in C++. Methods of this class:

constructor

you can create this class with input type combinations

type1 = ScType() # equal to ScType.Unknown
type2 = ScType.Node # see more constants below
type3 = ScType.Node | ScType.Const # equal to ScType.NodeConst

IsValid()

if type is not ScType.Unknown, then returns True; otherwise - False

__eq__()

equal operator ==. Can be used like this:

if type1 == type2:
  pass # do something

__ne__()

not equal operator !=. Can be used like this:

if type1 != type2:
  pass # do something

__or__()

bitwise or operator |. Can be used like this:

type1 = ScType.Node
type2 = ScType.Const

type3 = type1 | type2
type3 == ScType.NodeConst # will be True

__and__()

bitwise and operator &. Can be used like this:

type1 = ScType.NodeConst
type2 = ScType.Node

type3 = type1 & type2

type3 == type1 # will be True

IsLink()

if type represents a link, then returns True; otherwise - False

type1 = ScType.LinkConst
type1.IsLink() # returns True

IsEdge()

if type represents an edge, then returns True; otherwise - False

type1 = ScType.EdgeAccessConstFuzPerm
type1.IsEdge() # return True

IsNode()

if type represents a node, then returns True; otherwise - False

type1 = ScType.NodeConst
type1.IsNode() # returns True

IsUnknown()

if type is not Unknown, then returns True; otherwise - False

type1 = ScType.Node
type1.IsUnknown() # return True

type2 = ScType()
type2.IsUnknown() # return False

IsConst()

if type represents constant, then returns True; otherwise - False

type1 = ScType()
type1.IsConst() # returns False

type2 = ScType.NodeVar()
type2.IsConst() # return False

type3 = ScType.Node()
type3.IsConst() # return False

type4 = ScType.NodeConst()
type4.IsConst() # returns True

IsVar()

if type represents variable, then returns True; otherwise - False

type1 = ScType()
type1.IsVar() # returns False

type2 = ScType.NodeVar()
type2.IsVar() # returns True

type3= ScType.Node()
type3.IsVar() # returns False

type4 = ScType.NodeConst
type4.IsVar() # returns False

ToInt()

returns integer, that represents a type. Useful for debug purposes

There are some predefined types. You can find them in types table (see C++ table). In Python you should use ScType.Node instead of ScType::Node

ScIterator3

This class represents iterator for a triples (see iterators description). There are a list of available methods:

Next()

move iterator to next triple. Returns True if moved to next triple; otherwise - False. Example of usage:

while it3.Next():
  ... # do something

Get(idx)
  • idx - number that represents values index. Ыhould be in range [0; 2]

returns ScAddr of specified element in current triple. Example of usage:

while it3.Next():
  src = it3.Get(0)  # source element in triple
  edge = it3.Get(1) # edge in triple
  trg = it3.Get(2)  # target element in triple
  ...

IsValid()

returns True if iterator is valid; otherwise - False

ScIterator5

This class represents iterator of 5-element constructions (see iterators description). There are a list of available methods:

Next()

move iterator to next 5-element construction. Returns True if moved to next construction; otherwise - False. Example of usage:

while it5.Next():
  ... # do something

Get(idx)
  • idx - number that represents values index. Ыhould be in range [0; 4]

returns ScAddr of specified element in current construction. Example of usage:

while it5.Next():
  src = it5.Get(0)      # source element in triple
  edge = it5.Get(1)     # edge in triple
  trg = it5.Get(2)      # target element in triple
  attrEdge = it5.Get(3) # edge from attribute set
  attr = it5.Get(4)     # attribute set
  ...

IsValid()

returns True if iterator is valid; otherwise - False

ScLinkContent

This class wrap content of link. It allows to unpack it to string, int or float. There are methods of this class:

AsString()

get content of a link as string value.

content = ctx.GetLinkContent(linkAddr)
stringValue = content.AsString()

AsInt()

get content of a link as int value. If length of content not equal to 8 bytes, then ExceptionInvalidType will be raised.

content = ctx.GetLinkContent(linkAddr)
intValue = content.AsInt()

AsFloat()

get content of a link as float value. If length of content not equal to 8 bytes, then ExceptionInvalidType will be raised.

content = ctx.GetLinkContent(linkAddr)
floatValue = content.AsFloat()

AsBinary()

get content of a link as memoryview value.

content = ctx.GetLinkContent(linkAddr)
binaryValue = content.AsBinary()

Danger

Object content should be alive until memory used

GetType()

return type of content. There are possible values:

  • ScLinkContent.String
  • ScLinkContent.Int
  • ScLinkContent.Float

ScTemplateGenParams

This class accumulate parameters for a template generation. There are methods of this class:

Add(paramName, valueAddr)
  • paramName - parameter name (str)
  • valueAddr - ScAddr of element that should be used with specified name (see more in templates description)
params = ScTemplateGenParams()
params.Add("_item", itemAddr)
...
Get(paramName)
  • paramName - name of parameter (str)

returns value of parameter with a specified name. If parameter with specified identifier exists, then returns it ScAddr; otherwise - None

addr = params.Get("_item")

IsEmpty()

if there are no any parameters added, then returns True; otherwise - False

ScTemplateGenResult

This class wrap template generation result. There are methods of this class:

Aliases()

returns a dict of all used aliases in template (where key - alias, value - index in generate result). Keys can be used to get result values by __getitem__

Size()

return number of elements

__getitem__(alias)
  • alias - name of result parameter (str)

returns ScAddr by specified name. If there are no value with a specified name, then returns None

addr = genResult["node1"]

ScTemplateSearchResultItem

This class represents one result for a search by template. There are methods of this class:

Size()

return size of result (number of ScAddr's equal to search construction)

__getitem__(name_or_index)

allows to get result items by any index: int, str. In case of int index you will just get ScAddr by index in result array (length equal to Size()) this case suitable, when you need to iterate all addrs in result.

Warning

You will receive duplicate ScAddr's, because result stored as array of founded triples.

When you try to get ScAddr with str it will be found by alias (see templates for more info). If there are no element with specified index, then returns None

resultSize = searchResultItem.Size()
for i in range(resultSize):
  addr = searchResultItem[i] # iterate all addrs

addr1 = searchResultItem["alias"] # get by replacement name

ScTemplateSearchResult

This class represent list of results by template search. There are methods of this class:

Aliases()

returns a dict of all used aliases in template (where key - alias, value - index in search result). Keys can be used to get result values by __getitem__

Size()

returns number of results.

__getitem__()

get result by specified index (int should be less then Size()).

resultNum = searchResult.Size()
for i in range(resultNum):
  searchResultItem = searchResult[i]

  # work with searchResultItem there see (ScTemplateSearchResult)

ScMemoryContext

Danger

DO NOT use same context in different threads

There are methods of this class:

CreateNode(type)
  • type - ScType of a node
CreateEdge(type, src, trg)
  • type - ScType of edge
  • src - ScAddr of source edge element
  • trg - ScAddr of target edge element

create edge between src and trg elements. Returns ScAddr of created edge, but if returned ScAddr is not valid, then edge wasn't created.

Example:

edgeAddr = ctx.CreateEdge(ScType.EdgeAccessConstPosPerm, srcAddr, trgAddr)

CreateLink()

create link. Returns ScAddr of created link, but if returned ScAddr is not valid, then link wasn't created

Example:

linkAddr = ctx.CreateLink()

GetName()

returns name of context. Useful in debug purposes

IsElement(addr)
  • addr - ScAddr of element to check

if element exist, then returns True; otherwise - False

Example:

ctx.IsElement(elementAddr)

GetElementType(addr)
  • addr - ScAddr of element to get type

returns type of specified element. If element doesn't exist, then returns ScType.Unknown

Example:

t = ctx.GetElementType(elementAddr)

GetEdgeInfo(addr)
  • addr - ScAddr of edge

returns tuple (src, trg), where:

  • src - is a ScAddr of edge source element;
  • trg - target element.

If addr point to element that doesn't exist, or is not an edge, then returns (None, None).

Example:

src, trg = ctx.GetEdgeInfo(edgeAddr)

SetLinkContent(addr, content)
  • addr - ScAddr of sc-link to set content
  • content - content of sc-link, that should be set. Type of content should be one of: int, float, string.

Change content of sc-link. If content changed, then returns True; otherwise - False

Example:

...
ctx.SetLinkContent(linkAddr1, 56)
...
ctx.SetLinkContent(linkAddr1, 56.0)
...
ctx.SetLinkContent(linkAddr1, "any text")
...

GetLinkContent(addr)
  • addr - ScAddr of sc-link

returns content of a specified link. If specified addr is not a link, or it doesn't exist, then returns None. Returned value has type ScLinkContent.

Example:

...

value = ctx.GetLinkContent(elementAddr)
if value:
  print (value)

...

Iterator3(param1, param2, param3)
  • param1, param2, param3 - could be on of a type: ScAddr, ScType

create iterator for a specified triple template. For more information about iterators see iterators description. If iterator created, then return ScIterator3 object; otherwise - None

Example:

itFAA = ctx.Iterator3(addr1, ScType.EdgeAccessConstPosPerm, ScType.NodeVar)
while itFAA.Next():
  pass # process iterated constructions there

...

itFAF = ctx.Iterator3(addr1, ScType.EdgeAccessConstPosPerm, addr2)
while itFAF.Next():
  pass # process iterated constructions there

...

itAAF = ctx.Iterator3(ScType.NodeConst, ScType.EdgeAccessConstPosPerm, addr2)
while itAAF.Next():
  pass # process iterated constructions there

Iterator5(param1, param2, param3, param4, param5)
  • param1, param2, param3, param4, param5 - could be on of a type ScAddr, ScType

create iterator for a specified 5-element construction. For more information about iterators see iterators description. If iterator created, then return ScIterator5 object; otherwise - None

Example:

itFAFAF = ctx.Iterator5(
            addr1,
            ScType.EdgeAccessConstPosPerm,
            addr2,
            ScType.EdgeAccessVarPosTemp,
            attr)
while itFAFAF.Next():
  pass # process iterated constructions there

...

itFAFAA = ctx.Iterator5(
            addr1,
            ScType.EdgeAccessConstPosPerm,
            addr2,
            ScType.EdgeAccessVarPosTemp,
            ScType.Node)
while itFAFAA.Next():
  pass # process iterated constructions there

...

itFAAAF = ctx.Iterator5(
            addr1,
            ScType.EdgeAccessConstPosPerm,
            ScType.NodeVar,
            ScType.EdgeAccessVarPosTemp,
            attr)
while itFAAAF.Next():
  pass # process iterated constructions there

...

itFAAAA = ctx.Iterator5(
            addr1,
            ScType.EdgeAccessConstPosPerm,
            ScType.NodeVar,
            ScType.EdgeAccessVarPosTemp,
            ScType.Node)
while itFAAAA.Next():
  pass # process iterated constructions there

...
itAAFAF = ctx.Iterator5(
            ScType.NodeConst,
            ScType.EdgeAccessConstPosPerm,
            addr2,
            ScType.EdgeAccessVarPosTemp,
            attr)
while itAAFAF.Next():
  pass # process iterated constructions there

...

itAAFAA = ctx.Iterator5(
            ScType.NodeConst,
            ScType.EdgeAccessConstPosPerm,
            addr2,
            ScType.EdgeAccessVarPosTemp,
            ScType.Node)
while itAAFAA.Next():
  pass # process iterated constructions there

HelperResolveSystemIdtf(idtf, type)
  • idtf - str that represents a system identifier of sc-element
  • type - ScType of sc-element

resolve element by system identifier. This function tries to find element with specified system identifier - idtf. If element wasn't found and type is valid (ScType.IsValid()), then new element will be created with specified system identifier and type. If type is None, then new element wouldn't created and function returns invalid ScAddr. In other cases function returns ScAddr of element with specified system identifier.

Example:

addr = ctx.HelperResolveSystemIdtf("nrel_main_idtf", ScType.NodeConstNoRole)

HelperSetSystemIdtf(idtf, addr)
  • idtf - str new identifier of sc-element
  • addr - ScAddr of element

set specified system identifier - idtf, to element with specified addr. If identifier changed, then returns True; otherwise - False

Example:

if ctx.HelperSetSystemIdtf("new_idtf", addr):
  pass # identifier changed
else:
  pass # identifier not changed

HelperGetSystemIdtf(addr)
  • addr - ScAddr of sc-element to get identifier

returns system identifier of a specified element. If system identifier exist, then it returns as a non empty string; otherwise result string would be empty.

Example:

idtfValue = ctx.HelperGetSystemIdtf(addr)

HelperCheckEdge(srcAddr, trgAddr, edgeType)
  • srcAddr - ScAddr of source edge sc-element
  • trgAddr - ScAddr of target edge sc-element
  • edgeType - ScType of edge to check

if there are one or more edge with a specified type (edgeType) between srcAddr and trgAddr elements, then returns True; otherwise - False

Example:

if ctx.HelperCheckEdge(addr1, addr2, ScType.EdgeAccessConstPosPerm):
  pass # edge between addr1 and addr2 exist
else:
  pass # edge doesn't exists

HelperGenTemplate(templ, params)
  • templ - ScTemplate to generate construction
  • params - ScTemplateGenParams parameters for construction generation

generates construction by specified template with specified parameters. If construction generated, then returns instance of ScTemplateGenResult; otherwise - None.

Example:

templ = ScTemplate()
... # fill template

params = ScTemplateGenParams()
... # fill parameters

result = ctx.HelperGenTemplate(templ, params)

HelperSearchTemplate(templ)
  • templ - ScTemplate to search construction

searches construction by specified template. Returns ScTemplateSearchResult object instance. If it Size() equal to 0, then nothing was found

Example:

templ = ScTemplate()
... # fill template

result = ctx.HelperSearchTemplate(templ)

HelperBuildTemplate(data)
  • data - ScAddr or str. If it's a ScAddr then it should point to sc-struct in memory, that is a template. It it's a str, then it should contains SCs-code that describes template

returns ScTemplate instance. If template wasn't built, then return None

*Example:

templFromMemory = ctx.HelperBuildTemplate(templAddr)
templFromStr = ctx.HelperBuildTemplate('person _=> nrel_email:: _[];;')
... # work with template