I can also get concept by name
This commit is contained in:
@@ -27,10 +27,12 @@ class SheerkaCreateNewConcept:
|
||||
concepts_definitions = None
|
||||
init_ret_value = None
|
||||
|
||||
sdp = self.sheerka.sdp
|
||||
|
||||
# checks for duplicate concepts
|
||||
# TODO checks if it exists in cache first
|
||||
|
||||
if self.sheerka.sdp.exists(self.sheerka.CONCEPTS_BY_HASH_ENTRY, concept.get_definition_hash()):
|
||||
if sdp.exists(self.sheerka.CONCEPTS_BY_HASH_ENTRY, concept.get_definition_hash()):
|
||||
error = SheerkaDataProviderDuplicateKeyError(self.sheerka.CONCEPTS_ENTRY + "." + concept.key, concept)
|
||||
return self.sheerka.ret(
|
||||
self.logger_name,
|
||||
@@ -62,7 +64,7 @@ class SheerkaCreateNewConcept:
|
||||
# TODO : needs to make these calls atomic (or at least one single call)
|
||||
# save the new concept
|
||||
concept.metadata.full_serialization = True
|
||||
result = self.sheerka.sdp.add(
|
||||
result = sdp.add(
|
||||
context.event.get_digest(),
|
||||
self.sheerka.CONCEPTS_ENTRY,
|
||||
concept,
|
||||
@@ -73,20 +75,26 @@ class SheerkaCreateNewConcept:
|
||||
concept.set_origin(result.digest)
|
||||
|
||||
# save it by id
|
||||
self.sheerka.sdp.add(
|
||||
sdp.add(
|
||||
context.event.get_digest(),
|
||||
self.sheerka.CONCEPTS_BY_ID_ENTRY,
|
||||
SheerkaDataProviderRef(concept.id, result.digest))
|
||||
|
||||
# save it by name
|
||||
sdp.add(
|
||||
context.event.get_digest(),
|
||||
self.sheerka.CONCEPTS_BY_NAME_ENTRY,
|
||||
SheerkaDataProviderRef(concept.name, result.digest))
|
||||
|
||||
# records the hash
|
||||
self.sheerka.sdp.add(
|
||||
sdp.add(
|
||||
context.event.get_digest(),
|
||||
self.sheerka.CONCEPTS_BY_HASH_ENTRY,
|
||||
SheerkaDataProviderRef(concept.get_definition_hash(), result.digest))
|
||||
|
||||
# update the definition table
|
||||
if concepts_definitions is not None:
|
||||
self.sheerka.sdp.set(
|
||||
sdp.set(
|
||||
context.event.get_digest(),
|
||||
self.sheerka.CONCEPTS_DEFINITIONS_ENTRY,
|
||||
concept_lexer_parser.encode_grammar(init_ret_value.body),
|
||||
@@ -102,7 +110,8 @@ class SheerkaCreateNewConcept:
|
||||
|
||||
# Updates the caches
|
||||
|
||||
self.sheerka.cache_by_key[concept.key] = self.sheerka.sdp.get_safe(self.sheerka.CONCEPTS_ENTRY, concept.key)
|
||||
self.sheerka.cache_by_key[concept.key] = sdp.get_safe(self.sheerka.CONCEPTS_ENTRY, concept.key)
|
||||
self.sheerka.cache_by_name[concept.name] = sdp.get_safe(self.sheerka.CONCEPTS_BY_NAME_ENTRY, concept.name)
|
||||
self.sheerka.cache_by_id[concept.id] = concept
|
||||
if init_ret_value is not None and init_ret_value.status:
|
||||
self.sheerka.concepts_grammars = init_ret_value.body
|
||||
|
||||
@@ -254,7 +254,7 @@ class SheerkaEvaluateConcept:
|
||||
# validate where clause
|
||||
if ConceptParts.WHERE in concept.values:
|
||||
where_value = concept.values[ConceptParts.WHERE]
|
||||
if not (where_value is None or self.sheerka.value(where_value) is True):
|
||||
if not (where_value is None or self.sheerka.value(where_value)):
|
||||
return self.sheerka.new(BuiltinConcepts.WHERE_CLAUSE_FAILED, body=concept)
|
||||
|
||||
#
|
||||
|
||||
@@ -9,25 +9,34 @@ class SheerkaModifyConcept:
|
||||
|
||||
def modify_concept(self, context, concept):
|
||||
|
||||
sdp = self.sheerka.sdp
|
||||
|
||||
try:
|
||||
# modify the entry
|
||||
concept.metadata.full_serialization = True
|
||||
result = self.sheerka.sdp.modify(
|
||||
result = sdp.modify(
|
||||
context.event.get_digest(),
|
||||
self.sheerka.CONCEPTS_ENTRY,
|
||||
concept.key,
|
||||
concept)
|
||||
concept.metadata.full_serialization = False
|
||||
|
||||
# update its reference
|
||||
self.sheerka.sdp.modify(
|
||||
# update reference entry
|
||||
sdp.modify(
|
||||
context.event.get_digest(),
|
||||
self.sheerka.CONCEPTS_BY_ID_ENTRY,
|
||||
concept.id,
|
||||
SheerkaDataProviderRef(concept.id, result.digest, concept.get_origin()))
|
||||
|
||||
# update name entry
|
||||
sdp.modify(
|
||||
context.event.get_digest(),
|
||||
self.sheerka.CONCEPTS_BY_NAME_ENTRY,
|
||||
concept.name,
|
||||
SheerkaDataProviderRef(concept.name, result.digest, concept.get_origin()))
|
||||
|
||||
# update the hash entry
|
||||
self.sheerka.sdp.modify(
|
||||
sdp.modify(
|
||||
context.event.get_digest(),
|
||||
self.sheerka.CONCEPTS_BY_HASH_ENTRY,
|
||||
concept.get_original_definition_hash(),
|
||||
@@ -42,7 +51,8 @@ class SheerkaModifyConcept:
|
||||
error.args[0])
|
||||
|
||||
# update cache
|
||||
self.sheerka.cache_by_key[concept.key] = self.sheerka.sdp.get_safe(self.sheerka.CONCEPTS_ENTRY, concept.key)
|
||||
self.sheerka.cache_by_key[concept.key] = sdp.get_safe(self.sheerka.CONCEPTS_ENTRY, concept.key)
|
||||
self.sheerka.cache_by_name[concept.name] = sdp.get_safe(self.sheerka.CONCEPTS_BY_NAME_ENTRY, concept.name)
|
||||
self.sheerka.cache_by_id[concept.id] = concept
|
||||
|
||||
ret = self.sheerka.ret(self.logger_name, True, self.sheerka.new(BuiltinConcepts.NEW_CONCEPT, body=concept))
|
||||
|
||||
+62
-31
@@ -34,7 +34,8 @@ class Sheerka(Concept):
|
||||
|
||||
CONCEPTS_ENTRY = "All_Concepts" # to store all the concepts
|
||||
CONCEPTS_BY_ID_ENTRY = "Concepts_By_ID"
|
||||
CONCEPTS_BY_HASH_ENTRY = "Concepts_By_Hash" # store hash of concepts definitions (not values)
|
||||
CONCEPTS_BY_NAME_ENTRY = "Concepts_By_Name"
|
||||
CONCEPTS_BY_HASH_ENTRY = "Concepts_By_Hash" # store hash of concepts definitions (not values)
|
||||
CONCEPTS_DEFINITIONS_ENTRY = "Concepts_Definitions" # to store definitions (bnf) of concepts
|
||||
BUILTIN_CONCEPTS_KEYS = "Builtins_Concepts" # sequential key for builtin concepts
|
||||
USER_CONCEPTS_KEYS = "User_Concepts" # sequential key for user defined concepts
|
||||
@@ -53,6 +54,7 @@ class Sheerka(Concept):
|
||||
# key is the key of the concept (not the name or the id)
|
||||
self.cache_by_key = {}
|
||||
self.cache_by_id = {}
|
||||
self.cache_by_name = {}
|
||||
|
||||
# cache for concept definitions,
|
||||
# Primarily used for unit test that does not have access to sdp
|
||||
@@ -239,7 +241,7 @@ class Sheerka(Concept):
|
||||
with ExecutionContext(self.key, event, self, f"Evaluating '{text}'", self.log) as execution_context:
|
||||
user_input = self.ret(self.name, True, self.new(BuiltinConcepts.USER_INPUT, body=text, user_name=user_name))
|
||||
reduce_requested = self.ret(self.name, True, self.new(BuiltinConcepts.REDUCE_REQUESTED))
|
||||
#execution_context.local_hints.add(BuiltinConcepts.EVAL_WHERE_REQUESTED)
|
||||
# execution_context.local_hints.add(BuiltinConcepts.EVAL_WHERE_REQUESTED)
|
||||
|
||||
steps = [
|
||||
BuiltinConcepts.BEFORE_PARSING,
|
||||
@@ -375,38 +377,22 @@ class Sheerka(Concept):
|
||||
:return:
|
||||
"""
|
||||
|
||||
if concept_key is None:
|
||||
return ErrorConcept("Concept key is undefined.")
|
||||
by_key = self.internal_get("key", concept_key, self.cache_by_key, self.CONCEPTS_ENTRY, concept_id)
|
||||
if self.is_known(by_key):
|
||||
return by_key
|
||||
|
||||
if isinstance(concept_key, BuiltinConcepts):
|
||||
concept_key = str(concept_key)
|
||||
# else return by name
|
||||
by_name = self.internal_get("name", concept_key, self.cache_by_name, self.CONCEPTS_BY_NAME_ENTRY, concept_id)
|
||||
if self.is_known(by_name):
|
||||
return by_name
|
||||
|
||||
# first search in cache
|
||||
if concept_key in self.cache_by_key:
|
||||
result = self.cache_by_key[concept_key]
|
||||
else:
|
||||
result = self.sdp.get_safe(self.CONCEPTS_ENTRY, concept_key)
|
||||
if result is None:
|
||||
metadata = [("key", concept_key), ("id", concept_id)] if concept_id else ("key", concept_key)
|
||||
result = self._get_unknown(metadata)
|
||||
# Do not put in cache_by_key or cache_by_id unknown concept
|
||||
# TODO: implement an MRU cache for them
|
||||
else:
|
||||
self.cache_by_key[concept_key] = result
|
||||
for r in (result if isinstance(result, list) else [result]):
|
||||
if r.id:
|
||||
self.cache_by_id[r.id] = r
|
||||
return by_key # return not found for key
|
||||
|
||||
if not (isinstance(result, list) and concept_id):
|
||||
return result
|
||||
def get_by_key(self, concept_key, concept_id=None):
|
||||
return self.internal_get("key", concept_key, self.cache_by_key, self.CONCEPTS_ENTRY, concept_id)
|
||||
|
||||
# result is a list, but we have the concept_id to discriminate
|
||||
for c in result:
|
||||
if c.id == concept_id:
|
||||
return c
|
||||
|
||||
metadata = [("key", concept_key), ("id", concept_id)] if concept_id else ("key", concept_key)
|
||||
return self._get_unknown(metadata)
|
||||
def get_by_name(self, concept_name, concept_id=None):
|
||||
return self.internal_get("name", concept_name, self.cache_by_name, self.CONCEPTS_BY_NAME_ENTRY, concept_id)
|
||||
|
||||
def get_by_id(self, concept_id):
|
||||
if concept_id is None:
|
||||
@@ -419,10 +405,55 @@ class Sheerka(Concept):
|
||||
result = self.sdp.get_safe(self.CONCEPTS_BY_ID_ENTRY, concept_id)
|
||||
if result is None:
|
||||
result = self._get_unknown(('id', concept_id))
|
||||
self.cache_by_id[concept_id] = result
|
||||
else:
|
||||
self.cache_by_id[concept_id] = result
|
||||
|
||||
return result
|
||||
|
||||
def internal_get(self, index_name, index_value, cache_to_use, sdp_entry, concept_id=None):
|
||||
"""
|
||||
Tries to find an entry
|
||||
:param index_name:
|
||||
:param index_value:
|
||||
:param cache_to_use:
|
||||
:param sdp_entry:
|
||||
:param concept_id:
|
||||
:return:
|
||||
"""
|
||||
|
||||
if index_value is None:
|
||||
return ErrorConcept(f"Concept {index_name} is undefined.")
|
||||
|
||||
if isinstance(index_value, BuiltinConcepts):
|
||||
index_value = str(index_value)
|
||||
|
||||
# first search in cache
|
||||
if index_value in cache_to_use:
|
||||
result = cache_to_use[index_value]
|
||||
else:
|
||||
result = self.sdp.get_safe(sdp_entry, index_value)
|
||||
if result is None:
|
||||
metadata = [(index_name, index_value), ("id", concept_id)] if concept_id else (index_name, index_value)
|
||||
result = self._get_unknown(metadata)
|
||||
# Do not put in cache_by_key or cache_by_id unknown concept
|
||||
# TODO: implement an MRU cache for them
|
||||
else:
|
||||
cache_to_use[index_value] = result
|
||||
for r in (result if isinstance(result, list) else [result]):
|
||||
if r.id:
|
||||
self.cache_by_id[r.id] = r
|
||||
|
||||
if not (isinstance(result, list) and concept_id):
|
||||
return result
|
||||
|
||||
# result is a list, but we have the concept_id to discriminate
|
||||
for c in result:
|
||||
if c.id == concept_id:
|
||||
return c
|
||||
|
||||
metadata = [(index_name, index_value), ("id", concept_id)] if concept_id else (index_name, index_value)
|
||||
return self._get_unknown(metadata)
|
||||
|
||||
def get_concepts_definitions(self, context):
|
||||
|
||||
if self.concepts_definitions_cache:
|
||||
|
||||
Reference in New Issue
Block a user