@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(); }
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; }
@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 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 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); }
@Test public void testANTLRExpandWithKeywordClashes() throws Exception { DSLTokenizedMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]Invoke rule executor=ruleExec: RuleExecutor()" + NL + "[then]Execute rule \"{id}\"=ruleExec.ExecuteSubRule( new Long({id}));"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String source = "package something;" + NL + NL + "rule \"1\"" + NL + "when" + NL + " Invoke rule executor" + NL + "then" + NL + " Execute rule \"5\"" + NL + "end"; String expected = "package something;" + NL + NL + "rule \"1\"" + NL + "when" + NL + " ruleExec: RuleExecutor()" + NL + "then" + NL + " ruleExec.ExecuteSubRule( new Long(5));" + NL + "end"; String drl = ex.expand(source); // System.out.println("["+drl+"]" ); // System.out.println("["+expected+"]" ); assertFalse(ex.hasErrors()); assertEquals(expected, drl); }
@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 testExpandQuery() throws Exception { DSLTokenizedMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]There is a person=Person()" + NL + "[when]- {field:\\w*} {operator} {value:\\d*}={field} {operator} {value}" + NL + "[when]is greater than=>"; String source = "query \"isMature\"" + NL + "There is a person" + NL + "- age is greater than 18" + NL + "end" + NL; String expected = "query \"isMature\"" + NL + "Person(age > 18)" + NL + "end" + NL; 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); }
@Test public void testANTLREnumExpand() throws Exception { DSLTokenizedMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]When the credit rating is {rating:ENUM:Applicant.creditRating} = applicant:Applicant(credit=={rating})"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String source = "rule \"TestNewDslSetup\"" + NL + "dialect \"mvel\"" + NL + "when" + NL + "When the credit rating is AA" + NL + "then " + NL + "end"; // String source="rule \"TestNewDslSetup\"" + NL+ // "dialect \"mvel\"" + NL+ // "when" + NL+ // "When the credit rating is OK" + NL+ // "then" + NL+ // "end" + NL; String drl = ex.expand(source); String expected = "rule \"TestNewDslSetup\"" + NL + "dialect \"mvel\"" + NL + "when" + NL + "applicant:Applicant(credit==AA)" + NL + "then " + NL + "end"; assertFalse(ex.getErrors().toString(), ex.hasErrors()); assertEquals(expected, drl); //System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" )); }
@Test public void testExpandExpr() throws Exception { DSLTokenizedMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]Name of Applicant {nameVar:CF:Applicant.age}= System.out.println({nameVar})"; String source = "rule \"test rule for custom form in DSL\"" + NL + " dialect \"mvel\"" + NL + " when" + NL + " Name of Applicant Bojan Oklahoma and NJ,Andrew AMW Test" + NL + " then" + NL + "end"; String expected = "rule \"test rule for custom form in DSL\"" + NL + " dialect \"mvel\"" + NL + " when" + NL + " System.out.println(Bojan Oklahoma and NJ,Andrew AMW Test)" + NL + " then" + NL + "end"; 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); }
file.parseAndLoad(new StringReader(dsl)); assertEquals(file.getErrors().toString(), 0,
file.parseAndLoad(new StringReader(dsl)); assertEquals(file.getErrors().toString(), 0,
@Test public void testANTLRExpandFailure() 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()); String source = "rule 'q'" + NL + "agenda-group 'x'" + NL + "when" + NL + " foo " + NL + "then" + NL + " bar 42" + NL + "end"; String drl = ex.expand(source); assertFalse(ex.hasErrors()); ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); source = "rule 'q' agenda-group 'x'" + NL + "when" + NL + " foos " + NL + "then" + NL + " bar 42" + NL + " end"; drl = ex.expand(source); //System.out.println( drl ); assertTrue(ex.hasErrors()); assertEquals(1, ex.getErrors().size()); //System.err.println(( (ExpanderException) ex.getErrors().get( 0 )).getMessage()); }
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 ); } }
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() ); } }
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 ); } }
private List<DSLMappingFile> getDSLMappingFiles(final Path path) { final List<DSLMappingFile> dsls = new ArrayList<DSLMappingFile>(); final org.uberfire.backend.vfs.Path vfsPath = Paths.convert(path); final org.uberfire.backend.vfs.Path packagePath = moduleService.resolvePackage(vfsPath).getPackageMainResourcesPath(); final Path nioPackagePath = Paths.convert(packagePath); final Collection<Path> dslPaths = fileDiscoveryService.discoverFiles(nioPackagePath, FILTER_DSLS); for (final Path dslPath : dslPaths) { final String dslDefinition = ioService.readAllString(dslPath); final DSLTokenizedMappingFile dslFile = new DSLTokenizedMappingFile(); try { if (dslFile.parseAndLoad(new StringReader(dslDefinition))) { dsls.add(dslFile); } else { logger.error("Unable to parse DSL definition: " + dslDefinition); } } catch (IOException ioe) { logger.error(ioe.getMessage()); } } return dsls; }
private List<DSLMappingFile> getDSLMappingFiles(final Path path) { final List<DSLMappingFile> dsls = new ArrayList<DSLMappingFile>(); final org.uberfire.backend.vfs.Path vfsPath = Paths.convert(path); final org.uberfire.backend.vfs.Path packagePath = moduleService.resolvePackage(vfsPath).getPackageMainResourcesPath(); final org.uberfire.java.nio.file.Path nioPackagePath = Paths.convert(packagePath); final Collection<Path> dslPaths = fileDiscoveryService.discoverFiles(nioPackagePath, FILTER_DSLS); for (final org.uberfire.java.nio.file.Path dslPath : dslPaths) { final String dslDefinition = ioService.readAllString(dslPath); final DSLTokenizedMappingFile dslFile = new DSLTokenizedMappingFile(); try { if (dslFile.parseAndLoad(new StringReader(dslDefinition))) { dsls.add(dslFile); } else { logger.error("Unable to parse DSL definition: " + dslDefinition); } } catch (IOException ioe) { logger.error(ioe.getMessage()); } } return dsls; } }
private List<DSLMappingFile> getDSLMappingFiles(final Path path) { final List<DSLMappingFile> dsls = new ArrayList<DSLMappingFile>(); final org.uberfire.backend.vfs.Path vfsPath = Paths.convert(path); final org.uberfire.backend.vfs.Path packagePath = moduleService.resolvePackage(vfsPath).getPackageMainResourcesPath(); final org.uberfire.java.nio.file.Path nioPackagePath = Paths.convert(packagePath); final Collection<Path> dslPaths = fileDiscoveryService.discoverFiles(nioPackagePath, FILTER_DSLS); for (final org.uberfire.java.nio.file.Path dslPath : dslPaths) { final String dslDefinition = ioService.readAllString(dslPath); final DSLTokenizedMappingFile dslFile = new DSLTokenizedMappingFile(); try { if (dslFile.parseAndLoad(new StringReader(dslDefinition))) { dsls.add(dslFile); } else { logger.error("Unable to parse DSL definition: " + dslDefinition); } } catch (IOException ioe) { logger.error(ioe.getMessage()); } } return dsls; } }
private List<DSLMappingFile> getDSLMappingFiles(final Path path) { final List<DSLMappingFile> dsls = new ArrayList<DSLMappingFile>(); final org.uberfire.backend.vfs.Path vfsPath = Paths.convert(path); final org.uberfire.backend.vfs.Path packagePath = moduleService.resolvePackage(vfsPath).getPackageMainResourcesPath(); final org.uberfire.java.nio.file.Path nioPackagePath = Paths.convert(packagePath); final Collection<Path> dslPaths = fileDiscoveryService.discoverFiles(nioPackagePath, FILTER_DSLS); for (final org.uberfire.java.nio.file.Path dslPath : dslPaths) { final String dslDefinition = ioService.readAllString(dslPath); final DSLTokenizedMappingFile dslFile = new DSLTokenizedMappingFile(); try { if (dslFile.parseAndLoad(new StringReader(dslDefinition))) { dsls.add(dslFile); } else { logger.error("Unable to parse DSL definition: " + dslDefinition); } } catch (IOException ioe) { logger.error(ioe.getMessage()); } } return dsls; } }