private DSLMappingEntry createEntry(final String inputKey, final String inputValue) throws IOException { String mapping = "[condition][]" + inputKey + "=" + inputValue; StringReader dsl = new StringReader( mapping ); DSLMappingEntry entry = null; try { DSLTokenizedMappingFile parser = new DSLTokenizedMappingFile(); if ( parser.parseAndLoad( dsl ) ) { entry = parser.getMapping().getEntries().get( 0 ); } else { throw new RuntimeException( "Error parsing entry: " + mapping + ": " + parser.getErrors().toString() ); } } finally { dsl.close(); } return entry; }
@Before public void setUp() throws Exception { final String filename = "test_metainfo.dsl"; final Reader reader = new InputStreamReader(this.getClass().getResourceAsStream(filename)); this.file = new DSLTokenizedMappingFile(); this.tokenizedFile = new DSLTokenizedMappingFile(); this.file.parseAndLoad(reader); reader.close(); final Reader reader2 = new InputStreamReader(this.getClass().getResourceAsStream(filename)); this.tokenizedFile.parseAndLoad(reader2); reader2.close(); this.expander = new DefaultExpander(); }
@Test public void testMe() throws Exception{ DSLTokenizedMappingFile tokenizedFile = null; final String filename = "test_antlr.dsl"; final Reader reader = new InputStreamReader( this.getClass().getResourceAsStream( filename ) ); tokenizedFile = new DSLTokenizedMappingFile(); tokenizedFile.parseAndLoad( reader ); reader.close(); for (Iterator it = tokenizedFile.getMapping().getEntries().iterator(); it.hasNext();) { DSLMappingEntry entry = (DSLMappingEntry) it.next(); // System.out.println("ENTRY: " + entry.getKeyPattern() + " ::::: " + entry.getValuePattern()); } DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping( tokenizedFile.getMapping() ); System.err.println(ex.expand( "rule 'x' \n when \n address is present where name is \"foo\" and age is \"32\" \n then \n end" )); }
@Test public void testParseFile() { try { final Reader reader = new InputStreamReader( this.getClass().getResourceAsStream( this.filename ) ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); assertEquals( 31, this.file.getMapping().getEntries().size() ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
@Test public void testANTLRRegexp() throws Exception { this.expander.addDSLMapping(this.tokenizedFile.getMapping()); final Reader rules = new InputStreamReader(this.getClass().getResourceAsStream("test_expansion.dslr")); final String result = this.expander.expand(rules); }
private void logDslErrors( final DSLTokenizedMappingFile dslLoader ) { for ( final Object o : dslLoader.getErrors() ) { if ( o instanceof DSLMappingParseException ) { final DSLMappingParseException dslMappingParseException = (DSLMappingParseException) o; errors.add( "Line " + dslMappingParseException.getLine() + " : " + dslMappingParseException.getMessage() ); } else if ( o instanceof Exception ) { final Exception excp = (Exception) o; errors.add( "Exception " + excp.getClass() + " " + excp.getMessage() + " " + excp.getCause() ); } else { errors.add( "Uncategorized error " + o ); } } }
private void parseDslDefinition( final String dslDefinition ) { final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile(); try { if ( dslLoader.parseAndLoad( new StringReader( dslDefinition ) ) ) { populateDSLSentences( dslLoader ); } else { logDslErrors( dslLoader ); } } catch ( IOException e ) { errors.add( e.getMessage() ); } }
@Test public void testANTLRLineNumberError() throws Exception { DSLTokenizedMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});"; file.parseAndLoad(new StringReader(dsl)); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String source = "rule 'q'" + NL + "agenda-group 'x'" + NL + "when" + NL + " __ " + NL + "then" + NL + " bar 42" + NL + "\tgoober" + NL + "end"; ex.expand(source); assertTrue(ex.hasErrors()); assertEquals(2, ex.getErrors().size()); ExpanderException err = (ExpanderException) ex.getErrors().get(0); assertEquals(4, err.getLine()); err = (ExpanderException) ex.getErrors().get(1); assertEquals(7, err.getLine()); }
@Test public void testParseFile() { try { final Reader reader = new InputStreamReader( this.getClass().getResourceAsStream( this.filename ) ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); assertEquals( 31, this.file.getMapping().getEntries().size() ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
@Test public void testANTLRAddDSLMapping() { this.expander.addDSLMapping(this.tokenizedFile.getMapping()); // should not raise any exception }
private List<ValidationMessage> makeValidationMessages( final DSLTokenizedMappingFile dslLoader ) { final List<ValidationMessage> messages = new ArrayList<ValidationMessage>(); for ( final Object o : dslLoader.getErrors() ) { if ( o instanceof DSLMappingParseException ) { final DSLMappingParseException dslMappingParseException = (DSLMappingParseException) o; messages.add( makeNewValidationMessage( dslMappingParseException ) ); } else if ( o instanceof Exception ) { final Exception e = (Exception) o; messages.add( makeNewValidationMessage( e ) ); } else { messages.add( makeNewValidationMessage( o ) ); } } return messages; }
@Test public void testANTLRExpandParts() throws Exception { DSLTokenizedMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); //System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" )); }
private List<ValidationMessage> doValidation( final String content ) { final List<ValidationMessage> validationMessages = new ArrayList<ValidationMessage>(); final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile(); try { if ( !dslLoader.parseAndLoad( new StringReader( content ) ) ) { validationMessages.addAll( makeValidationMessages( dslLoader ) ); } return validationMessages; } catch ( IOException e ) { throw ExceptionUtilities.handleException( e ); } }
@Override public IndexBuilder fillIndexBuilder(final Path path) throws Exception { final List<String> lhs = new ArrayList<String>(); final List<String> rhs = new ArrayList<String>(); final String dsl = ioService.readAllString(path); //Construct a dummy DRL file to parse index elements final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile(); if (dslLoader.parseAndLoad(new StringReader(dsl))) { DSLMapping dslMapping = dslLoader.getMapping(); for (DSLMappingEntry e : dslMapping.getEntries()) { switch (e.getSection()) { case CONDITION: lhs.add(e.getValuePattern()); break; case CONSEQUENCE: rhs.add(e.getValuePattern()); break; default: // no-op } } final String drl = makeDrl(path, lhs, rhs); return fillDrlIndexBuilder(path, drl); } return null; }
@Test public void testExpandKeyword() throws Exception { DSLMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[keyword]key {param}=Foo( attr=={param} )"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String source = "rule x" + NL + "when" + NL + " key 1 " + NL + " key 2 " + NL + "then" + NL + "end"; String drl = ex.expand(source); System.out.println(drl); assertTrue(drl.contains("attr==1")); assertTrue(drl.contains("attr==2")); //System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" )); }
private void populateDSLSentences( final DSLTokenizedMappingFile dslLoader ) { for ( DSLMappingEntry entry : dslLoader.getMapping().getEntries() ) { if ( entry.getSection() == DSLMappingEntry.CONDITION ) { addDSLConditionSentence( entry.getMappingKey() ); } else if ( entry.getSection() == DSLMappingEntry.CONSEQUENCE ) { addDSLActionSentence( entry.getMappingKey() ); } else if ( entry.getSection() == DSLMappingEntry.KEYWORD ) { addDSLKeywordMapping( entry.getMappingKey() ); } else if ( entry.getSection() == DSLMappingEntry.ANY ) { addDSLAnyScopeMapping( entry.getMappingKey() ); } } }
private List<ValidationMessage> makeValidationMessages( final DSLTokenizedMappingFile dslLoader ) { final List<ValidationMessage> messages = new ArrayList<ValidationMessage>(); for ( final Object o : dslLoader.getErrors() ) { if ( o instanceof DSLMappingParseException ) { final DSLMappingParseException dslMappingParseException = (DSLMappingParseException) o; messages.add( makeNewValidationMessage( dslMappingParseException ) ); } else if ( o instanceof Exception ) { final Exception e = (Exception) o; messages.add( makeNewValidationMessage( e ) ); } else { messages.add( makeNewValidationMessage( o ) ); } } return messages; }
private void checkExpansion(String source, String expected) throws Exception { DSLTokenizedMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]There is an TestObject=TestObject()" + NL + "[when]-startDate is before {date}=startDate>DateUtils.parseDate(\"{date}\")" + NL + "[when]-endDate is after {date}=endDate>DateUtils.parseDate(\"{date}\")"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String drl = ex.expand(source); assertFalse(ex.hasErrors()); assertEquals(expected, drl); }
private List<ValidationMessage> doValidation( final String content ) { final List<ValidationMessage> validationMessages = new ArrayList<ValidationMessage>(); final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile(); try { if ( !dslLoader.parseAndLoad( new StringReader( content ) ) ) { validationMessages.addAll( makeValidationMessages( dslLoader ) ); } return validationMessages; } catch ( IOException e ) { throw ExceptionUtilities.handleException( e ); } }
@Override public IndexBuilder fillIndexBuilder(final Path path) throws Exception { final List<String> lhs = new ArrayList<String>(); final List<String> rhs = new ArrayList<String>(); final String dsl = ioService.readAllString(path); //Construct a dummy DRL file to parse index elements final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile(); if (dslLoader.parseAndLoad(new StringReader(dsl))) { DSLMapping dslMapping = dslLoader.getMapping(); for (DSLMappingEntry e : dslMapping.getEntries()) { switch (e.getSection()) { case CONDITION: lhs.add(e.getValuePattern()); break; case CONSEQUENCE: rhs.add(e.getValuePattern()); break; default: // no-op } } final String drl = makeDrl(path, lhs, rhs); return fillDrlIndexBuilder(path, drl); } return null; }