From b7d26ee8c8d300d1a15327eaf16682c15e1b19c7 Mon Sep 17 00:00:00 2001 From: Konrad Weiss Date: Mon, 26 Jun 2023 16:47:58 +0200 Subject: [PATCH] Small renaming --- .../fraunhofer/aisec/cpg_vis_neo4j/Schema.kt | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/cpg-neo4j/src/main/kotlin/de/fraunhofer/aisec/cpg_vis_neo4j/Schema.kt b/cpg-neo4j/src/main/kotlin/de/fraunhofer/aisec/cpg_vis_neo4j/Schema.kt index 1fda1cfe9c..f121ebe80a 100644 --- a/cpg-neo4j/src/main/kotlin/de/fraunhofer/aisec/cpg_vis_neo4j/Schema.kt +++ b/cpg-neo4j/src/main/kotlin/de/fraunhofer/aisec/cpg_vis_neo4j/Schema.kt @@ -57,30 +57,35 @@ class Schema { private val hierarchy: MutableMap>> = mutableMapOf() /** - * Set of fields that are translated into a relationship. The pair saves the field name, and the - * relationship name. Saves MutableMap>> + * Map of entities and the relationships they can have, including relationships defined in + * subclasses. The pair saves the field name, and the relationship name. Saves + * MutableMap>> */ - private val relCanHave: MutableMap>> = mutableMapOf() + private val allRels: MutableMap>> = mutableMapOf() /** * Relationships newly defined in this specific entity. Saves * MutableMap>> */ - private val inherentFields: MutableMap>> = mutableMapOf() + private val inherentRels: MutableMap>> = mutableMapOf() /** * Relationships inherited from a parent in the inheritance hierarchy. A node with this label * can have this relationship if it is non-nullable. Saves * MutableMap>> */ - private val inheritedFields: MutableMap>> = mutableMapOf() + private val inheritedRels: MutableMap>> = mutableMapOf() /** * Relationships defined by children in the inheritance hierarchy. A node with this label can * have this relationship also has the label of the defining child entity. Saves * MutableMap>> */ - private val childrenFields: MutableMap>> = mutableMapOf() + private val childrensRels: MutableMap>> = mutableMapOf() + /** + * Stores a mapping from class information in combination with a relationship name, to the field + * information that contains the relationship. + */ private val relationshipFields: MutableMap, FieldInfo> = mutableMapOf() fun extractSchema() { @@ -96,15 +101,13 @@ class Schema { if (it in entities) { val superC = it.directSuperclass() - hierarchy.put( - it, + hierarchy[it] = Pair( if (superC in entities) superC else null, it.directSubclasses() .filter { it in entities } .distinct() // Filter out duplicates ) - ) } } @@ -112,12 +115,12 @@ class Schema { entities.forEach { val key = meta.schema.findNode(it.neo4jName()) - relCanHave[it.neo4jName() ?: it.underlyingClass.simpleName] = + allRels[it.neo4jName() ?: it.underlyingClass.simpleName] = key.relationships().entries.map { Pair(it.key, it.value.type()) }.toSet() } // Complements the hierarchy and relationship information for abstract classes - completeSchema(relCanHave, hierarchy, nodeClassInfo) + completeSchema(allRels, hierarchy, nodeClassInfo) // Searches for all relationships backed by a class field to know which relationships are // newly defined in the // entity class @@ -129,8 +132,8 @@ class Schema { } fields.forEach { relationshipFields.put(Pair(entity, it.name), it) } val name = it.neo4jName() ?: it.underlyingClass.simpleName - relCanHave[name]?.let { - inherentFields[name] = + allRels[name]?.let { + inherentRels[name] = it.filter { val rel = it.first fields.any { it.name.equals(rel) } @@ -144,30 +147,30 @@ class Schema { val entityRoots: MutableList = hierarchy.filter { it.value.first == null }.map { it.key }.toMutableList() entityRoots.forEach { - inheritedFields[it.neo4jName() ?: it.underlyingClass.simpleName] = mutableSetOf() + inheritedRels[it.neo4jName() ?: it.underlyingClass.simpleName] = mutableSetOf() } entityRoots.forEach { buildInheritedFields(it) } - relCanHave.forEach { - childrenFields[it.key] = + allRels.forEach { + childrensRels[it.key] = it.value - .subtract(inheritedFields[it.key] ?: emptyList()) - .subtract(inherentFields[it.key] ?: emptyList()) + .subtract(inheritedRels[it.key] ?: emptyList()) + .subtract(inherentRels[it.key] ?: emptyList()) } println() } private fun buildInheritedFields(classInfo: ClassInfo) { val fields: MutableSet> = mutableSetOf() - inherentFields[classInfo.neo4jName() ?: classInfo.underlyingClass.simpleName]?.let { + inherentRels[classInfo.neo4jName() ?: classInfo.underlyingClass.simpleName]?.let { fields.addAll(it) } - inheritedFields[classInfo.neo4jName() ?: classInfo.underlyingClass.simpleName]?.let { + inheritedRels[classInfo.neo4jName() ?: classInfo.underlyingClass.simpleName]?.let { fields.addAll(it) } hierarchy[classInfo]?.second?.forEach { - inheritedFields[it.neo4jName() ?: it.underlyingClass.simpleName] = fields + inheritedRels[it.neo4jName() ?: it.underlyingClass.simpleName] = fields buildInheritedFields(it) } } @@ -255,10 +258,10 @@ class Schema { } } - if (inherentFields.isNotEmpty() && inheritedFields.isNotEmpty()) { + if (inherentRels.isNotEmpty() && inheritedRels.isNotEmpty()) { out.println("### Relationships") - noLabelDups(inherentFields[entityLabel])?.forEach { + noLabelDups(inherentRels[entityLabel])?.forEach { out.println( getBoxWithClass( "relationship", @@ -266,12 +269,12 @@ class Schema { ) ) } - noLabelDups(inheritedFields[entityLabel])?.forEach { + noLabelDups(inheritedRels[entityLabel])?.forEach { var inherited = it var current = classInfo var baseClass: ClassInfo? = null while (baseClass == null) { - inherentFields[toLabel(current)]?.let { + inherentRels[toLabel(current)]?.let { if (it.any { it.second.equals(inherited.second) }) { baseClass = current } @@ -286,7 +289,7 @@ class Schema { ) } - noLabelDups(inherentFields[entityLabel])?.forEach { + noLabelDups(inherentRels[entityLabel])?.forEach { printRelationships(classInfo, it, out) } }