Synchronize schema storage and UI behaviour.

This commit is contained in:
Antonin Delpeuch 2017-08-31 15:45:25 +01:00
parent 3fd71965fa
commit decef38f85
15 changed files with 263 additions and 72 deletions

View File

@ -171,19 +171,18 @@ SchemaAlignmentDialog.launch = function(onDone) {
this._hasUnsavedChanges = false;
this._createDialog();
// this._reset(schema, true);
this._reset(theProject.overlayModels.wikibaseSchema, true);
}
SchemaAlignmentDialog._reset = function(protograph, initial) {
this._originalProtograph = protograph || { rootNodes: [] };
this._protograph = cloneDeep(this._originalProtograph); // this is what can be munched on
SchemaAlignment
if (!this._protograph.rootNodes.length) {
this._protograph.rootNodes.push(SchemaAlignment.createNewRootNode());
}
SchemaAlignmentDialog._reset = function(schema, initial) {
this._originalSchema = schema || { changes: [] };
this._schema = cloneDeep(this._originalSchema); // this is what can be munched on
$(this._nodeTable).empty();
$('#schema-alignment-statements-container').empty();
/*
this._nodeUIs = [];
for (var i = 0; i < this._protograph.rootNodes.length; i++) {
this._nodeUIs.push(new SchemaAlignmentDialog.UINode(
@ -195,9 +194,13 @@ SchemaAlignmentDialog._reset = function(protograph, initial) {
mustBeCellTopic: true
}
));
}
}*/
// TODO
this.preview(initial);
if (!this._schema.changes.length) {
// this._addItem();
}
// this.preview(initial);
};
SchemaAlignmentDialog._save = function(onDone) {
@ -312,8 +315,8 @@ SchemaAlignmentDialog._itemToJSON = function (item) {
lst.push(SchemaAlignmentDialog._statementGroupToJSON($(this)));
});
var inputContainer = item.find(".wbs-item-input").first();
return {item: SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
values: lst};
return {subject: SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
statementGroups: lst};
};
SchemaAlignmentDialog._addStatementGroup = function(item) {
@ -337,8 +340,8 @@ SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) {
lst.push(SchemaAlignmentDialog._statementToJSON($(this)));
});
var inputContainer = statementGroup.find(".wbs-prop-input").first();
return {prop: SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
values: lst};
return {property: SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
statements: lst};
};
@ -360,7 +363,10 @@ SchemaAlignmentDialog._addStatement = function(statementGroup) {
SchemaAlignmentDialog._statementToJSON = function (statement) {
var inputContainer = statement.find(".wbs-target-input").first();
return SchemaAlignmentDialog._inputContainerToJSON(inputContainer);
return {
value:SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
qualifiers:[],
};
};
SchemaAlignmentDialog._initField = function(inputContainer, mode) {
@ -370,19 +376,28 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode) {
var endpoint = null;
if (mode === "item" || mode === "target") {
endpoint = this._reconService.suggest.entity;
} else if (mode == "property") {
} else if (mode === "property") {
endpoint = this._reconService.suggest.property;
}
var suggestConfig = $.extend({}, endpoint);
suggestConfig.key = null;
suggestConfig.query_param_name = "prefix";
input.suggestP(suggestConfig).bind("fb-select", function(evt, data) {
inputContainer.data("jsonValue", {
type : "wbitemconstant",
id : data.id,
name: data.name,
});
if (mode === "item") {
inputContainer.data("jsonValue", {
type : "wbitemconstant",
qid : data.id,
label: data.name,
});
} else if (mode === "property") {
inputContainer.data("jsonValue", {
type : "wbpropconstant",
pid : data.id,
label: data.name,
});
}
});
}
@ -407,7 +422,7 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode) {
});
inputContainer.data("jsonValue", {
type : "wbitemvariable",
name: ui.draggable.text(),
columnName: ui.draggable.text(),
});
return true;
}).on("dropactivate", function(evt, ui) {
@ -448,7 +463,7 @@ SchemaAlignmentDialog.getJSON = function() {
list.push(SchemaAlignmentDialog._itemToJSON($(this)));
});
return {
'items': list,
'changes': list,
'wikibasePrefix': this._wikibasePrefix,
};
};

View File

@ -38,7 +38,7 @@ public class SaveWikibaseSchemaOperation extends AbstractOperation {
writer.key("op");
writer.value(OperationRegistry.s_opClassToName.get(this.getClass()));
writer.key("description");
writer.value("Save RDF schema skeleton");
writer.value("Save Wikibase schema skeleton");
writer.key("schema");
_schema.write(writer, options);
writer.endObject();

View File

@ -10,8 +10,9 @@ import com.google.refine.Jsonizable;
public abstract class BiJsonizable implements Jsonizable {
public static String jsonType;
public static final String jsonTypeKey = "@type";
// public static String jsonType;
public static String jsonType = null;
public static final String jsonTypeKey = "type";
public abstract void writeFields(JSONWriter writer, Properties options)
throws JSONException;
@ -20,9 +21,10 @@ public abstract class BiJsonizable implements Jsonizable {
public void write(JSONWriter writer, Properties options) throws JSONException {
writer.object();
writer.key(jsonTypeKey);
writer.value(jsonType);
writer.value(getJsonType());
writeFields(writer, options);
writer.endObject();
}
public abstract String getJsonType();
}

View File

@ -13,6 +13,8 @@ public class WbItemConstant extends WbItemExpr {
/* Represents an item that does not vary,
* it is independent of the row. */
public static final String jsonType = "wbitemconstant";
private String qid;
private String label;
@ -38,5 +40,8 @@ public class WbItemConstant extends WbItemExpr {
public ItemIdValue evaluate(ExpressionContext ctxt) {
return ItemIdValueImpl.create(qid, ctxt.getBaseIRI());
}
public String getJsonType() {
return jsonType;
}
}

View File

@ -15,7 +15,8 @@ public abstract class WbItemExpr extends WbValueExpr {
return WbItemConstant.fromJSON(obj);
} else if (WbItemVariable.jsonType.equals(type)) {
return WbItemVariable.fromJSON(obj);
} else {
throw new JSONException("unknown type for WbItemExpr");
}
return null;
}
}

View File

@ -0,0 +1,64 @@
package org.openrefine.wikidata.schema;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.StatementGroup;
public class WbItemStatementsExpr extends WbChangeExpr {
public static final String jsonType = "wbitemstatements";
private WbItemExpr subjectExpr;
private List<WbStatementGroupExpr> statementGroupExprs;
public WbItemStatementsExpr(WbItemExpr subjectExpr, List<WbStatementGroupExpr> statementGroupExprs) {
this.subjectExpr = subjectExpr;
this.statementGroupExprs = statementGroupExprs;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("subject");
subjectExpr.write(writer, options);
writer.key("statementGroups");
writer.array();
for(WbStatementGroupExpr expr : statementGroupExprs) {
expr.write(writer, options);
}
writer.endArray();
}
public static WbItemStatementsExpr fromJSON(JSONObject obj) throws JSONException {
JSONObject subjectObj = obj.getJSONObject("subject");
JSONArray statementsArr = obj.getJSONArray("statementGroups");
List<WbStatementGroupExpr> statementExprs = new ArrayList<WbStatementGroupExpr>();
for (int i = 0; i != statementsArr.length(); i++) {
statementExprs.add(WbStatementGroupExpr.fromJSON(statementsArr.getJSONObject(i)));
}
return new WbItemStatementsExpr(
WbItemExpr.fromJSON(subjectObj),
statementExprs);
}
public List<StatementGroup> evaluate(ExpressionContext ctxt) {
List<StatementGroup> results = new ArrayList<StatementGroup>(statementGroupExprs.size());
ItemIdValue subjectId = subjectExpr.evaluate(ctxt);
for(WbStatementGroupExpr expr : statementGroupExprs) {
results.add(expr.evaluate(ctxt, subjectId));
}
return results;
}
public String getJsonType() {
return jsonType;
}
}

View File

@ -14,6 +14,8 @@ import com.google.refine.model.ReconCandidate;
public class WbItemVariable extends WbItemExpr {
/* An item that depends on a reconciled value in a column */
public static final String jsonType = "wbitemvariable";
private String columnName;
public WbItemVariable(String columnName) {
@ -41,4 +43,7 @@ public class WbItemVariable extends WbItemExpr {
return null;
}
public String getJsonType() {
return jsonType;
}
}

View File

@ -15,9 +15,11 @@ public class WbPropConstant extends WbPropExpr {
public static final String jsonType = "wbpropconstant";
private String pid;
private String label;
public WbPropConstant(String pid) {
public WbPropConstant(String pid, String label) {
this.pid = pid;
this.label = label;
}
@Override
@ -25,14 +27,20 @@ public class WbPropConstant extends WbPropExpr {
throws JSONException {
writer.key("pid");
writer.value(pid);
writer.key("label");
writer.value(label);
}
public static WbPropConstant fromJSON(JSONObject obj) throws JSONException {
return new WbPropConstant(obj.getString("pid"));
return new WbPropConstant(obj.getString("pid"), obj.getString("label"));
}
@Override
public PropertyIdValue evaluate(ExpressionContext ctxt) {
return PropertyIdValueImpl.create(pid, ctxt.getBaseIRI());
}
public String getJsonType() {
return jsonType;
}
}

View File

@ -1,5 +1,7 @@
package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
@ -7,4 +9,9 @@ public abstract class WbPropExpr extends WbValueExpr {
/* An expression that represents a property */
public abstract PropertyIdValue evaluate(ExpressionContext ctxt);
public static WbPropExpr fromJSON(JSONObject obj) throws JSONException {
return WbPropConstant.fromJSON(obj);
}
}

View File

@ -38,17 +38,7 @@ public class WbSnakExpr implements Jsonizable {
JSONObject propObj = obj.getJSONObject("prop");
WbPropExpr propExpr = WbPropConstant.fromJSON(propObj);
JSONObject valueObj = obj.getJSONObject("value");
String type = valueObj.getString(WbValueExpr.jsonTypeKey);
WbValueExpr valueExpr = null;
if (WbPropConstant.jsonType.equals(type)) {
valueExpr = WbPropConstant.fromJSON(valueObj);
} else if (WbItemConstant.jsonType.equals(type)) {
valueExpr = WbItemConstant.fromJSON(valueObj);
} else if (WbItemVariable.jsonType.equals(type)) {
valueExpr = WbItemVariable.fromJSON(valueObj);
} else if (WbStringConstant.jsonType.equals(type)) {
valueExpr = WbStringConstant.fromJSON(valueObj);
}
WbValueExpr valueExpr = WbValueExpr.fromJSON(valueObj);
return new WbSnakExpr(propExpr, valueExpr);
}

View File

@ -13,35 +13,34 @@ import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Reference;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
import org.wikidata.wdtk.datamodel.interfaces.Value;
public class WbClaimExpr extends WbChangeExpr {
public class WbStatementExpr extends BiJsonizable {
public static String jsonType = "wbclaimexpr";
public static final String jsonType = "wbstatementexpr";
private WbItemExpr subjectExpr;
private WbSnakExpr mainSnakExpr;
private WbValueExpr mainSnakValueExpr;
private List<WbSnakExpr> qualifierExprs;
// TODO: references
public WbClaimExpr(WbItemExpr subjectExpr,
WbSnakExpr mainSnakExpr,
public WbStatementExpr(WbValueExpr mainSnakValueExpr,
List<WbSnakExpr> qualifierExprs) {
this.subjectExpr = subjectExpr;
this.mainSnakExpr = mainSnakExpr;
this.mainSnakValueExpr = mainSnakValueExpr;
this.qualifierExprs = qualifierExprs;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("subject");
subjectExpr.write(writer, options);
writer.key("mainsnak");
mainSnakExpr.write(writer, options);
writer.key("value");
mainSnakValueExpr.write(writer, options);
writer.key("qualifiers");
writer.array();
for (WbSnakExpr expr : qualifierExprs) {
@ -50,17 +49,15 @@ public class WbClaimExpr extends WbChangeExpr {
writer.endArray();
}
public static WbClaimExpr fromJSON(JSONObject obj) throws JSONException {
JSONObject subjObj = obj.getJSONObject("subject");
JSONObject mainSnakObj = obj.getJSONObject("mainsnak");
public static WbStatementExpr fromJSON(JSONObject obj) throws JSONException {
JSONObject mainSnakObj = obj.getJSONObject("value");
JSONArray qualifiersArr = obj.getJSONArray("qualifiers");
List<WbSnakExpr> qualifierExprs = new ArrayList<WbSnakExpr>();
for (int i = 0; i != qualifiersArr.length(); i++) {
qualifierExprs.add(WbSnakExpr.fromJSON(qualifiersArr.getJSONObject(i)));
}
return new WbClaimExpr(
WbItemExpr.fromJSON(subjObj),
WbSnakExpr.fromJSON(mainSnakObj),
return new WbStatementExpr(
WbValueExpr.fromJSON(mainSnakObj),
qualifierExprs);
}
@ -80,12 +77,18 @@ public class WbClaimExpr extends WbChangeExpr {
return snakGroups;
}
public Claim evaluate(ExpressionContext ctxt) {
ItemIdValue subject = subjectExpr.evaluate(ctxt);
Snak mainSnak = mainSnakExpr.evaluate(ctxt);
public Statement evaluate(ExpressionContext ctxt, ItemIdValue subject, PropertyIdValue propertyId) {
Value mainSnakValue = mainSnakValueExpr.evaluate(ctxt);
Snak mainSnak = Datamodel.makeValueSnak(propertyId, mainSnakValue);
List<Snak> qualifiers = new ArrayList<Snak>(qualifierExprs.size());
List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
return Datamodel.makeClaim(subject, mainSnak, groupedQualifiers);
Claim claim = Datamodel.makeClaim(subject, mainSnak, groupedQualifiers);
List<Reference> references = new ArrayList<Reference>();
StatementRank rank = StatementRank.NORMAL;
return Datamodel.makeStatement(claim, references, rank, "");
}
public String getJsonType() {
return jsonType;
}
}

View File

@ -0,0 +1,72 @@
package org.openrefine.wikidata.schema;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
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.Value;
public class WbStatementGroupExpr extends BiJsonizable {
public static final String jsonType = "wbstatementgroupexpr";
private WbPropExpr propertyExpr;
private List<WbStatementExpr> claimExprs;
public WbStatementGroupExpr(WbPropExpr propertyExpr, List<WbStatementExpr> claimExprs) {
this.propertyExpr = propertyExpr;
this.claimExprs = claimExprs;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("property");
propertyExpr.write(writer, options);
writer.key("statements");
writer.array();
for(WbStatementExpr expr : claimExprs) {
expr.write(writer, options);
}
writer.endArray();
}
public static WbStatementGroupExpr fromJSON(JSONObject obj) throws JSONException {
JSONObject propertyObj = obj.getJSONObject("property");
JSONArray claimsArr = obj.getJSONArray("statements");
List<WbStatementExpr> claimExprs = new ArrayList<WbStatementExpr>();
for (int i = 0; i != claimsArr.length(); i++) {
claimExprs.add(WbStatementExpr.fromJSON(claimsArr.getJSONObject(i)));
}
return new WbStatementGroupExpr(
WbPropExpr.fromJSON(propertyObj),
claimExprs);
}
public StatementGroup evaluate(ExpressionContext ctxt, ItemIdValue subject) {
PropertyIdValue propertyId = propertyExpr.evaluate(ctxt);
List<Statement> statements = new ArrayList<Statement>(claimExprs.size());
for(WbStatementExpr expr : claimExprs) {
statements.add(expr.evaluate(ctxt, subject, propertyId));
}
// List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
return Datamodel.makeStatementGroup(statements);
}
public String getJsonType() {
return jsonType;
}
}

View File

@ -35,4 +35,7 @@ public class WbStringConstant extends WbValueExpr {
return Datamodel.makeStringValue(value);
}
public String getJsonType() {
return jsonType;
}
}

View File

@ -1,5 +1,7 @@
package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.Value;
@ -15,4 +17,21 @@ public abstract class WbValueExpr extends BiJsonizable {
* returns a wikibase value suitable to be the target of a claim.
*/
public abstract Value evaluate(ExpressionContext ctxt);
public static WbValueExpr fromJSON(JSONObject obj) throws JSONException {
String type = obj.getString(WbValueExpr.jsonTypeKey);
WbValueExpr valueExpr = null;
if (WbPropConstant.jsonType.equals(type)) {
valueExpr = WbPropConstant.fromJSON(obj);
} else if (WbItemConstant.jsonType.equals(type)) {
valueExpr = WbItemConstant.fromJSON(obj);
} else if (WbItemVariable.jsonType.equals(type)) {
valueExpr = WbItemVariable.fromJSON(obj);
} else if (WbStringConstant.jsonType.equals(type)) {
valueExpr = WbStringConstant.fromJSON(obj);
} else {
throw new JSONException("unknown type for WbValueExpr");
}
return valueExpr;
}
}

View File

@ -4,12 +4,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/*
import org.deri.grefine.rdf.ResourceNode.RdfType;
import org.deri.grefine.rdf.app.ApplicationContext;
import org.deri.grefine.rdf.vocab.PrefixExistException;
import org.deri.grefine.rdf.vocab.Vocabulary;
import org.deri.grefine.rdf.vocab.VocabularyIndexException; */
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -62,7 +56,7 @@ public class WikibaseSchema implements OverlayModel {
JSONArray changeArr = o.getJSONArray("changes");
WikibaseSchema schema = new WikibaseSchema();
for (int i = 0; i != changeArr.length(); i++) {
WbChangeExpr changeExpr = WbClaimExpr.fromJSON(changeArr.getJSONObject(i));
WbChangeExpr changeExpr = WbItemStatementsExpr.fromJSON(changeArr.getJSONObject(i));
schema.changeExprs.add(changeExpr);
}
return schema;
@ -71,11 +65,14 @@ public class WikibaseSchema implements OverlayModel {
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("changes");
writer.array();
for (WbChangeExpr changeExpr : changeExprs) {
changeExpr.write(writer, options);
}
writer.endArray();
writer.endObject();
}
static public WikibaseSchema load(Project project, JSONObject obj) throws Exception {