Skip to content

Commit

Permalink
Rename ImplementationGuide library to Knowledge (google#1977)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktarasenko authored Apr 21, 2023
1 parent be21768 commit db56f2b
Show file tree
Hide file tree
Showing 36 changed files with 174 additions and 166 deletions.
2 changes: 1 addition & 1 deletion benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ dependencies {
androidTestImplementation(Dependencies.truth)

androidTestImplementation(project(":engine"))
androidTestImplementation(project(":implementationguide"))
androidTestImplementation(project(":knowledge"))
androidTestImplementation(project(":workflow"))
androidTestImplementation(project(":workflow-testing"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.FhirEngineProvider
import com.google.android.fhir.implementationguide.IgManager
import com.google.android.fhir.knowledge.KnowledgeManager
import com.google.android.fhir.workflow.FhirOperatorBuilder
import com.google.common.truth.Truth.assertThat
import java.io.File
Expand Down Expand Up @@ -57,14 +57,14 @@ class G_CqlEvaluatorBenchmark {
val patientImmunizationHistory =
jsonParser.parseResource(open("/immunity-check/ImmunizationHistory.json")) as Bundle
val fhirEngine = FhirEngineProvider.getInstance(ApplicationProvider.getApplicationContext())
val igManager = IgManager.createInMemory(context)
val knowledgeManager = KnowledgeManager.createInMemory(context)
val lib = jsonParser.parseResource(open("/immunity-check/ImmunityCheck.json")) as Library

runBlocking {
for (entry in patientImmunizationHistory.entry) {
fhirEngine.create(entry.resource)
}
igManager.install(
knowledgeManager.install(
File(context.filesDir, lib.name).apply {
writeText(jsonParser.encodeResourceToString(lib))
}
Expand All @@ -74,7 +74,7 @@ class G_CqlEvaluatorBenchmark {
FhirOperatorBuilder(context)
.withFhirContext(fhirContext)
.withFhirEngine(fhirEngine)
.withIgManager(igManager)
.withIgManager(knowledgeManager)
.build()
}

Expand Down
6 changes: 3 additions & 3 deletions buildSrc/src/main/kotlin/Releases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ object Releases {
}
}

object ImplementationGuide : LibraryArtifact {
override val artifactId = "implementationguide"
object Knowledge : LibraryArtifact {
override val artifactId = "knowledger"
override val version = "0.1.0-alpha001"
override val name = "Android FHIR Implementation Guide Library"
override val name = "Android FHIR Knowledge Manager Library"
}

// Demo apps
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
id(Plugins.BuildPlugins.dokka).version(Plugins.Versions.dokka)
}

publishArtifact(Releases.ImplementationGuide)
publishArtifact(Releases.Knowledge)

createJacocoTestReportTask()

Expand All @@ -21,7 +21,7 @@ android {
targetSdk = Sdk.targetSdk
testInstrumentationRunner = Dependencies.androidJunitRunner
// Need to specify this to prevent junit runner from going deep into our dependencies
testInstrumentationRunnerArguments["package"] = "com.google.android.fhir.implementationguide"
testInstrumentationRunnerArguments["package"] = "com.google.android.fhir.knowledge"
}

sourceSets {
Expand Down Expand Up @@ -110,15 +110,13 @@ dependencies {

tasks.dokkaHtml.configure {
outputDirectory.set(
file(
"../docs/${Releases.ImplementationGuide.artifactId}/${Releases.ImplementationGuide.version}"
)
file("../docs/${Releases.Knowledge.artifactId}/${Releases.Knowledge.version}")
)
suppressInheritedMembers.set(true)
dokkaSourceSets {
named("main") {
moduleName.set(Releases.ImplementationGuide.artifactId)
moduleVersion.set(Releases.ImplementationGuide.version)
moduleName.set(Releases.Knowledge.artifactId)
moduleVersion.set(Releases.Knowledge.version)
noAndroidSdkLink.set(false)
externalDocumentationLink {
url.set(URL("https://hapifhir.io/hapi-fhir/apidocs/hapi-fhir-structures-r4/"))
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

package com.google.android.fhir.implementationguide.db.impl
package com.google.android.fhir.knowledge.db.impl

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.android.fhir.implementationguide.db.impl.entities.ImplementationGuideEntity
import com.google.android.fhir.implementationguide.db.impl.entities.ResourceMetadataEntity
import com.google.android.fhir.knowledge.db.impl.entities.ImplementationGuideEntity
import com.google.android.fhir.knowledge.db.impl.entities.ResourceMetadataEntity
import com.google.common.truth.Truth.assertThat
import java.io.File
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand All @@ -33,27 +33,28 @@ import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
internal class ImplementationGuideDatabaseTest {
internal class KnowledgeDatabaseTest {

private val context: Context = ApplicationProvider.getApplicationContext()
private val igDb =
Room.inMemoryDatabaseBuilder(context, ImplementationGuideDatabase::class.java).build()
private val igDao = igDb.implementationGuideDao()
private val knowledgeDb =
Room.inMemoryDatabaseBuilder(context, KnowledgeDatabase::class.java).build()
private val knowledgeDao = knowledgeDb.knowledgeDao()

@After
fun tearDownDb() {
igDb.close()
knowledgeDb.close()
}

@Test
fun igInserted(): Unit = runTest {
assertThat(igDao.insert(IG_ENTITY)).isGreaterThan(0)
assertThat(igDao.getImplementationGuides().map { it.packageId }).containsExactly(IG_PACKAGE_ID)
assertThat(knowledgeDao.insert(IG_ENTITY)).isGreaterThan(0)
assertThat(knowledgeDao.getImplementationGuides().map { it.packageId })
.containsExactly(IG_PACKAGE_ID)
}

@Test
fun resourcesInserted() = runTest {
val igId = igDao.insert(IG_ENTITY)
val igId = knowledgeDao.insert(IG_ENTITY)
val resource =
ResourceMetadataEntity(
0L,
Expand All @@ -64,18 +65,19 @@ internal class ImplementationGuideDatabaseTest {
File("resId")
)

igDao.insertResource(igId, resource)
knowledgeDao.insertResource(igId, resource)

assertThat(igDao.getResources(ResourceType.ValueSet).map { it.url }).containsExactly(RES_URL)
assertThat(igDao.getResources(ResourceType.Account)).isEmpty()
assertThat(igDao.getImplementationGuidesWithResources(igId)?.resources?.map { it.url })
assertThat(knowledgeDao.getResources(ResourceType.ValueSet).map { it.url })
.containsExactly(RES_URL)
assertThat(igDao.getImplementationGuidesWithResources(-1)).isNull()
assertThat(knowledgeDao.getResources(ResourceType.Account)).isEmpty()
assertThat(knowledgeDao.getImplementationGuidesWithResources(igId)?.resources?.map { it.url })
.containsExactly(RES_URL)
assertThat(knowledgeDao.getImplementationGuidesWithResources(-1)).isNull()
}

@Test
fun resourcesDeleted() = runTest {
val igId = igDao.insert(IG_ENTITY)
val igId = knowledgeDao.insert(IG_ENTITY)
val resource =
ResourceMetadataEntity(
0L,
Expand All @@ -85,19 +87,19 @@ internal class ImplementationGuideDatabaseTest {
RES_VERSION,
File("resId")
)
igDao.insertResource(igId, resource)
knowledgeDao.insertResource(igId, resource)

igDao.deleteImplementationGuide(IG_PACKAGE_ID, IG_VERSION)
knowledgeDao.deleteImplementationGuide(IG_PACKAGE_ID, IG_VERSION)

assertThat(igDao.getImplementationGuides()).isEmpty()
assertThat(igDao.getResources()).isEmpty()
assertThat(igDao.getImplementationGuidesWithResources(igId)).isNull()
assertThat(knowledgeDao.getImplementationGuides()).isEmpty()
assertThat(knowledgeDao.getResources()).isEmpty()
assertThat(knowledgeDao.getImplementationGuidesWithResources(igId)).isNull()
}

@Test
fun resourcesReused() = runTest {
val igId1 = igDao.insert(IG_ENTITY)
val igId2 = igDao.insert(IG_ENTITY.copy(version = "2.0.0"))
val igId1 = knowledgeDao.insert(IG_ENTITY)
val igId2 = knowledgeDao.insert(IG_ENTITY.copy(version = "2.0.0"))
val resource =
ResourceMetadataEntity(
0L,
Expand All @@ -108,15 +110,15 @@ internal class ImplementationGuideDatabaseTest {
File("resId")
)

igDao.insertResource(igId1, resource)
igDao.insertResource(igId2, resource)
knowledgeDao.insertResource(igId1, resource)
knowledgeDao.insertResource(igId2, resource)

assertThat(igDao.getImplementationGuidesWithResources(igId1)?.resources?.map { it.url })
assertThat(knowledgeDao.getImplementationGuidesWithResources(igId1)?.resources?.map { it.url })
.containsExactly(RES_URL)
assertThat(igDao.getImplementationGuidesWithResources(igId2)?.resources?.map { it.url })
assertThat(knowledgeDao.getImplementationGuidesWithResources(igId2)?.resources?.map { it.url })
.containsExactly(RES_URL)
assertThat(igDao.getResources()).hasSize(1)
assertThat(igDao.getImplementationGuidesWithResources(-1)).isNull()
assertThat(knowledgeDao.getResources()).hasSize(1)
assertThat(knowledgeDao.getImplementationGuidesWithResources(-1)).isNull()
}

private companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<manifest package="com.google.android.fhir.implementationguide">
<manifest package="com.google.android.fhir.knowledge">

<application />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.android.fhir.implementationguide
package com.google.android.fhir.knowledge

/**
* Holds Implementation Guide attributes. Used to define dependencies, load dependencies from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
* limitations under the License.
*/

package com.google.android.fhir.implementationguide
package com.google.android.fhir.knowledge

import android.content.Context
import androidx.room.Room
import ca.uhn.fhir.context.FhirContext
import com.google.android.fhir.implementationguide.db.impl.ImplementationGuideDatabase
import com.google.android.fhir.implementationguide.db.impl.entities.ResourceMetadataEntity
import com.google.android.fhir.implementationguide.db.impl.entities.toEntity
import ca.uhn.fhir.parser.IParser
import com.google.android.fhir.knowledge.db.impl.KnowledgeDatabase
import com.google.android.fhir.knowledge.db.impl.entities.ResourceMetadataEntity
import com.google.android.fhir.knowledge.db.impl.entities.toEntity
import java.io.File
import java.io.FileInputStream
import kotlinx.coroutines.Dispatchers
Expand All @@ -33,10 +34,13 @@ import org.hl7.fhir.r4.model.ResourceType
import timber.log.Timber

/** Responsible for importing, accessing and deleting Implementation Guides. */
class IgManager internal constructor(private val igDatabase: ImplementationGuideDatabase) {
class KnowledgeManager
internal constructor(
private val knowledgeDatabase: KnowledgeDatabase,
private val jsonParser: IParser = FhirContext.forR4().newJsonParser(),
) {

private val igDao = igDatabase.implementationGuideDao()
private val jsonParser = FhirContext.forR4().newJsonParser()
private val knowledgeDao = knowledgeDatabase.knowledgeDao()

/**
* * Checks if the [implementationGuides] are present in DB. If necessary, downloads the
Expand All @@ -53,7 +57,7 @@ class IgManager internal constructor(private val igDatabase: ImplementationGuide
*/
suspend fun install(implementationGuide: ImplementationGuide, rootDirectory: File) {
// TODO(ktarasenko) copy files to the safe space?
val igId = igDao.insert(implementationGuide.toEntity(rootDirectory))
val igId = knowledgeDao.insert(implementationGuide.toEntity(rootDirectory))
rootDirectory.listFiles()?.forEach { file ->
try {
val resource = jsonParser.parseResource(FileInputStream(file))
Expand All @@ -68,7 +72,7 @@ class IgManager internal constructor(private val igDatabase: ImplementationGuide
}
}

/** Imports the IG from the provided [file] to the default dependency. */
/** Imports the Knolwedge Artifact from the provided [file] to the default dependency. */
suspend fun install(file: File) {
importFile(null, file)
}
Expand All @@ -84,22 +88,23 @@ class IgManager internal constructor(private val igDatabase: ImplementationGuide
val resType = ResourceType.fromCode(resourceType)
val resourceEntities =
when {
url != null -> listOfNotNull(igDao.getResourceWithUrl(url))
id != null -> listOfNotNull(igDao.getResourceWithUrlLike("%$id"))
url != null -> listOfNotNull(knowledgeDao.getResourceWithUrl(url))
id != null -> listOfNotNull(knowledgeDao.getResourceWithUrlLike("%$id"))
name != null && version != null ->
listOfNotNull(igDao.getResourcesWithNameAndVersion(resType, name, version))
name != null -> igDao.getResourcesWithName(resType, name)
else -> igDao.getResources(resType)
listOfNotNull(knowledgeDao.getResourcesWithNameAndVersion(resType, name, version))
name != null -> knowledgeDao.getResourcesWithName(resType, name)
else -> knowledgeDao.getResources(resType)
}
return resourceEntities.map { loadResource(it) }
}

/** Deletes Implementation Guide, cleans up files. */
suspend fun delete(vararg igDependencies: ImplementationGuide) {
igDependencies.forEach { igDependency ->
val igEntity = igDao.getImplementationGuide(igDependency.packageId, igDependency.version)
val igEntity =
knowledgeDao.getImplementationGuide(igDependency.packageId, igDependency.version)
if (igEntity != null) {
igDao.deleteImplementationGuide(igEntity)
knowledgeDao.deleteImplementationGuide(igEntity)
igEntity.rootDirectory.deleteRecursively()
}
}
Expand Down Expand Up @@ -130,30 +135,28 @@ class IgManager internal constructor(private val igDatabase: ImplementationGuide
metadataResource?.version,
file
)
igDao.insertResource(igId, res)
knowledgeDao.insertResource(igId, res)
}

private fun loadResource(resourceEntity: ResourceMetadataEntity): IBaseResource {
return jsonParser.parseResource(FileInputStream(resourceEntity.resourceFile))
}

fun close() {
igDatabase.close()
knowledgeDatabase.close()
}

companion object {
private const val DB_NAME = "implementationguide.db"
private const val DB_NAME = "knowledge.db"

/** Creates an [IgManager] backed by the Room DB. */
/** Creates an [KnowledgeManager] backed by the Room DB. */
fun create(context: Context) =
IgManager(
Room.databaseBuilder(context, ImplementationGuideDatabase::class.java, DB_NAME).build()
KnowledgeManager(
Room.databaseBuilder(context, KnowledgeDatabase::class.java, DB_NAME).build()
)

/** Creates an [IgManager] backed by the in-memory DB. */
/** Creates an [KnowledgeManager] backed by the in-memory DB. */
fun createInMemory(context: Context) =
IgManager(
Room.inMemoryDatabaseBuilder(context, ImplementationGuideDatabase::class.java).build()
)
KnowledgeManager(Room.inMemoryDatabaseBuilder(context, KnowledgeDatabase::class.java).build())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.android.fhir.implementationguide.db.impl
package com.google.android.fhir.knowledge.db.impl

import androidx.room.TypeConverter
import java.io.File
Expand Down
Loading

0 comments on commit db56f2b

Please sign in to comment.