Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions puan/logic/plog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,14 +1582,55 @@ class Equal(All):
Examples
--------
Meaning exactly two of x, y and z.
>>> Equal(2, list("xyz"), variable='A')
A: +(VAR658adc74c6913fb83b42c3968866f4bdb967fcad34692fc71d3de5f9a94b6970,VARe4568f4a0e4e55b7e3afa57b3527153272b53fde10f6e292b510a5c3bf797d3d)>=2
>>> Equal(2, list("xyz"), variable='A').propositions
[A: +(x,y,z)>=2, A: -(x,y,z)>=-2]
[VAR658adc74c6913fb83b42c3968866f4bdb967fcad34692fc71d3de5f9a94b6970: -(x,y,z)>=-2, VARe4568f4a0e4e55b7e3afa57b3527153272b53fde10f6e292b510a5c3bf797d3d: +(x,y,z)>=2]
"""

Copy link
Copy Markdown
Collaborator

@ourmarina ourmarina Dec 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to add here that you added the methods to/from json?

    Methods
    -------
    from_json
    to_json

And: Do you need the method from_list?

def __init__(self, value: int, propositions: typing.List[typing.Union[str, puan.variable]], variable: typing.Union[str, puan.variable] = None):
super().__init__(
AtLeast(value=value, propositions=propositions, variable=variable),
AtMost(value=value, propositions=propositions, variable=variable))
AtLeast(value=value, propositions=propositions),
AtMost(value=value, propositions=propositions), variable=variable)

@staticmethod
def from_json(data: dict, class_map) -> "Equal":
"""
Convert from JSON data to a proposition.

Returns
-------
out : :class:`Equal`
"""
propositions = data.get('propositions', [])
return Equal(
value=data.get('value', 1),
propositions=list(map(functools.partial(from_json, class_map=class_map), propositions)),
variable=data.get('id', None)
)

def to_json(self) -> typing.Dict[str, typing.Any]:

"""
Returns proposition as a readable JSON.

Returns
-------
out : Dict[str, Any]
"""
d = {
'type': self.__class__.__name__,
'value': self.propositions[0].sign*self.propositions[0].value,
'propositions': list(
map(
operator.methodcaller("to_json"),
self.propositions[0].propositions
)
) if len(self.propositions) > 0 else [],
}
if not self.generated_id:
d['id'] = self.id
return d


class Any(AtLeast):
Expand Down Expand Up @@ -2054,7 +2095,7 @@ def to_json(self) -> typing.Dict[str, typing.Any]:
d['id'] = self.id
return d

def from_json(data: dict, class_map: list = [puan.variable,AtLeast,AtMost,All,Any,Xor,ExactlyOne,Not,XNor,Imply]) -> typing.Any:
def from_json(data: dict, class_map: list = [puan.variable,AtLeast,AtMost,Equal,All,Any,Xor,ExactlyOne,Not,XNor,Imply]) -> typing.Any:

"""
Convert from json data to a proposition.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_puan.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,9 @@ def test_json_conversion():

json_model = {"type": "AtMost", "value": 1, "propositions": [{"id": "x", "bounds": {"lower": -10, "upper": 10}}]}
assert json_model == pg.AtMost.from_json(json_model, [puan.variable]).to_json()

json_model = {"type": "Equal", "value": 1, "id": "A", "propositions": [{"id": "x", "bounds": {"lower": -10, "upper": 10}}]}
assert pg.from_json(json_model).to_json() == pg.Equal.from_json(json_model, [puan.variable]).to_json()
Copy link
Copy Markdown
Collaborator

@ourmarina ourmarina Dec 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is json_model not a json to begin with? Can this not be the same as line 1322 assert json_model == ...?




Expand Down