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);
}
}
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;
}
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();
}
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();
}
}
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();
}
}
Aggregations