Basic Mortgage Calculator with Rule Engine Feedback

  • Updated

Mortgage Calculator - Rule Engine Feedback Sample

This sample builds on the Basic sample, but adds how to get the Rule Execution Log and process Notifications and Validations.

<script src="MortgageCalculator.min.js"></script>
<script language="javascript">
function runRules() {
// make JavaScript object to pass to InRule
var mortgage = {};
mortgage.LoanInfo = {};
mortgage.LoanInfo.Principal = document.getElementById('loanAmount').value;
mortgage.LoanInfo.APR = document.getElementById('apr').value;
mortgage.LoanInfo.TermInYears =
document.getElementById('termInYears').value;
// run rules, entity is not used here, but is available if needed
var session = inrule.createRuleSession();
var entity = session.createEntity('Mortgage', mortgage);
session.applyRules(function(log){
if(!log.hasErrors){
// write exec log, notifications and validations to form
document.getElementById('log').value = log.messages.join('\n');
document.getElementById('notifications').value =
session.getActiveNotifications().map(function(x){return x.message}).join('\n');
document.getElementById('validations').value =
session.getActiveValidations().map(function(x){return x.message}).join('\n');
// get data from updated JavaScript object and write back to form
document.getElementById('montlyPayment').value =
mortgage.PaymentSummary.MonthlyPayment.toFixed(2);
document.getElementById('totalCost').value =
mortgage.PaymentSummary.TotalCost.toFixed(2);
}
else{
document.getElementById('errors').value =
log.runtimeErrors.join('\n');
}
});
}
</script>

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.