Skip to content

Commit 7ee3050

Browse files
Refactor getOrElseBoolean (#92)
1 parent 8f454d0 commit 7ee3050

File tree

1 file changed

+18
-27
lines changed
  • nebula-algorithm/src/main/scala/com/vesoft/nebula/algorithm/config

1 file changed

+18
-27
lines changed

nebula-algorithm/src/main/scala/com/vesoft/nebula/algorithm/config/AlgoConfig.scala

+18-27
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object PRConfig {
2626
if (prConfig.contains("algorithm.pagerank.resetProb"))
2727
prConfig("algorithm.pagerank.resetProb").toDouble
2828
else 0.15
29-
encodeId = ConfigUtil.getOrElseBoolean(prConfig, "algorithm.pagerank.encodeId", false)
29+
encodeId = ConfigUtil.getOrElseBoolean(prConfig, "algorithm.pagerank.encodeId")
3030
PRConfig(maxIter, resetProb, encodeId)
3131
}
3232
}
@@ -44,7 +44,7 @@ object LPAConfig {
4444
val lpaConfig = configs.algorithmConfig.map
4545

4646
maxIter = lpaConfig("algorithm.labelpropagation.maxIter").toInt
47-
encodeId = ConfigUtil.getOrElseBoolean(lpaConfig, "algorithm.labelpropagation.encodeId", false)
47+
encodeId = ConfigUtil.getOrElseBoolean(lpaConfig, "algorithm.labelpropagation.encodeId")
4848
LPAConfig(maxIter, encodeId)
4949
}
5050
}
@@ -60,7 +60,7 @@ object CcConfig {
6060

6161
def getCcConfig(configs: Configs): CcConfig = {
6262
val ccConfig = configs.algorithmConfig.map
63-
encodeId = ConfigUtil.getOrElseBoolean(ccConfig, "algorithm.connectedcomponent.encodeId", false)
63+
encodeId = ConfigUtil.getOrElseBoolean(ccConfig, "algorithm.connectedcomponent.encodeId")
6464

6565
maxIter = ccConfig("algorithm.connectedcomponent.maxIter").toInt
6666
CcConfig(maxIter, encodeId)
@@ -80,7 +80,7 @@ object ShortestPathConfig {
8080
val spConfig = configs.algorithmConfig.map
8181

8282
landmarks = spConfig("algorithm.shortestpaths.landmarks").split(",").toSeq.map(_.toLong)
83-
encodeId = ConfigUtil.getOrElseBoolean(spConfig, "algorithm.shortestpaths.encodeId", false)
83+
encodeId = ConfigUtil.getOrElseBoolean(spConfig, "algorithm.shortestpaths.encodeId")
8484
ShortestPathConfig(landmarks, encodeId)
8585
}
8686
}
@@ -102,7 +102,7 @@ object LouvainConfig {
102102
maxIter = louvainConfig("algorithm.louvain.maxIter").toInt
103103
internalIter = louvainConfig("algorithm.louvain.internalIter").toInt
104104
tol = louvainConfig("algorithm.louvain.tol").toDouble
105-
encodeId = ConfigUtil.getOrElseBoolean(louvainConfig, "algorithm.louvain.encodeId", false)
105+
encodeId = ConfigUtil.getOrElseBoolean(louvainConfig, "algorithm.louvain.encodeId")
106106

107107
LouvainConfig(maxIter, internalIter, tol, encodeId)
108108
}
@@ -118,7 +118,7 @@ object DegreeStaticConfig {
118118

119119
def getDegreeStaticConfig(configs: Configs): DegreeStaticConfig = {
120120
val degreeConfig = configs.algorithmConfig.map
121-
encodeId = ConfigUtil.getOrElseBoolean(degreeConfig, "algorithm.degreestatic.encodeId", false)
121+
encodeId = ConfigUtil.getOrElseBoolean(degreeConfig, "algorithm.degreestatic.encodeId")
122122
DegreeStaticConfig(encodeId)
123123
}
124124
}
@@ -132,8 +132,7 @@ object TriangleConfig {
132132
var encodeId: Boolean = false
133133
def getTriangleConfig(configs: Configs): TriangleConfig = {
134134
val triangleConfig = configs.algorithmConfig.map
135-
encodeId =
136-
ConfigUtil.getOrElseBoolean(triangleConfig, "algorithm.trianglecount.encodeId", false)
135+
encodeId = ConfigUtil.getOrElseBoolean(triangleConfig, "algorithm.trianglecount.encodeId")
137136
TriangleConfig(encodeId)
138137
}
139138
}
@@ -152,7 +151,7 @@ object KCoreConfig {
152151
val kCoreConfig = configs.algorithmConfig.map
153152
maxIter = kCoreConfig("algorithm.kcore.maxIter").toInt
154153
degree = kCoreConfig("algorithm.kcore.degree").toInt
155-
encodeId = ConfigUtil.getOrElseBoolean(kCoreConfig, "algorithm.kcore.encodeId", false)
154+
encodeId = ConfigUtil.getOrElseBoolean(kCoreConfig, "algorithm.kcore.encodeId")
156155
KCoreConfig(maxIter, degree, false)
157156
}
158157
}
@@ -169,8 +168,7 @@ object BetweennessConfig {
169168
def getBetweennessConfig(configs: Configs): BetweennessConfig = {
170169
val betweennessConfig = configs.algorithmConfig.map
171170
maxIter = betweennessConfig("algorithm.betweenness.maxIter").toInt
172-
encodeId =
173-
ConfigUtil.getOrElseBoolean(betweennessConfig, "algorithm.betweenness.encodeId", false)
171+
encodeId = ConfigUtil.getOrElseBoolean(betweennessConfig, "algorithm.betweenness.encodeId")
174172
BetweennessConfig(maxIter, encodeId)
175173
}
176174
}
@@ -190,9 +188,8 @@ object CoefficientConfig {
190188
algoType = coefficientConfig("algorithm.clusteringcoefficient.type")
191189
assert(algoType.equalsIgnoreCase("local") || algoType.equalsIgnoreCase("global"),
192190
"ClusteringCoefficient only support local or global type.")
193-
encodeId = ConfigUtil.getOrElseBoolean(coefficientConfig,
194-
"algorithm.clusteringcoefficient.encodeId",
195-
false)
191+
encodeId =
192+
ConfigUtil.getOrElseBoolean(coefficientConfig, "algorithm.clusteringcoefficient.encodeId")
196193
CoefficientConfig(algoType, encodeId)
197194
}
198195
}
@@ -210,7 +207,7 @@ object BfsConfig {
210207
val bfsConfig = configs.algorithmConfig.map
211208
maxIter = bfsConfig("algorithm.bfs.maxIter").toInt
212209
root = bfsConfig("algorithm.bfs.root").toString
213-
encodeId = ConfigUtil.getOrElseBoolean(bfsConfig, "algorithm.bfs.encodeId", false)
210+
encodeId = ConfigUtil.getOrElseBoolean(bfsConfig, "algorithm.bfs.encodeId")
214211
BfsConfig(maxIter, root, encodeId)
215212
}
216213
}
@@ -228,7 +225,7 @@ object DfsConfig {
228225
val dfsConfig = configs.algorithmConfig.map
229226
maxIter = dfsConfig("algorithm.dfs.maxIter").toInt
230227
root = dfsConfig("algorithm.dfs.root").toString
231-
encodeId = ConfigUtil.getOrElseBoolean(dfsConfig, "algorithm.dfs.encodeId", false)
228+
encodeId = ConfigUtil.getOrElseBoolean(dfsConfig, "algorithm.dfs.encodeId")
232229
DfsConfig(maxIter, root, encodeId)
233230
}
234231
}
@@ -251,7 +248,7 @@ object HanpConfig {
251248
hopAttenuation = hanpConfig("algorithm.hanp.hopAttenuation").toDouble
252249
maxIter = hanpConfig("algorithm.hanp.maxIter").toInt
253250
preference = hanpConfig("algorithm.hanp.preference").toDouble
254-
encodeId = ConfigUtil.getOrElseBoolean(hanpConfig, "algorithm.hanp.encodeId", false)
251+
encodeId = ConfigUtil.getOrElseBoolean(hanpConfig, "algorithm.hanp.encodeId")
255252
HanpConfig(hopAttenuation, maxIter, preference, encodeId)
256253
}
257254
}
@@ -306,7 +303,7 @@ object Node2vecConfig {
306303
degree = node2vecConfig("algorithm.node2vec.degree").toInt
307304
embSeparate = node2vecConfig("algorithm.node2vec.embSeparate")
308305
modelPath = node2vecConfig("algorithm.node2vec.modelPath")
309-
encodeId = ConfigUtil.getOrElseBoolean(node2vecConfig, "algorithm.node2vec.encodeId", false)
306+
encodeId = ConfigUtil.getOrElseBoolean(node2vecConfig, "algorithm.node2vec.encodeId")
310307
Node2vecConfig(maxIter,
311308
lr,
312309
dataNumPartition,
@@ -336,7 +333,7 @@ object JaccardConfig {
336333
def getJaccardConfig(configs: Configs): JaccardConfig = {
337334
val jaccardConfig = configs.algorithmConfig.map
338335
tol = jaccardConfig("algorithm.jaccard.tol").toDouble
339-
encodeId = ConfigUtil.getOrElseBoolean(jaccardConfig, "algorithm.jaccard.encodeId", false)
336+
encodeId = ConfigUtil.getOrElseBoolean(jaccardConfig, "algorithm.jaccard.encodeId")
340337
JaccardConfig(tol, encodeId)
341338
}
342339
}
@@ -351,12 +348,6 @@ object AlgoConfig {
351348
}
352349

353350
object ConfigUtil {
354-
def getOrElseBoolean(config: Map[String, String], key: String, defaultValue: Boolean): Boolean = {
355-
if (config.contains(key)) {
356-
config(key).toBoolean
357-
} else {
358-
defaultValue
359-
}
360-
}
361-
351+
def getOrElseBoolean(config: Map[String, String], key: String): Boolean =
352+
config.get(key).exists(_.toBoolean)
362353
}

0 commit comments

Comments
 (0)