-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
81 lines (66 loc) · 3.16 KB
/
Copy pathserver.py
File metadata and controls
81 lines (66 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from concurrent import futures
import logging
import time
import grpc
import unified_planning
from unified_planning.engines.results import POSITIVE_OUTCOMES
from unified_planning.shortcuts import Problem, OneshotPlanner
from unified_planning.grpc.proto_reader import ProtobufReader
from unified_planning.grpc.proto_writer import ProtobufWriter
import unified_planning.grpc.generated.unified_planning_pb2 as up_pb2
import unified_planning.grpc.generated.unified_planning_pb2_grpc as up_pb2_grpc
from unified_planning.shortcuts import *
class UnifiedPlanningServer(up_pb2_grpc.UnifiedPlanningServicer):
def __init__(self, port):
self.port = port
self.log_format = (
'[%(asctime)s] %(levelname)-8s %(name)-12s %(message)s')
self.logger = logging.getLogger("Unified Planning Service")
logging.basicConfig(level=logging.INFO, format=self.log_format)
self.reader = ProtobufReader()
self.writer = ProtobufWriter()
def planAnytime(self, request, context):
problem = self.reader.convert(request.problem)
with AnytimePlanner(problem_kind=problem.kind, anytime_guarantee="INCREASING_QUALITY") as planner:
for p in planner.get_solutions(problem):
next_result = self.writer.convert(p)
yield next_result
def planOneShot(self, request, context):
problem = self.reader.convert(request.problem)
with OneshotPlanner(problem_kind=problem.kind) as planner:
result = planner.solve(problem)
if result.status in unified_planning.engines.results.POSITIVE_OUTCOMES:
self.logger.info(f"{planner.name} found this plan: {result.plan}")
else:
self.logger.info("No plan found.")
answer = self.writer.convert(result)
return answer
def validatePlan(self, request, context):
problem = self.reader.convert(request.problem)
plan = self.reader.convert(request.plan)
with PlanValidator(problem_kind=problem.kind, plan_kind=plan.kind) as validator:
validation_result = validator.validate(problem, plan)
answer = self.writer.convert(validation_result)
return answer
def compile(self, request, context):
problem = self.reader.convert(request)
with Compiler(
problem_kind=problem.kind,
compilation_kinds=[
# TODO: This should be supplied with request
CompilationKind.CONDITIONAL_EFFECTS_REMOVING,
CompilationKind.NEGATIVE_CONDITIONS_REMOVING
]) as compiler:
result = compiler.compile(problem)
answer = self.writer.convert(result)
return answer
def start(self):
connection = '0.0.0.0:%d' % (self.port)
self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
up_pb2_grpc.add_UnifiedPlanningServicer_to_server(
self, self.server)
self.server.add_insecure_port(connection)
self.server.start()
self.logger.info("server started on %s" % connection)
def wait_for_termination(self):
self.server.wait_for_termination()