Skip to content

Instantly share code, notes, and snippets.

@Druwerd
Last active June 20, 2020 00:40
Show Gist options
  • Select an option

  • Save Druwerd/98f955e151d0f6ee372111fb3624f25f to your computer and use it in GitHub Desktop.

Select an option

Save Druwerd/98f955e151d0f6ee372111fb3624f25f to your computer and use it in GitHub Desktop.
outbound partner decision engine
require 'wongi-engine'
include Wongi::Engine::DSL
class LoanApplication
attr_accessor :id, :years_in_business, :bankruptcy, :industry, :fico, :decline, :outbound_partners
def initialize(id:, years_in_business:, bankruptcy:, industry:, fico:)
@id = id
@years_in_business = years_in_business
@bankruptcy = bankruptcy
@industry = industry
@fico = fico
@outbound_partners = []
end
end
loan1 = LoanApplication.new(id: 1, years_in_business: 5, bankruptcy: true, industry: "Entertainment", fico: 700) # should be declined
loan2 = LoanApplication.new(id: 2, years_in_business: 5, bankruptcy: false, industry: "Healthcare", fico: 700) # should not be declined
loan3 = LoanApplication.new(id: 3, years_in_business: 5, bankruptcy: false, industry: "Healthcare", fico: 600)
engine = Wongi::Engine.create
# Add facts
engine << [loan1, :years_in_business, loan1.years_in_business]
engine << [loan1, :bankruptcy, loan1.bankruptcy]
engine << [loan1, :industry, loan1.industry]
engine << [loan1, :fico, loan1.fico]
engine << [loan2, :years_in_business, loan2.years_in_business]
engine << [loan2, :bankruptcy, loan2.bankruptcy]
engine << [loan2, :industry, loan2.industry]
engine << [loan2, :fico, loan2.fico]
engine << [loan3, :years_in_business, loan3.years_in_business]
engine << [loan3, :bankruptcy, loan3.bankruptcy]
engine << [loan3, :industry, loan3.industry]
engine << [loan3, :fico, loan3.fico]
# Rules
funding_circle_decline_rules = engine.rule 'Funding Circle Decline Rules' do
forall {
any {
option {
# matches the years in business fact and checks if it's less than two yeara
has :Loan, :years_in_business, :Years
less :Years, 2
}
option {
# matches bankruptcy fact and checks if it's true
has :Loan, :bankruptcy, :Bankruptcy
equal :Bankruptcy, true
}
}
}
make {
action { |token|
loan = token[:Loan]
loan.decline = true
}
}
end
bhg_rules = engine.rule 'BHG Rules' do
forall {
has :Loan, :industry, :Industry
equal :Industry, "Healthcare"
has :Loan, :fico, :Fico
greater :Fico, 630
}
make {
action { |token|
loan = token[:Loan]
loan.outbound_partners << 'BHG'
}
}
end
[loan1, loan2, loan3].each do |loan|
puts "Loan id: #{loan.id}, Decline: #{loan.decline == true}, Partners: #{loan.outbound_partners}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment