/** * 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 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 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(); }
/** * 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(); }
if ( getValues().isEmpty() ) { return fieldValueMap;
if ( getValues().isEmpty() ) { return fieldValueMap;
@Test public void testDSLComplexVariableValueInterpolation() { final DSLSentence sen = new DSLSentence(); sen.setDefinition( "a {here} and {here}" ); sen.getValues().set( 0, new DSLComplexVariableValue("123","word")); sen.getValues().set( 1, new DSLComplexVariableValue("some-other-value","word") ); assertEquals( "a word and word", sen.interpolate() ); }
DSLSentence dsl0 = new DSLSentence(); dsl0.setDefinition( "DSL Sentence 0" ); dsl0.getValues().add( new DSLVariableValue( "dsl0v0" ) ); dsl0.getValues().add( new DSLVariableValue( "dsl0v1" ) ); model.lhs[0] = dsl0; DSLSentence dsl1 = new DSLSentence(); dsl1.setDefinition( "DSL Sentence 1" ); dsl1.getValues().add( new DSLVariableValue( "dsl1v0" ) ); dsl1.getValues().add( new DSLVariableValue( "dsl1v1" ) ); model.lhs[1] = dsl1; dsl0Clone.getValues().size() ); assertNotSame( dsl0.getValues().get( 0 ), dsl0Clone.getValues().get( 0 ) ); assertNotNull( dsl0Clone.getValues().get( 0 ) ); assertTrue( dsl0Clone.getValues().get( 0 ) instanceof DSLVariableValue ); DSLVariableValue dsl0v0Clone = (DSLVariableValue) dsl0Clone.getValues().get( 0 ); assertEquals( dsl0.getValues().get( 0 ).getValue(), dsl0v0Clone.getValue() ); assertNotSame( dsl0.getValues().get( 1 ), dsl0Clone.getValues().get( 1 ) ); assertNotNull( dsl0Clone.getValues().get( 1 ) ); assertTrue( dsl0Clone.getValues().get( 1 ) instanceof DSLVariableValue ); DSLVariableValue dsl0v1Clone = (DSLVariableValue) dsl0Clone.getValues().get( 1 ); assertEquals( dsl0.getValues().get( 1 ).getValue(), dsl0v1Clone.getValue() ); dsl1Clone.getValues().size() );
@Test public void testEnumSentenceContainingRegEx() { final DSLSentence sen = new DSLSentence(); sen.setDefinition( "When a person exists with social security number \"{chooseSSN:\\d{3}-\\d{2}-\\d{4}}\"" ); sen.getValues().set( 0, new DSLVariableValue("333-22-4444")); assertEquals( "When a person exists with social security number \"333-22-4444\"", sen.interpolate() ); }
@Test public void testEnumSentenceWithDate() { final DSLSentence sen = new DSLSentence(); sen.setDefinition( "When a person was born on \"{dob:DATE:default}\"" ); sen.getValues().set( 0, new DSLVariableValue("31-Dec-1999")); assertEquals( "When a person was born on \"31-Dec-1999\"", sen.interpolate() ); }
@Test public void testEnumSentenceWithBoolean() { final DSLSentence sen = new DSLSentence(); sen.setDefinition( "When a person is alive {alive:BOOLEAN:checked}" ); sen.getValues().set( 0, new DSLVariableValue("true" )); assertEquals( "When a person is alive true", sen.interpolate() ); }
@Test public void testInterpolate1() { final DSLSentence sen = new DSLSentence(); sen.setDefinition( "{something} here" ); sen.getValues().set( 0, new DSLVariableValue("word")); assertEquals( "word here", sen.interpolate() ); }
@Test public void testInterpolate2() { final DSLSentence sen = new DSLSentence(); sen.setDefinition( "a {here}" ); sen.getValues().set( 0, new DSLVariableValue("word")); assertEquals( "a word", sen.interpolate() ); }
@Test public void testEnumSentenceWithEnumeration() { final DSLSentence sen = new DSLSentence(); sen.setDefinition( "When a person is \"{rating:ENUM:Person.gender}\"" ); sen.getValues().set( 0, new DSLVariableValue("Male")); assertEquals( "When a person is \"Male\"", sen.interpolate() ); }
dslPattern.getDefinition() ); assertEquals( 0, dslPattern.getValues().size() ); dslAction.getDefinition() ); assertEquals( 2, dslAction.getValues().size() ); assertTrue( dslAction.getValues().get( 0 ) instanceof DSLVariableValue ); assertTrue( dslAction.getValues().get( 1 ) instanceof DSLVariableValue ); dslAction.getValues().get( 0 ).getValue() ); assertEquals( "myout", dslAction.getValues().get( 1 ).getValue() );
dslPattern.getDefinition() ); assertEquals( 0, dslPattern.getValues().size() ); dslAction.getDefinition() ); assertEquals( 2, dslAction.getValues().size() ); assertTrue( dslAction.getValues().get( 0 ) instanceof DSLVariableValue ); assertTrue( dslAction.getValues().get( 1 ) instanceof DSLVariableValue ); dslAction.getValues().get( 0 ).getValue() ); assertEquals( "myout", dslAction.getValues().get( 1 ).getValue() );
@Test public void testSentence() { final DSLSentence sen = new DSLSentence(); sen.setDefinition( "this is {something} here and {here}" ); assertEquals( "this is {something} here and {here}", sen.toString() ); sen.setDefinition( "foo bar" ); assertEquals( "foo bar", sen.toString() ); final DSLSentence newOne = sen.copy(); assertFalse( newOne == sen ); assertEquals( newOne.getDefinition(), sen.getDefinition() ); assertEquals( newOne.getValues(), sen.getValues() ); }