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

mediapipe_arcs converts HandleProtos from mediapipe graphs. #7156

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions java/arcs/core/data/proto/HandleProtoDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ fun HandleProto.Fate.decode() = when (this) {
throw IllegalArgumentException("Invalid HandleProto.Fate value.")
}

/** Converts [Handle.Fate] into [HandleProto.Fate] enum. */
fun Handle.Fate.encode(): HandleProto.Fate = when (this) {
Handle.Fate.CREATE -> HandleProto.Fate.CREATE
Handle.Fate.USE -> HandleProto.Fate.USE
Handle.Fate.MAP -> HandleProto.Fate.MAP
Handle.Fate.COPY -> HandleProto.Fate.COPY
Handle.Fate.JOIN -> HandleProto.Fate.JOIN
}

/**
* Converts [HandleProto] into [Handle].
*
Expand All @@ -43,3 +52,19 @@ fun HandleProto.decode(knownHandles: Map<String, Handle> = emptyMap()) = Handle(
annotations = annotationsList.map { it.decode() },
associatedHandles = associatedHandlesList.map { requireNotNull(knownHandles[it]) }
)

/** Converts a [Handle] to a [HandleProto]. */
fun Handle.encode(): HandleProto {
val builder = HandleProto.newBuilder()
.setName(name)
.setId(id)
.setFate(fate.encode())
.addAllTags(tags)
.setType(type.encode())
.addAllAnnotations(annotations.map { it.encode() })
.addAllAssociatedHandles(associatedHandles.map { it.name })

storageKey?.let { builder.setStorageKey(it) }

return builder.build()
}
9 changes: 9 additions & 0 deletions java/arcs/core/data/proto/ManifestProtoDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ fun ManifestProto.decodeRecipes(): List<Recipe> {

/** Extracts [ParticleSpec]s from the [ManifestProto]. */
fun ManifestProto.decodeParticleSpecs() = particleSpecsList.map { it.decode() }

/** Convert a series of [Recipe]s into a [ManifestProto]. */
fun Collection<Recipe>.encodeManifest(): ManifestProto = ManifestProto.newBuilder()
.addAllParticleSpecs(
this.flatMap { it.particles.map { particle -> particle.spec } }
.map { it.encode() }
)
.addAllRecipes(this.map { it.encode() })
.build()
18 changes: 16 additions & 2 deletions java/arcs/core/data/proto/ParticleProtoDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ data class DecodingContext(
var recipeHandles: Map<String, Handle>
)

/** Converts a [HandleConnectionProto] into a [Recipe.Particle.HandleConnection]. */
/** Converts a [HandleConnectionProto] into a [HandleConnection]. */
fun HandleConnectionProto.decode(
particleSpec: ParticleSpec,
context: DecodingContext
Expand All @@ -41,11 +41,25 @@ fun HandleConnectionProto.decode(
return HandleConnection(handleSpec, recipeHandle, type.decode())
}

/** Converts a [ParticleProto] into a [Recipe.Particle]. */
/** Converts a [HandleConnection] into a [HandleConnectionProto]. */
fun HandleConnection.encode(): HandleConnectionProto =
HandleConnectionProto.newBuilder()
.setName(spec.name)
.setHandle(handle.name)
.setType(type.encode())
.build()

/** Converts a [ParticleProto] into a [Particle]. */
fun ParticleProto.decode(context: DecodingContext): Particle {
val particleSpec = requireNotNull(context.particleSpecs[specName]) {
"ParticleSpec '$specName' not found in decoding context."
}
val handleConnections = connectionsList.map { it.decode(particleSpec, context) }
return Particle(particleSpec, handleConnections)
}

/** Converts a [Particle] to a [ParticleProto]. */
fun Particle.encode(): ParticleProto = ParticleProto.newBuilder()
.setSpecName(spec.name)
.addAllConnections(handleConnections.map { it.encode() })
.build()
38 changes: 38 additions & 0 deletions java/arcs/core/data/proto/ParticleSpecProtoDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import arcs.core.data.HandleConnectionSpec
import arcs.core.data.HandleMode
import arcs.core.data.ParticleSpec
import arcs.core.data.expression.PaxelParser
import arcs.core.data.expression.stringify

typealias DirectionProto = HandleConnectionSpecProto.Direction

Expand All @@ -26,11 +27,26 @@ fun DirectionProto.decode() =
throw IllegalArgumentException("Direction not set in [HandleConnectionSpec]")
DirectionProto.READS -> HandleMode.Read
DirectionProto.WRITES -> HandleMode.Write
DirectionProto.QUERY -> HandleMode.Query
DirectionProto.READS_WRITES -> HandleMode.ReadWrite
DirectionProto.READS_QUERY -> HandleMode.ReadQuery
DirectionProto.WRITES_QUERY -> HandleMode.WriteQuery
DirectionProto.READS_WRITES_QUERY -> HandleMode.ReadWriteQuery
DirectionProto.UNRECOGNIZED ->
throw IllegalArgumentException("Invalid direction when decoding [HandleConnectionSpec]")
}

/** Converts a [HandleMode] to a [HandleConnectionSpecProto.Direction]. */
fun HandleMode.encode(): DirectionProto = when (this) {
HandleMode.Read -> DirectionProto.READS
HandleMode.Write -> DirectionProto.WRITES
HandleMode.Query -> DirectionProto.QUERY
HandleMode.ReadWrite -> DirectionProto.READS_WRITES
HandleMode.ReadQuery -> DirectionProto.READS_QUERY
HandleMode.WriteQuery -> DirectionProto.WRITES_QUERY
HandleMode.ReadWriteQuery -> DirectionProto.READS_WRITES_QUERY
}

/** Converts a [HandleConnnectionSpecProto] to the corresponding [HandleConnectionSpec] instance. */
fun HandleConnectionSpecProto.decode() = HandleConnectionSpec(
name = name,
Expand All @@ -43,6 +59,18 @@ fun HandleConnectionSpecProto.decode() = HandleConnectionSpec(
}
)

/** Converts a [HandleConnectionSpec] into a [HandeConnectionSpecProto]. */
fun HandleConnectionSpec.encode(): HandleConnectionSpecProto {
val builder = HandleConnectionSpecProto.newBuilder()
.setName(name)
.setDirection(direction.encode())
.setType(type.encode())

expression?.let { builder.setExpression(it.stringify()) }

return builder.build()
}

/** Converts a [ParticleSpecProto] to the corresponding [ParticleSpec] instance. */
fun ParticleSpecProto.decode(): ParticleSpec {
val connections = mutableMapOf<String, HandleConnectionSpec>()
Expand All @@ -59,3 +87,13 @@ fun ParticleSpecProto.decode(): ParticleSpec {
val annotations = annotationsList.map { it.decode() }
return ParticleSpec(name, connections, location, claims, checks, annotations)
}

/** Converts a [ParticleSpec] into a [ParticleSpecProto]. */
fun ParticleSpec.encode(): ParticleSpecProto = ParticleSpecProto.newBuilder()
.setName(name)
.addAllConnections(connections.values.map { it.encode() })
.setLocation(location)
.addAllClaims(claims.map { it.encode() })
.addAllChecks(checks.map { it.encode() })
.addAllAnnotations(annotations.map { it.encode() })
.build()
12 changes: 12 additions & 0 deletions java/arcs/core/data/proto/RecipeProtoDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ fun RecipeProto.decode(particleSpecs: Map<String, ParticleSpec>): Recipe {
val annotations = annotationsList.map { it.decode() }
return Recipe(name.ifBlank { null }, recipeHandles, particles, annotations)
}

/** Converts a [Recipe] into [RecipeProto]. */
fun Recipe.encode(): RecipeProto {
val builder = RecipeProto.newBuilder()
.addAllHandles(handles.values.map { it.encode() })
.addAllParticles(particles.map { it.encode() })
.addAllAnnotations(annotations.map { it.encode() })

name?.let { builder.setName(it) }

return builder.build()
}
4 changes: 4 additions & 0 deletions java/arcs/core/data/proto/manifest.proto
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ message HandleConnectionSpecProto {
READS = 1;
WRITES = 2;
READS_WRITES = 3;
QUERY = 4;
READS_QUERY = 5;
WRITES_QUERY = 6;
READS_WRITES_QUERY = 7;
}
// Identifies a connection in a particle spec.
string name = 1;
Expand Down
Loading