1
votes

As you can see, all rules can be listed in project explorer, i am wondering Drools workbench has such a Rest API for this, but I went through online document document, there is no such API. any suggestion on this? thanks in advance. https://docs.jboss.org/drools/release/latest/drools-docs/html/ch20.html#d0e22619

Best Regards Yuhua

2
I am looking for similar functionality. Just wanted to check if you were able to get any info on this.DBS

2 Answers

1
votes

As far as I know, there is no REST API to do that (public at least). One option do you have though is to use git to get that information from the workbench.

The storage of the workbench is based on git. Each repository in the workbench is actually a git repository. The workbench allows you to clone those repositories and to do whatever you need with them just as with any other git repo out there.

Inside each of the git repositories you will find zero or more maven projects. Indeed, each of the projects you see in the workbench is a real maven project. The different assets in your projects (drl rules, guided rules, decision table, etc.) will be available under the resources directory of the corresponding project.

Hope it helps,

0
votes

As Esteban Aliverti mentioned, there is no ready to use API to achieve this. However, we can write a custom extension to KIE Server to fetch all the rules deployed.

It is explained in detailed here.

I have similar use case in my application and did the following implementation for fetching rules.

CusomtDroolsKieServerApplicationComponentsService

public class CusomtDroolsKieServerApplicationComponentsService implements KieServerApplicationComponentsService {

private static final String OWNER_EXTENSION = "Drools";

public Collection<Object> getAppComponents(String extension, SupportedTransports type, Object... services) {
    // skip calls from other than owning extension
    if (!OWNER_EXTENSION.equals(extension)) {
        return Collections.emptyList();
    }

    RulesExecutionService rulesExecutionService = null;
    KieServerRegistry context = null;

    for (Object object : services) {
        if (RulesExecutionService.class.isAssignableFrom(object.getClass())) {
            rulesExecutionService = (RulesExecutionService) object;
            continue;
        } else if (KieServerRegistry.class.isAssignableFrom(object.getClass())) {
            context = (KieServerRegistry) object;
            continue;
        }
    }

    List<Object> components = new ArrayList<Object>(1);
    if (SupportedTransports.REST.equals(type)) {
        components.add(new RuleRESTService(rulesExecutionService, context));
    }

    return components;
}

RuleRestService

@Path("server/containers/instances/{id}/ksession")
public class RuleRESTService {

private RulesExecutionService rulesExecutionService;
private KieServerRegistry registry;

public RuleRESTService() {

}

public RuleRESTService(RulesExecutionService rulesExecutionService, KieServerRegistry registry) {
    this.rulesExecutionService = rulesExecutionService;
    this.registry = registry;
}

public RulesExecutionService getRulesExecutionService() {
    return rulesExecutionService;
}

public void setRulesExecutionService(RulesExecutionService rulesExecutionService) {
    this.rulesExecutionService = rulesExecutionService;
}

public KieServerRegistry getRegistry() {
    return registry;
}

public void setRegistry(KieServerRegistry registry) {
    this.registry = registry;
}

@POST
@Path("/{ksessionId}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response fetchAllRules(@Context HttpHeaders headers, @PathParam("id") String id,
        @PathParam("ksessionId") String ksessionId, String cmdPayload) {

    Variant v = getVariant(headers);
    try {
        System.out.println("CREATING KieContainerInstance ");
        KieContainerInstance kci = registry.getContainer(id);

        String contentType = getContentType(headers);

        MarshallingFormat format = MarshallingFormat.fromType(contentType);
        if (format == null) {
            format = MarshallingFormat.valueOf(contentType);
        }

        Marshaller marshaller = kci.getMarshaller(format);
        RuleAccessor accessor = new RuleAccessor();

        List<RuleData> rules = accessor.fetchAllRules(kci.getKieContainer());

        String result = marshaller.marshall(rules);

        return createResponse(result, v, Response.Status.OK);
    } catch (Exception ex) {
        ex.printStackTrace();
        String response = "Execution failed with error : " + ex.getMessage();
        System.out.println("Returning Failure response with content '{}' :" + response);
        return createResponse(response, v, Response.Status.INTERNAL_SERVER_ERROR);
    }

}

RuleAccessor

        public class RuleAccessor {

        public List<RuleData> fetchAllRules(KieContainer kContainer) {

    kContainer.getKieBaseNames().stream()
            .forEach(kieBase -> rules.addAll(fetchRules(kContainer1.getKieBase(kieBase))));

    return rules;

}

    public List<RuleData> fetchRules(KieBase kieBase) {
    List<RuleData> ruleData = new ArrayList<>();
    for (KiePackage kp : kieBase.getKiePackages()) {
        RuleData data = new RuleData();
        for (Rule r1 : kp.getRules()) {
            RuleImpl r = (RuleImpl) r1;
            data.agendaGroup(r.getAgendaGroup()).packageId(r.getPackageName()).ruleName(r.getName())
                    .enabled(Boolean.getBoolean((((EnabledBoolean) r.getEnabled()).toString())))
                    .effectiveDate(String.valueOf(r.getDateEffective()))
                    .dateExpires(String.valueOf(r.getDateExpires())).dialect(r.getDialect())
                    .salience(r.getSalienceValue()).metaData(r.getMetaData());
            try {
                Resource resource = r.getResource();
                Reader reader = resource.getReader();
                BufferedReader bufferedReader = new BufferedReader(reader);
                String line = null;
                StringBuilder builder = new StringBuilder();
                while ((line = bufferedReader.readLine()) != null) {
                    builder.append(line);
                }
                data.ruleContent(builder.toString());
                ruleData.add(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return ruleData;

}

    public static class RuleData {

    private String packageId;

    private String ruleName;

    private String type;

    private String agendaGroup;

    private String ruleContent;

    private boolean isEnabled;
    private String effectiveDate;
    private String dateExpires;
    private String dialect;
    private int salience;
    private Map<String, Object> metaData;

    public boolean isEnabled() {
        return isEnabled;
    }

    public RuleData enabled(boolean isEnabled) {
        this.isEnabled = isEnabled;
        return this;
    }

    public String effectiveDate() {
        return effectiveDate;
    }

    public RuleData effectiveDate(String effectiveDate) {
        this.effectiveDate = effectiveDate;
        return this;
    }

    public String getDateExpires() {
        return dateExpires;
    }

    public RuleData dateExpires(String dateExpires) {
        this.dateExpires = dateExpires;
        return this;
    }

    public String getDialect() {
        return dialect;
    }

    public RuleData dialect(String dialect) {
        this.dialect = dialect;
        return this;
    }

    public int getSalience() {
        return salience;
    }

    public RuleData salience(int salience) {
        this.salience = salience;
        return this;
    }

    public Map<String, Object> getMetaData() {
        return metaData;
    }

    public RuleData metaData(Map<String, Object> metaData) {
        this.metaData = metaData;
        return this;
    }

    public String getRuleContent() {
        return ruleContent;
    }

    public RuleData ruleContent(String ruleContent) {
        this.ruleContent = ruleContent;
        return this;
    }

    public String getPackageId() {
        return packageId;
    }

    public RuleData packageId(String packageId) {
        this.packageId = packageId;
        return this;
    }

    public String getRuleName() {
        return ruleName;
    }

    public RuleData ruleName(String ruleName) {
        this.ruleName = ruleName;
        return this;
    }

    public String getType() {
        return type;
    }

    public RuleData type(String type) {
        this.type = type;
        return this;
    }

    public String getAgendaGroup() {
        return agendaGroup;
    }

    public RuleData agendaGroup(String agendaGroup) {
        this.agendaGroup = agendaGroup;
        return this;
    }

}

}

You can make a REST call to Kie Server from you application to access all the rules available for the given container.

http://localhost:8080/kie-server/services/rest/server/containers/instances/<container-id>/ksession/<session-id>