Abstract Data Structures

Definition of the AbstractSet.

author:

Athanasios Anastasiou

date:

Mar 2023

class neoads.ads_abstractset.AbstractSet(name=None, **kwargs)

A Set of UNIQUE elements.

Warning

Attractive as its functionality might be, the Set lacks functions to populate it with CYPHER queries (e.g. from_query). The key problem with that is that the item’s hash_value cannot be set consistently (no similar function in APOC) at the server side.

TODO: However, it might be possible to establish a hash-like user procedure in CYPHER at server side which could be invoked by neoads to implement such queries server side too. (With APOC’s sha this can definitely be done now)

DoesNotExist

alias of AbstractSetDoesNotExist

add(an_item)

Adds an item to the set. Similar to Python’s set.add().

Warning

The item must be hashable.

Parameters:

an_item (PersistentElement) – An object to be added to the AbstractSet.

Returns:

AbstractSet (self)

add_with_hash(an_item, a_hash)

Adds an item to the set with a particular hash value.

Warning

Not meant to be called directly. Used by the AbstractMap.

Parameters:
  • an_item (PersistentElement) – An object that is to be added to the set

  • a_hash (int) – A hash value

Returns:

clear()

Clears the set.

contains_hash(a_hash)

Determines if the set contains a specific hash value.

Parameters:

a_hash (int) – The hash value to test

Returns:

bool

destroy()

Clears the set and removes it from the DBMS completely.

from_abstractset(an_abstractSet, auto_reset=False)

Initialises an abstract set from another abstract set.

Parameters:
  • an_abstractSet

  • auto_reset

Returns:

AbstractSet (self)

from_hash_nodeid_list(a_hash_nodeid_list, auto_reset=False)

Initialises an abstract set from a list of hash, Node ID tuples

Warning

Not to be called directly.

Parameters:
  • a_hash_nodeid_list (list) – A list of tuples

  • auto_reset (boolean) – Whether to clear the contents of the set automatically

Returns:

AbstractSet (self)

remove_by_hash(a_hash)

Removes an element from the set, given its hash value.

Warning

Not meant to be called directly. Used by AbstractMap.

Parameters:

a_hash (int) – An object’s hash value

Returns:

AbstractSet (self)

retrieve_by_hash(a_hash)

Retrieves the value that the set element points to given the set element’s hash value.

Warning

Not meant to be called directly. Used by AbstractMap.

Parameters:

a_hash (int) – An object’s hash value

Returns:

PersistentElement

class neoads.ads_abstractset.SetItem(hash_value, **kwargs)

A struct item that is an element of a set.

A set item maintains its item’s hash_value for fast lookups.

DoesNotExist

alias of SetItemDoesNotExist

Definitions for abstract data structures.

The module defines both end-user data structures as well as intermediate (or helper) data structures.

author:

Athanasios Anastasiou

date:

Jan 2018

class neoads.ads_abstractmap.AbstractMap(name=None, **kwargs)

A very simple mapping that maps a hash value (that can be computed by any hashable) to an entity.

Note

The abstract map is implemented via two neoads.AbstractSets, one for the keys and one for the values.

DoesNotExist

alias of AbstractMapDoesNotExist

clear()

Clears the map.

destroy()

Clears the map and completely removes it from the DBMS.

from_keyvalue_node_query(a_query, auto_reset=False)

Instantiates an AbstractMap via a query.

Note

The query must have a specific structure and return two arrays, one for the keys and one for the values. For example:

SimpleNumber(1).save() SimpleNumber(2).save() SimpleNumber(3).save()

CompositeString(“One”).save() CompositeString(“Two”).save() CompositeString(“Three”).save()

Q = AbstractSet(name=”ASET”).save()

Q.from_keyvalue_node_query(“MATCH (a:ElementVariable) WHERE a.value IN [1,2,3] WITH collect(a) AS Keys MATCH (b:ElementVariable) WHERE b.value IN [“One”,”Two”,”Three”] WITH Keys, collect(b) as Values”)

The objects in the array will have to be inflated in to Python, their hash calculated and then used to construct the sets.

Parameters:
  • a_query (str) – A COMPLETE CYPHER query.

  • auto_reset

Returns:

AbstractMap (self)

Definitions for abstract data structures.

The module defines both end-user data structures as well as intermediate (or helper) data structures.

author:

Athanasios Anastasiou

date:

Jan 2018

class neoads.ads_abstractdllist.AbstractDLList(name=None, **kwargs)

A doubly linked list with indexing.

Note

Although the list is Doubly Linked, only the list’s head is preserved with the List entry.

DoesNotExist

alias of AbstractDLListDoesNotExist

append(an_element)

Appends any PersistentElement to the Doubly Linked List.

Parameters:

an_element – PersistentElement

Returns:

AbstractDLList (self)

clear()

Clears the list.

Note

To delete the list itself, use destroy()

destroy()

Clears the list and completely removes it from the DBMS.

extend_by_merging(another_dlList)

Concatenates this list with another_dlList and ERASES another_dlList as a separate list from the backend.

Parameters:

another_dlList (str or AbstractDLList.) – The identifier or object of a list that already exists on the DBMS.

Returns:

AbstractDLList

from_id_array(array_of_ids, auto_reset=False)

Initialises the doubly linked list from a numeric array of node IDs.

Note

This array_of_ids is usually constructed via a call to CompositeArrayNumber.from_query_IDs(). Because of the dangers associated with maintaining IDs for long intervals it is best if these two are called in quick succession.

Parameters:

array_of_ids (str or CompositeArrayNumber) – The name or actual object of an array of IDs.

Returns:

AbstractDLList (self)

from_query(query, auto_reset=False, no_duplicates=False)

Creates a doubly linked list at server side.

Note

The list’s items point to the return result of query. The query MUST return PersistentElement and be a CYPHER READ query.

Warning

At the moment, the way the CYPHER queries that build the list are expressed, they seem to “explode” with the number of items returned by from_query(…, query). So use with caution.

If the query returns duplicates, these are retained in the list because a list does not behave like a set.

EXAMPLE:

"MATCH (ListItem:Institute)-[:CITY]-(:City)-[:IN_COUNTRY]-(:Country{countryName:'Australia'})"

with a possible WHERE clause too

Parameters:
  • no_duplicates (bool) – Whether or not to retain potential ListItem duplicates that might be returned by query

  • query – A CYPHER READ query WITHOUT the return clause. The entity that is to be pointed to by list items should be named ListItem.

Returns:

AbstractDLList (self)

iterate_by_query(this_list_known_as)

Generates a query that iterates over all items of the list.

Note

This can be used to “trigger” further queries / operations over each item within the list.

Warning

These are the ACTUAL ITEMS that the list is holding. If this list is pointing to other lists, those lists are not automatically UNWINDED!!!!

EXAMPLE:

neomodel.db.cypher_query(list1.iterate_by_query("pubmed")+"MATCH (Author1:Author)-[:AUTHOR]-
                                                (pubmed_listItemValue)-[:AUTHOR]-(Author2:Author)
                                                where Author1<>Author2 return count(Author1)")
Parameters:

this_list_known_as (str) – An identifier by which this list will be known within the query. It is possible to concatenate many of these from different lists and therefore should not have name collisions.

project_as(this_list_known_as, projection_known_as, projected_field=None, pass_through=None)

Returns a query that converts the Doubly Linked List to a native neo4j list so that it can participate in subsequent queries. This function returns part of a query that can be concatenated with other lists as part of a bigger query.

Note

Collects the values of projected_field from this list into a new, sequential array that is being known as projection_known_as.

This is the only way to implement “IN” at server side as somehow CYPHER has to iterate the array to extract specific values from it.

Parameters:
  • pass_through (list) – List of other parameters that have to be propagated through this part of the query.

  • this_list_known_as (str) – The logical name that this list will be made known as, server-side.

  • projected_field (str) – The list item value field that is to be extracted from this list.

  • projection_known_as (str) – The logical name that the generated list will be made known as, server-side.

Returns:

str (CYPHER query fragment)

with_this_list_as(this_list_known_as, other_lists=None)

Starts a CYPHER query in which an AbstractDLList is exposed with a given name.

Parameters:
  • this_list_known_as (str) – The name that this list will be made known as, server-side.

  • other_lists (list) – Other lists that may precede this particular list.

Returns:

str (CYPHER query fragment)

class neoads.ads_abstractdllist.DLListItem(*args, **kwargs)

A struct item of a doubly linked list.

DoesNotExist

alias of DLListItemDoesNotExist