use of org.drools.compiler.compiler.ParserError in project drools by kiegroup.
the class KnowledgeBuilderImpl method validateUniqueRuleNames.
protected void validateUniqueRuleNames(final PackageDescr packageDescr) {
final Set<String> names = new HashSet<String>();
PackageRegistry packageRegistry = this.pkgRegistryMap.get(packageDescr.getNamespace());
InternalKnowledgePackage pkg = null;
if (packageRegistry != null) {
pkg = packageRegistry.getPackage();
}
for (final RuleDescr rule : packageDescr.getRules()) {
validateRule(packageDescr, rule);
final String name = rule.getName();
if (names.contains(name)) {
addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
}
if (pkg != null) {
RuleImpl duplicatedRule = pkg.getRule(name);
if (duplicatedRule != null) {
Resource resource = rule.getResource();
Resource duplicatedResource = duplicatedRule.getResource();
if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath() == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
addBuilderResult(new DuplicateRule(rule, packageDescr, this.configuration));
} else {
addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
}
}
}
names.add(name);
}
}
use of org.drools.compiler.compiler.ParserError in project drools by kiegroup.
the class KnowledgeBuilderImpl method generatedDrlToPackageDescr.
private PackageDescr generatedDrlToPackageDescr(Resource resource, String generatedDrl) throws DroolsParserException {
// dump the generated DRL if the dump dir was configured
if (this.configuration.getDumpDir() != null) {
dumpDrlGeneratedFromDTable(this.configuration.getDumpDir(), generatedDrl, resource.getSourcePath());
}
DrlParser parser = new DrlParser(configuration.getLanguageLevel());
PackageDescr pkg = parser.parse(resource, new StringReader(generatedDrl));
this.results.addAll(parser.getErrors());
if (pkg == null) {
addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
} else {
pkg.setResource(resource);
}
return parser.hasErrors() ? null : pkg;
}
use of org.drools.compiler.compiler.ParserError in project drools by kiegroup.
the class KnowledgeBuilderImpl method addPackageFromDrl.
/**
* Load a rule package from DRL source and associate all loaded artifacts
* with the given resource.
*
* @param reader
* @param sourceResource the source resource for the read artifacts
* @throws DroolsParserException
* @throws IOException
*/
public void addPackageFromDrl(final Reader reader, final Resource sourceResource) throws DroolsParserException, IOException {
this.resource = sourceResource;
final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
final PackageDescr pkg = parser.parse(sourceResource, reader);
this.results.addAll(parser.getErrors());
if (pkg == null) {
addBuilderResult(new ParserError(sourceResource, "Parser returned a null Package", 0, 0));
}
if (!parser.hasErrors()) {
addPackage(pkg);
}
this.resource = null;
}
use of org.drools.compiler.compiler.ParserError in project drools by kiegroup.
the class DSLTokenizedMappingFile method parseAndLoad.
@Override
public boolean parseAndLoad(Reader dsl) throws IOException {
List<ParserError> errors = new ArrayList<ParserError>();
String text = readFile(dsl);
dsl = new StringReader(text);
try {
DSLMapping mapping = buildFileMapping(errors, dsl);
mapping.setOptions(optionSet);
setMapping(mapping);
List<ParserError> moderr = new ArrayList<ParserError>();
for (ParserError err : errors) {
int row = err.getRow();
int col = err.getCol();
if (row > 0) {
int len;
while ((len = lineLengths.get(row)) < col) {
col -= len + 1;
row++;
}
}
moderr.add(new ParserError(err.getMessage(), row, col));
}
errors = moderr;
} catch (Exception e) {
final String msg = "Error parsing DSL mapping: " + e.getMessage();
ParserError parserError = new ParserError(msg, -1, 0);
errors.add(parserError);
}
setErrors(errors);
return errors.isEmpty();
}
use of org.drools.compiler.compiler.ParserError in project drools by kiegroup.
the class ParserTest method testErrorsParser.
@Test
public void testErrorsParser() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
assertEquals(0, parser.getErrors().size());
parser.parse(new InputStreamReader(getClass().getResourceAsStream("errors_parser_multiple.drl")));
assertTrue(parser.hasErrors());
assertTrue(parser.getErrors().size() > 0);
assertTrue(parser.getErrors().get(0) instanceof ParserError);
final ParserError first = ((ParserError) parser.getErrors().get(0));
assertTrue(first.getMessage() != null);
assertFalse(first.getMessage().equals(""));
}
Aggregations