Search in sources :

Example 16 with KieContainerInstanceImpl

use of org.kie.server.services.impl.KieContainerInstanceImpl in project droolsjbpm-integration by kiegroup.

the class SolverServiceBase method createSolver.

public ServiceResponse<SolverInstance> createSolver(String containerId, String solverId, SolverInstance instance) {
    if (instance == null || instance.getSolverConfigFile() == null) {
        logger.error("Error creating solver. Configuration file name is null: " + instance);
        return new ServiceResponse<>(ServiceResponse.ResponseType.FAILURE, "Failed to create solver for container " + containerId + ". Solver configuration file is null: " + instance);
    }
    instance.setContainerId(containerId);
    instance.setSolverId(solverId);
    try {
        KieContainerInstanceImpl ci = context.getContainer(containerId);
        if (ci == null) {
            logger.error("Error creating solver. Container does not exist: " + containerId);
            return new ServiceResponse<>(ServiceResponse.ResponseType.FAILURE, "Failed to create solver. Container does not exist: " + containerId);
        }
        // have to synchronize on the ci or a concurrent call to dispose may create inconsistencies
        synchronized (ci) {
            if (solvers.containsKey(instance.getSolverInstanceKey())) {
                logger.error("Error creating solver. Solver '" + solverId + "' already exists for container '" + containerId + "'.");
                return new ServiceResponse<>(ServiceResponse.ResponseType.FAILURE, "Failed to create solver. Solver '" + solverId + "' already exists for container '" + containerId + "'.");
            }
            SolverInstanceContext sic = new SolverInstanceContext(instance);
            if (instance.getStatus() == null) {
                instance.setStatus(SolverInstance.SolverStatus.NOT_SOLVING);
            }
            try {
                Solver<Object> solver = buildSolver(ci.getKieContainer(), instance.getSolverConfigFile());
                sic.setSolver(solver);
                registerListener(solver, solverId);
                updateSolverInstance(sic);
                solvers.put(instance.getSolverInstanceKey(), sic);
                logger.info("Solver '" + solverId + "' successfully created in container '" + containerId + "'");
                return new ServiceResponse<>(ServiceResponse.ResponseType.SUCCESS, "Solver '" + solverId + "' successfully created in container '" + containerId + "'", instance);
            } catch (Exception e) {
                logger.error("Error creating solver factory for solver " + instance, e);
                return new ServiceResponse<>(ServiceResponse.ResponseType.FAILURE, "Error creating solver factory for solver: " + e.getMessage(), instance);
            }
        }
    } catch (Exception e) {
        logger.error("Error creating solver '" + solverId + "' in container '" + containerId + "'", e);
        return new ServiceResponse<>(ServiceResponse.ResponseType.FAILURE, "Error creating solver '" + solverId + "' in container '" + containerId + "': " + e.getMessage(), instance);
    }
}
Also used : ServiceResponse(org.kie.server.api.model.ServiceResponse) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl)

Example 17 with KieContainerInstanceImpl

use of org.kie.server.services.impl.KieContainerInstanceImpl in project droolsjbpm-integration by kiegroup.

the class TaskAssigningPlanningKieServerExtension method initUserSystemService.

private boolean initUserSystemService() {
    ClassLoader classLoader;
    if (userSystemContainer != null) {
        LOGGER.debug("User system service {} will be loaded from container {} class loader", userSystemName, userSystemContainer.getContainerId());
        KieContainerInstanceImpl container = prepareContainer(userSystemContainer);
        if (container == null) {
            String msg = String.format(USER_SYSTEM_CONTAINER_NOT_AVAILABLE, userSystemContainer.getContainerId());
            LOGGER.error(msg);
            registerMessage(Severity.ERROR, msg);
            return false;
        }
        classLoader = container.getKieContainer().getClassLoader();
        registerExtractors(container);
    } else {
        LOGGER.debug("User system service {} will be loaded from application class loader", userSystemName);
        classLoader = this.getClass().getClassLoader();
    }
    userSystemService = lookupUserSystem(userSystemName, classLoader);
    if (userSystemService == null) {
        final String msg = String.format(USER_SYSTEM_SERVICE_NOT_FOUND, userSystemName);
        LOGGER.error(msg);
        registerMessage(Severity.ERROR, msg);
        return false;
    }
    try {
        userSystemService.start();
    } catch (Exception e) {
        final String msg = String.format(USER_SYSTEM_SERVICE_START_ERROR, userSystemName, e.getMessage());
        LOGGER.error(msg, e);
        registerMessage(Severity.ERROR, msg);
        return false;
    }
    try {
        userSystemService.test();
        LOGGER.debug("User system service {} test check was successful.", userSystemName);
    } catch (Exception e) {
        LOGGER.warn("User system service {} test check failed, but " + EXTENSION_NAME + " startup procedure will continue. error: ", e.getMessage());
    }
    return true;
}
Also used : KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) KieServicesException(org.kie.server.api.exception.KieServicesException)

Example 18 with KieContainerInstanceImpl

use of org.kie.server.services.impl.KieContainerInstanceImpl in project droolsjbpm-integration by kiegroup.

the class SolverBuilder method buildFromContainer.

private Solver<TaskAssigningSolution> buildFromContainer() {
    final KieContainerInstanceImpl containerInstance = registry.getContainer(solverDef.getContainerId());
    if (containerInstance == null) {
        throw new SolverBuilderException("Container " + solverDef.getContainerId() + " was not found un current registry." + " No solvers can be created for this container");
    }
    if (containerInstance.getStatus() != KieContainerStatus.STARTED) {
        throw new SolverBuilderException("Container " + solverDef.getContainerId() + " must be in " + KieContainerStatus.STARTED + " status for creating solvers, but current status is: " + containerInstance.getStatus());
    }
    final SolverFactory<TaskAssigningSolution> solverFactory = SolverFactory.createFromKieContainerXmlResource(containerInstance.getKieContainer(), solverDef.getSolverConfigResource());
    return solverFactory.buildSolver();
}
Also used : TaskAssigningSolution(org.kie.server.services.taskassigning.core.model.TaskAssigningSolution) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl)

Example 19 with KieContainerInstanceImpl

use of org.kie.server.services.impl.KieContainerInstanceImpl in project droolsjbpm-integration by kiegroup.

the class ModelEvaluatorServiceBase method getModel.

public Response getModel(String containerId, String modelId) {
    try {
        KieContainerInstanceImpl kContainer = context.getContainer(containerId, ContainerLocatorProvider.get().getLocator());
        DMNRuntime dmnRuntime = KieRuntimeFactory.of(kContainer.getKieContainer().getKieBase()).get(DMNRuntime.class);
        List<DMNModel> modelsWithID = dmnRuntime.getModels().stream().filter(m -> m.getName().equals(modelId)).collect(Collectors.toList());
        if (modelsWithID.isEmpty()) {
            return Response.status(Status.NOT_FOUND).entity("No model identifies with modelId: " + modelId).build();
        } else if (modelsWithID.size() > 1) {
            return Response.status(Status.NOT_FOUND).entity("More than one existing DMN model having modelId: " + modelId).build();
        }
        DMNModel dmnModel = modelsWithID.get(0);
        Definitions definitions = dmnModel.getDefinitions();
        for (DRGElement drg : definitions.getDrgElement()) {
            if (drg instanceof Decision) {
                Decision decision = (Decision) drg;
                decision.setExpression(null);
            } else if (drg instanceof BusinessKnowledgeModel) {
                BusinessKnowledgeModel bkm = (BusinessKnowledgeModel) drg;
                bkm.setEncapsulatedLogic(null);
            }
        }
        String xml = DMNMarshallerFactory.newDefaultMarshaller().marshal(definitions);
        return Response.ok().entity(xml).build();
    } catch (Exception e) {
        LOG.error("Error from container '" + containerId + "'", e);
        return Response.serverError().entity(e.getMessage()).build();
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNItemDefinitionInfo(org.kie.server.api.model.dmn.DMNItemDefinitionInfo) LoggerFactory(org.slf4j.LoggerFactory) ItemDefNodeImpl(org.kie.dmn.core.ast.ItemDefNodeImpl) DynamicDMNContextBuilder(org.kie.dmn.core.internal.utils.DynamicDMNContextBuilder) DMNContextKS(org.kie.server.api.model.dmn.DMNContextKS) ServiceResponse(org.kie.server.api.model.ServiceResponse) ReleaseId(org.kie.api.builder.ReleaseId) KieRuntimeFactory(org.kie.api.runtime.KieRuntimeFactory) DMNModel(org.kie.dmn.api.core.DMNModel) Map(java.util.Map) DMNFEELComparablePeriodSerializer(org.kie.server.services.dmn.modelspecific.DMNFEELComparablePeriodSerializer) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DMNRuntimeEventListener(org.kie.dmn.api.core.event.DMNRuntimeEventListener) DMNResultKS(org.kie.server.api.model.dmn.DMNResultKS) DMNQNameInfo(org.kie.server.api.model.dmn.DMNQNameInfo) Collection(java.util.Collection) InputData(org.kie.dmn.model.api.InputData) Collectors(java.util.stream.Collectors) KieServerRegistry(org.kie.server.services.api.KieServerRegistry) MSConsts(org.kie.server.services.dmn.modelspecific.MSConsts) List(java.util.List) Response(javax.ws.rs.core.Response) DMNDecisionInfo(org.kie.server.api.model.dmn.DMNDecisionInfo) DMNContext(org.kie.dmn.api.core.DMNContext) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) QName(javax.xml.namespace.QName) DMNDecisionServiceInfo(org.kie.server.api.model.dmn.DMNDecisionServiceInfo) KogitoDMNResult(org.kie.server.services.dmn.modelspecific.KogitoDMNResult) OASGenerator(org.kie.server.services.dmn.modelspecific.OASGenerator) DMNEvaluationUtils(org.kie.dmn.core.internal.utils.DMNEvaluationUtils) DMNModelInfo(org.kie.server.api.model.dmn.DMNModelInfo) MarshallerHelper(org.kie.server.services.impl.marshal.MarshallerHelper) Definitions(org.kie.dmn.model.api.Definitions) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNMarshallerFactory(org.kie.dmn.backend.marshalling.v1x.DMNMarshallerFactory) PrometheusMetricsDMNListener(org.kie.server.services.prometheus.PrometheusMetricsDMNListener) Decision(org.kie.dmn.model.api.Decision) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) InputDataNodeImpl(org.kie.dmn.core.ast.InputDataNodeImpl) DMNEvaluationResult(org.kie.dmn.core.internal.utils.DMNEvaluationUtils.DMNEvaluationResult) XMLConstants(javax.xml.XMLConstants) Status(javax.ws.rs.core.Response.Status) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) DMNModelInfoList(org.kie.server.api.model.dmn.DMNModelInfoList) Logger(org.slf4j.Logger) DMNUnaryTestsInfo(org.kie.server.api.model.dmn.DMNUnaryTestsInfo) PrometheusKieServerExtension(org.kie.server.services.prometheus.PrometheusKieServerExtension) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) DMNInputDataInfo(org.kie.server.api.model.dmn.DMNInputDataInfo) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) DRGElement(org.kie.dmn.model.api.DRGElement) ContainerLocatorProvider(org.kie.server.services.impl.locator.ContainerLocatorProvider) Definitions(org.kie.dmn.model.api.Definitions) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Decision(org.kie.dmn.model.api.Decision) DRGElement(org.kie.dmn.model.api.DRGElement)

Example 20 with KieContainerInstanceImpl

use of org.kie.server.services.impl.KieContainerInstanceImpl in project droolsjbpm-integration by kiegroup.

the class ModelEvaluatorServiceBase method evaluateModel.

public Response evaluateModel(String containerId, String modelId, String contextPayload, boolean asDmnResult, String decisionServiceId) {
    try {
        KieContainerInstanceImpl kContainer = context.getContainer(containerId, ContainerLocatorProvider.get().getLocator());
        DMNRuntime dmnRuntime = KieRuntimeFactory.of(kContainer.getKieContainer().getKieBase()).get(DMNRuntime.class);
        List<DMNModel> modelsWithID = dmnRuntime.getModels().stream().filter(m -> m.getName().equals(modelId)).collect(Collectors.toList());
        if (modelsWithID.isEmpty()) {
            return Response.status(Status.NOT_FOUND).entity("No model identifies with modelId: " + modelId).build();
        } else if (modelsWithID.size() > 1) {
            return Response.status(Status.NOT_FOUND).entity("More than one existing DMN model having modelId: " + modelId).build();
        }
        DMNModel dmnModel = modelsWithID.get(0);
        DecisionServiceNode determinedDS = null;
        if (decisionServiceId != null) {
            Optional<DecisionServiceNode> dsOpt = dmnModel.getDecisionServices().stream().filter(ds -> ds.getName().equals(decisionServiceId)).findFirst();
            if (!dsOpt.isPresent()) {
                return Response.status(Status.NOT_FOUND).entity("No decisionService found: " + decisionServiceId).build();
            }
            determinedDS = dsOpt.get();
        }
        Map<String, Object> jsonContextMap = objectMapper.readValue(contextPayload, new TypeReference<Map<String, Object>>() {
        });
        DMNContext dmnContext = new DynamicDMNContextBuilder(dmnRuntime.newContext(), dmnModel).populateContextWith(jsonContextMap);
        wirePrometheus(kContainer, dmnRuntime);
        DMNResult determinedResult = null;
        if (determinedDS != null) {
            determinedResult = dmnRuntime.evaluateDecisionService(dmnModel, dmnContext, determinedDS.getName());
        } else {
            determinedResult = dmnRuntime.evaluateAll(dmnModel, dmnContext);
        }
        // at this point the DMN service has executed the evaluation, so it's full model-specific endpoint semantics.
        KogitoDMNResult result = new KogitoDMNResult(dmnModel.getNamespace(), dmnModel.getName(), determinedResult);
        if (asDmnResult) {
            return Response.ok().entity(objectMapper.writeValueAsString(result)).build();
        }
        String responseJSON = null;
        if (determinedDS != null && determinedDS.getDecisionService().getOutputDecision().size() == 1) {
            responseJSON = objectMapper.writeValueAsString(result.getDecisionResults().get(0).getResult());
        } else {
            responseJSON = objectMapper.writeValueAsString(result.getDmnContext());
        }
        ResponseBuilder response = Response.ok();
        if (result.hasErrors()) {
            String infoWarns = result.getMessages().stream().map(m -> m.getLevel() + " " + m.getMessage()).collect(java.util.stream.Collectors.joining(", "));
            response.header(MSConsts.KOGITO_DECISION_INFOWARN_HEADER, infoWarns);
        }
        response.entity(responseJSON);
        return response.build();
    } catch (Exception e) {
        LOG.error("Error from container '" + containerId + "'", e);
        return Response.serverError().entity(e.getMessage()).build();
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNItemDefinitionInfo(org.kie.server.api.model.dmn.DMNItemDefinitionInfo) LoggerFactory(org.slf4j.LoggerFactory) ItemDefNodeImpl(org.kie.dmn.core.ast.ItemDefNodeImpl) DynamicDMNContextBuilder(org.kie.dmn.core.internal.utils.DynamicDMNContextBuilder) DMNContextKS(org.kie.server.api.model.dmn.DMNContextKS) ServiceResponse(org.kie.server.api.model.ServiceResponse) ReleaseId(org.kie.api.builder.ReleaseId) KieRuntimeFactory(org.kie.api.runtime.KieRuntimeFactory) DMNModel(org.kie.dmn.api.core.DMNModel) Map(java.util.Map) DMNFEELComparablePeriodSerializer(org.kie.server.services.dmn.modelspecific.DMNFEELComparablePeriodSerializer) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DMNRuntimeEventListener(org.kie.dmn.api.core.event.DMNRuntimeEventListener) DMNResultKS(org.kie.server.api.model.dmn.DMNResultKS) DMNQNameInfo(org.kie.server.api.model.dmn.DMNQNameInfo) Collection(java.util.Collection) InputData(org.kie.dmn.model.api.InputData) Collectors(java.util.stream.Collectors) KieServerRegistry(org.kie.server.services.api.KieServerRegistry) MSConsts(org.kie.server.services.dmn.modelspecific.MSConsts) List(java.util.List) Response(javax.ws.rs.core.Response) DMNDecisionInfo(org.kie.server.api.model.dmn.DMNDecisionInfo) DMNContext(org.kie.dmn.api.core.DMNContext) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) QName(javax.xml.namespace.QName) DMNDecisionServiceInfo(org.kie.server.api.model.dmn.DMNDecisionServiceInfo) KogitoDMNResult(org.kie.server.services.dmn.modelspecific.KogitoDMNResult) OASGenerator(org.kie.server.services.dmn.modelspecific.OASGenerator) DMNEvaluationUtils(org.kie.dmn.core.internal.utils.DMNEvaluationUtils) DMNModelInfo(org.kie.server.api.model.dmn.DMNModelInfo) MarshallerHelper(org.kie.server.services.impl.marshal.MarshallerHelper) Definitions(org.kie.dmn.model.api.Definitions) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNMarshallerFactory(org.kie.dmn.backend.marshalling.v1x.DMNMarshallerFactory) PrometheusMetricsDMNListener(org.kie.server.services.prometheus.PrometheusMetricsDMNListener) Decision(org.kie.dmn.model.api.Decision) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) InputDataNodeImpl(org.kie.dmn.core.ast.InputDataNodeImpl) DMNEvaluationResult(org.kie.dmn.core.internal.utils.DMNEvaluationUtils.DMNEvaluationResult) XMLConstants(javax.xml.XMLConstants) Status(javax.ws.rs.core.Response.Status) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) DMNModelInfoList(org.kie.server.api.model.dmn.DMNModelInfoList) Logger(org.slf4j.Logger) DMNUnaryTestsInfo(org.kie.server.api.model.dmn.DMNUnaryTestsInfo) PrometheusKieServerExtension(org.kie.server.services.prometheus.PrometheusKieServerExtension) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) DMNInputDataInfo(org.kie.server.api.model.dmn.DMNInputDataInfo) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) DRGElement(org.kie.dmn.model.api.DRGElement) ContainerLocatorProvider(org.kie.server.services.impl.locator.ContainerLocatorProvider) KogitoDMNResult(org.kie.server.services.dmn.modelspecific.KogitoDMNResult) DMNResult(org.kie.dmn.api.core.DMNResult) KogitoDMNResult(org.kie.server.services.dmn.modelspecific.KogitoDMNResult) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) DMNContext(org.kie.dmn.api.core.DMNContext) DynamicDMNContextBuilder(org.kie.dmn.core.internal.utils.DynamicDMNContextBuilder) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Map(java.util.Map) DMNModel(org.kie.dmn.api.core.DMNModel)

Aggregations

KieContainerInstanceImpl (org.kie.server.services.impl.KieContainerInstanceImpl)29 ArrayList (java.util.ArrayList)8 ReleaseId (org.kie.server.api.model.ReleaseId)8 ServiceResponse (org.kie.server.api.model.ServiceResponse)8 HashMap (java.util.HashMap)7 KieServerRegistry (org.kie.server.services.api.KieServerRegistry)6 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)5 KieModuleMetaData (org.kie.scanner.KieModuleMetaData)5 Message (org.kie.server.api.model.Message)5 List (java.util.List)4 Map (java.util.Map)4 Test (org.junit.Test)4 DMNModel (org.kie.dmn.api.core.DMNModel)4 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 DeploymentUnit (org.jbpm.services.api.model.DeploymentUnit)3 ReleaseId (org.kie.api.builder.ReleaseId)3 Header (org.kie.server.remote.rest.common.Header)3 OASGenerator (org.kie.server.services.dmn.modelspecific.OASGenerator)3 MarshallerHelper (org.kie.server.services.impl.marshal.MarshallerHelper)3