Skip to content

Commit 022839f

Browse files
authored
feat: adding resolving of csm pkl module (#42)
1 parent 5062379 commit 022839f

File tree

11 files changed

+71
-18
lines changed

11 files changed

+71
-18
lines changed

src/main/java/at/ac/uibk/dps/cirrina/cirrina/Runtime.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Runtime(
6666
val collaborativeStateMachineClass =
6767
CollaborativeStateMachineClassBuilder.from(
6868
// A main.pkl file is required in the CSML project
69-
CsmlParser.parse(Path(path, "main.pkl").pathString)
69+
CsmlParser.parse(Path(path).pathString)
7070
)
7171
.build()
7272

src/main/java/at/ac/uibk/dps/cirrina/io/description/CsmlParser.kt

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
11
package at.ac.uibk.dps.cirrina.io.description
22

33
import at.ac.uibk.dps.cirrina.csml.description.Csml
4-
import org.pkl.config.java.ConfigEvaluator
4+
import java.net.URI
5+
import java.util.*
6+
import java.util.regex.Pattern
7+
import org.pkl.config.java.ConfigEvaluatorBuilder
58
import org.pkl.core.ModuleSource
9+
import org.pkl.core.SecurityManager
10+
import org.pkl.core.module.ModuleKey
11+
import org.pkl.core.module.ModuleKeyFactory
12+
import org.pkl.core.module.ResolvedModuleKey
13+
import org.pkl.core.util.IoUtils
614

7-
/** CsmlParser, used to parse a Pkl file into a Csml object. */
15+
/** CsmModuleKey, used to load CSM modules from the classpath. */
16+
class CsmModuleKey(private val uri: URI) : ModuleKey, ResolvedModuleKey {
17+
18+
/** Get the original module key. */
19+
override fun getOriginal(): ModuleKey = this
20+
21+
/** Get the URI of the module. */
22+
override fun getUri(): URI = uri
23+
24+
/** Load the source code of the module. */
25+
override fun loadSource(): String =
26+
IoUtils.readClassPathResourceAsString(javaClass, "/pkl/csm/${uri.schemeSpecificPart}.pkl")
27+
28+
/** Resolve the module. */
29+
override fun resolve(securityManager: SecurityManager): ResolvedModuleKey {
30+
securityManager.checkResolveModule(uri)
31+
return this
32+
}
33+
34+
/** Csm modules are always hierarchical. */
35+
override fun hasHierarchicalUris(): Boolean = true
36+
37+
/** Csm modules cannot be globbed. */
38+
override fun isGlobbable(): Boolean = false
39+
}
40+
41+
/** Factory for CsmModuleKey, converts csm: modules to CsmModuleKey. */
42+
class CsmModuleKeyFactory : ModuleKeyFactory {
43+
/** Converts a URI into a CsmModuleKey if the URI scheme is csm. */
44+
override fun create(uri: URI): Optional<ModuleKey> =
45+
Optional.ofNullable(
46+
uri.takeIf { it.scheme.equals("csm", ignoreCase = true) }?.let { CsmModuleKey(it) }
47+
)
48+
}
49+
50+
/** Csml parser, used to parse a Pkl file into a Csml object. */
851
object CsmlParser {
952
/**
1053
* Parse a Pkl module at a path. Returns an instance of Csml upon success. Any errors will result
@@ -16,7 +59,17 @@ object CsmlParser {
1659
*/
1760
fun parse(mainModulePath: String): Csml {
1861
try {
19-
ConfigEvaluator.preconfigured().use { evaluator ->
62+
// Create a preconfigured ConfigEvaluatorBuilder
63+
val builder = ConfigEvaluatorBuilder.preconfigured()
64+
65+
// Mark the CSM module as an allowed module
66+
builder.allowedModules.add(Pattern.compile("csm:.*"))
67+
68+
// Add the CsmModuleKeyFactory to the builder
69+
builder.evaluatorBuilder.addModuleKeyFactory(CsmModuleKeyFactory())
70+
71+
// Build and load the main module
72+
builder.build().use { evaluator ->
2073
return evaluator.evaluate(ModuleSource.file(mainModulePath)).`as`(Csml::class.java)
2174
}
2275
} catch (e: Exception) {

src/test/java/at/ac/uibk/dps/cirrina/classes/statemachine/StateMachineClassTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class StateMachineClassTest {
2121
assertDoesNotThrow {
2222
stateMachineClass =
2323
CollaborativeStateMachineClassBuilder.from(
24-
CsmlParser.parse(Path(DefaultDescriptions.complete, "main.pkl").pathString)
24+
CsmlParser.parse(Path(DefaultDescriptions.complete).pathString)
2525
)
2626
.build()
2727
.findStateMachineClassByName("stateMachine1")
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package at.ac.uibk.dps.cirrina.data
22

33
object DefaultDescriptions {
4-
const val complete: String = "src/test/resources/pkl/complete"
4+
const val complete: String = "src/test/resources/pkl/complete/main.pkl"
55

6-
const val invoke: String = "src/test/resources/pkl/invoke"
6+
const val invoke: String = "src/test/resources/pkl/invoke/main.pkl"
77

8-
const val timeout: String = "src/test/resources/pkl/timeout"
8+
const val timeout: String = "src/test/resources/pkl/timeout/main.pkl"
99

10-
const val pingPong: String = "src/test/resources/pkl/pingPong"
10+
const val pingPong: String = "src/test/resources/pkl/pingPong/main.pkl"
1111
}

src/test/resources/pkl/complete/main.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
amends "modulepath:/pkl/csm/Csml.pkl"
1+
amends "csm:Csml"
22
import "stateMachineOne.pkl"
33

44
name = "complete"

src/test/resources/pkl/complete/stateMachineOne.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import "modulepath:/pkl/csm/Csml.pkl"
1+
import "csm:Csml"
22

33
// State Machine One
44
// ┌─────────────────────────────────────────────────────────────────────────────────────────────────────┐

src/test/resources/pkl/invoke/main.pkl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
amends "modulepath:/pkl/csm/Csml.pkl"
2-
import "modulepath:/pkl/csm/Csml.pkl"
1+
amends "csm:Csml"
2+
import "csm:Csml"
33

44
name = "invoke"
55
version = "3.0.0"

src/test/resources/pkl/pingPong/main.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
amends "modulepath:/pkl/csm/Csml.pkl"
1+
amends "csm:Csml"
22
import "stateMachineOne.pkl"
33
import "stateMachineTwo.pkl"
44

src/test/resources/pkl/pingPong/stateMachineOne.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import "modulepath:/pkl/csm/Csml.pkl"
1+
import "csm:Csml"
22

33
// State Machine One
44
// ┌──────────────────────────────────────────┐

src/test/resources/pkl/pingPong/stateMachineTwo.pkl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import "modulepath:/pkl/csm/Csml.pkl"
1+
import "csm:Csml"
22

33
// State Machine Two
44
// ┌──────────────────────────────────────────┐

0 commit comments

Comments
 (0)