Search in sources :

Example 1 with BAPCState

use of won.node.facet.businessactivity.participantcompletion.BAPCState in project webofneeds by researchstudio-sat.

the class BAAtomicPCCoordinatorFacetImpl method isCoordinatorInFirstPhase.

private boolean isCoordinatorInFirstPhase(Connection con) {
    List<Connection> listOfCons = getAllCoordinatorParticipantConnections(con);
    URI ownerUri, needUri, currentStateURI;
    BAPCState currentState;
    for (Connection c : listOfCons) {
        ownerUri = c.getNeedURI();
        needUri = c.getRemoteNeedURI();
        currentStateURI = stateManager.getStateForNeedUri(ownerUri, needUri, getFacetType().getURI());
        // logger.debug("Current state URI is {}: ", currentStateURI);
        if (currentStateURI == null) {
            // c.getConnectionURI());
            continue;
        }
        currentState = BAPCState.parseString(currentStateURI.toString());
        // logger.debug("phase is {}: ", currentState.getPhase());
        BAPCState.Phase phase = BAPCState.parsePhase(stateManager.getStatePhaseForNeedUri(ownerUri, needUri, getFacetType().getURI()).toString());
        if (// redundant, this or the previous condition can be omitted
        phase == null) {
            continue;
        }
        if (BAPCState.getPhaseURI(phase).toString().equals(WON_TX.PHASE_SECOND.getURI().toString())) {
            return false;
        }
    }
    return true;
}
Also used : Connection(won.protocol.model.Connection) URI(java.net.URI) BAPCState(won.node.facet.businessactivity.participantcompletion.BAPCState)

Example 2 with BAPCState

use of won.node.facet.businessactivity.participantcompletion.BAPCState in project webofneeds by researchstudio-sat.

the class BAPCCoordinatorFacetImpl method sendMessageFromOwner.

// Coordinator sends message to Participant
public void sendMessageFromOwner(final Connection con, final Model message, final WonMessage wonMessage) throws NoSuchConnectionException, IllegalMessageForConnectionStateException {
    final URI remoteConnectionURI = con.getRemoteConnectionURI();
    // inform the other side
    executorService.execute(new Runnable() {

        @Override
        public void run() {
            try {
                String messageForSending = new String();
                BAPCEventType eventType = null;
                Model myContent = null;
                Resource r = null;
                // message (event) for sending
                // message as TEXT
                messageForSending = WonRdfUtils.MessageUtils.getTextMessage(message);
                if (messageForSending != null) {
                    eventType = BAPCEventType.getCoordinationEventTypeFromString(messageForSending);
                    logger.debug("*** Coordinator sends the text message {}", eventType);
                    logger.debug("coordinator {}, participant {}", con.getNeedURI(), con.getRemoteNeedURI());
                    logger.debug("con {}", con.getConnectionURI());
                } else // message as MODEL
                {
                    NodeIterator ni = message.listObjectsOfProperty(message.getProperty(WON_TX.COORDINATION_MESSAGE.getURI().toString()));
                    if (ni.hasNext()) {
                        String eventTypeURI = ni.toList().get(0).asResource().getURI().toString();
                        eventType = BAPCEventType.getBAEventTypeFromURI(eventTypeURI);
                        logger.debug("*** Coordinator sends the text message {}", eventType.getURI());
                        logger.debug("coordinator {}, participant {}", con.getNeedURI(), con.getRemoteNeedURI());
                        logger.debug("con {}", con.getConnectionURI());
                    } else {
                        logger.debug("ERROR: Message {} does not contain a proper content.", message.toString());
                        return;
                    }
                }
                myContent = ModelFactory.createDefaultModel();
                myContent.setNsPrefix("", "no:uri");
                Resource baseResource = myContent.createResource("no:uri");
                if ((eventType != null)) {
                    if (BAPCEventType.isBAPCCoordinatorEventType(eventType)) {
                        BAPCState state, newState;
                        state = BAPCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                        logger.debug("Before sending Coordinator has the BAState {} ", state.getURI().toString());
                        newState = state.transit(eventType);
                        stateManager.setStateForNeedUri(newState.getURI(), con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI());
                        storeBAStateForConnection(con, newState.getURI());
                        logger.debug("New state of the Coordinator:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()));
                        // eventType -> URI Resource
                        r = myContent.createResource(eventType.getURI().toString());
                        baseResource.addProperty(WON_TX.COORDINATION_MESSAGE, r);
                    // TODO: use new system
                    // needFacingConnectionClient.sendMessage(con, myContent, wonMessage);
                    } else {
                        logger.debug("The eventType: " + eventType.getURI().toString() + " can not be triggered by Coordinator.");
                    }
                } else {
                    logger.debug("The event type denoted by " + messageForSending + " is not allowed.");
                }
            } catch (Exception e) {
                logger.warn("caught Exception;", e);
            }
        }
    });
}
Also used : BAPCEventType(won.node.facet.businessactivity.participantcompletion.BAPCEventType) URI(java.net.URI) WonProtocolException(won.protocol.exception.WonProtocolException) IllegalMessageForConnectionStateException(won.protocol.exception.IllegalMessageForConnectionStateException) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) BAPCState(won.node.facet.businessactivity.participantcompletion.BAPCState)

Example 3 with BAPCState

use of won.node.facet.businessactivity.participantcompletion.BAPCState in project webofneeds by researchstudio-sat.

the class BAPCParticipantFacetImpl method sendMessageFromOwner.

// Participant sends message to Coordinator
public void sendMessageFromOwner(final Connection con, final Model message, final WonMessage wonMessage) throws NoSuchConnectionException, IllegalMessageForConnectionStateException {
    final URI remoteConnectionURI = con.getRemoteConnectionURI();
    // inform the other side
    executorService.execute(new Runnable() {

        @Override
        public void run() {
            try {
                String messageForSending = new String();
                BAPCEventType eventType = null;
                Model myContent = null;
                Resource r = null;
                // message (event) for sending
                messageForSending = WonRdfUtils.MessageUtils.getTextMessage(message);
                if (messageForSending != null) {
                    eventType = BAPCEventType.getCoordinationEventTypeFromString(messageForSending);
                    logger.debug("*** Participant sends the text message:" + eventType + " coordinator:" + con.getRemoteNeedURI() + " participant:" + con.getNeedURI() + " con:" + con.getConnectionURI() + " baState:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                } else // message as MODEL
                {
                    NodeIterator ni = message.listObjectsOfProperty(message.getProperty(WON_TX.COORDINATION_MESSAGE.getURI().toString()));
                    if (ni.hasNext()) {
                        String eventTypeURI = ni.toList().get(0).asResource().getURI().toString();
                        eventType = BAPCEventType.getBAEventTypeFromURI(eventTypeURI);
                        logger.debug("*** Participant sends the RDF message:" + eventType.getURI() + " coordinator:" + con.getRemoteNeedURI() + " participant:" + con.getNeedURI() + " con:" + con.getConnectionURI() + " baState:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                    }
                }
                myContent = ModelFactory.createDefaultModel();
                myContent.setNsPrefix("", "no:uri");
                Resource baseResource = myContent.createResource("no:uri");
                // message -> eventType
                if ((eventType != null)) {
                    if (eventType.isBAPCParticipantEventType(eventType)) {
                        BAPCState state, newState;
                        state = BAPCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                        newState = state.transit(eventType);
                        stateManager.setStateForNeedUri(newState.getURI(), con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI());
                        storeBAStateForConnection(con, newState.getURI());
                        logger.debug("New state = coordinator:" + con.getRemoteNeedURI() + " participant:" + con.getNeedURI() + " con:" + con.getConnectionURI() + " baState:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                        // eventType -> URI Resource
                        r = myContent.createResource(eventType.getURI().toString());
                        baseResource.addProperty(WON_TX.COORDINATION_MESSAGE, r);
                        // TODO: use new system
                        // needFacingConnectionClient.sendMessage(con, myContent, wonMessage);
                        logger.debug("Participant sent the message.");
                    } else {
                        logger.info("The eventType: {} can not be triggered by Participant.", eventType.getURI().toString());
                    }
                } else {
                    logger.info("The event type denoted by {} is not allowed.", messageForSending);
                }
            } catch (Exception e) {
                logger.debug("caught Exception", e);
            }
        }
    });
}
Also used : BAPCEventType(won.node.facet.businessactivity.participantcompletion.BAPCEventType) URI(java.net.URI) WonProtocolException(won.protocol.exception.WonProtocolException) IllegalMessageForConnectionStateException(won.protocol.exception.IllegalMessageForConnectionStateException) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) BAPCState(won.node.facet.businessactivity.participantcompletion.BAPCState)

Example 4 with BAPCState

use of won.node.facet.businessactivity.participantcompletion.BAPCState in project webofneeds by researchstudio-sat.

the class BAAtomicPCCoordinatorFacetImpl method sendMessageFromOwner.

// Coordinator sends message to Participant
public void sendMessageFromOwner(final Connection con, final Model message, final WonMessage wonMessage) throws NoSuchConnectionException, IllegalMessageForConnectionStateException {
    final URI remoteConnectionURI = con.getRemoteConnectionURI();
    // inform the other side
    executorService.execute(new Runnable() {

        @Override
        public void run() {
            try {
                String messageForSending = new String();
                BAPCEventType eventType = null;
                Model myContent = null;
                Resource r = null;
                // message (event) for sending
                // message as TEXT
                messageForSending = WonRdfUtils.MessageUtils.getTextMessage(message);
                if (messageForSending != null) {
                    eventType = BAPCEventType.getCoordinationEventTypeFromString(messageForSending);
                    logger.debug("Coordinator sends the text message:" + eventType + " coordinator:" + con.getNeedURI() + " participant:" + con.getRemoteNeedURI() + " con:" + con.getConnectionURI() + " baState:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString() + " phase:" + BAPCState.parsePhase(stateManager.getStatePhaseForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString()));
                } else // message as MODEL
                {
                    NodeIterator ni = message.listObjectsOfProperty(message.getProperty(WON_TX.COORDINATION_MESSAGE.getURI().toString()));
                    if (ni.hasNext()) {
                        String eventTypeURI = ni.toList().get(0).asResource().getURI().toString();
                        eventType = BAPCEventType.getBAEventTypeFromURI(eventTypeURI);
                        logger.debug("Coordinator sends the RDF message:" + eventType.getURI() + " coordinator:" + con.getNeedURI() + " participant:" + con.getRemoteNeedURI() + " con:" + con.getConnectionURI() + " baState:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString() + " phase:" + BAPCState.parsePhase(stateManager.getStatePhaseForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString()));
                    } else {
                        logger.debug("Message {} does not contain a proper content.", message.toString());
                        return;
                    }
                }
                myContent = ModelFactory.createDefaultModel();
                myContent.setNsPrefix("", "no:uri");
                Resource baseResource = myContent.createResource("no:uri");
                if ((eventType != null)) {
                    if (eventType.isBAPCCoordinatorEventType(eventType)) {
                        if (isCoordinatorInFirstPhase(con) && eventType.getURI().toString().equals(WON_TX.MESSAGE_CLOSE.getURI().toString())) {
                            // if no -> wait for other participants to vote
                            if (canFirstPhaseFinish(con)) {
                                BAPCState state = BAPCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                                state = state.transit(eventType);
                                // Second phase is starting!
                                state.setPhase(BAPCState.Phase.SECOND);
                                stateManager.setStateForNeedUri(state.getURI(), URI.create(WON_TX.PHASE_SECOND.getURI().toString()), con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI());
                                storeBAStateForConnection(con, state.getURI());
                                logger.debug("New state = coordinator:" + con.getNeedURI() + " participant:" + con.getRemoteNeedURI() + " con:" + con.getConnectionURI() + " baState:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString() + " phase:" + BAPCState.parsePhase(stateManager.getStatePhaseForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString()));
                                // eventType -> URI Resource
                                r = myContent.createResource(eventType.getURI().toString());
                                baseResource.addProperty(WON_TX.COORDINATION_MESSAGE, r);
                                // TODO: use new system
                                // needFacingConnectionClient.sendMessage(con, myContent, wonMessage);
                                logger.debug("Coordinator sent message to participant");
                                // trigger the second phase in the corresponding states
                                propagateSecondPhase(con);
                            } else {
                                logger.debug("Not all votes are received from Participants! First phase can not finish.");
                            }
                        } else {
                            BAPCState state = BAPCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                            BAPCState newState = null;
                            newState = state.transit(eventType);
                            BAPCState.Phase newPhase;
                            if (!(isCoordinatorInFirstPhase(con) && eventType.getURI().toString().equals(WON_TX.MESSAGE_CANCEL.getURI().toString())))
                                newPhase = BAPCState.parsePhase(stateManager.getStatePhaseForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
                            else
                                // propagate phase,
                                newPhase = BAPCState.Phase.CANCELED_FROM_COORDINATOR;
                            newState.setPhase(newPhase);
                            stateManager.setStateForNeedUri(newState.getURI(), BAPCState.getPhaseURI(newPhase), con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI());
                            storeBAStateForConnection(con, newState.getURI());
                            logger.debug("New state = coordinator:" + con.getNeedURI() + " participant:" + con.getRemoteNeedURI() + " con:" + con.getConnectionURI() + " baState:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString() + " phase:" + BAPCState.parsePhase(stateManager.getStatePhaseForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString()));
                            // eventType -> URI Resource
                            r = myContent.createResource(eventType.getURI().toString());
                            baseResource.addProperty(WON_TX.COORDINATION_MESSAGE, r);
                            // TODO: use new system
                            // needFacingConnectionClient.sendMessage(con, myContent, wonMessage);
                            logger.debug("Coordinator sent message to participant");
                        }
                    } else {
                        logger.debug("The eventType: {} can not be triggered by Coordinator.", eventType.getURI().toString());
                    }
                } else {
                    logger.debug("The event type denoted by {} is not allowed.", messageForSending);
                }
            } catch (Exception e) {
                logger.debug("caught Exception", e);
            }
        }
    });
}
Also used : BAPCEventType(won.node.facet.businessactivity.participantcompletion.BAPCEventType) URI(java.net.URI) WonProtocolException(won.protocol.exception.WonProtocolException) IllegalMessageForConnectionStateException(won.protocol.exception.IllegalMessageForConnectionStateException) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) BAPCState(won.node.facet.businessactivity.participantcompletion.BAPCState)

Example 5 with BAPCState

use of won.node.facet.businessactivity.participantcompletion.BAPCState in project webofneeds by researchstudio-sat.

the class BAAtomicPCCoordinatorFacetImpl method propagateSecondPhase.

private void propagateSecondPhase(Connection con) {
    List<Connection> listOfCons = getAllCoordinatorParticipantConnections(con);
    BAPCState state;
    for (Connection currentCon : listOfCons) {
        state = BAPCState.parseString(stateManager.getStateForNeedUri(currentCon.getNeedURI(), currentCon.getRemoteNeedURI(), getFacetType().getURI()).toString());
        BAPCState.Phase phase = BAPCState.parsePhase(stateManager.getStatePhaseForNeedUri(currentCon.getNeedURI(), currentCon.getRemoteNeedURI(), getFacetType().getURI()).toString());
        if (BAPCState.getPhaseURI(phase).toString().equals(WON_TX.PHASE_FIRST.getURI().toString())) {
            state.setPhase(BAPCState.Phase.SECOND);
            stateManager.setStateForNeedUri(state.getURI(), URI.create(WON_TX.PHASE_SECOND.getURI().toString()), currentCon.getNeedURI(), currentCon.getRemoteNeedURI(), getFacetType().getURI());
            storeBAStateForConnection(currentCon, state.getURI());
        }
    }
}
Also used : Connection(won.protocol.model.Connection) BAPCState(won.node.facet.businessactivity.participantcompletion.BAPCState)

Aggregations

BAPCState (won.node.facet.businessactivity.participantcompletion.BAPCState)5 URI (java.net.URI)4 BAPCEventType (won.node.facet.businessactivity.participantcompletion.BAPCEventType)3 IllegalMessageForConnectionStateException (won.protocol.exception.IllegalMessageForConnectionStateException)3 NoSuchConnectionException (won.protocol.exception.NoSuchConnectionException)3 WonProtocolException (won.protocol.exception.WonProtocolException)3 Connection (won.protocol.model.Connection)2