From f613fb52f05af6086f7e28ddfbaea85e22eee494 Mon Sep 17 00:00:00 2001 From: augustearth Date: Sun, 18 Aug 2024 18:56:53 -0700 Subject: [PATCH] add containsFeature() method --- .../groovy/carnival/util/FeatureReport.groovy | 26 +++++++++++++++++++ .../carnival/util/FeatureReportSpec.groovy | 18 +++++++++++++ 2 files changed, 44 insertions(+) diff --git a/app/carnival-util/src/main/groovy/carnival/util/FeatureReport.groovy b/app/carnival-util/src/main/groovy/carnival/util/FeatureReport.groovy index fbfdef41..a665f6ef 100644 --- a/app/carnival-util/src/main/groovy/carnival/util/FeatureReport.groovy +++ b/app/carnival-util/src/main/groovy/carnival/util/FeatureReport.groovy @@ -1224,6 +1224,32 @@ class FeatureReport extends MappedDataTable { } + /** + * Check if a feature with the given name already exists for the given + * subjectId. + * + * @param subjectId The subject identifier. + * @param featureName The feature name. + * @return True if the subject has a feature of the given name, false + * otherwise. + * + */ + public boolean containsFeature(String subjectId, String featureName) { + assert subjectId != null : "subjectId cannot be null. featureName:${featureName} featureValue:${featureValue}" + assert featureName != null : "featureName cannot be null. subjectId:${subjectId} featureValue:${featureValue}" + + // check if feature already exists + def fv = this.dataGet(subjectId) + if (fv == null) throw new RuntimeException("no feature vector for ${subjectId}") + + def ffn = this.toFieldName(featureName) + + return fv.containsKey(ffn) + } + + + + /** * Add a feature to the subject identified by subjectId. * diff --git a/app/carnival-util/src/test/groovy/carnival/util/FeatureReportSpec.groovy b/app/carnival-util/src/test/groovy/carnival/util/FeatureReportSpec.groovy index a2328f8e..3d15c344 100644 --- a/app/carnival-util/src/test/groovy/carnival/util/FeatureReportSpec.groovy +++ b/app/carnival-util/src/test/groovy/carnival/util/FeatureReportSpec.groovy @@ -717,6 +717,24 @@ class FeatureReportSpec extends Specification { } + def "contains feature"() { + given: + Exception e + def frp = new FeatureReport(name:'frp-test', idFieldName:'ID') + frp.dataModes([FeatureReport.DataMode.ADD_SUBJECT, FeatureReport.DataMode.ADD_FEATURE]) + frp.addSubject('id1') + + when: + frp.addFeature('id1', 'v1', 'v11') + boolean b1 = frp.containsFeature('id1', 'v1') + boolean b2 = frp.containsFeature('id1', 'v2') + + then: + b1 + !b2 + } + + def "add feature"() { given: Exception e