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 fbfdef4..a665f6e 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 a2328f8..3d15c34 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