Skip to content

Commit

Permalink
added support for dateliteralExp and reject construct
Browse files Browse the repository at this point in the history
  • Loading branch information
FitashUlHaq committed Dec 16, 2024
1 parent 0f82302 commit 070f287
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 20 deletions.
1 change: 1 addition & 0 deletions bocl/OCLWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ def evaluate(self,ocl):
evalualor = Evaluator()

return evalualor.evaluate(root_handler, self.om)
# return True
35 changes: 35 additions & 0 deletions bocl/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def get_value(self, name, obj):
if name == slot.attribute.name:
if slot.attribute.type.name == 'str':
return '"' + slot.value.value + '"'
if slot.attribute.type.name == 'date':
return int(str(slot.value.value.year)+ str(slot.value.value.month) + str(slot.value.value.day))
return slot.value.value
return None

Expand Down Expand Up @@ -187,6 +189,22 @@ def handle_select(self, tree, all_objs):
self.preprocess_logical_exp(expression)
if eval(expression[0]) is True:
self.all_obj_sat[-1].append(obj)
def handle_reject(self, tree, all_objs):
"""The handle_reject function handles reject construct.
Args:
tree: Tree that is constructed using OCL Parser
all_objs: all objects from object model
"""
self.all_obj_sat.append([])
for obj in all_objs:
expression = [""]
if len(tree.get_body) > 0:
self.update_logical_exp(tree.get_body[0], expression, obj)
self.preprocess_logical_exp(expression)
if eval(expression[0]) is False:
self.all_obj_sat[-1].append(obj)


def handle_collect(self, tree, all_objs):
"""The handle_collect function handles collect construct.
Expand Down Expand Up @@ -231,6 +249,9 @@ def verify_body(self, tree, obj, logical_exp, source, all_objs = None):
self.handle_collect(tree, all_objs)
elif expression_type == "select":
self.handle_select(tree, all_objs)
elif expression_type == "reject":
self.handle_reject(tree, all_objs)



def get_id(self, slots):
Expand Down Expand Up @@ -342,6 +363,17 @@ def handle_ocl_is_type_of(self, tree, logical_exp):
logical_exp[0] = logical_exp[0] + '\"' + source_type + '\"' + " = \"float\""
if index > 0 and index < len(tree.arguments) - 1:
self.check_and_add(logical_exp, "and")
def handle_date_literal_expression(self,date):
from datetime import datetime,timedelta
now = datetime.now()
if "addDays" in str(date):
days = str(date).split("addDays")[1].replace(")","").replace("(","")
now= now+timedelta(days = int(days))

if "today" in str(date):
date_time = int(now.strftime("%Y%m%d"))
return str(date_time)


def update_logical_exp(self, tree, logical_exp, obj):
"""The update_logical_exp function updates the logical expression.
Expand Down Expand Up @@ -383,6 +415,9 @@ def update_logical_exp(self, tree, logical_exp, obj):
logical_exp[0] = logical_exp[0] + str(arg.value)
elif isinstance(arg, StringLiteralExpression):
logical_exp[0] = logical_exp[0] + '"' + str(arg.value) + '"'
elif isinstance(arg, DateLiteralExpression):
logical_exp[0] = logical_exp[0] + self.handle_date_literal_expression(arg)

else:
logical_exp[0] = logical_exp[0] + str(self.get_value(arg.name, obj))
else:
Expand Down
3 changes: 2 additions & 1 deletion docs/source/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Release Notes

.. toctree::
releases/v0.1.0
releases/v0.2.0
releases/v0.2.0
releases/v0.3.0
2 changes: 1 addition & 1 deletion docs/source/releases/v0.2.0.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 0.1.0
Version 0.2.0
=============

Release Notes
Expand Down
7 changes: 7 additions & 0 deletions docs/source/releases/v0.3.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Version 0.3.0
=============

Release Notes
------------
* added support for DateLiteralExpression
* added support for Reject construct
38 changes: 21 additions & 17 deletions models/library_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,32 @@

constraintNameOCLIsTypeOfIntFalse: Constraint = Constraint(name ="BookIsTypeOfIntFalse", context=book, expression="context Book inv inv3: self.pages.oclIsTypeOf(String)", language="OCL")
constraintifElseSize: Constraint = Constraint(name ="constraintifElseSize", context=library, expression="context Library inv inv3: if self.name <> 'NI' then self.has->exists( i_book : Book | i_book.pages <= 110 )->size()<3 else self.has->forAll(b:Book|b.pages>0) endif", language="OCL")
constraintDate: Constraint = Constraint(name ="constraintDate", context=book, expression="context Book inv inv3: self.release < Date::today().addDays(10)", language="OCL")

constraintLibraryReject: Constraint = Constraint(name = "LibaryReject",context=library,
expression="context Library inv inv3: self.has->reject(i_book : Book | i_book.pages <= 110)"
"->size()>0",language="OCL")

# Domain model definition
library_model : DomainModel = DomainModel(name="Library model", types={library, book, author},
associations={lib_book_association, book_author_association},
constraints={
constraintBookPageNumber,
# constraintTitleIncorrect,
constraintPageNumber,
constraintTitleCorrect,
constraintLibraryExists,
constraintLibrarySize,
constraintLibraryCollect,
constraintLibraryIf,
constraintLibraryElse,
constraintDate,
constraintLibraryReject,
constraintBookPageNumber,
# constraintTitleIncorrect,
constraintPageNumber,
constraintTitleCorrect,
constraintLibraryExists,
constraintLibrarySize,
constraintLibraryCollect,
constraintLibraryIf,
constraintLibraryElse,
# constraintLibraryElseFalse,
constraintNameOCLIsTypeOf,
constraintNameOCLIsTypeOfInt,
# constraintNameOCLIsTypeOfIntFalse
constraintNameOCLIsTypeOf,
constraintNameOCLIsTypeOfInt,
# constraintNameOCLIsTypeOfIntFalse
}

# constraints={constraintLibrarySize}
)


Expand All @@ -116,18 +120,18 @@

# Book object attributes
book_obj_name: AttributeLink = AttributeLink(attribute=title, value=DataValue(classifier=t_str, value="Book tittle"))
book_obj_pages: AttributeLink = AttributeLink(attribute=pages, value=DataValue(classifier=t_int, value=100))
book_obj_pages: AttributeLink = AttributeLink(attribute=pages, value=DataValue(classifier=t_int, value=200))
book_obj_release: AttributeLink = AttributeLink(attribute=release, value=DataValue(classifier=t_date, value=datetime.datetime(2020, 3, 15)))
# Book object
book_obj: Object = Object(name="Book Object", classifier=book, slots=[book_obj_name, book_obj_pages])
book_obj: Object = Object(name="Book Object", classifier=book, slots=[book_obj_name, book_obj_pages,book_obj_release])

# Book_2 object attributes

book_obj_name_2: AttributeLink = AttributeLink(attribute=title, value=DataValue(classifier=t_str, value="Book tittle_2"))
book_obj_pages_2: AttributeLink = AttributeLink(attribute=pages, value=DataValue(classifier=t_int, value=400))
book_obj_release_2: AttributeLink = AttributeLink(attribute=release, value=DataValue(classifier=t_date, value=datetime.datetime(2024, 3, 15)))
# Book object
book_obj_2: Object = Object(name="Book 2 Object", classifier=book, slots=[book_obj_name_2, book_obj_pages_2])
book_obj_2: Object = Object(name="Book 2 Object", classifier=book, slots=[book_obj_name_2, book_obj_pages_2,book_obj_release_2])

# Author object attributes
author_obj_name: AttributeLink = AttributeLink(attribute=author_name, value=DataValue(classifier=t_str, value="John Doe"))
Expand Down
6 changes: 5 additions & 1 deletion models/team_player_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@

constraintTeamCenter: Constraint = Constraint(name = "teamCenter",context=team,expression="context team inv inv2: self.many -> collect(p:player| p.position = 'center')->size()<3",language="OCL")

constraintTeamOtherPlayers: Constraint = Constraint(name = "teamCenterPlayer",context=team,expression="context team inv inv2: self.many -> select(p:player| p.position = 'center')->size()>0",language="OCL")


# Domain model definition
team_player_model : DomainModel = DomainModel(name="Team-Player model", types={team,player},
associations={team_player_association },
constraints={
constraintPlayerAge,
constraintTeamCenter
constraintTeamCenter,
constraintTeamOtherPlayers,
}
)

Expand Down
1 change: 1 addition & 0 deletions test/ocl_interpreter_test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@ def test_10():
res = None
assert(res==True)


0 comments on commit 070f287

Please sign in to comment.