Skip to content

Commit

Permalink
[new] is-relevant-if
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Aug 30, 2016
1 parent ee46f63 commit f0d36ae
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/components/Question.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Configuration from "../model/Configuration";
import Constants from "../constants/Constants";
import FormUtils from "../util/FormUtils";
import HelpIcon from "./HelpIcon";
import JsonObjectMap from "../util/JsonObjectMap";
import QuestionAnswerProcessor from "../model/QuestionAnswerProcessor";

export default class Question extends React.Component {
Expand All @@ -21,6 +22,7 @@ export default class Question extends React.Component {

constructor(props) {
super(props);
JsonObjectMap.addObject(this.props.question["@id"], this.props.question);
}

onAnswerChange = (answerIndex, change) => {
Expand All @@ -34,6 +36,8 @@ export default class Question extends React.Component {
_onChange(att, valueIndex, newValue) {
var newState = assign({}, this.props.question);
newState[att][valueIndex] = newValue;

JsonObjectMap.addObject(newState["@id"], newState);
this.props.onChange(this.props.index, newState);
}

Expand All @@ -42,6 +46,9 @@ export default class Question extends React.Component {
if (FormUtils.isHidden(question)) {
return null;
}
if (! FormUtils.isRelevant(question)) {
return null;
}
if (FormUtils.isAnswerable(question)) {
return <div>
{this.renderAnswers()}
Expand Down
54 changes: 53 additions & 1 deletion src/util/FormUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import JsonLdUtils from "jsonld-utils";

import Constants from "../constants/Constants";
import Utils from "./Utils";
import JsonObjectMap from "./JsonObjectMap";

export default class FormUtils {

Expand Down Expand Up @@ -77,4 +79,54 @@ export default class FormUtils {
return JsonLdUtils.getJsonAttValue(answer, Constants.HAS_DATA_VALUE);
}
}
}

static isRelevant(question) {

if (!question[Constants.IS_RELEVANT_IF]) {
return true;
}

for (var cond of Utils.asArray(question[Constants.IS_RELEVANT_IF])) {

if (FormUtils.testCondition(cond)) {
return true;
}
}
return false;
}

static testCondition(condition) {

var questionsWithValidAnswer = condition[Constants.HAS_VALID_ANSWER],
acceptedAnswerValues = condition[Constants.ACCEPTS_ANSWER_VALUE],
testedQuestion = condition[Constants.HAS_TESTED_QUESTION];

// valid answers
if (questionsWithValidAnswer) {
for (var question of Utils.asArray(questionsWithValidAnswer)) {
return true; //TODO not implemented
}
}

// concrete values
if (acceptedAnswerValues && testedQuestion) {
var question = JsonObjectMap.getObject(testedQuestion["@id"]);
for (var expValueObj of Utils.asArray(acceptedAnswerValues)) {
var expValue = expValueObj['@id'];
var answer = question[Constants.HAS_ANSWER];

if (!answer) {
return false;
}

var qValue = FormUtils.resolveValue(answer);

if (qValue === expValue) {
return true;
}
}
}
return false;
}

}
21 changes: 21 additions & 0 deletions src/util/JsonObjectMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

import JsonLdUtils from "jsonld-utils";

import Constants from "../constants/Constants";
import Utils from "./Utils";

export default class JsonObjectMap {

static objectMap = {};

static addObject(id, question) {
JsonObjectMap.objectMap[id] = question;
}

static getObject(id) {
return JsonObjectMap.objectMap[id];
}


}
16 changes: 16 additions & 0 deletions src/util/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,20 @@ export default class Utils {
return Configuration.dateTimeFormat;
}
}

/**
* Wraps passed object into new array if it is not array already.
* @param object_or_array An object or array.
* @returns {*} New array containing passed object or passed array.
*/
static asArray(object_or_array) {
if (! object_or_array) {
return {};
}
if (object_or_array.constructor === Array) {
return object_or_array;
}
return [ object_or_array ];
}

}
61 changes: 61 additions & 0 deletions test/__tests__/FormUtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import FormUtils from "../../src/util/FormUtils";
import Constants from "../../src/constants/Constants";
import JsonObjectMap from "../../src/util/JsonObjectMap";
import assign from "object-assign";

describe('FormUtils', () => {

Expand Down Expand Up @@ -194,4 +196,63 @@ describe('FormUtils', () => {
expect(FormUtils.resolveValue(answer)).toEqual(value);
});
});

describe('testCondition', () => {

var condition = {
"@type": ["http://onto.fel.cvut.cz/ontologies/form/condition"],
"http://onto.fel.cvut.cz/ontologies/form/accepts-answer-value": [
{
"@id": "http://vfn.cz/ontologies/fss-form/follow-up-and-recurrence/current-status/dod"
},
{
"@id": "http://vfn.cz/ontologies/fss-form/follow-up-and-recurrence/current-status/doc"
}],
"http://onto.fel.cvut.cz/ontologies/form/has-tested-question": [{
"@id": "http://vfn.cz/ontologies/fss-form/follow-up-and-recurrence/current-status"
}]
},
question = {
"@id": "http://vfn.cz/ontologies/fss-form/follow-up-and-recurrence/current-status",
"@type": "http://onto.fel.cvut.cz/ontologies/documentation/question",
"http://onto.fel.cvut.cz/ontologies/documentation/has_answer": {
"@type": "http://onto.fel.cvut.cz/ontologies/documentation/answer",
"http://onto.fel.cvut.cz/ontologies/documentation/has_object_value": {
"@id": "http://vfn.cz/ontologies/fss-form/follow-up-and-recurrence/current-status/dod"
}
}
};

beforeEach(function () {
spyOn(JsonObjectMap, "getObject").and.returnValue(question);
});

it('returns false in condition without answer values.', () => {

var noAnswerCondition = assign({}, condition);
delete noAnswerCondition["http://onto.fel.cvut.cz/ontologies/form/accepts-answer-value"];
expect(FormUtils.testCondition(noAnswerCondition));

expect(JsonObjectMap.getObject).not.toHaveBeenCalled();
});


it('return true if accepts value that exists in a question.', () => {

expect(FormUtils.testCondition(condition)).toEqual(true);
});

it('return false if accepts value that does not exists in a question.', () => {

var wrongAnswerQuestion = assign({}, question);
wrongAnswerQuestion
["http://onto.fel.cvut.cz/ontologies/documentation/has_answer"]
["http://onto.fel.cvut.cz/ontologies/documentation/has_object_value"] = {
"@id": "http://bad-value"
};
expect(FormUtils.testCondition(wrongAnswerQuestion)).toEqual(false);
});


});
});

0 comments on commit f0d36ae

Please sign in to comment.