Knowledge

Model

Knowledge database models.

class invenio.modules.knowledge.models.KnwKB(**kwargs)

Represent a KnwKB record.

static exists(kb_name)

Return True if a kb with the given name exists.

Parameters:kb_name – the name of the knowledge base
Returns:True if kb exists
static generate_slug(name)

Generate a slug for the knowledge.

Parameters:name – text to slugify
Returns:slugified text
get_filename()

Construct the file name for taxonomy knoledge.

get_kbr_items(searchkey='', searchvalue='', searchtype='s')

Return dicts of ‘key’ and ‘value’ from a knowledge base.

:param kb_name the name of the knowledge base :param searchkey search using this key :param searchvalue search using this value :param searchtype s=substring, e=exact, sw=startswith :return a list of dictionaries [{‘key’=>x, ‘value’=>y},..]

get_kbr_values(searchkey='', searchvalue='', searchtype='s')

Return dicts of ‘key’ and ‘value’ from a knowledge base.

:param kb_name the name of the knowledge base :param searchkey search using this key :param searchvalue search using this value :param searchtype s=substring, e=exact, sw=startswith :return a list of dictionaries [{‘key’=>x, ‘value’=>y},..]

is_dynamic()

Return true if the type is dynamic.

static query_exists(filters)

Return True if a kb with the given filters exists.

E.g: KnwKB.query_exists(KnwKB.name.like(‘FAQ’))

Parameters:filters – filter for sqlalchemy
Returns:True if kb exists
set_dyn_config(*a, **k)

Wrapper function to manage DB session.

to_dict()

Return a dict representation of KnwKB.

class invenio.modules.knowledge.models.KnwKBDDEF(**kwargs)

Represent a KnwKBDDEF record.

to_dict()

Return a dict representation of KnwKBDDEF.

class invenio.modules.knowledge.models.KnwKBRVAL(**kwargs)

Represent a KnwKBRVAL record.

static query_kb_mappings(kbid, sortby='to', key='', value='', match_type='s')

Return a list of all mappings from the given kb, ordered by key.

If key given, give only those with left side (mapFrom) = key. If value given, give only those with right side (mapTo) = value.

Parameters:
  • kb_name – knowledge base name. if “”, return all
  • sortby – the sorting criteria (‘from’ or ‘to’)
  • key – return only entries where key matches this
  • value – return only entries where value matches this
  • match_type – s=substring, e=exact, sw=startswith
to_dict()

Return a dict representation of KnwKBRVAL.

API

Provide API-callable functions for knowledge base management.

invenio.modules.knowledge.api.add_dynamic_kb(kbname, tag, collection='', searchwith='')

A convenience method.

invenio.modules.knowledge.api.add_kb(kb_name=u'Untitled', kb_type=None, tries=10)

Add a new kb in database, return the id.

Add a new kb in database, and returns its id The name of the kb will be ‘Untitled#’ such that it is unique.

Parameters:
  • kb_name – the name of the kb
  • kb_type – the type of the kb, incl ‘taxonomy’ and ‘dynamic’. None for typical (leftside-rightside).
  • tries – exit after <n> retry
Returns:

the id of the newly created kb

invenio.modules.knowledge.api.get_all_kb_names()

Return all knowledge base names.

Returns:list of names
invenio.modules.knowledge.api.get_kb_by_id(kb_id)

Return the knwKB object with given id.

Parameters:kb_id – id of knowledge
Returns:knowledge’s object
Raises:NoResultFound in case not exist.
invenio.modules.knowledge.api.get_kb_by_name(kb_name)

Return the knwKB object with given name.

Raises:NoResultFound in case not exist.
invenio.modules.knowledge.api.get_kb_by_slug(slug)

Return the knwKB object with given slug.

Parameters:slug – slug of knowledge
Returns:knowledge’s object
Raises:NoResultFound in case not exist.
invenio.modules.knowledge.api.get_kb_id(kb_name)

Get the id by name.

Parameters:kb_name – knowledge base name
invenio.modules.knowledge.api.get_kb_mapping(kb_name='', key='', value='', match_type='e', default='', limit=None)

Get one unique mapping. If not found, return default.

Parameters:
  • kb_name – the name of the kb
  • key – include only lines matching this on left side in the results
  • value – include only lines matching this on right side in the results
  • match_type – s = substring match, e = exact match
  • default – default value if no mapping is found
Returns:

a mapping

invenio.modules.knowledge.api.get_kb_mappings(kb_name='', key='', value='', match_type='s', sortby='to', limit=None)

Return a list of all mappings from the given kb, ordered by key.

If key given, give only those with left side (mapFrom) = key. If value given, give only those with right side (mapTo) = value.

Parameters:
  • kb_name – knowledge base name. if “”, return all
  • sortby – the sorting criteria (‘from’ or ‘to’)
  • key – return only entries where key matches this
  • value – return only entries where value matches this
  • limit – return only X number of entries
Returns:

list of knowledge converted in dictionary

invenio.modules.knowledge.api.get_kb_mappings_embedded_json(kb_name='', key='', value='', match_type='s', limit=None)

Get leftside/rightside mappings from kb kb_name formatted as json dict.

The rightside is actually considered as a json string and hence embedded within the final result.

If key given, give only those with left side (mapFrom) = key. If value given, give only those with right side (mapTo) = value.

Parameters:
  • kb_name – the name of the kb
  • key – include only lines matching this on left side in the results
  • value – include only lines matching this on right side in the results
  • match_type – s = substring match, e = exact match
  • limit – maximum number of results to return (are ALL if set to None)
Returns:

a list of mappings

invenio.modules.knowledge.api.get_kb_mappings_json(kb_name='', key='', value='', match_type='s', limit=None)

Get leftside/rightside mappings from kb kb_name formatted as json dict.

If key given, give only those with left side (mapFrom) = key. If value given, give only those with right side (mapTo) = value.

Parameters:
  • kb_name – the name of the kb
  • key – include only lines matching this on left side in the results
  • value – include only lines matching this on right side in the results
  • match_type – s = substring match, e = exact match
  • limit – maximum number of results to return (are ALL if set to None)
Returns:

a list of mappings

invenio.modules.knowledge.api.get_kb_name(kb_id)

Return the name of the kb given by id.

Parameters:kb_id – the id of the knowledge base
invenio.modules.knowledge.api.get_kba_values(kb_name, searchname='', searchtype='s')

Return an array of values “authority file” type = just values.

Parameters:
  • kb_name – name of kb
  • searchname – get these values, according to searchtype
  • searchtype – s=substring, e=exact, , sw=startswith
invenio.modules.knowledge.api.get_kbd_values(kbname, searchwith='')

Return a list of values by searching a dynamic kb.

Parameters:
  • kbname – name of the knowledge base
  • searchwith – a term to search with
Returns:

list of values

invenio.modules.knowledge.api.get_kbd_values_by_def(confdict, searchwith='')

Return a list of values by searching a dynamic kb.

Parameters:
  • confdict – dictionary with keys “field”, “expression” and “collection” name
  • searchwith – a term to search with
Returns:

list of values

invenio.modules.knowledge.api.get_kbd_values_for_bibedit(tag, collection='', searchwith='', expression='')

Get list of kbd values for bibedit.

Example1: tag=100__a : return values of 100__a Example2: tag=100__a, searchwith=Jill: return values of 100__a that match with Jill Example3: tag=100__a, searchwith=Ellis, expression=”700__a:%: return values of 100__a for which Ellis matches some 700__a

Note: the performace of this function is ok compared to a plain
perform_request_search / get most popular fields -pair. The overhead is about 5% with large record sets; the lookups are the xpensive part.
Parameters:
  • tag – the tag like 100__a
  • collection – collection id
  • searchwith – the string to search. If empty, match all.
  • expression – the search expression for perform_request_search; if present, ‘%’ is substituted with /searcwith/. If absent, /searchwith/ is searched for in /tag/.
invenio.modules.knowledge.api.get_kbd_values_json(kbname, searchwith='')

Return values from searching a dynamic kb as a json-formatted string.

This IS probably the method you want.

Parameters:
  • kbname – name of the knowledge base
  • searchwith – a term to search with
invenio.modules.knowledge.api.get_kbr_items(kb_name, searchkey='', searchvalue='', searchtype='s')

Return a list of dictionaries that match the search.

Parameters:
  • kb_name – the name of the knowledge base
  • searchkey – search using this key
  • searchvalue – search using this value
  • searchtype – s = substring, e=exact
Returns:

a list of dictionaries [{‘key’=>x, ‘value’=>y},..]

invenio.modules.knowledge.api.get_kbr_keys(kb_name, searchkey='', searchvalue='', searchtype='s')

Return an array of keys.

Parameters:
  • kb_name – the name of the knowledge base
  • searchkey – search using this key
  • searchvalue – search using this value
  • searchtype – s = substring, e=exact
invenio.modules.knowledge.api.get_kbr_values(kb_name, searchkey='', searchvalue='', searchtype='s', use_memoise=False)

Return a tuple of values from key-value mapping kb.

Parameters:
  • kb_name – the name of the knowledge base
  • searchkey – search using this key
  • searchvalue – search using this value
  • searchtype – s=substring; e=exact
  • use_memoise (bool) – can we memoise while doing lookups?
invenio.modules.knowledge.api.get_kbs_info(kbtype='', searchkbname='')

A convenience method.

Parameters:
  • kbtype – type of kb – get only kb’s of this type
  • searchkbname – get only kb’s where this sting appears in the name
invenio.modules.knowledge.api.get_kbt_items(taxonomyfilename, templatefilename, searchwith='')

Get items from taxonomy file using a templatefile.

If searchwith is defined, return only items that match with it. :param taxonomyfilename: full path+name of the RDF file :param templatefile: full path+name of the XSLT file :param searchwith: a term to search with

invenio.modules.knowledge.api.get_kbt_items_for_bibedit(kbtname, tag='', searchwith='')

A simplifield, customized version of the function get_kbt_items.

Traverses an RDF document. By default returns all leaves. If tag defined returns the content of that tag. If searchwith defined, returns leaves that match it. Warning! In order to make this faster, the matching field values cannot be multi-line!

Parameters:
  • kbtname – name of the taxonony kb
  • tag – name of tag whose content
  • searchwith – a term to search with
invenio.modules.knowledge.api.kb_exists(kb_name)

Return True if a kb with the given name exists.

Parameters:kb_name – the name of the knowledge base
Returns:True if kb exists
invenio.modules.knowledge.api.kb_mapping_exists(kb_name, key)

Return the information if a mapping exists.

Parameters:
  • kb_name – knowledge base name
  • key – left side (mapFrom)
invenio.modules.knowledge.api.query_get_kb_by_type(kbtype)

Return a query to filter kb by type.

Parameters:kbtype – type to filter (e.g: taxonomy)
Returns:query to filter kb
invenio.modules.knowledge.api.query_kb_mappings(kbid, sortby='to', key='', value='', match_type='s')

Return a list of all mappings from the given kb, ordered by key.

If key given, give only those with left side (mapFrom) = key. If value given, give only those with right side (mapTo) = value.

Parameters:
  • kb_name – knowledge base name. if “”, return all
  • sortby – the sorting criteria (‘from’ or ‘to’)
  • key – return only entries where key matches this
  • value – return only entries where value matches this
  • match_type – s=substring, e=exact, sw=startswith
invenio.modules.knowledge.api.save_kb_dyn_config(kb_id, field, expression, collection=None)

Save a dynamic knowledge base configuration.

Parameters:
  • kb_id – the id
  • field – the field where values are extracted
  • expression – ..using this expression
  • collection – ..in a certain collection (default is all)