Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problem with interpreter Pattern

  Asked By: Robert    Date: Aug 07    Category: Java    Views: 784
  

I have a condition and formula generator in my application. I must save this condition and formula in database and calculate formulas for each condition.

I want to model these conditions and formulas. For example:

Condition:

Education=BS AND MaritalStatus=Married AND YearsOfService>10

Formula:

Salary=2.5*EducationFactor+BaseSalary

I modeled conditions and formula separately and using Interpreter Pattern.

*Condition

[url=http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/eadad349d2.jpg[/img][/url]

*Formula

[url=http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/de66e96277.jpg[/img][/url]



Now my questions:

1-Are these model Correct?

2-What are context and client in this pattern?

3-In these models , I have to model Coefficients and Items as subclass of Expression, because of Interpreter Pattern, but naturally they aren't. How can I handle this problem?

4-What are equal tables for these models?

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Patty Freeman     Answered On: Aug 07

In this Context, I notice some rules that you have.
And you wanna implement these rules into your business logic.
I suggest to you that use any Rule Engine like Drools to handle  you rules and ease of your business.

 
Answer #2    Answered By: Johnathan Nelson     Answered On: Aug 07

if the formula  has already been generated and validated, you need a parser not an interpreter to do the calculation. this pattern  only interprets the expression, you need another process to actually perform the operation / calculation on the interpreted expression. (this is arguable as both can be implemented under the same pattern)



the expression  could be parsed by a composite process to generate a binary tree (class hierarchy) like:



addition node |--- right= base salary

|--- operation = +

|--- left = multiplication node |--- right = EdutationFactor

|--- op = *

|--- left = 2.5 (literal / value)



but if this is only to validate the expression, it is the correct pattern to use, however is not going to fully do the job.



to briefly answer your questions:



1) possibly yes, you have the condition  and the expression(formula) that is generated from it

2) the context  could carry the rules such as the mapping between education factor and education that is coming from condition and so on

and also the client  can be the builder process/pattern that takes the condition and generates the expression

3) I have seen that something similar to the coefficient has been wrapped in a literal expression

4) I didn't get this question



to complete the calculation you need to generate the class hierarchy from your expression (start of my email)



others might argue that you could find alternative patterns for simplicity (spc composite pattern) which could be a valid point but I'd like to mention that your approach resembles how the OCL mathematical expressions are interpreted and mapped to rule engines but in a very very simpler way of course

 
Answer #3    Answered By: Horia Ahmed     Answered On: Aug 07

It's my entire Problem domain: I want to get formula  and conditions  from user and validate them save  them in database  and fetch them and calculate  them. And I want the class diagram and data model  of solution domain. Does the interpreter pattern  help me in anyway?

 
Answer #4    Answered By: Sophie Williamson     Answered On: Aug 07

Sorry to reply to your questions  we need to analyze your problem  more
but for now I can answer only the question you asked about context  and client

Context usually refers to a structure in which you need to transfer data to your methods and perhapse from that method to another. In your case, this context will go through all your interepret() method and will pass all of them you can send any information processed in one method through this structure to another.

Client is a class that uses the whole pattern  you are implemented. client  can be another class or another API or another application. in a single project, consider you have a button that uses your interpreter to analyze a formula  so the class in which you engaged a button to do so is the client

for more information check out this :
http://en.wikipedia.org/wiki/Interpreter_pattern

 
Didn't find what you were looking for? Find more on Problem with interpreter Pattern Or get search suggestion and latest updates.




Tagged: