use of org.drools.verifier.components.NumberRestriction in project drools by kiegroup.
the class FindMissingNumber method testForPattern.
/**
* Test if the values in constraints are in pattern.
*
* @param restrictions
* @return false if can't find a pattern or constraints list is null or size
* of the list is under 3.
*/
public static Number testForPattern(Collection<NumberRestriction> restrictions) {
if (restrictions == null || restrictions.size() < MIN_NUMBER_OF_RESTRICTIONS) {
return null;
}
BigDecimal[] numbers = new BigDecimal[restrictions.size()];
int index = 0;
for (NumberRestriction restriction : restrictions) {
if (restriction.isInt()) {
numbers[index++] = BigDecimal.valueOf(restriction.getValue().intValue());
} else {
numbers[index++] = BigDecimal.valueOf(restriction.getValue().doubleValue());
}
}
Arrays.sort(numbers);
Number missingNumber = findSumPattern(numbers);
if (missingNumber != null) {
return missingNumber;
} else {
missingNumber = findMultiplicationPattern(numbers);
if (missingNumber != null) {
return missingNumber;
}
}
return null;
}
use of org.drools.verifier.components.NumberRestriction in project drools by kiegroup.
the class DataRow method visitRestrictionsCollection.
public static Collection<String> visitRestrictionsCollection(String sourceFolder, Collection<Restriction> restrictions, Collection<MissingRange> causes) {
Multimap<String, DataRow> dt = TreeMultimap.create();
Collection<String> stringRows = new ArrayList<String>();
for (MissingRange cause : causes) {
dt.put(cause.getValueAsString(), new DataRow(null, null, cause.getOperator(), cause.getValueAsString()));
}
for (Restriction r : restrictions) {
if (r instanceof NumberRestriction) {
try {
NumberRestriction restriction = (NumberRestriction) r;
dt.put(restriction.getValue().toString(), new DataRow(restriction.getRulePath(), restriction.getRuleName(), restriction.getOperator(), restriction.getValueAsString()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
DataRow previous = null;
for (Iterator<DataRow> iterator = dt.values().iterator(); iterator.hasNext(); ) {
DataRow current = iterator.next();
if (previous != null) {
// Check if previous and current are from the same rule.
if (previous.ruleId == null && current.ruleId == null && !previous.operator.equals(Operator.EQUAL) && !previous.operator.equals(Operator.NOT_EQUAL) && !current.operator.equals(Operator.EQUAL) && !current.operator.equals(Operator.NOT_EQUAL)) {
// Combine these two.
stringRows.add("Missing : " + previous + " .. " + current);
if (iterator.hasNext())
current = iterator.next();
} else if (previous.ruleId != null && previous.ruleId.equals(current.ruleId)) {
// Combine these two.
stringRows.add(UrlFactory.getRuleUrl(sourceFolder, current.ruleId, current.ruleName) + " : " + previous.toString() + " " + current.toString());
if (iterator.hasNext())
current = iterator.next();
} else if (!iterator.hasNext()) {
// If this is last row
// Print previous and current if they were not merged.
processRangeOutput(previous, stringRows, sourceFolder);
processRangeOutput(current, stringRows, sourceFolder);
} else {
// If they are not from the same rule
// Print previous.
processRangeOutput(previous, stringRows, sourceFolder);
}
} else if (!iterator.hasNext()) {
processRangeOutput(current, stringRows, sourceFolder);
}
// Set current as previous.
previous = current;
}
return stringRows;
}
Aggregations