Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Linting #2

Merged
merged 1 commit into from
Aug 21, 2018
Merged

Rule Linting #2

merged 1 commit into from
Aug 21, 2018

Conversation

JimRoepcke
Copy link
Owner

Rule Linting was added to the TextRulesToJSON program. Linting becomes a step of the .rules to .json conversion process.

  • better error handling in TextRulesToJSON, support for linting added
  • finds rules with obviously invalid predicates (comparing true/false using one of < <= > >=)
  • set of accepted RHS questions along with their expected answer type
  • set of accepted questions in LHS expressions along with their expected answer type
  • check for duplicate rules (rudimentary right now due to Rule's lack of Hashable)

Test coverage needs to improve further.

Example output:

TextRulesToJSON: no fallback rule found for favouritePlayer
TextRulesToJSON: line 1: the answer for favouriteTeam rules must be one of: Capitals, Oilers, Lightning, Canucks
TextRulesToJSON: line 2: lhs question city not found in the linter file
TextRulesToJSON: line 3: lhs question city not found in the linter file
TextRulesToJSON: line 4: firstName may only be compared to one of: Jim, Cheryl, Cyan, Xavier
TextRulesToJSON: line 4: lhs question city not found in the linter file
TextRulesToJSON: line 5: invalid predicate: boolean values are not comparable
TextRulesToJSON: line 5: rhs question shouldnot not found in the linter file
TextRulesToJSON: line 5: type mismatch, firstName should be a string but it is being compared to a bool
TextRulesToJSON: line 6: invalid predicate: boolean values are not comparable
TextRulesToJSON: line 6: rhs question shouldnot not found in the linter file
TextRulesToJSON: line 7: duplicate rule found
TextRulesToJSON: line 7: invalid predicate: boolean values are not comparable
TextRulesToJSON: line 7: rhs question shouldnot not found in the linter file
TextRulesToJSON: line 8: rhs question kid not found in the linter file

This output was produced for this input:

Rules file:

0: true => favouriteTeam = X
10: (firstName = 'Jim' AND lastName = 'Roepcke') OR (city = 'Edmonton') => favouriteTeam = Oilers
10: (firstName = 'Xavier' AND lastName = 'Roepcke') OR (city = 'Tampa Bay') => favouriteTeam = Lightning
10: (firstName = 'Bob' AND lastName = 'Roepcke') OR (city = 'Tampa Bay') => favouriteTeam = Lightning
10: firstName < true => shouldnot = work
10: true < true => shouldnot = workeither
10: true < true => shouldnot = workeither
10: age < 10 => kid = true

Linter specification file:

{
  "lhs": {
    "firstName": ["Jim", "Cheryl", "Cyan", "Xavier"],
    "lastName": "string",
    "age": "int"
  },
  "rhs": {
    "favouriteTeam": ["Capitals", "Oilers", "Lightning", "Canucks"],
    "favouritePlayer": "any"
  }
}

- better error handling in TextRulesToJSON
- no rules with obviously invalid predicates (comparing predicates using one of < <= > >=)
- set of accepted RHS questions along with their expected answer type
- set of accepted questions in LHS expressions along with their expected answer type
- check for duplicate rules (rudimentary right now due to Rule's lack of Hashable)
@JimRoepcke
Copy link
Owner Author

The next step will be to amend the buddybuild_postclone.sh script in the core-news-shared-configs repo to clone, build and run the TextRulesToJSON program. I'll start on that script on Monday as well as the linter specification json file for our rules file.

(.int, is Int),
(.double, is Double),
(.string, is String): return []
case (.bool, let x): return [(value, "type mismatch, \(question) should be compared to a bool, not a \(typeName(of: x))")]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I've gotten this far without knowing about typeName(of:) :-)

"city": "string"
},
"rhs": {
"favouriteTeam": ["Capitals", "Oilers", "Lightning", "Canucks"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"favoriteTeam" :trollface:

case .comparison(.question, _, .question):
return [] // not yet implemented
case .comparison(.answer(let left), _, .answer(let right)) where type(of: left.value) == type(of: right.value):
// it could be argued this shouldn't be allowed because it's silly
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 agreed.

@JimRoepcke JimRoepcke merged commit 26203b6 into master Aug 21, 2018
@JimRoepcke JimRoepcke deleted the linting-rules branch August 21, 2018 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants