Skip to content

Commit

Permalink
Add "one", "any" and "exists"
Browse files Browse the repository at this point in the history
  • Loading branch information
aranega committed Sep 18, 2024
1 parent 2b71ea5 commit 2a5382e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
29 changes: 29 additions & 0 deletions pyecoreocl/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,28 @@ def rule_for_all(emitter, ctx):
emitter.inline(")")


@call_rule
def rule_exists(emitter, ctx):
emitter.inline(f"any(")
emitter.visit(ctx.argExp().oclExp())
variables = [arg.text for arg in ctx.argExp().varnames]
emitter.inline(f" for {', '.join(variables)} in ")
if len(variables) > 1:
emitter.inline("itertools.combinations_with_replacement(")
emitter.visit(ctx.expression)
emitter.inline(f", {len(variables)})")
else:
emitter.visit(ctx.expression)
emitter.inline(")")


@call_rule
def rule_one(emitter, ctx):
emitter.inline("(len(list(")
rule_select(emitter, ctx)
emitter.inline(")) == 1)")


@call_rule
def rule_select(emitter, ctx):
variables = [arg.text for arg in ctx.argExp().varnames]
Expand Down Expand Up @@ -467,6 +489,13 @@ def rule_as_bag(emitter, ctx):
emitter.inline(")")


@call_rule
def rule_any(emitter, ctx):
emitter.inline("next(iter(")
emitter.visit(ctx.expression)
emitter.inline("), None)")


def default_collection_call(emitter, ctx):
operation = ctx.attname.text
emitter.visit(ctx.expression)
Expand Down
23 changes: 22 additions & 1 deletion tests/test_collection_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,25 @@ def test__collect():
assert !l->collect(e: int | e + 1)->asSequence()! == [4, 5, 6]

l = !Sequence{Tuple{value=3}, Tuple{value=4}, Tuple{value='stuff'}}!
assert !l->collect(e | e.value)->asSequence()! == [3, 4, "stuff"]
assert !l->collect(e | e.value)->asSequence()! == [3, 4, "stuff"]


def test__any():
l = [3, 4, 5]
assert !l->any()! == 3

assert !Bag{3, 4, 5}->collect(e | e + 1)->any()! == 4


def test__exists():
l = [3, 4, 5]
assert !l->exists(e | e = 3)! is True

assert !Bag{3, 4, 5}->collect(e | e + 1)->exists(e | e = 3)! is False


def test__one():
l = [3, 4, 5]
assert !l->one(e | e = 3)! is True

assert !Bag{3, 4, 5}->collect(e | e + 1)->one(e | e = 3)! is False
4 changes: 4 additions & 0 deletions tests/test_object_ptypes_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# coding: magic_ocl.strict

def test__trigger():
assert !true implies false! is False

0 comments on commit 2a5382e

Please sign in to comment.