Skip to content

Commit

Permalink
add containsFeature() method
Browse files Browse the repository at this point in the history
  • Loading branch information
augustearth committed Aug 19, 2024
1 parent 6c08c36 commit f613fb5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f613fb5

Please sign in to comment.