In the scenario described at Negotiation Scenario, the negotiation process is driven by the consumer. Please refer to Sample Negotiator Client for detailed implementation.
AgreementFactoryClient[] factories = getAgreementFactoryClients();
AgreementFactoryClient factory = null;
if(factories[0].getResourceId().equals(FACTORY_RESOURCE_ID)) {
factory = getAgreementFactoryClients()[0];
}
else {
factory = getAgreementFactoryClients()[1];
}
NegotiationContextDocument negContextDoc = NegotiationContextDocument.Factory.newInstance(); NegotiationContextType negContext = negContextDoc.addNewNegotiationContext(); negContext.setAgreementFactoryEPR(factory.getEndpoint()); negContext.setAgreementResponder(NegotiationRoleType.NEGOTIATION_RESPONDER); GregorianCalendar expireDate = new GregorianCalendar(); expireDate.add(Calendar.HOUR, 1); negContext.setExpirationTime(expireDate);
NegotiationType negotiationType = negContext.addNewNegotiationType(); negotiationType.addNewNegotiation();
NegotiationClient negotiation = factory.initiateNegotiation(negContext);
AgreementTemplateType[] negotiableTemplates = negotiation.getNegotiableTemplates();
AgreementTemplateType template = null;
for (int i = 0; i < negotiableTemplates.length; i++) {
AgreementTemplateType agreementTemplate = negotiableTemplates[i];
if(agreementTemplate.getName().equals(TEMPLATE_NAME)) {
template = agreementTemplate;
}
}
SampleAgreementTemplate negotiationTemplate = new SampleAgreementTemplate(template); String offerID = negotiationTemplate.getContext().getTemplateId() + "-" + negotiationTemplate.getName();
<wsag:ServiceDescriptionTerm wsag:Name="RESOURCE_STD" wsag:ServiceName="SAMPLE-NEGOTIATION_SERVICE">
<jsdl:JobDefinition xmlns:jsdl="http://schemas.ggf.org/jsdl/2005/11/jsdl">
<jsdl:JobDescription>
<jsdl:Resources>
<jsdl:CandidateHosts>
<jsdl:HostName>some_host</jsdl:HostName>
</jsdl:CandidateHosts>
<jsdl:TotalResourceCount>
<jsdl:Exact>$TOTALRESOURCES</jsdl:Exact>
</jsdl:TotalResourceCount>
</jsdl:Resources>
</jsdl:JobDescription>
</jsdl:JobDefinition>
</wsag:ServiceDescriptionTerm>
We negotiate 5 resources (*TotalResourceCount*).
ResourcesType jobResources_1 = negotiationTemplate.getResourceDefinition(); RangeValueType totalCountRange_1 = RangeValueType.Factory.newInstance(); totalCountRange_1.addNewExact().setDoubleValue(5); jobResources_1.setTotalResourceCount(totalCountRange_1);
<wsag:ServiceDescriptionTerm wsag:Name="TIME_CONSTRAINT_SDT" wsag:ServiceName="SAMPLE-NEGOTIATION_SERVICE">
<wsag4jt:TimeConstraint xmlns:wsag4jt="http://schemas.wsag4j.org/2009/07/wsag4j-scheduling-extensions">
<wsag4jt:StartTime>$STARTTIME</wsag4jt:StartTime>
<wsag4jt:EndTime>$ENDTIME</wsag4jt:EndTime>
<wsag4jt:Duration>$DURATION</wsag4jt:Duration>
</wsag4jt:TimeConstraint>
</wsag:ServiceDescriptionTerm>
We specify the start time (*StartTime*) as 5 minute ahead of the current time and end time (*EndTime*) after 60 minutes (based on the start time) and the effective duration (*Duration*) of the advance reservation is 15 minutes.
TimeConstraintType timeConstraint_1 = negotiationTemplate.getTimeConstraint(); Calendar startTime_1 = (Calendar) timeConstraint_1.getStartTime().clone(); timeConstraint_1.setStartTime(startTime_1); Calendar endTime_1 = (Calendar) timeConstraint_1.getEndTime().clone(); timeConstraint_1.setEndTime(endTime_1); timeConstraint_1.setDuration(15);
SampleNegotiationOffer negotiationOffer_1 = negotiationTemplate.getNegotiationOffer(); negotiationOffer_1.setOfferId(offerID);
NegotiationOfferContextType negOfferContext = NegotiationOfferContextType.Factory.newInstance(); negOfferContext.setCreator(NegotiationRoleType.NEGOTIATION_INITIATOR); GregorianCalendar expireDate = new GregorianCalendar(); expireDate.add(Calendar.MINUTE, 5); negOfferContext.setExpirationTime(expireDate); NegotiationOfferStateType negOfferState = NegotiationOfferStateType.Factory.newInstance(); negOfferState.addNewAdvisory(); negOfferContext.setState(negOfferState); negOfferContext.setCounterOfferTo(offerID); negotiationOffer_1.setNegotiationOfferContext(negOfferContext);
String CONSTRAINT_ITEM_NAME = "TimeConstraintSDT_TimeConstraint_START_TIME";
String CONSTRAINT_XPATH = "declare namespace wsag-tc='http://schemas.wsag4j.org/2009/07/wsag4j-scheduling-extensions';" +
"declare namespace wsag='http://schemas.ggf.org/graap/2007/03/ws-agreement';" +
"$this/wsag:Terms/wsag:All/wsag:ServiceDescriptionTerm[@wsag:Name = 'TIME_CONSTRAINT_SDT']/wsag4jt:TimeConstraint";
NegotiationConstraintSectionType constraints_1 = NegotiationConstraintSectionType.Factory.newInstance();
NegotiationOfferItemType offerItem = constraints.addNewItem();
offerItem.setName(CONSTRAINT_ITEM_NAME);
offerItem.setLocation(CONSTRAINT_XPATH);
ItemConstraint constraint = offerItem.addNewItemConstraint();
constraint.addNewMinInclusive().setValue(XmlDateTime.Factory.newValue(timeConstraint_1.getStartTime()));
Calendar preferredEndTime = (Calendar) timeConstraint_1.getStartTime().clone();
preferredEndTime.add(Calendar.MINUTE, 45);
constraint.addNewMaxInclusive().setValue(XmlDateTime.Factory.newValue(preferredEndTime));
negotiationOffer_1.setNegotiationConstraints(constraints_1);
ServiceDescriptionTermType resourcesSDT = null;
ServiceDescriptionTermType[] sdts = negotiationOffer.getTerms().getAll().getServiceDescriptionTermArray();
if(sdts != null) {
for (int i = 0; i < sdts.length; i++) {
if(sdts[i].getName().equals(resourcesSDTName)) {
resourcesSDT = sdts[i];
break;
}
}
}
JobDefinitionDocument resourcesDoc = JobDefinitionDocument.Factory.newInstance();
resourcesDoc.addNewJobDefinition().addNewJobDescription().addNewResources();
resourcesDoc.getJobDefinition().getJobDescription().getResources().set(jobResources);
resourcesSDT.set(resourcesDoc);
String name = resourcesSDT.getName();
resourcesSDT.setName(name);
String serviceName = resourcesSDT.getServiceName();
resourcesSDT.setServiceName(serviceName);
ServiceDescriptionTermType timeConstraintSDT = null;
ServiceDescriptionTermType[] sdts = negotiationOffer.getTerms().getAll().getServiceDescriptionTermArray();
if(sdts != null) {
for (int i = 0; i < sdts.length; i++) {
if(sdts[i].getName().equals(timeConstraintSDTName)) {
timeConstraintSDT = sdts[i];
break;
}
}
}
TimeConstraintDocument timeConstraintDoc = TimeConstraintDocument.Factory.newInstance();
timeConstraintDoc.addNewTimeConstraint();
timeConstraintDoc.getTimeConstraint().set(timeConstraint);
timeConstraintSDT.set(timeConstraintDoc);
String name = timeConstraintSDT.getName();
timeConstraintSDT.setName(name);
String serviceName = timeConstraintSDT.getServiceName();
timeConstraintSDT.setServiceName(serviceName);
NegotiationOfferType[] negotiationOfferTypes_1 = {negotiationOffer_1.getXMLObject()};
NegotiationOfferType[] counterOffers_1 = negotiation.negotiate(negotiationOfferTypes_1);
NegotiationOfferType counterOffer_1 = counterOffers_1[0];
if(counterOffer_1.getNegotiationOfferContext().getState().isSetRejected()) {
... do something ...
}
SampleNegotiationOffer negotiationOffer_2 = new SampleNegotiationOffer(counterOffer_1); ResourcesType jobResources_2 = negotiationOffer_2.getResourceDefinition(); RangeValueType totalCountRange_2 = RangeValueType.Factory.newInstance(); totalCountRange_2.addNewExact().setDoubleValue(5); jobResources_2.setTotalResourceCount(totalCountRange_2); TimeConstraintType timeConstraint_2 = negotiationOffer_2.getTimeConstraint(); Calendar startTime_2 = (Calendar) timeConstraint_2.getStartTime().clone(); startTime_2.add(Calendar.MINUTE, 10); timeConstraint_2.setStartTime(startTime_2); Calendar endTime_2 = (Calendar) startTime_2.clone(); endTime_2.add(Calendar.MINUTE, 20); timeConstraint_2.setEndTime(endTime_2); timeConstraint_2.setDuration(15);
NegotiationOfferType[] negotiationOfferTypes_2 = {negotiationOffer_2.getXMLObject()};
NegotiationOfferType[] counterOffers_2 = negotiation.negotiate(negotiationOfferTypes_2);
NegotiationOfferType counterOffer_2 = counterOffers_2[0];
if(counterOffer_2.getNegotiationOfferContext().getState().isSetRejected()) {
... do something ...
}
NegotiationOfferType selectedCounterOffer = counterOffers_2[0];
AgreementOffer offer = new AgreementOfferType(selectedCounterOffer); AgreementClient agreement = factory.createAgreement(offer);
negotiation.terminate();