/** * @param idx Remove this index from the LHS. returns false if it was NOT allowed to remove this * item (ie it is used on the RHS). */ public boolean removeLhsItem(final int idx) { final IPattern[] newList = new IPattern[this.lhs.length - 1]; int newIdx = 0; for (int i = 0; i < this.lhs.length; i++) { if (i != idx) { newList[newIdx] = this.lhs[i]; newIdx++; } else { IPattern pat = this.lhs[i]; if (pat instanceof FromCompositeFactPattern) { pat = ((FromCompositeFactPattern) pat).getFactPattern(); } if (pat instanceof FactPattern) { final FactPattern p = (FactPattern) pat; if (p.getBoundName() != null && isBoundFactUsed(p.getBoundName())) { return false; } } } } this.lhs = newList; return true; }
/** * This will return the FactPattern that a variable is bound to. If the variable is bound to a * FieldConstraint the parent FactPattern will be returned. * * @param var The variable binding * @return null or the FactPattern found. */ public FactPattern getLHSParentFactPatternForBinding(final String var) { if (this.lhs == null) { return null; } for (int i = 0; i < this.lhs.length; i++) { IPattern pat = this.lhs[i]; if (pat instanceof FromCompositeFactPattern) { pat = ((FromCompositeFactPattern) pat).getFactPattern(); } if (pat instanceof FactPattern) { final FactPattern p = (FactPattern) pat; if (p.getBoundName() != null && var.equals(p.getBoundName())) { return p; } for (int j = 0; j < p.getFieldConstraints().length; j++) { FieldConstraint fc = p.getFieldConstraints()[j]; List<String> fieldBindings = getFieldBinding(fc); if (fieldBindings.contains(var)) { return p; } } } } return null; }
/** * This will return the FactPattern that a variable is bound Eto. * * @param var The bound fact variable (NOT bound field). * @return null or the FactPattern found. */ public FactPattern getLHSBoundFact(final String var) { if (this.lhs == null) { return null; } for (int i = 0; i < this.lhs.length; i++) { IPattern pat = this.lhs[i]; if (pat instanceof FromCompositeFactPattern) { pat = ((FromCompositeFactPattern) pat).getFactPattern(); } if (pat instanceof FactPattern) { final FactPattern p = (FactPattern) pat; if (p.getBoundName() != null && var.equals(p.getBoundName())) { return p; } } } return null; }
/** * This will return a List<String> of all FactPattern bindings * * @return The bindings or an empty list if no bindings are found. */ public List<String> getLHSBoundFacts() { if (this.lhs == null) { return Collections.emptyList(); } final List<String> list = new ArrayList<String>(); for (int i = 0; i < this.lhs.length; i++) { IPattern pat = this.lhs[i]; if (pat instanceof FromCompositeFactPattern) { pat = ((FromCompositeFactPattern) pat).getFactPattern(); } if (pat instanceof FactPattern) { final FactPattern p = (FactPattern) pat; if (p.getBoundName() != null) { list.add(p.getBoundName()); } } } return list; }
/** This will get a list of all LHS bound variables, including bound fields.. */ public List<String> getAllLHSVariables() { List<String> result = new ArrayList<String>(); for (int i = 0; i < this.lhs.length; i++) { IPattern pat = this.lhs[i]; if (pat instanceof FromCompositeFactPattern) { pat = ((FromCompositeFactPattern) pat).getFactPattern(); } if (pat instanceof FactPattern) { FactPattern fact = (FactPattern) pat; if (fact.isBound()) { result.add(fact.getBoundName()); } for (int j = 0; j < fact.getFieldConstraints().length; j++) { FieldConstraint fc = fact.getFieldConstraints()[j]; if (fc instanceof SingleFieldConstraintEBLeftSide) { SingleFieldConstraintEBLeftSide exp = (SingleFieldConstraintEBLeftSide) fc; if (exp.getExpressionLeftSide() != null && exp.getExpressionLeftSide().isBound()) { result.add(exp.getExpressionLeftSide().getBinding()); } } else if (fc instanceof SingleFieldConstraint) { SingleFieldConstraint con = (SingleFieldConstraint) fc; if (con.isBound()) { result.add(con.getFieldBinding()); } if (con.getExpressionValue() != null && con.getExpressionValue().isBound()) { result.add(con.getExpressionValue().getBinding()); } } } } } return result; }
/** * Get the data-type associated with the binding * * @param var * @return The data-type, or null if the binding could not be found */ public String getLHSBindingType(final String var) { if (this.lhs == null) { return null; } for (int i = 0; i < this.lhs.length; i++) { IPattern pat = this.lhs[i]; if (pat instanceof FromCompositeFactPattern) { pat = ((FromCompositeFactPattern) pat).getFactPattern(); } if (pat instanceof FactPattern) { final FactPattern p = (FactPattern) pat; if (p.isBound() && var.equals(p.getBoundName())) { return p.getFactType(); } for (FieldConstraint fc : p.getFieldConstraints()) { String type = getFieldBinding(fc, var); if (type != null) { return type; } } } } return null; }
/** * This uses a deceptively simple algorithm to determine what bound variables are in scope for a * given constraint (including connectives). Does not take into account globals. */ public List<String> getBoundVariablesInScope(final BaseSingleFieldConstraint con) { final List<String> result = new ArrayList<String>(); for (int i = 0; i < this.lhs.length; i++) { IPattern pat = this.lhs[i]; if (pat instanceof FromCompositeFactPattern) { pat = ((FromCompositeFactPattern) pat).getFactPattern(); } if (pat instanceof FactPattern) { final FactPattern fact = (FactPattern) pat; if (fact.getConstraintList() != null) { final FieldConstraint[] cons = fact.getConstraintList().getConstraints(); if (cons != null) { for (int k = 0; k < cons.length; k++) { FieldConstraint fc = cons[k]; if (fc instanceof SingleFieldConstraint) { final SingleFieldConstraint c = (SingleFieldConstraint) fc; if (c == con) { return result; } if (c.getConnectives() != null) { for (int j = 0; j < c.getConnectives().length; j++) { if (con == c.getConnectives()[j]) { return result; } } } if (c.isBound()) { result.add(c.getFieldBinding()); } } } } if (fact.isBound()) { result.add(fact.getBoundName()); } } else { if (fact.isBound()) { result.add(fact.getBoundName()); } } } } return result; }