Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary work for JSON import/export (Sourcery refactored) #103

Merged
merged 1 commit into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion processscheduler/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def add_constraint(self, constraint: Constraint) -> None:
"""Add a constraint to the problem. A constraint can be either
a z3 assertion or a processscheduler Constraint instance."""
if isinstance(constraint, Constraint):
if not constraint in self.constraints:
if constraint not in self.constraints:
Copy link
Author

Choose a reason for hiding this comment

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

Function SchedulingContext.add_constraint refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

self.constraints.append(constraint)
else:
raise AssertionError("constraint already added to the problem.")
Expand Down
47 changes: 24 additions & 23 deletions processscheduler/json_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ def export_json_to_file(scheduling_problem, scheduling_solver, json_filename):
def export_json_to_string(scheduling_problem, scheduling_solver) -> str:
d = {}
# SchedulingProblem general properties
problem_properties = {}
problem_properties["name"] = scheduling_problem.name
problem_properties["horizon"] = scheduling_problem.horizon_defined_value
problem_properties["delta_time"] = scheduling_problem.delta_time
problem_properties["start_time"] = scheduling_problem.start_time
problem_properties["end_time"] = scheduling_problem.end_time
problem_properties = {
'name': scheduling_problem.name,
'horizon': scheduling_problem.horizon_defined_value,
'delta_time': scheduling_problem.delta_time,
'start_time': scheduling_problem.start_time,
'end_time': scheduling_problem.end_time,
}

d["ProblemParameters"] = problem_properties
# Tasks
tasks = {}
for task in scheduling_problem.context.tasks:
new_task_entry = {}
new_task_entry["type"] = type(task).__name__
new_task_entry["optional"] = task.optional
new_task_entry = {'type': type(task).__name__, 'optional': task.optional}
Comment on lines -73 to +85
Copy link
Author

Choose a reason for hiding this comment

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

Function export_json_to_string refactored with the following changes:

  • Merge dictionary assignment with declaration (merge-dict-assign)
  • Simplify logical expression using De Morgan identities (de-morgan)

if isinstance(task, ps.FixedDurationTask):
new_task_entry["duration"] = task.duration_defined_value
if isinstance(task, (ps.FixedDurationTask, ps.VariableDurationTask)):
Expand All @@ -101,30 +101,32 @@ def export_json_to_string(scheduling_problem, scheduling_solver) -> str:
all_workers_but_cumulative = [
res
for res in scheduling_problem.context.resources
if not "CumulativeWorker_" in res.name
if "CumulativeWorker_" not in res.name
]

for resource in all_workers_but_cumulative: # Worker
new_resource_entry = {}
new_resource_entry["productivity"] = resource.productivity
new_resource_entry["cost"] = resource.cost
new_resource_entry = {
'productivity': resource.productivity,
'cost': resource.cost,
}

workers[resource.name] = new_resource_entry
resources["Workers"] = workers
# SelectWorkers
select_workers = []
for sw in scheduling_problem.context.select_workers: # Worker
new_sw = {}
new_sw["list_of_workers"] = [w.name for w in sw.list_of_workers]
new_sw["nb_workers_to_select"] = sw.nb_workers_to_select
new_sw["kind"] = sw.kind
new_sw = {
'list_of_workers': [w.name for w in sw.list_of_workers],
'nb_workers_to_select': sw.nb_workers_to_select,
'kind': sw.kind,
}

select_workers.append(new_sw)
resources["SelectWorkers"] = select_workers
# CumulativeWorker
cumulative_workers = {}
for cw in scheduling_problem.context.cumulative_workers: # Worker
new_cw = {}
new_cw["size"] = cw.size
new_cw["productivity"] = cw.productivity
new_cw["cost"] = cw.cost
new_cw = {'size': cw.size, 'productivity': cw.productivity, 'cost': cw.cost}
cumulative_workers[cw.name] = new_cw
resources["CumulativeWorkers"] = cumulative_workers
d["Resources"] = resources
Expand All @@ -133,8 +135,7 @@ def export_json_to_string(scheduling_problem, scheduling_solver) -> str:
#
constraints = []
for constraint in scheduling_problem.context.constraints:
new_cstr = {}
new_cstr["type"] = type(constraint).__name__
new_cstr = {'type': type(constraint).__name__}
constraints.append(new_cstr)
d["Constraints"] = constraints

Expand Down