/** * This is used by the GUI when adding a sentence to LHS or RHS. * @return */ public DSLSentence copy() { final DSLSentence newOne = new DSLSentence(); newOne.definition = getDefinition(); List<DSLVariableValue> variableValues = getValues(); if ( variableValues != null ) { for ( DSLVariableValue value : getValues() ) { newOne.getValues().add( value ); } } return newOne; }
/** * This will go through the widgets and extract the values */ protected void updateSentence() { int iVariable = 0; for ( Iterator<Widget> iter = widgets.iterator(); iter.hasNext(); ) { Widget wid = iter.next(); if ( wid instanceof DSLVariableEditor ) { sentence.getValues().set( iVariable++, ( (DSLVariableEditor) wid ).getSelectedValue() ); } } this.setModified( true ); }
/** * This will strip off any "{" stuff, substituting values accordingly */ public String interpolate() { getValues(); if ( definition == null ) { return ""; } int variableStart = definition.indexOf( "{" ); if ( variableStart < 0 ) { return definition; } int index = 0; int variableEnd = 0; StringBuilder sb = new StringBuilder(); while ( variableStart >= 0 ) { sb.append( definition.substring( variableEnd, variableStart ) ); variableEnd = getIndexForEndOfVariable( definition, variableStart ) + 1; variableStart = definition.indexOf( "{", variableEnd ); sb.append( values.get( index++ ).getValue() ); } if ( variableEnd < definition.length() ) { sb.append( definition.substring( variableEnd ) ); } return sb.toString(); }
private DSLSentence toDSLSentence( List<String> dslPatterns, String dslLine ) { DSLSentence dslSentence = new DSLSentence(); for ( String dslPattern : dslPatterns ) { String regex = dslPattern.replaceAll( "\\{\\s*[\\:\\.\\w]+\\s*\\}", "(.*)" ); Matcher m = Pattern.compile( regex ).matcher( dslLine ); if ( m.matches() ) { dslSentence.setDefinition( dslPattern ); for ( int i = 0; i < m.groupCount(); i++ ) { dslSentence.getValues().get( i ).setValue( m.group( i + 1 ) ); } return dslSentence; } } dslSentence.setDefinition( dslLine ); return dslSentence; }
if ( getValues().isEmpty() ) { return fieldValueMap;
List<DSLVariableValue> dslValues = sentence.getValues(); int index = 0;
final List<DSLVariableValue> values = dsl.getValues(); assertEquals( 1, values.size() ); dsl.getValues().get( 0 ).setValue( "AA" ); assertEquals( 1, dslSentence.getValues().size() ); assertTrue( dslSentence.getValues().get( 0 ) instanceof DSLComplexVariableValue ); DSLComplexVariableValue dslComplexVariableValue = (DSLComplexVariableValue) dslSentence.getValues().get( 0 ); assertEquals( "AA", dslComplexVariableValue.getValue() ); assertEquals( "ENUM:Applicant.creditRating", dslComplexVariableValue.getId() );
@Test public void testDSLExpansionLHS() { final String dslDefinition = "The credit rating is {rating:ENUM:Applicant.creditRating}"; final String drlExpected = "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "The credit rating is AA\n" + "then\n" + "end"; final DSLSentence dsl = new DSLSentence(); dsl.setDefinition( dslDefinition ); //The following line is normally performed by the UI when the user sets values dsl.getValues().get( 0 ).setValue( "AA" ); //Append DSL to RuleModel to check marshalling final RuleModel m = new RuleModel(); m.name = "r1"; m.addLhsItem( dsl ); final String drlActual = brlPersistence.marshal( m ); assertEqualsIgnoreWhitespace( drlExpected, drlActual ); }