public EntitlementCombiner evaluate(XACMLEvalContext pip) {

    boolean indeterminate = true;
    FunctionArgument evalResult = null;
    EntitlementCombiner results = CombinerManager.getInstance(ruleCombiner);
    pip.setPolicyRef(this);

    System.out.println("Evaluating Policy " + policyName);
    try {
      evalResult = target.evaluate(pip);
    } catch (XACML3EntitlementException ex) {
      XACML3Decision result =
          new XACML3Decision(policyName, pip.getRequest().getContextID(), "Indeterminate");
      results.add(result);
      return results;
    }

    if (evalResult.isTrue()) { // we  match,  so evaluate
      System.out.println("Target true, evaluating rules ");
      for (XACML3PolicyRule r : rules) {
        XACML3Decision decision = r.evaluate(pip);
        results.add(decision);
      }
    } else {
      XACML3Decision result =
          new XACML3Decision(policyName, pip.getRequest().getContextID(), "NotApplicable");
      results.add(result);
    }
    return results;
  }
  public String asRPNExpression() {
    String retVal = policyName + "\n\n";

    retVal = retVal + target.asRPNExpression() + "\n\n";

    for (XACML3PolicyRule r : rules) {
      retVal = retVal + r.asRPNExpression() + "\n\n";
    }

    return retVal;
  }
  public XACMLRootElement getXACMLRoot() {
    Policy policy = new Policy();

    policy.setPolicyId(policyName);
    policy.setRuleCombiningAlgId(ruleCombiner);
    policy.setTarget((Target) target.getXACMLRoot());

    List<Object> elements =
        policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();

    for (XACML3PolicyRule r : rules) {
      elements.add(r.getXACMLRoot());
    }

    return policy;
  }
  public JSONObject toJSONObject() throws JSONException {
    JSONObject jo = new JSONObject();
    jo.put("classname", this.getClass().getName());

    jo.put("policyName", policyName);
    jo.put("ruleCombiner", ruleCombiner);
    jo.put("target", target.toJSONObject());

    Set<String> keys = definedVars.keySet();
    JSONObject dv = new JSONObject();

    for (String k : keys) {
      dv.put(k, definedVars.get(k).toJSONObject());
    }
    jo.put("definedVars", dv);

    for (XACML3PolicyRule r : rules) {
      jo.append("rules", r.toJSONObject());
    }
    return jo;
  }
  public void init(JSONObject jo) throws JSONException {
    policyName = jo.optString("policyName");
    ruleCombiner = jo.optString("ruleCombiner");

    target = FunctionArgument.getInstance(jo.getJSONObject("target"));
    definedVars = new HashMap<String, FunctionArgument>();

    JSONObject dv = jo.getJSONObject("definedVars");
    Iterator iter = dv.keys();
    while (iter.hasNext()) {
      String s = (String) iter.next();
      definedVars.put(s, FunctionArgument.getInstance(dv.getJSONObject(s)));
    }

    rules = new ArrayList<XACML3PolicyRule>();

    JSONArray array = jo.getJSONArray("rules");
    for (int i = 0; i < array.length(); i++) {
      JSONObject json = (JSONObject) array.get(i);
      rules.add(XACML3PolicyRule.getInstance(json));
    }
  }