是否有任何REST API来从Drools中的Repository中获取所有规则

时间:2016-06-16 18:03:14

标签: rest drools rules workbench

正如你所看到的,所有规则都可以在项目资源管理器中列出,我很想知道Drools工作台有这样的Rest API,但是我浏览了在线文档文档,没有这样的API。有什么建议吗?提前致谢。 https://docs.jboss.org/drools/release/latest/drools-docs/html/ch20.html#d0e22619

最诚挚的问候 裕华

2 个答案:

答案 0 :(得分:1)

据我所知,没有REST API可以做到这一点(至少公开)。但是,您可以使用git从工作台获取该信息。

工作台的存储基于git。工作台中的每个存储库实际上都是一个git存储库。工作台允许你clone those repositories和你做任何你需要的事情,就像那里的任何其他git repo一样。

在每个git存储库中,您将找到零个或多个maven项目。实际上,您在工作台中看到的每个项目都是一个真正的maven项目。您的项目中的不同资产(drl规则,指导规则,决策表等)将在相应项目的resources目录下提供。

希望它有所帮助,

答案 1 :(得分:0)

正如Esteban Aliverti所说,没有准备好使用API​​来实现这一目标。但是,我们可以为KIE Server编写自定义扩展以获取部署的所有规则。

详细解释here

我的应用程序中有类似的用例,并为获取规则执行了以下实现。

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;
    }

}

}

您可以从您的应用程序对Kie Server进行REST调用,以访问给定容器的所有可用规则。

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