@@ -856,8 +856,13 @@ public List<Question> loadQuestionsFromKB(String url) {
856856 this .start_read ();
857857 KBObject typeprop = kb .getProperty (KBConstants .RDF_NS + "type" );
858858 KBObject labelprop = kb .getProperty (KBConstants .RDFS_NS + "label" );
859+ // Load question variables:
860+ for (KBTriple t : kb .genericTripleQuery (null , typeprop , SQOnt .getClass (SQO .QUESTION_VARIABLE ))) {
861+ KBObject qv = t .getSubject ();
862+ allVariables .put (qv .getID (), LoadQuestionVariableFromKB (qv , kb ));
863+ }
859864
860- // Load question classes and properties
865+ // Load questions and subproperties
861866 for (KBTriple t : kb .genericTripleQuery (null , typeprop , SQOnt .getClass (SQO .QUESTION ))) {
862867 // System.out.println(t.toString());
863868 KBObject question = t .getSubject ();
@@ -873,13 +878,12 @@ public List<Question> loadQuestionsFromKB(String url) {
873878 if (variables != null && variables .size () > 0 ) {
874879 vars = new ArrayList <QuestionVariable >();
875880 for (KBObject var : variables ) {
876- QuestionVariable qv = LoadQuestionVariableFromKB (var , kb );
881+ QuestionVariable qv = allVariables . get (var . getID () );
877882 if (qv != null )
878883 vars .add (qv );
884+ else
885+ System .err .println ("Could not find " + var .getID ());
879886 }
880- // Store question variables
881- for (QuestionVariable v : vars )
882- allVariables .put (v .getId (), v );
883887 }
884888
885889 Question q = new Question (question .getID (), name .getValueAsString (), template .getValueAsString (),
@@ -1093,34 +1097,39 @@ public Map<String,List<VariableOption>> listDynamicOptions (QuestionOptionsReque
10931097 Map <String , String > bindings = cfg .getBindings ();
10941098 Question q = allQuestions .get (cfg .getId ());
10951099 String query = (q != null ) ? q .getConstraint () : null ;
1096- if (q == null ) return null ;
1097-
1098- if (bindings != null && query != null )
1099- for (String name : bindings .keySet ()) {
1100- String value = bindings .get (name );
1101- if (value .startsWith ("http" )) {
1102- query += "VALUES " + name + " { <" + value + "> }\n " ;
1100+ if (q == null ) return null ; // TODO: If no query constraint should go to variable queries.
1101+
1102+ // Create map variableName -> filter
1103+ Map <String , String > filters = new HashMap <String , String >();
1104+ if (bindings != null && query != null ) {
1105+ for (String varUrl : bindings .keySet ()) {
1106+ QuestionVariable curVar = allVariables .get (varUrl );
1107+ if (curVar != null ) {
1108+ String value = bindings .get (varUrl );
1109+ String name = curVar .getVariableName ();
1110+ String sparqlValue = value .startsWith ("http" ) ? "<" + value + ">" : "\" " + value + "\" " ;
1111+ filters .put (name , "VALUES " + name + " { " + sparqlValue + " }" );
11031112 } else {
1104- query = query . replace ( name , " \" " + value + " \" " );
1113+ System . err . println ( "Cannot find variable ID: " + varUrl );
11051114 }
11061115 }
1116+ }
11071117
11081118 for (QuestionVariable qv : q .getVariables ()) {
11091119 if (qv .getSubType () == null ) {
11101120 String varName = qv .getVariableName ();
1111- List < VariableOption > options = null ;
1112- if (bindings != null && bindings . containsKey ( varName ) ) {
1113- String value = bindings . get ( varName );
1114- if (!value . startsWith ( "http" )) {
1115- options = new ArrayList < VariableOption >( );
1116- options . add ( new VariableOption ( value , value ));
1121+ String curQuery = query ;
1122+ if (filters != null && query != null ) {
1123+ for ( String diffVar : filters . keySet ()) {
1124+ if (!diffVar . equals ( varName )) {
1125+ curQuery += " \n " + filters . get ( diffVar );
1126+ }
11171127 }
11181128 }
1119- if (options == null && query != null ) {
1120- options = queryForOptions (varName , query );
1121- } else if (options == null ) {
1122- options = listVariableOptions (qv .getId ().replaceAll ("^.*\\ /" , "" ));
1123- }
1129+ //System.out.println("Variable " + varName + ":\n" + curQuery);
1130+ List <VariableOption > options = curQuery != null ?
1131+ queryForOptions (varName , curQuery )
1132+ : listVariableOptions (qv .getId ().replaceAll ("^.*\\ /" , "" ));
11241133 all .put (varName , options );
11251134 }
11261135 }
0 commit comments