Add support for qualifiers

This commit is contained in:
Antonin Delpeuch 2017-09-07 11:37:00 +01:00
parent c0f2468236
commit 66b3ddf76b
4 changed files with 116 additions and 12 deletions

View File

@ -292,8 +292,6 @@ SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) {
SchemaAlignmentDialog._addStatement = function(container, datatype, json) {
console.log('addStatement');
console.log(datatype);
var qualifiers = null;
var value = null;
if (json) {
@ -309,21 +307,63 @@ SchemaAlignmentDialog._addStatement = function(container, datatype, json) {
var inputContainer = $('<div></div>').addClass('wbs-target-input').appendTo(statement);
SchemaAlignmentDialog._initField(inputContainer, datatype, value);
var right = $('<div></div>').addClass('wbs-right').appendTo(statement);
$('<div></div>').addClass('wbs-qualifier-container').appendTo(right);
// If we are in a mainsnak, add qualifiers and references
if (container.parents('.wbs-statement').length == 0) {
var qualifierContainer = $('<div></div>').addClass('wbs-qualifier-container').appendTo(right);
var toolbar2 = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
$('<a></a>').addClass('wbs-add-qualifier').text('add qualifier').appendTo(toolbar2);
$('<a></a>').addClass('wbs-add-qualifier').text('add qualifier').click(function() {
SchemaAlignmentDialog._addQualifier(qualifierContainer, null);
}).appendTo(toolbar2);
if (qualifiers) {
for (var i = 0; i != qualifiers.length; i++) {
SchemaAlignmentDialog._addQualifier(qualifierContainer, qualifiers[i]);
}
}
}
container.append(statement);
}
SchemaAlignmentDialog._statementToJSON = function (statement) {
var inputContainer = statement.find(".wbs-target-input").first();
var qualifiersList = new Array();
statement.find('.wbs-qualifier').each(function () {
qualifiersList.push(SchemaAlignmentDialog._qualifierToJSON($(this)));
});
return {
value:SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
qualifiers:[],
qualifiers: qualifiersList,
references:[],
};
};
SchemaAlignmentDialog._addQualifier = function(container, json) {
var property = null;
if (json) {
property = json.property;
}
var qualifier = $('<div></div>').addClass('wbs-qualifier').appendTo(container);
var toolbar1 = $('<div></div>').addClass('wbs-toolbar').appendTo(qualifier);
var inputContainer = $('<div></div>').addClass('wbs-prop-input').appendTo(qualifier);
var right = $('<div></div>').addClass('wbs-right').appendTo(qualifier);
var statementContainer = $('<div></div>').addClass('wbs-statement-container').appendTo(right);
SchemaAlignmentDialog._initPropertyField(inputContainer, statementContainer, property);
}
SchemaAlignmentDialog._removeQualifier = function(qualifier) {
qualifier.remove();
}
SchemaAlignmentDialog._qualifierToJSON = function(elem) {
var prop = elem.find(".wbs-prop-input").first();
var target = elem.find(".wbs-target-input").first();
return {
prop: SchemaAlignmentDialog._inputContainerToJSON(prop),
value: SchemaAlignmentDialog._inputContainerToJSON(target),
};
}
SchemaAlignmentDialog._getPropertyType = function(pid, callback) {
$.ajax({
url:'https://www.wikidata.org/w/api.php',
@ -350,7 +390,7 @@ SchemaAlignmentDialog._initPropertyField = function(inputContainer, targetContai
input.suggestP(suggestConfig).bind("fb-select", function(evt, data) {
// Fetch the type of this property and add the appropriate target value type
var statementGroup = inputContainer.parents(".wbs-statement-group").first();
var statementGroup = inputContainer.parents(".wbs-statement-group, .wbs-qualifier").first();
SchemaAlignmentDialog._getPropertyType(data.id, function(datatype) {
inputContainer.data("jsonValue", {
type : "wbpropconstant",
@ -400,7 +440,7 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue)
});
SchemaAlignmentDialog._hasChanged();
});
} else if (mode === "external-id") {
} else { /* if (mode === "external-id") { */
var propagateValue = function(val) {
inputContainer.data("jsonValue", {
type: "wbstringconstant",
@ -471,11 +511,16 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue)
}
SchemaAlignmentDialog._inputContainerToJSON = function (inputContainer) {
return inputContainer.data().jsonValue;
var data = inputContainer.data();
if (data) {
return data.jsonValue;
} else {
return null;
}
};
SchemaAlignmentDialog._removeStatement = function(statement) {
var statementGroup = statement.parents('.wbs-statement-group').first();
var statementGroup = statement.parents('.wbs-statement-group, .wbs-qualifier').first();
statement.remove();
var remainingStatements = statementGroup.find('.wbs-statement').length;
if (remainingStatements === 0) {

View File

@ -82,6 +82,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
width: 120px;
}
.wbs-qualifier .wbs-prop-input, .wbs-reference .wbs-prop-input {
width: 85px;
}
.wbs-item-input input, .wbs-prop-input input, .wbs-target-input input {
width: 100%;
}
@ -107,7 +111,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
overflow: auto;
}
.wbs-item, .wbs-statement-group, .wbs-statement {
.wbs-item, .wbs-statement-group, .wbs-statement, .wbs-qualifier {
display: block;
overflow: auto;
}
@ -123,9 +127,36 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.wbs-right {
float: right;
width: 75%;
}
.wbs-statement-group .wbs-right {
width: 70%;
}
.wbs-statement .wbs-right {
width: 90%;
}
.wbs-qualifier, .wbs-reference {
position: relative;
}
.wbs-qualifier .wbs-right, .wbs-reference .wbs-right {
width: 70%;
position: absolute;
top: 0px;
margin-left: 100px;
}
.wbs-qualifier .wbs-target-input {
width: 90px;
}
.wbs-qualifier .wbs-statement {
overflow: hidden;
}
.schema-alignment-columns-header {
margin-bottom: 0.3em;
}

View File

@ -20,6 +20,8 @@ import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.QuantityValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementGroup;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
@ -83,7 +85,30 @@ public class QuickStatementsExporter implements WriterExporter {
ValueVisitor<String> vv = new ValuePrinter();
String targetValue = val.accept(vv);
if (targetValue != null) {
writer.write(qid + "\t" + pid + "\t" + targetValue + "\n");
writer.write(qid + "\t" + pid + "\t" + targetValue);
for(SnakGroup q : claim.getQualifiers()) {
translateSnakGroup(q, false, writer);
}
writer.write("\n");
}
}
protected void translateSnakGroup(SnakGroup sg, boolean reference, Writer writer) throws IOException {
for(Snak s : sg.getSnaks()) {
translateSnak(s, reference, writer);
}
}
protected void translateSnak(Snak s, boolean reference, Writer writer) throws IOException {
String pid = s.getPropertyId().getId();
if (reference) {
pid = pid.replace('P', 'S');
}
Value val = s.getValue();
ValueVisitor<String> vv = new ValuePrinter();
String valStr = val.accept(vv);
if(valStr != null) {
writer.write("\t" + pid + "\t" + valStr);
}
}

View File

@ -96,6 +96,9 @@ public class WbStatementExpr extends BiJsonizable {
Value mainSnakValue = mainSnakValueExpr.evaluate(ctxt);
Snak mainSnak = Datamodel.makeValueSnak(propertyId, mainSnakValue);
List<Snak> qualifiers = new ArrayList<Snak>(qualifierExprs.size());
for (WbSnakExpr qExpr : qualifierExprs) {
qualifiers.add(qExpr.evaluate(ctxt));
}
List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
Claim claim = Datamodel.makeClaim(subject, mainSnak, groupedQualifiers);
List<Reference> references = new ArrayList<Reference>();