Search in sources :

Example 1 with PackageDescrIndexVisitor

use of org.kie.workbench.common.services.refactoring.backend.server.indexing.PackageDescrIndexVisitor in project kie-wb-common by kiegroup.

the class AbstractDrlFileIndexer method fillDrlIndexBuilder.

/**
 * All Drools-related {@link Indexer} implementations end up extracting the DRL from the related Rule representation
 * (see {@link ResourceType}).
 * </p>
 * The following method then parses the DRL and returns all relevant reference information.
 * @param path The {@link Path} of the asset/resource, necessary for extracting reference information.
 * @param drl A {@link String} representation of the DRL.
 * @return The {@link DefaultIndexBuilder}
 * @throws Exception
 */
public DefaultIndexBuilder fillDrlIndexBuilder(final Path path, final String drl) throws Exception {
    final DrlParser drlParser = new DrlParser();
    final PackageDescr packageDescr = drlParser.parse(true, drl);
    if (drlParser.hasErrors()) {
        final List<DroolsError> errors = drlParser.getErrors();
        logger.warn(ErrorMessageUtilities.makeErrorMessage(path, errors.toArray(new DroolsError[errors.size()])));
        return null;
    }
    if (packageDescr == null) {
        logger.warn(ErrorMessageUtilities.makeErrorMessage(path));
        return null;
    }
    final ModuleDataModelOracle dmo = getModuleDataModelOracle(path);
    // responsible for basic index info: project name, branch, etc
    final DefaultIndexBuilder builder = getIndexBuilder(path);
    if (builder == null) {
        return null;
    }
    builder.setPackageName(packageDescr.getName());
    // Retrieves info from the parsed syntac tree (PackageDescr)
    final PackageDescrIndexVisitor visitor = new PackageDescrIndexVisitor(dmo, builder, packageDescr);
    visitor.visit();
    addReferencedResourcesToIndexBuilder(builder, visitor);
    return builder;
}
Also used : DroolsError(org.drools.compiler.compiler.DroolsError) ModuleDataModelOracle(org.kie.soup.project.datamodel.oracle.ModuleDataModelOracle) DrlParser(org.drools.compiler.compiler.DrlParser) DefaultIndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) PackageDescrIndexVisitor(org.kie.workbench.common.services.refactoring.backend.server.indexing.PackageDescrIndexVisitor)

Example 2 with PackageDescrIndexVisitor

use of org.kie.workbench.common.services.refactoring.backend.server.indexing.PackageDescrIndexVisitor in project kie-wb-common by kiegroup.

the class TestPackageNameDrlFileIndexer method toKObject.

@Override
public KObject toKObject(final Path path) {
    KObject index = null;
    try {
        final String drl = ioService.readAllString(path);
        final DrlParser drlParser = new DrlParser();
        final PackageDescr packageDescr = drlParser.parse(true, drl);
        if (drlParser.hasErrors()) {
            final List<DroolsError> errors = drlParser.getErrors();
            logger.warn(ErrorMessageUtilities.makeErrorMessage(path, errors.toArray(new DroolsError[errors.size()])));
            return index;
        }
        if (packageDescr == null) {
            logger.warn(ErrorMessageUtilities.makeErrorMessage(path));
            return index;
        }
        final ModuleDataModelOracle dmo = getModuleDataModelOracle(path);
        final Module module = moduleService.resolveModule(Paths.convert(path));
        // This is the reason we're overriding toKObject and not using fillDrlIndextBuilder(..)
        final Package pkg = mock(Package.class);
        when(pkg.getPackageName()).thenReturn(packageDescr.getName());
        final DefaultIndexBuilder builder = new DefaultIndexBuilder(Paths.convert(path).getFileName(), module, pkg);
        final PackageDescrIndexVisitor visitor = new PackageDescrIndexVisitor(dmo, builder, packageDescr);
        visitor.visit();
        addReferencedResourcesToIndexBuilder(builder, visitor);
        index = KObjectUtil.toKObject(path, IndexTerm.REFACTORING_CLASSIFIER, builder.build());
    } catch (Exception e) {
        logger.error("Unable to index '" + path.toUri().toString() + "'.", e);
    }
    return index;
}
Also used : DroolsError(org.drools.compiler.compiler.DroolsError) KObject(org.uberfire.ext.metadata.model.KObject) ModuleDataModelOracle(org.kie.soup.project.datamodel.oracle.ModuleDataModelOracle) DrlParser(org.drools.compiler.compiler.DrlParser) Package(org.guvnor.common.services.project.model.Package) DefaultIndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Module(org.guvnor.common.services.project.model.Module) PackageDescrIndexVisitor(org.kie.workbench.common.services.refactoring.backend.server.indexing.PackageDescrIndexVisitor)

Aggregations

DrlParser (org.drools.compiler.compiler.DrlParser)2 DroolsError (org.drools.compiler.compiler.DroolsError)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 ModuleDataModelOracle (org.kie.soup.project.datamodel.oracle.ModuleDataModelOracle)2 DefaultIndexBuilder (org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder)2 PackageDescrIndexVisitor (org.kie.workbench.common.services.refactoring.backend.server.indexing.PackageDescrIndexVisitor)2 Module (org.guvnor.common.services.project.model.Module)1 Package (org.guvnor.common.services.project.model.Package)1 KObject (org.uberfire.ext.metadata.model.KObject)1