Added is_lesser and is_greatest in SheerkaComparison
This commit is contained in:
@@ -72,7 +72,7 @@ class BuiltinConcepts(Enum):
|
||||
IS_EMPTY = "is empty" # when a set is empty
|
||||
NO_RESULT = "no result" # no return value returned
|
||||
INVALID_RETURN_VALUE = "invalid return value" # the return value of an evaluator is not correct
|
||||
CONCEPT_ALREADY_DEFINED = "concept already defined" # when you try to add the same concept twice
|
||||
ALREADY_DEFINED = "already defined" # when you try to add the same object twice (a concept or whatever)
|
||||
NOP = "no operation" # no operation concept. Does nothing
|
||||
CONCEPT_EVAL_ERROR = "concept evaluation error" # cannot evaluate a property or metadata of a concept
|
||||
ENUMERATION = "enum" # represents a list or a set
|
||||
@@ -91,6 +91,8 @@ class BuiltinConcepts(Enum):
|
||||
FORMAT_INSTRUCTIONS = "format instructions" # to express how to print the concept
|
||||
NOT_IMPLEMENTED = "not implemented" # instead of raise an error
|
||||
PYTHON_SECURITY_ERROR = "security error" # when trying to execute statement when only expression is allowed
|
||||
INVALID_LESSER_OPERATION = "Invalid lesser operation"
|
||||
INVALID_GREATEST_OPERATION = "Invalid greatest operation"
|
||||
|
||||
NODE = "node"
|
||||
GENERIC_NODE = "generic node"
|
||||
@@ -152,6 +154,9 @@ BuiltinUnique = [
|
||||
|
||||
BuiltinConcepts.ISA,
|
||||
BuiltinConcepts.COMMAND,
|
||||
|
||||
BuiltinConcepts.INVALID_LESSER_OPERATION,
|
||||
BuiltinConcepts.INVALID_GREATEST_OPERATION,
|
||||
]
|
||||
|
||||
BuiltinErrors = [str(e) for e in {
|
||||
@@ -164,14 +169,16 @@ BuiltinErrors = [str(e) for e in {
|
||||
BuiltinConcepts.TOO_MANY_ERRORS,
|
||||
BuiltinConcepts.MULTIPLE_ERRORS,
|
||||
BuiltinConcepts.INVALID_RETURN_VALUE,
|
||||
BuiltinConcepts.CONCEPT_ALREADY_DEFINED,
|
||||
BuiltinConcepts.ALREADY_DEFINED,
|
||||
BuiltinConcepts.CONCEPT_EVAL_ERROR,
|
||||
BuiltinConcepts.CONCEPT_ALREADY_IN_SET,
|
||||
BuiltinConcepts.NOT_A_SET,
|
||||
BuiltinConcepts.CONDITION_FAILED,
|
||||
BuiltinConcepts.CHICKEN_AND_EGG,
|
||||
BuiltinConcepts.NOT_INITIALIZED,
|
||||
BuiltinConcepts.NOT_FOUND
|
||||
BuiltinConcepts.NOT_FOUND,
|
||||
BuiltinConcepts.INVALID_LESSER_OPERATION,
|
||||
BuiltinConcepts.INVALID_GREATEST_OPERATION,
|
||||
}]
|
||||
|
||||
"""
|
||||
|
||||
@@ -3,7 +3,7 @@ from dataclasses import dataclass
|
||||
from cache.Cache import Cache
|
||||
from cache.ListCache import ListCache
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import ensure_concept
|
||||
from core.concept import ensure_concept, Concept
|
||||
from core.sheerka.services.sheerka_service import ServiceObj, BaseService
|
||||
|
||||
|
||||
@@ -27,15 +27,30 @@ class SheerkaComparisonManager(BaseService):
|
||||
COMPARISON_ENTRY = "ComparisonManager:Comparison"
|
||||
RESOLVED_COMPARISON_ENTRY = "ComparisonManager:Resolved_Comparison"
|
||||
|
||||
# to use an initialisation value for attributes that will make use of computed weights
|
||||
# the lesser and the greatest weight will be given relatively to this value
|
||||
DEFAULT_COMPARISON_VALUE = 1
|
||||
|
||||
def __init__(self, sheerka):
|
||||
super().__init__(sheerka)
|
||||
|
||||
@staticmethod
|
||||
def _compute_key(prop_name, comparison_context):
|
||||
return f"{prop_name}|{comparison_context}"
|
||||
"""
|
||||
Key to use to store the comparisons
|
||||
:param prop_name:
|
||||
:param comparison_context:
|
||||
:return:
|
||||
"""
|
||||
if isinstance(prop_name, Concept):
|
||||
prefix = prop_name.key if prop_name.metadata.is_builtin else prop_name.id
|
||||
else:
|
||||
prefix = prop_name
|
||||
|
||||
return f"{prefix}|{comparison_context}"
|
||||
|
||||
@staticmethod
|
||||
def _compute_weights(comparison_objs):
|
||||
def _get_weights(comparison_objs):
|
||||
"""
|
||||
For every element in greater_than_s, give it a weight
|
||||
if weight(a) > weight(b) it means that a > b
|
||||
@@ -45,8 +60,8 @@ class SheerkaComparisonManager(BaseService):
|
||||
|
||||
values = {}
|
||||
for comparison_obj in comparison_objs:
|
||||
values[comparison_obj.a] = 1
|
||||
values[comparison_obj.b] = 1
|
||||
values[comparison_obj.a] = SheerkaComparisonManager.DEFAULT_COMPARISON_VALUE
|
||||
values[comparison_obj.b] = SheerkaComparisonManager.DEFAULT_COMPARISON_VALUE
|
||||
|
||||
for _ in range(len(comparison_objs)):
|
||||
for comparison_obj in comparison_objs:
|
||||
@@ -57,6 +72,54 @@ class SheerkaComparisonManager(BaseService):
|
||||
|
||||
return values
|
||||
|
||||
def _compute_weights(self, comparison_objs, lesser_objs_ids=None, greatest_objs_ids=None):
|
||||
"""
|
||||
|
||||
:param comparison_objs:
|
||||
:return:
|
||||
"""
|
||||
|
||||
def is_not_in_objs(obj, objs_ids):
|
||||
return obj.op in ("<", ">") and obj.a not in objs_ids and obj.b not in objs_ids
|
||||
|
||||
def is_in_objs(obj, objs_ids):
|
||||
return obj.op in ("<", ">") and (obj.a in objs_ids or obj.b in objs_ids)
|
||||
|
||||
lesser_objs_ids = lesser_objs_ids or {co.a for co in comparison_objs if co.op == "<<"}
|
||||
greatest_objs_ids = greatest_objs_ids or {co.a for co in comparison_objs if co.op == ">>"}
|
||||
|
||||
default_weight = SheerkaComparisonManager.DEFAULT_COMPARISON_VALUE
|
||||
|
||||
# get the weights for all the lesser
|
||||
lesser_objs = [co for co in comparison_objs if is_in_objs(co, lesser_objs_ids)]
|
||||
lesser_objs_weights = self._get_weights(lesser_objs)
|
||||
|
||||
# rearrange the weight to have the highest equals to DEFAULT_COMPARISON_VALUE - 1
|
||||
highest_weight = len(lesser_objs_weights)
|
||||
for co_id in lesser_objs_weights:
|
||||
lesser_objs_weights[co_id] = lesser_objs_weights[co_id] - highest_weight + default_weight - 1
|
||||
for concept_id in lesser_objs_ids:
|
||||
if concept_id not in lesser_objs_weights:
|
||||
lesser_objs_weights[concept_id] = default_weight - 1
|
||||
|
||||
# get the weights for concepts that are not lesser or greatest
|
||||
in_between_objs = [o for o in comparison_objs if is_not_in_objs(o, lesser_objs_ids | greatest_objs_ids)]
|
||||
in_between_weights = self._get_weights(in_between_objs)
|
||||
|
||||
# get the weights for all the greatest
|
||||
greatest_objs = [co for co in comparison_objs if is_in_objs(co, greatest_objs_ids)]
|
||||
greatest_objs_weights = self._get_weights(greatest_objs)
|
||||
|
||||
# rearrange the weight to have the lowest equals to DEFAULT_COMPARISON_VALUE + 1
|
||||
highest_weight = max(default_weight, len(in_between_weights))
|
||||
for co_id in greatest_objs_weights:
|
||||
greatest_objs_weights[co_id] = greatest_objs_weights[co_id] + highest_weight
|
||||
for concept_id in greatest_objs_ids:
|
||||
if concept_id not in greatest_objs_weights:
|
||||
greatest_objs_weights[concept_id] = highest_weight + 1
|
||||
|
||||
return {**lesser_objs_weights, **in_between_weights, **greatest_objs_weights}
|
||||
|
||||
@staticmethod
|
||||
def _get_partition(weighted_concepts):
|
||||
|
||||
@@ -65,21 +128,52 @@ class SheerkaComparisonManager(BaseService):
|
||||
res.setdefault(v, []).append(k)
|
||||
return res
|
||||
|
||||
def _inner_add_comparison(self, comparison_obj):
|
||||
def _add_comparison(self, comparison_obj):
|
||||
key = self._compute_key(comparison_obj.property, comparison_obj.context)
|
||||
previous = self.sheerka.cache_manager.get(self.COMPARISON_ENTRY, key)
|
||||
|
||||
new = previous.copy() if previous else []
|
||||
|
||||
for co in new:
|
||||
if co.property == comparison_obj.property and \
|
||||
co.a == comparison_obj.a and \
|
||||
co.b == comparison_obj.b and \
|
||||
co.op == comparison_obj.op and \
|
||||
co.context == comparison_obj.context:
|
||||
return self.sheerka.ret(self.NAME, False, self.sheerka.new(BuiltinConcepts.ALREADY_DEFINED))
|
||||
|
||||
new.append(comparison_obj)
|
||||
|
||||
lesser_objs_ids = {co.a for co in new if co.op == "<<"}
|
||||
greatest_objs_ids = {co.a for co in new if co.op == ">>"}
|
||||
|
||||
# check if it is a valid operation regarding other lesser and greatest concepts
|
||||
if comparison_obj.op in ("<", ">"):
|
||||
a_is_lesser = comparison_obj.a in lesser_objs_ids
|
||||
b_is_lesser = comparison_obj.b in lesser_objs_ids
|
||||
if a_is_lesser != b_is_lesser: # XOR operation
|
||||
return self.sheerka.ret(self.NAME, False, self.sheerka.new(BuiltinConcepts.INVALID_LESSER_OPERATION))
|
||||
|
||||
a_is_greatest = comparison_obj.a in greatest_objs_ids
|
||||
b_is_greatest = comparison_obj.b in greatest_objs_ids
|
||||
if a_is_greatest != b_is_greatest: # XOR operation
|
||||
return self.sheerka.ret(self.NAME, False, self.sheerka.new(BuiltinConcepts.INVALID_GREATEST_OPERATION))
|
||||
|
||||
if comparison_obj.op == "<<" and comparison_obj.a in greatest_objs_ids:
|
||||
return self.sheerka.ret(self.NAME, False, self.sheerka.new(BuiltinConcepts.INVALID_GREATEST_OPERATION))
|
||||
|
||||
if comparison_obj.op == ">>" and comparison_obj.a in lesser_objs_ids:
|
||||
return self.sheerka.ret(self.NAME, False, self.sheerka.new(BuiltinConcepts.INVALID_LESSER_OPERATION))
|
||||
|
||||
cycles = self.detect_cycles(new)
|
||||
if cycles:
|
||||
concepts_in_cycle = [self.sheerka.get_by_id(c) for c in cycles]
|
||||
chicken_an_egg = self.sheerka.new(BuiltinConcepts.CHICKEN_AND_EGG, body=concepts_in_cycle)
|
||||
return self.sheerka.ret(self.NAME, False, chicken_an_egg)
|
||||
|
||||
self.sheerka.cache_manager.put(self.RESOLVED_COMPARISON_ENTRY, key, self._compute_weights(new))
|
||||
self.sheerka.cache_manager.put(self.COMPARISON_ENTRY, key, comparison_obj)
|
||||
self.sheerka.cache_manager.put(self.RESOLVED_COMPARISON_ENTRY, key, self._compute_weights(new,
|
||||
lesser_objs_ids,
|
||||
greatest_objs_ids))
|
||||
|
||||
return self.sheerka.ret(self.NAME, True, self.sheerka.new(BuiltinConcepts.SUCCESS))
|
||||
|
||||
@@ -92,6 +186,8 @@ class SheerkaComparisonManager(BaseService):
|
||||
|
||||
self.sheerka.bind_service_method(self.set_is_greater_than, True)
|
||||
self.sheerka.bind_service_method(self.set_is_less_than, True)
|
||||
self.sheerka.bind_service_method(self.set_is_lesser, True)
|
||||
self.sheerka.bind_service_method(self.set_is_greatest, True)
|
||||
self.sheerka.bind_service_method(self.get_partition, False)
|
||||
self.sheerka.bind_service_method(self.get_concepts_weights, False)
|
||||
|
||||
@@ -110,7 +206,7 @@ class SheerkaComparisonManager(BaseService):
|
||||
|
||||
event_digest = context.event.get_digest()
|
||||
comparison_obj = ComparisonObj(event_digest, prop_name, concept_a.id, concept_b.id, ">", comparison_context)
|
||||
return self._inner_add_comparison(comparison_obj)
|
||||
return self._add_comparison(comparison_obj)
|
||||
|
||||
def set_is_less_than(self, context, prop_name, concept_a, concept_b, comparison_context="#"):
|
||||
"""
|
||||
@@ -127,7 +223,66 @@ class SheerkaComparisonManager(BaseService):
|
||||
|
||||
event_digest = context.event.get_digest()
|
||||
comparison_obj = ComparisonObj(event_digest, prop_name, concept_a.id, concept_b.id, "<", comparison_context)
|
||||
return self._inner_add_comparison(comparison_obj)
|
||||
return self._add_comparison(comparison_obj)
|
||||
|
||||
def set_is_lesser(self, context, prop_name, concept, comparison_context="#"):
|
||||
"""
|
||||
Records that the concept is less than any other concept if no direct comparison is given
|
||||
|
||||
* A lesser concept has a weight smaller than any other concept that is not a lesser
|
||||
* When two concepts are lesser, you can compare (using set_is_less_than or set_is_greater_than)
|
||||
* If a concept is lesser, you cannot compare it with a non lesser concept
|
||||
* All lesser concepts that have no comparison directive are greater than the others (and share the same weight)
|
||||
:param context:
|
||||
:param prop_name:
|
||||
:param concept:
|
||||
:param comparison_context:
|
||||
:return:
|
||||
"""
|
||||
context.log(f"Setting concept {concept} is lesser", who=self.NAME)
|
||||
ensure_concept(concept)
|
||||
|
||||
event_digest = context.event.get_digest()
|
||||
comparison_obj = ComparisonObj(event_digest, prop_name, concept.id, None, "<<", comparison_context)
|
||||
return self._add_comparison(comparison_obj)
|
||||
|
||||
def set_is_greatest(self, context, prop_name, concept, comparison_context="#"):
|
||||
"""
|
||||
Records that the concept is greater than any other concept if no direct comparison is given
|
||||
|
||||
* A greatest concept has a weight bigger than any other concept that is not a greatest
|
||||
* When two concepts are greatest, you can compare them (using set_is_less_than or set_is_greater_than)
|
||||
* If a concept is greatest, you cannot compare it with a non greatest concept
|
||||
* All greatest concepts that have no comparison directive are less than the others (and share the same weight)
|
||||
:param context:
|
||||
:param prop_name:
|
||||
:param concept:
|
||||
:param comparison_context:
|
||||
:return:
|
||||
"""
|
||||
context.log(f"Setting concept {concept} is greatest", who=self.NAME)
|
||||
ensure_concept(concept)
|
||||
|
||||
event_digest = context.event.get_digest()
|
||||
comparison_obj = ComparisonObj(event_digest, prop_name, concept.id, None, ">>", comparison_context)
|
||||
return self._add_comparison(comparison_obj)
|
||||
|
||||
def set_are_equivalent(self, context, prop_name, concept_a, concept_b, comparison_context="#"):
|
||||
"""
|
||||
Records that two concepts have the same weight
|
||||
|
||||
* You cannot set the weight
|
||||
:param context:
|
||||
:param prop_name:
|
||||
:param concept_a:
|
||||
:param concept_b:
|
||||
:param comparison_context:
|
||||
:return:
|
||||
"""
|
||||
pass
|
||||
|
||||
def set_are_equiv(self, context, prop_name, concept_a, concept_b, comparison_context="#"):
|
||||
pass
|
||||
|
||||
def get_partition(self, prop_name, comparison_context="#"):
|
||||
"""
|
||||
@@ -141,21 +296,21 @@ class SheerkaComparisonManager(BaseService):
|
||||
return self._get_partition(weighted_concept)
|
||||
|
||||
def get_concepts_weights(self, prop_name, comparison_context="#"):
|
||||
weighted_concept = self.sheerka.cache_manager.get(
|
||||
weighted_concepts = self.sheerka.cache_manager.get(
|
||||
self.RESOLVED_COMPARISON_ENTRY,
|
||||
self._compute_key(prop_name, comparison_context))
|
||||
|
||||
if weighted_concept is None:
|
||||
if weighted_concepts is None:
|
||||
key = self._compute_key(prop_name, comparison_context)
|
||||
entries = self.sheerka.cache_manager.get(self.COMPARISON_ENTRY, key)
|
||||
|
||||
if entries is None:
|
||||
return {}
|
||||
else:
|
||||
weighted_concept = self._compute_weights(entries)
|
||||
self.sheerka.cache_manager.put(self.RESOLVED_COMPARISON_ENTRY, key, weighted_concept)
|
||||
weighted_concepts = self._compute_weights(entries)
|
||||
self.sheerka.cache_manager.put(self.RESOLVED_COMPARISON_ENTRY, key, weighted_concepts)
|
||||
|
||||
return weighted_concept
|
||||
return weighted_concepts
|
||||
|
||||
@staticmethod
|
||||
def detect_cycles(comparison_objs):
|
||||
|
||||
@@ -45,7 +45,7 @@ class SheerkaCreateNewConcept(BaseService):
|
||||
return sheerka.ret(
|
||||
self.NAME,
|
||||
False,
|
||||
sheerka.new(BuiltinConcepts.CONCEPT_ALREADY_DEFINED, body=concept),
|
||||
sheerka.new(BuiltinConcepts.ALREADY_DEFINED, body=concept),
|
||||
error.args[0])
|
||||
|
||||
# set id before saving in db
|
||||
|
||||
@@ -34,7 +34,7 @@ class SheerkaModifyConcept(BaseService):
|
||||
return self.sheerka.ret(
|
||||
self.NAME, False,
|
||||
self.sheerka.new(
|
||||
BuiltinConcepts.CONCEPT_ALREADY_DEFINED,
|
||||
BuiltinConcepts.ALREADY_DEFINED,
|
||||
body=concept))
|
||||
|
||||
old_references = self.sheerka.cache_manager.get(self.sheerka.CONCEPTS_REFERENCES_ENTRY, concept.id)
|
||||
|
||||
@@ -37,6 +37,8 @@ class Expando:
|
||||
for k, v in bag.items():
|
||||
setattr(self, k, v)
|
||||
|
||||
def __repr__(self):
|
||||
return f"{dir(self)}"
|
||||
|
||||
@dataclass
|
||||
class PythonEvalError:
|
||||
|
||||
@@ -6,6 +6,7 @@ from typing import List
|
||||
from core import builtin_helpers
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept, DEFINITION_TYPE_BNF
|
||||
from core.sheerka.services.SheerkaComparisonManager import SheerkaComparisonManager
|
||||
from core.sheerka.services.SheerkaExecute import ParserInput
|
||||
from core.tokenizer import Token, TokenKind, Tokenizer
|
||||
from parsers.BaseNodeParser import UnrecognizedTokensNode, ConceptNode, SourceCodeNode, SyaAssociativity, \
|
||||
@@ -77,7 +78,7 @@ class SyaConceptDef:
|
||||
It gives the precedence and the associativity for the concept
|
||||
"""
|
||||
concept: Concept
|
||||
precedence: int = 0
|
||||
precedence: int = SheerkaComparisonManager.DEFAULT_COMPARISON_VALUE
|
||||
associativity: SyaAssociativity = SyaAssociativity.Right
|
||||
|
||||
|
||||
@@ -541,11 +542,6 @@ class InFixToPostFix:
|
||||
if stack.associativity == SyaAssociativity.No and current.associativity == SyaAssociativity.No:
|
||||
self._add_error(NoneAssociativeSequenceErrorNode(current.concept, stack_head.start, concept_node.start))
|
||||
|
||||
if not current.precedence:
|
||||
# precedence is not set (None or zero)
|
||||
# Do not apply any rule
|
||||
return False
|
||||
|
||||
if current.associativity == SyaAssociativity.Left and current.precedence <= stack.precedence:
|
||||
return True
|
||||
|
||||
@@ -947,6 +943,7 @@ class SyaNodeParser(BaseNodeParser):
|
||||
def _get_sya_concept_def(parser, concept):
|
||||
sya_concept_def = SyaConceptDef(concept)
|
||||
if concept.id in parser.sya_definitions:
|
||||
# Manage when precedence and associativity are given in the unit tests
|
||||
sya_def = parser.sya_definitions.get(concept.id)
|
||||
if sya_def[0] is not None:
|
||||
sya_concept_def.precedence = sya_def[0]
|
||||
|
||||
Reference in New Issue
Block a user