Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 65ca430

Browse files
phui01Peter Hui
andauthored
feat: add ability to create string conditions (#45)
* String matching code * Code review feedback * Removing log * Code review comments Co-authored-by: Peter Hui <pehui@microsoft.com>
1 parent ca808ad commit 65ca430

File tree

8 files changed

+218
-48
lines changed

8 files changed

+218
-48
lines changed

packages/models/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,4 @@
111111
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
112112
}
113113
}
114-
}
114+
}

packages/models/src/Action.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ export enum ConditionType {
2323
GREATER_THAN_OR_EQUAL = "GREATER_THAN_OR_EQUAL",
2424
LESS_THAN = "LESS_THAN",
2525
LESS_THAN_OR_EQUAL = "LESS_THAN_OR_EQUAL",
26+
STRING_EQUAL = "STRING_EQUAL",
27+
}
28+
29+
export enum ComparisonType {
30+
NUMBER_OF_VALUES = "NUMBER_OF_VALUES",
31+
NUMERIC_VALUE = "NUMERIC_VALUE",
32+
STRING = "STRING",
2633
}
2734

2835
export interface Condition {
2936
entityId: string
3037
valueId?: string
3138
value?: number
39+
stringValue?: string
3240
condition: ConditionType
3341
}
3442

packages/sdk/src/Utils.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ export const isConditionTrue = (
227227
filledEntities: CLM.FilledEntity[],
228228
entities: CLM.EntityBase[]
229229
): boolean => {
230-
if (condition.valueId) {
230+
if (condition.stringValue) {
231+
return isStringConditionTrue(condition, filledEntities)
232+
}
233+
else if (condition.valueId) {
231234
return isEnumConditionTrue(condition, filledEntities)
232235
}
233236
else if (condition.value) {
@@ -239,6 +242,21 @@ export const isConditionTrue = (
239242
}
240243
}
241244

245+
const isStringConditionTrue = (
246+
condition: CLM.Condition,
247+
filledEntities: CLM.FilledEntity[]
248+
): boolean => {
249+
if (condition.stringValue && condition.condition === CLM.ConditionType.STRING_EQUAL) {
250+
const filledEntity = filledEntities.find(e => e.entityId === condition.entityId)
251+
const stringValue = filledEntity && filledEntity.values.length > 0 && filledEntity.values[0].userText
252+
if (stringValue === condition.stringValue) {
253+
return true
254+
}
255+
}
256+
257+
return false
258+
}
259+
242260
const isEnumConditionTrue = (
243261
condition: CLM.Condition,
244262
filledEntities: CLM.FilledEntity[]

packages/ui/src/Utils/actionCondition.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ export const conditionDisplay: Record<CLM.ConditionType, string> = {
1818
[CLM.ConditionType.GREATER_THAN_OR_EQUAL]: '>=',
1919
[CLM.ConditionType.LESS_THAN]: '<',
2020
[CLM.ConditionType.LESS_THAN_OR_EQUAL]: '<=',
21+
[CLM.ConditionType.STRING_EQUAL]: 'Matches exactly'
22+
}
23+
24+
export const comparisonTypeDisplay: Record<CLM.ComparisonType, string> = {
25+
[CLM.ComparisonType.NUMBER_OF_VALUES]: 'Number of Items',
26+
[CLM.ComparisonType.NUMERIC_VALUE]: 'Numeric Value',
27+
[CLM.ComparisonType.STRING]: 'String',
2128
}
2229

2330
export const getEnumConditionName = (entity: CLM.EntityBase, enumValue: CLM.EnumValue): string => {
@@ -28,6 +35,10 @@ export const getValueConditionName = (entity: CLM.EntityBase, condition: CLM.Con
2835
return `${entity.entityName} ${conditionDisplay[condition.condition]} ${condition.value}`
2936
}
3037

38+
export const getStringConditionName = (entity: CLM.EntityBase, condition: CLM.Condition): string => {
39+
return `${entity.entityName} ${conditionDisplay[condition.condition]} "${condition.stringValue}"`
40+
}
41+
3142
export const convertConditionToConditionalTag = (condition: CLM.Condition, entities: CLM.EntityBase[]): IConditionalTag => {
3243
const entity = entities.find(e => e.entityId === condition.entityId)
3344
if (!entity) {
@@ -57,7 +68,9 @@ export const convertConditionToConditionalTag = (condition: CLM.Condition, entit
5768
}
5869
}
5970
else {
60-
const name = getValueConditionName(entity, condition)
71+
const name = condition.value
72+
? getValueConditionName(entity, condition)
73+
: getStringConditionName(entity, condition)
6174
const key = CLM.hashText(name)
6275
conditionalTag = {
6376
key,
@@ -94,6 +107,22 @@ export const findNumberFromMemory = (memory: CLM.Memory, isMultivalue: boolean):
94107
: undefined
95108
}
96109

110+
/**
111+
* Given memory value,
112+
* return the userText field of the given memory value, if it exists, or undefined otherwise.
113+
*/
114+
export const findStringFromMemory = (memory: CLM.Memory): string | undefined => {
115+
return (memory?.entityValues?.[0]?.userText) ?? undefined
116+
}
117+
118+
119+
export const isStringConditionTrue = (condition: CLM.Condition, stringValue: string | undefined): boolean => {
120+
if (condition.stringValue) {
121+
return condition.condition == CLM.ConditionType.STRING_EQUAL && stringValue === condition.stringValue
122+
}
123+
return false
124+
}
125+
97126
export const isValueConditionTrue = (condition: CLM.Condition, numberValue: number): boolean => {
98127
let isTrue = false
99128

packages/ui/src/Utils/dialogUtils.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import TagsReadOnly from '../components/TagsReadOnly'
1111
import { compareTwoStrings } from 'string-similarity'
1212
import { deepCopy, getDefaultEntityMap } from './util'
1313
import { ImportedAction } from '../types/models'
14-
import { getValueConditionName, findNumberFromMemory, isValueConditionTrue, isEnumConditionTrue } from './actionCondition'
14+
import { getValueConditionName, findNumberFromMemory, findStringFromMemory, isValueConditionTrue, isEnumConditionTrue, isStringConditionTrue, getStringConditionName } from './actionCondition'
1515
import { fromLogTag } from '../types'
1616

1717
const MAX_SAMPLE_INPUT_LENGTH = 150
@@ -400,7 +400,20 @@ export function convertToScorerCondition(condition: CLM.Condition, entities: CLM
400400
name
401401
}
402402
}
403-
// Other conditions (StringCondition in future)
403+
else if (condition.stringValue) {
404+
const name = getStringConditionName(entity, condition)
405+
let match = false
406+
if (memory) {
407+
const value = findStringFromMemory(memory)
408+
match = isStringConditionTrue(condition, value)
409+
}
410+
411+
return {
412+
match,
413+
name
414+
}
415+
}
416+
// Other conditions
404417
else {
405418
return {
406419
match: false,

packages/ui/src/components/ActionDetailsList.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { injectIntl, InjectedIntl, InjectedIntlProps } from 'react-intl'
1818
import { FM } from '../react-intl-messages'
1919
import './ActionDetailsList.css'
2020
import { autobind } from 'core-decorators'
21-
import { getValueConditionName, getEnumConditionName } from '../Utils/actionCondition'
21+
import { getValueConditionName, getEnumConditionName, getStringConditionName } from '../Utils/actionCondition'
2222
import * as MockResultUtil from '../Utils/mockResults'
2323

2424
interface ComponentState {
@@ -344,6 +344,9 @@ function renderConditions(entityIds: string[], conditions: CLM.Condition[], allE
344344
? `Error - Missing Enum: ${condition.valueId}`
345345
: getEnumConditionName(entity, enumValue)
346346
}
347+
else if (condition.stringValue) {
348+
name = getStringConditionName(entity, condition)
349+
}
347350
else {
348351
name = getValueConditionName(entity, condition)
349352
}

packages/ui/src/components/modals/ConditionModal.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.cl-condition-creator__expression {
22
display: grid;
33
grid-gap: 1em;
4-
grid-template-columns: repeat(3, 1fr);
4+
grid-template-columns: repeat(4, 1fr);
55
}
66

77
.cl-condition-creator__existing-conditions {

0 commit comments

Comments
 (0)