Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.drools.workbench.models.guided.dtable.backend.util;
 
 
 import java.util.List;

Utility class to build Field Constraints for cells with "Otherwise" values
 
 public class GuidedDTDRLOtherwiseHelper {

    
OtherwiseBuilder for equals operators. This assembles a comma separated list of non-null values contained in other cells in the column and constructs a FieldConstraint with operator "not in" and the derived list: e.g. not in ("a", "b", "c")
 
     public static class EqualsOtherwiseBuilder extends AbstractOtherwiseBuilder {
 
         private EqualsOtherwiseBuilder() {
         }
 
         @Override
                                                         List<DTCellValue52columnData ) {
             SingleFieldConstraint sfc = new SingleFieldConstraintc.getFactField() );
             sfc.setConstraintValueTypec.getConstraintValueType() );
             sfc.setFieldTypec.getFieldType() );
             sfc.setOperator"not in" );
 
             List<StringconsumedValues = new ArrayList<String>();
             StringBuilder value = new StringBuilder();
             value.append"( " );
             for ( DTCellValue52 cv : columnData ) {
 
                 //Ensure cell values start and end with quotes
                 String scv = GuidedDTDRLUtilities.convertDTCellValueToStringcv );
                 if ( scv != null ) {
                     if ( !consumedValues.containsscv ) ) {
                         value.appendscv ).append", " );
                     }
                     consumedValues.addscv );
                 }
             }
             value.deletevalue.lastIndexOf"," ),
                           value.length() - 1 );
             value.append")" );
             sfc.setValuevalue.toString() );
             return sfc;
         }
 
     }

    
OtherwiseBuilder for not-equals operators. This assembles a comma separated list of non-null values contained in other cells in the column and constructs a FieldConstraint with operator "in" and the derived list: e.g. in ("a", "b", "c")
 
     public static class NotEqualsOtherwiseBuilder extends AbstractOtherwiseBuilder {
 
         private NotEqualsOtherwiseBuilder() {
         }
 
         @Override
                                                               List<DTCellValue52columnData ) {
             SingleFieldConstraint sfc = new SingleFieldConstraintc.getFactField() );
             sfc.setConstraintValueTypec.getConstraintValueType() );
             sfc.setFieldTypec.getFieldType() );
             sfc.setOperator"in" );
 
             List<StringconsumedValues = new ArrayList<String>();
             StringBuilder value = new StringBuilder();
             value.append"( " );
             for ( DTCellValue52 cv : columnData ) {
 
                 //Ensure cell values start and end with quotes
                 String scv = GuidedDTDRLUtilities.convertDTCellValueToStringcv );
                if ( scv != null ) {
                    if ( !consumedValues.containsscv ) ) {
                        value.appendscv ).append", " );
                    }
                    consumedValues.addscv );
                }
            }
            value.deletevalue.lastIndexOf"," ),
                          value.length() - 1 );
            value.append")" );
            sfc.setValuevalue.toString() );
            return sfc;
        }
    }

    
Interface defining a factory method to build a FieldConstraint
    public interface OtherwiseBuilder {

        
Build a Field Constraint

Parameters:
c Condition Column that contains the "Otherwise" cell
allColumns All Decision Table columns. Decision Tables have an implied "and" between multiple SingleFieldConstraints for the same Fact field. OtherwiseBuilders for less-than, greater-than etc need access to other Condition Columns bound to the same Fact and Field to construct a CompositeFieldConstraint.
data Decision Table values
Returns:
                                             List<BaseColumnallColumns,
                                             List<List<DTCellValue52>> data );
    }

    
Base OtherwiseBuilder that extracts a single column of data relating to the ConditionCol from which the FieldConstraint will be constructed. This will need to be re-factored if it is agreed that the implementation of "Otherwise" for certain operators needs to provide CompositeFieldConstraints.
    static abstract class AbstractOtherwiseBuilder
            implements
            OtherwiseBuilder {
                                                    List<BaseColumnallColumns,
                                                    List<List<DTCellValue52>> data ) {
            int index = allColumns.indexOfc );
            List<DTCellValue52columnData = extractColumnDatadata,
                                                                index );
            return constructSingleFieldConstraintc,
                                                   columnData );
        }
        //Template pattern, provide method for implementations to override
                                                                 List<DTCellValue52columnData );
    }

    
Retrieve the correct OtherwiseBuilder for the given column

Parameters:
c
Returns:
    public static OtherwiseBuilder getBuilderConditionCol52 c ) {
        if ( c.getOperator().equals"==" ) ) {
            return new EqualsOtherwiseBuilder();
        } else if ( c.getOperator().equals"!=" ) ) {
            return new NotEqualsOtherwiseBuilder();
        }
        throw new IllegalArgumentException"ConditionCol operator does not support Otherwise values" );
    }
    //Extract data relating to a single column
    private static List<DTCellValue52extractColumnDataList<List<DTCellValue52>> data,
                                                          int columnIndex ) {
        List<DTCellValue52columnData = new ArrayList<DTCellValue52>();
        for ( List<DTCellValue52row : data ) {
            columnData.addrow.getcolumnIndex ) );
        }
        return columnData;
    }
    //Utility factory class, no constructor
    private GuidedDTDRLOtherwiseHelper() {
    }
New to GrepCode? Check out our FAQ X