Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Well I'm working on a personal project that needs to check if a user has met certain requirements, and they come in a form of

Requirement: [c1 OR c2] AND [d1 OR d2]

Requirement: [c1 AND c2] OR [d1 AND d2]

Requirement: c1 AND any dn(n can be any integer)

I'm just not sure how to store these sorts of requirements, I'm thinking of using another object to hold c1,c2,d1,d2....dn and OR, but that seems like a roundabout way of doing things. Is there a better method?

share|improve this question
1  
What exactly do you mean by "storing a requirement"? Are you talking about "implementation of a given specification in a programming language" or about "creating a specification"? –  k3b Jun 17 '12 at 6:20
add comment

4 Answers 4

Typically, expressions are represented by abstract syntax trees.

public interface Expression {};
public class AND implements Expression {
    public Expression lhs;
    public Expression rhs;
}
public class OR implements Expression {
    public Expression lhs;
    public Expression rhs;
}
public class Requirement implements Expression {
    ...
}
share|improve this answer
    
+1 for ASTs. Also, take a look at FOPL (en.wikipedia.org/wiki/First-order_logic) and implementations thereof, because it looks like you also need quantifiers. –  vski Jun 16 '12 at 10:06
add comment

You can use Reverse Polish Notation for storing and evaluating your requirements. For your first requirement the sequence is basically:

c1 c2 OR d1 d2 OR AND

As you see you don't need brackets so the storage format is simple. The evaluation is also very easy to implement, you just need a stack object to push parameter values and binary results to the stack, and to pop and evaluate parameters when an operator is met.

share|improve this answer
add comment

IOFileFilter and its subclasses (for example, AndFileFilter, NotFileFilter and OrFileFilter) in Apache Commons IO could be a good example.

share|improve this answer
add comment

Think of [c1 OR c2] AND [d1 OR d2] as a single requirement. Consider c1, c2, d1, d2 as requirements, say sub requirements or atomic requirements. Now they all have one thing in common. They can be checked if they are met. So let's introduce an interface for that:

public interface Requirement {
    public boolean isMet();
}

Now atomic requirements can be implemented like

public class AtomicRequirement implements Requirement {
    private final boolean met;

    public AtomicRequirement(boolean isMet) {
        this.met = isMet;
    }

    public boolean isMet() {
        return true;
    }
}

You can add compound requirements and build upon the Requirement interface.

public class AndRequirement implements Requirement {
    private final Requirement r1, r2;

    public AndRequirement(Requirement r1, Requirement r2) {
        this.r1 = r1;
        this.r2 = r2;
    }

    public boolean isMet() {
        return r1.isMet() && r2.isMet();
    }
}

Same goes for OR. You can do it with more complex combinators

public class AnyOfTheseRequirement implements Requirement {
    private final List<Requirement> anyOfThese;

    public AnyOfTheseRequirement(List<Requirement> anyOfThese) {
        this.anyOfThese = anyOfThese;
    }

    public boolean isMet() {
        for (Requirement r : anyofThese) {
            if (r.isMet()) return true;
        }
        return false;
    }

}

and of course

public class MakseeRequirement implements Requirement {
    private final AndRequirement r;

    public MakseeRequirement(Requirement c1, List<Requirement> dns) {
        AnyOfTheseRequirement d = new AnyOfTheseRequirement(dns);
        this.r = new AndRequirement(c1, d);
    }

    public boolean isMet() {
        return r.isMet();
    }

}

This approach is quite extendable but you can trim it down to the three actual types of requirements you got.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.