use of won.node.facet.businessactivity.coordinatorcompletion.BACCState in project webofneeds by researchstudio-sat.
the class BAAtomicCCCoordinatorFacetImpl method isCoordinatorInFirstPhase.
private boolean isCoordinatorInFirstPhase(Connection con) {
List<Connection> listOfCons = getAllCoordinatorParticipantConnections(con);
URI ownerUri, needUri, currentStateURI;
BACCState currentState;
logger.debug("Checking if coordinator is in the first phase for coordinator " + con.getNeedURI() + "participant" + con.getRemoteNeedURI() + "and connection" + con.getConnectionURI());
for (Connection c : listOfCons) {
ownerUri = c.getNeedURI();
needUri = c.getRemoteNeedURI();
logger.debug("checking if coordinator is in first phase for coordinator {} and participant {}", ownerUri, needUri);
currentStateURI = stateManager.getStateForNeedUri(ownerUri, needUri, getFacetType().getURI());
logger.debug("Current coordinator state URI is {}: ", currentStateURI);
if (currentStateURI == null) {
// this happens when a connection is being opened in a parallel thread
// and execution hasn't yet reached the point where its state is recorded
// That other connection definitely hasn't caused phase 2 to start, so
// we can safely say we're still in phase 1.
// Therefore, continue with the next check.
logger.debug("ignoring connection {} for phase one check as it" + " hasn't been processed by the facet logic yet", c.getConnectionURI());
continue;
}
BACCState.Phase phase = BACCState.parsePhase(stateManager.getStatePhaseForNeedUri(ownerUri, needUri, getFacetType().getURI()).toString());
if (// redundant, this or the previous condition can be omitted
phase == null) {
continue;
}
if (BACCState.getPhaseURI(phase).toString().equals(WON_TX.PHASE_SECOND.getURI().toString())) {
return false;
}
}
return true;
}
use of won.node.facet.businessactivity.coordinatorcompletion.BACCState in project webofneeds by researchstudio-sat.
the class BACCParticipantFacetImpl 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();
BACCEventType eventType = null;
Model myContent = null;
Resource r = null;
// message (event) for sending
// message as TEXT
messageForSending = WonRdfUtils.MessageUtils.getTextMessage(message);
if (messageForSending != null) {
logger.debug("Participant sends: " + messageForSending);
eventType = BACCEventType.getCoordinationEventTypeFromString(messageForSending);
} else // message as MODEL
{
NodeIterator ni = message.listObjectsOfProperty(message.getProperty(WON_TX.COORDINATION_MESSAGE.getURI()));
if (ni.hasNext()) {
String eventTypeURI = ni.toList().get(0).asResource().getURI().toString();
eventType = BACCEventType.getBAEventTypeFromURI(eventTypeURI);
}
}
myContent = ModelFactory.createDefaultModel();
myContent.setNsPrefix("", "no:uri");
Resource baseResource = myContent.createResource("no:uri");
// message -> eventType
if ((eventType != null)) {
if (eventType.isBACCParticipantEventType(eventType)) {
BACCState state, newState;
state = BACCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
logger.debug("Current state of the Participant {} for Coordinator {} ", state.getURI().toString(), con.getRemoteNeedURI());
newState = state.transit(eventType);
stateManager.setStateForNeedUri(newState.getURI(), con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI());
storeBAStateForConnection(con, newState.getURI());
logger.debug("New state of the Participant:" + 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 Participant.");
}
} else {
logger.debug("The event type denoted by " + messageForSending + " is not allowed.");
}
} catch (Exception e) {
logger.warn("caught Exception:", e);
}
}
});
}
use of won.node.facet.businessactivity.coordinatorcompletion.BACCState in project webofneeds by researchstudio-sat.
the class BAAtomicCCCoordinatorFacetImpl method propagateSecondPhase.
private void propagateSecondPhase(Connection con) {
List<Connection> listOfCons = getAllCoordinatorParticipantConnections(con);
BACCState state;
for (Connection currentCon : listOfCons) {
state = BACCState.parseString(stateManager.getStateForNeedUri(currentCon.getNeedURI(), currentCon.getRemoteNeedURI(), getFacetType().getURI()).toString());
if (state.getPhase() == BACCState.Phase.FIRST) {
state.setPhase(BACCState.Phase.SECOND);
stateManager.setStateForNeedUri(state.getURI(), URI.create(WON_TX.PHASE_SECOND.getURI().toString()), currentCon.getNeedURI(), currentCon.getRemoteNeedURI(), getFacetType().getURI());
storeBAStateForConnection(currentCon, state.getURI());
}
}
}
use of won.node.facet.businessactivity.coordinatorcompletion.BACCState in project webofneeds by researchstudio-sat.
the class BAAtomicCCCoordinatorFacetImpl 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();
BACCEventType eventType = null;
Model myContent = null;
Resource r = null;
// message (event) for sending
// message as TEXT
messageForSending = WonRdfUtils.MessageUtils.getTextMessage(message);
if (messageForSending != null) {
eventType = BACCEventType.getCoordinationEventTypeFromString(messageForSending);
logger.debug("Coordinator sends the text message: {}", eventType);
} 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 = BACCEventType.getBAEventTypeFromURI(eventTypeURI);
logger.debug("Coordinator sends the RDF: {}", eventType.getURI());
} 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.isBACCCoordinatorEventType(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)) {
logger.debug("First phase can be finished.");
BACCState state = BACCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
logger.debug("Current state of the Coordinator {} for participant {}: ", state.getURI().toString(), con.getRemoteNeedURI().toString());
logger.debug("This Coordinator state phase: {}", BACCState.parsePhase(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString()));
state = state.transit(eventType);
// Second phase is starting!
state.setPhase(BACCState.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 of the Coordinator:" + stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()));
logger.debug("This Coordinator state phase: {}", BACCState.parsePhase(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);
// trigger the second phase in the corresponding states
propagateSecondPhase(con);
} else {
logger.debug("Not all votes are received from Participants!");
}
} else {
BACCState state = BACCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
BACCState newState = null;
logger.debug("Current state of the Coordinator {} for participant {}: ", state.getURI().toString(), con.getRemoteNeedURI());
logger.debug("This Coordinator state phase: {}", BACCState.parsePhase(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString()));
newState = state.transit(eventType);
BACCState.Phase newPhase;
if (!(isCoordinatorInFirstPhase(con) && eventType.getURI().toString().equals(WON_TX.MESSAGE_CANCEL.getURI().toString())))
newPhase = BACCState.parsePhase(stateManager.getStatePhaseForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
else
// propagate phase,
newPhase = BACCState.Phase.CANCELED_FROM_COORDINATOR;
newState.setPhase(newPhase);
stateManager.setStateForNeedUri(newState.getURI(), BACCState.getPhaseURI(newPhase), 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()));
logger.debug("Coordinator phase: {}", BACCState.parsePhase(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);
}
} 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.debug("caught Exception", e);
}
}
});
}
use of won.node.facet.businessactivity.coordinatorcompletion.BACCState in project webofneeds by researchstudio-sat.
the class BACCCoordinatorFacetImpl 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();
BACCEventType eventType = null;
Model myContent = null;
Resource r = null;
// message (event) for sending
// message as TEXT
messageForSending = WonRdfUtils.MessageUtils.getTextMessage(message);
if (messageForSending != null) {
eventType = BACCEventType.getCoordinationEventTypeFromString(messageForSending);
logger.debug("Coordinator sends the text message: {}", eventType);
} 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 = BACCEventType.getBAEventTypeFromURI(eventTypeURI);
logger.debug("Coordinator sends the RDF: {}", eventType.getURI());
} 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.isBACCCoordinatorEventType(eventType)) {
BACCState state, newState;
state = BACCState.parseString(stateManager.getStateForNeedUri(con.getNeedURI(), con.getRemoteNeedURI(), getFacetType().getURI()).toString());
logger.debug("Current state of the Coordinator: " + 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);
}
}
});
}
Aggregations