-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPlanningTestUtil.kt
98 lines (89 loc) · 2.95 KB
/
PlanningTestUtil.kt
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.softbankrobotics.pddlplanning.test
import android.content.Context
import com.softbankrobotics.pddlplanning.*
import com.softbankrobotics.pddlplanning.utils.searchPlanForInit
import kotlinx.coroutines.runBlocking
import org.junit.Assert
/**
* Checks whether the right plan is found for a given init in a problem.
* Note that it replaces the init of the problem.
*/
suspend fun checkPlanForInit(
domain: String,
problem: String,
init: Collection<Expression>,
searchPlan: PlanSearchFunction,
logFunction: LogFunction,
expectedPlan: Tasks
) {
val plan = searchPlanForInit(domain, problem, init, searchPlan, logFunction)
Assert.assertEquals(expectedPlan, plan)
}
/**
* Checks whether the right plans are found for given inits in a problem.
* Note that it replaces the init of the problem all along.
* The problem ends up with the last init.
*/
suspend fun checkPlansForInits(
domain: String, problem: String,
initsToExpectedPlans: ExpressionsToTasks,
searchPlan: PlanSearchFunction,
logFunction: LogFunction
) {
initsToExpectedPlans.forEach { initToExpectedPlan ->
checkPlanForInit(
domain,
problem,
initToExpectedPlan.first,
searchPlan,
logFunction,
initToExpectedPlan.second
)
}
}
/**
* Loads a domain & problem from a raw resources, and runs a planning function on it.
* The result plan is printed to the debug output.
*/
fun searchPlanAndPrint(
context: Context,
resourceName: String,
searchPlan: PlanSearchFunction,
logFunction: LogFunction
) {
val pddl = stringFromRawResourceName(context, resourceName)
val (domain, problem) = splitDomainAndProblem(pddl)
val plan = runBlocking { searchPlan(domain, problem, logFunction) }
logFunction("Plan: $plan")
}
/**
* Loads a domain & problem from a raw resources, and runs a planning function on it.
* The result plan is returned.
*/
fun searchPlanFromResource(
context: Context,
resource: Int,
searchPlan: PlanSearchFunction,
logFunction: LogFunction
): List<Task> {
val pddl = stringFromRawResource(context, resource)
val (domain, problem) = splitDomainAndProblem(pddl)
return runBlocking { searchPlan(domain, problem, logFunction) }
}
/**
* Reads PDDL domain and problem from a raw resource, given its identifier.
*/
fun domainAndProblemFromRaw(context: Context, id: Int): Pair<String, String> {
val pddl = stringFromRawResource(context, id)
println("Using base PDDL: $pddl")
return splitDomainAndProblem(pddl)
}
fun stringFromRawResource(context: Context, id: Int): String {
val input = context.resources.openRawResource(id)
return String(input.readBytes(), Charsets.UTF_8)
}
fun stringFromRawResourceName(context: Context, resourceName: String): String {
val resourceId =
context.resources.getIdentifier(resourceName, "raw", context.packageName)
return stringFromRawResource(context, resourceId)
}