Menu

[r91]: / trunk / samples / simple_survey / SimpleSurvey.java  Maximize  Restore  History

Download this file

148 lines (123 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright 2007-2012 Amazon Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://aws.amazon.com/apache2.0
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and
* limitations under the License.
*/
package simple_survey;
import com.amazonaws.mturk.addon.HITQuestion;
import com.amazonaws.mturk.requester.Comparator;
import com.amazonaws.mturk.requester.HIT;
import com.amazonaws.mturk.requester.Locale;
import com.amazonaws.mturk.requester.QualificationRequirement;
import com.amazonaws.mturk.service.axis.RequesterService;
import com.amazonaws.mturk.util.PropertiesClientConfig;
/**
* The Simple Survey sample application will create a HIT asking a worker to indicate their
* political party preferences.
*
* mturk.properties must be found in the current file path.
*
* The following concepts are covered:
* - File based QAP HIT loading
* - Using a locale qualification
*/
public class SimpleSurvey {
private RequesterService service;
//Defining the attributes of the HIT
private String title = "What is your political preference?";
private String description = "This is a simple survey HIT created by MTurk SDK.";
private int numAssignments = 1;
private double reward = 0.05;
private String keywords = "sample, SDK, survey";
private long assignmentDurationInSeconds = 60 * 60; // 1 hour
private long autoApprovalDelayInSeconds = 60 * 60 * 24 * 15; // 15 days
private long lifetimeInSeconds = 60 * 60 * 24 * 3; // 3 days
private String requesterAnnotation = "sample#survey";
//Defining the location of the externalized question (QAP) file.
private String rootDir = ".";
private String questionFile = rootDir + "/simple_survey.question";
/**
* Constructor
*
*/
public SimpleSurvey() {
service = new RequesterService(new PropertiesClientConfig("../mturk.properties"));
}
/**
* Checks to see if there are sufficient funds in your account to run this sample.
* @return true if there are sufficient funds. False if not.
*/
public boolean hasEnoughFund() {
double balance = service.getAccountBalance();
System.out.println("Got account balance: " + RequesterService.formatCurrency(balance));
return balance > 0;
}
/**
* Creates the simple survey.
*
*/
public void createSimpleSurvey() {
try {
// This is an example of creating a qualification.
// This is a built-in qualification -- user must be based in the US
QualificationRequirement qualReq = new QualificationRequirement();
qualReq.setQualificationTypeId(RequesterService.LOCALE_QUALIFICATION_TYPE_ID);
qualReq.setComparator(Comparator.EqualTo);
Locale country = new Locale();
country.setCountry("US");
qualReq.setLocaleValue(country);
// The create HIT method takes in an array of QualificationRequirements
// since a HIT can have multiple qualifications.
QualificationRequirement[] qualReqs = null;
qualReqs = new QualificationRequirement[] { qualReq };
// Loading the question (QAP) file. HITQuestion is a helper class that
// contains the QAP of the HIT defined in the external file. This feature
// allows you to write the entire QAP externally as a file and be able to
// modify it without recompiling your code.
HITQuestion question = new HITQuestion(questionFile);
//Creating the HIT and loading it into Mechanical Turk
HIT hit = service.createHIT(null, // HITTypeId
title,
description, keywords,
question.getQuestion(),
reward, assignmentDurationInSeconds,
autoApprovalDelayInSeconds, lifetimeInSeconds,
numAssignments, requesterAnnotation,
qualReqs,
null // responseGroup
);
System.out.println("Created HIT: " + hit.getHITId());
System.out.println("You may see your HIT with HITTypeId '"
+ hit.getHITTypeId() + "' here: ");
System.out.println(service.getWebsiteURL()
+ "/mturk/preview?groupId=" + hit.getHITTypeId());
//Demonstrates how a HIT can be retrieved if you know its HIT ID
HIT hit2 = service.getHIT(hit.getHITId());
System.out.println("Retrieved HIT: " + hit2.getHITId());
if (!hit.getHITId().equals(hit2.getHITId())) {
System.err.println("The HIT Ids should match: "
+ hit.getHITId() + ", " + hit2.getHITId());
}
} catch (Exception e) {
System.err.println(e.getLocalizedMessage());
}
}
/**
* @param args
*/
public static void main(String[] args) {
SimpleSurvey app = new SimpleSurvey();
if (app.hasEnoughFund()) {
app.createSimpleSurvey();
}
}
}