Search in sources :

Example 31 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class OptaplannerCommandServiceImpl method executeScript.

@Override
public ServiceResponsesList executeScript(CommandScript commands, MarshallingFormat marshallingFormat, String classType) {
    List<ServiceResponse<?>> responses = new ArrayList<>();
    for (KieServerCommand command : commands.getCommands()) {
        try {
            ServiceResponse<?> response;
            logger.debug("About to execute command: {}", command);
            if (command instanceof CreateSolverCommand) {
                CreateSolverCommand createSolverCommand = (CreateSolverCommand) command;
                String containerId = context.getContainerId(createSolverCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                SolverInstance instance = new SolverInstance();
                instance.setContainerId(containerId);
                instance.setSolverId(createSolverCommand.getSolverId());
                instance.setSolverConfigFile(createSolverCommand.getSolverConfigFile());
                response = solverService.createSolver(containerId, createSolverCommand.getSolverId(), instance);
            } else if (command instanceof GetSolversCommand) {
                GetSolversCommand getSolversCommand = (GetSolversCommand) command;
                String containerId = context.getContainerId(getSolversCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.getSolvers(containerId);
            } else if (command instanceof GetSolverCommand) {
                GetSolverCommand getSolverCommand = (GetSolverCommand) command;
                String containerId = context.getContainerId(getSolverCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.getSolver(containerId, getSolverCommand.getSolverId());
            } else if (command instanceof GetSolverWithBestSolutionCommand) {
                GetSolverWithBestSolutionCommand getSolverWithBestSolutionCommand = (GetSolverWithBestSolutionCommand) command;
                String containerId = context.getContainerId(getSolverWithBestSolutionCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.getSolverWithBestSolution(containerId, getSolverWithBestSolutionCommand.getSolverId());
            } else if (command instanceof SolvePlanningProblemCommand) {
                SolvePlanningProblemCommand solvePlanningProblemCommand = (SolvePlanningProblemCommand) command;
                String containerId = context.getContainerId(solvePlanningProblemCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                KieContainerInstanceImpl kc = context.getContainer(containerId);
                Marshaller marshaller = kc.getMarshaller(marshallingFormat);
                Object planningProblem = marshaller.unmarshall(solvePlanningProblemCommand.getPlanningProblem(), Object.class);
                response = solverService.solvePlanningProblem(containerId, solvePlanningProblemCommand.getSolverId(), planningProblem);
            } else if (command instanceof TerminateSolverEarlyCommand) {
                TerminateSolverEarlyCommand terminateSolverEarlyCommand = (TerminateSolverEarlyCommand) command;
                String containerId = context.getContainerId(terminateSolverEarlyCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.terminateSolverEarly(containerId, terminateSolverEarlyCommand.getSolverId());
            } else if (command instanceof AddProblemFactChangeCommand) {
                AddProblemFactChangeCommand addProblemFactChangeCommand = (AddProblemFactChangeCommand) command;
                String containerId = context.getContainerId(addProblemFactChangeCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.addProblemFactChanges(containerId, addProblemFactChangeCommand.getSolverId(), addProblemFactChangeCommand.getProblemFactChange());
            } else if (command instanceof AddProblemFactChangesCommand) {
                AddProblemFactChangesCommand addProblemFactChangesCommand = (AddProblemFactChangesCommand) command;
                String containerId = context.getContainerId(addProblemFactChangesCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.addProblemFactChanges(containerId, addProblemFactChangesCommand.getSolverId(), addProblemFactChangesCommand.getProblemFactChanges());
            } else if (command instanceof IsEveryProblemFactChangeProcessedCommand) {
                IsEveryProblemFactChangeProcessedCommand isEveryProblemFactChangeProcessedCommand = (IsEveryProblemFactChangeProcessedCommand) command;
                String containerId = context.getContainerId(isEveryProblemFactChangeProcessedCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                ServiceResponse<Boolean> everyProblemFactChangeProcessedResponse = solverService.isEveryProblemFactChangeProcessed(containerId, isEveryProblemFactChangeProcessedCommand.getSolverId());
                if (marshallingFormat.equals(MarshallingFormat.JAXB)) {
                    Object wrappedResult = ModelWrapper.wrap(everyProblemFactChangeProcessedResponse.getResult());
                    response = new ServiceResponse<>(everyProblemFactChangeProcessedResponse.getType(), everyProblemFactChangeProcessedResponse.getMsg(), wrappedResult);
                } else {
                    response = everyProblemFactChangeProcessedResponse;
                }
            } else if (command instanceof DisposeSolverCommand) {
                DisposeSolverCommand disposeSolverCommand = (DisposeSolverCommand) command;
                String containerId = context.getContainerId(disposeSolverCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.disposeSolver(containerId, disposeSolverCommand.getSolverId());
            } else {
                throw new IllegalStateException("Unsupported command: " + command);
            }
            logger.debug("Service returned response {}", response);
            // return successful result
            responses.add(response);
        } catch (Throwable e) {
            logger.error("Error while processing {} command", command, e);
            // return failure result
            responses.add(new ServiceResponse<>(ServiceResponse.ResponseType.FAILURE, e.getMessage()));
        }
    }
    logger.debug("About to return responses '{}'", responses);
    return new ServiceResponsesList(responses);
}
Also used : ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) KieServerCommand(org.kie.server.api.model.KieServerCommand) ArrayList(java.util.ArrayList) TerminateSolverEarlyCommand(org.kie.server.api.commands.optaplanner.TerminateSolverEarlyCommand) SolverInstance(org.kie.server.api.model.instance.SolverInstance) ServiceResponse(org.kie.server.api.model.ServiceResponse) AddProblemFactChangesCommand(org.kie.server.api.commands.optaplanner.AddProblemFactChangesCommand) IsEveryProblemFactChangeProcessedCommand(org.kie.server.api.commands.optaplanner.IsEveryProblemFactChangeProcessedCommand) GetSolverCommand(org.kie.server.api.commands.optaplanner.GetSolverCommand) Marshaller(org.kie.server.api.marshalling.Marshaller) GetSolversCommand(org.kie.server.api.commands.optaplanner.GetSolversCommand) SolvePlanningProblemCommand(org.kie.server.api.commands.optaplanner.SolvePlanningProblemCommand) GetSolverWithBestSolutionCommand(org.kie.server.api.commands.optaplanner.GetSolverWithBestSolutionCommand) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) CreateSolverCommand(org.kie.server.api.commands.optaplanner.CreateSolverCommand) AddProblemFactChangeCommand(org.kie.server.api.commands.optaplanner.AddProblemFactChangeCommand) DisposeSolverCommand(org.kie.server.api.commands.optaplanner.DisposeSolverCommand)

Example 32 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class ControllerUtils method unmarshal.

public static <T> T unmarshal(String data, String marshallingFormat, Class<T> unmarshalType) {
    if (data == null || data.isEmpty()) {
        return null;
    }
    MarshallingFormat format = getFormat(marshallingFormat);
    Marshaller marshaller = null;
    switch(format) {
        case JAXB:
            {
                marshaller = jaxbMarshaller;
                break;
            }
        case JSON:
            {
                marshaller = jsonMarshaller;
                break;
            }
        default:
            {
                marshaller = jsonMarshaller;
                break;
            }
    }
    Object instance = marshaller.unmarshall(data, unmarshalType);
    if (instance instanceof Wrapped) {
        return (T) ((Wrapped) instance).unwrap();
    }
    return (T) instance;
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) MarshallingFormat(org.kie.server.api.marshalling.MarshallingFormat) Wrapped(org.kie.server.api.model.Wrapped)

Example 33 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class HibernateXStreamMarshallerExtensionTest method testMarshallDummyHibernatePersistenceBag.

@Test
public void testMarshallDummyHibernatePersistenceBag() {
    SharedSessionContractImplementor session = Mockito.mock(SharedSessionContractImplementor.class);
    Mockito.when(session.isOpen()).thenReturn(true);
    Mockito.when(session.isConnected()).thenReturn(true);
    PersistentBag bag = new PersistentBag(session, new ArrayList<String>());
    String expectedOutput = "<list/>";
    Marshaller marshaller = MarshallerFactory.getMarshaller(MarshallingFormat.XSTREAM, getClass().getClassLoader());
    Assert.assertEquals(expectedOutput, marshaller.marshall(bag));
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) PersistentBag(org.hibernate.collection.internal.PersistentBag) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Test(org.junit.Test)

Example 34 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class CloudEventReader method readEvent.

@Override
public <T> T readEvent(byte[] value, Class<T> valueType) throws IOException {
    JsonNode node = mapper.readTree(value);
    MarshallingFormat contentType = MarshallingFormat.JSON;
    if (node.has("datacontenttype")) {
        contentType = MarshallingFormat.fromType(node.get("datacontenttype").asText());
    }
    if (node.has("data")) {
        Marshaller marshaller = marshallers.computeIfAbsent(contentType, c -> MarshallerFactory.getMarshaller(c, cl));
        return marshaller.unmarshall(node.get("data").toString(), valueType);
    }
    throw new IOException("Missing data field in cloud event " + new String(value));
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) MarshallingFormat(org.kie.server.api.marshalling.MarshallingFormat) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 35 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class ControllerUtils method marshal.

public static String marshal(String marshallingFormat, Object entity) {
    MarshallingFormat format = getFormat(marshallingFormat);
    if (format == null) {
        throw new IllegalArgumentException("Unknown marshalling format " + marshallingFormat);
    }
    Marshaller marshaller = null;
    switch(format) {
        case JAXB:
            {
                marshaller = jaxbMarshaller;
                break;
            }
        case JSON:
            {
                marshaller = jsonMarshaller;
                break;
            }
        default:
            {
                marshaller = jsonMarshaller;
                break;
            }
    }
    return marshaller.marshall(entity);
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) MarshallingFormat(org.kie.server.api.marshalling.MarshallingFormat)

Aggregations

Marshaller (org.kie.server.api.marshalling.Marshaller)65 Test (org.junit.Test)48 HashMap (java.util.HashMap)12 WebTarget (javax.ws.rs.client.WebTarget)11 BeforeClass (org.junit.BeforeClass)10 ArrayList (java.util.ArrayList)9 MarshallingFormat (org.kie.server.api.marshalling.MarshallingFormat)9 Response (javax.ws.rs.core.Response)8 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)8 Command (org.kie.api.command.Command)8 ExecutionResults (org.kie.api.runtime.ExecutionResults)8 HashSet (java.util.HashSet)7 JaxbLong (org.kie.server.api.model.type.JaxbLong)6 KieContainerInstance (org.kie.server.services.api.KieContainerInstance)4 CallContainerCommand (org.kie.server.api.commands.CallContainerCommand)3 KieContainerResource (org.kie.server.api.model.KieContainerResource)3 KieServerCommand (org.kie.server.api.model.KieServerCommand)3 ServiceResponse (org.kie.server.api.model.ServiceResponse)3 Date (java.util.Date)2 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2