PatternSparqlGenerator { public String generateQuery(final List<String> classesIn, final List<String> propertiesIn, final List<String> namedEntitiesIn) { classes = emptyIfNull(classesIn); properties = emptyIfNull(propertiesIn); namedEntities = emptyIfNull(namedEntitiesIn); switch (classes.size()) { case 0: switch (properties.size()) { case 0: switch (namedEntities.size()) { case 0: return NOT_DEFINED; case 1: return "SELECT " + named(0).get() + " WHERE {}"; case 2: return construct(Querytype.SELECT, named(0), var(PROJ), named(1)); default: return NOT_DEFINED; } case 1: switch (namedEntities.size()) { case 0: return construct(Querytype.SELECT, var(PROJ), prop(0), var(PROJ2)); case 1: return construct(Querytype.SELECT, named(0), prop(0), var(PROJ)); case 2: return construct(Querytype.SELECT, named(0), prop(0), var(PROJ), named(1), prop(0), var(PROJ)); default: return NOT_DEFINED; } case 2: switch (namedEntities.size()) { case 0: return construct(Querytype.SELECT, var(PROJ), prop(0), var(PROJ2), var(PROJ), prop(1), var(PROJ2)); case 1: return construct(Querytype.SELECT, named(0), prop(0), var(PROJ), named(0), prop(1), var(PROJ)); case 2: return construct(Querytype.SELECT, named(0), prop(0), var(PROJ), named(1), prop(1), var(PROJ)); default: return NOT_DEFINED; } default: return NOT_DEFINED; } case 1: switch (properties.size()) { case 0: switch (namedEntities.size()) { case 0: return construct(Querytype.SELECT, var(PROJ), clazz(0)); case 1: return construct(Querytype.ASK, named(0), clazz(0)); case 2: return construct(Querytype.ASK, named(0), clazz(0), named(1), clazz(0)); default: return NOT_DEFINED; } case 1: switch (namedEntities.size()) { case 0: return construct(Querytype.SELECT, var(PROJ), clazz(0), var(PROJ), prop(0), var(PROJ2)); case 1: return construct(Querytype.SELECT, var(PROJ), clazz(0), named(0), prop(0), var(PROJ)); case 2: return construct(Querytype.SELECT, var(PROJ), clazz(0), named(0), prop(0), var(PROJ), named(1), prop(0), var(PROJ)); default: return NOT_DEFINED; } case 2: switch (namedEntities.size()) { case 0: return construct(Querytype.SELECT, var(PROJ), clazz(0), var(PROJ), prop(0), var(PROJ2), var(PROJ), prop(1), var(PROJ2)); case 1: return construct(Querytype.SELECT, var(PROJ), clazz(0), named(0), prop(0), var(PROJ), named(0), prop(1), var(PROJ)); case 2: return construct(Querytype.SELECT, var(PROJ), clazz(0), named(0), prop(0), var(PROJ), named(1), prop(1), var(PROJ)); default: return NOT_DEFINED; } default: return NOT_DEFINED; } case 2: switch (properties.size()) { case 0: switch (namedEntities.size()) { case 0: return construct(Querytype.SELECT, var(PROJ), clazz(0), var(PROJ), clazz(1)); case 1: return construct(Querytype.ASK, named(0), clazz(0), named(0), clazz(1)); case 2: return construct(Querytype.ASK, named(0), clazz(0), named(1), clazz(1)); default: return NOT_DEFINED; } case 1: switch (namedEntities.size()) { case 0: return construct(Querytype.SELECT, var(PROJ), clazz(0), var(PROJ), clazz(1), var(PROJ), prop(0), var(PROJ2)); case 1: return construct(Querytype.SELECT, var(PROJ), clazz(0), var(PROJ), clazz(1), named(0), prop(0), var(PROJ)); case 2: return construct(Querytype.SELECT, var(PROJ), clazz(0), var(PROJ), clazz(1), named(0), prop(0), var(PROJ), named(1), prop(0), var(PROJ)); default: return NOT_DEFINED; } case 2: switch (namedEntities.size()) { case 0: return construct(Querytype.SELECT, var(PROJ), clazz(0), var(PROJ), clazz(1), var(PROJ), prop(0), var(PROJ2), var(PROJ), prop(1), var(PROJ2)); case 1: return construct(Querytype.SELECT, var(PROJ), clazz(0), var(PROJ), clazz(1), named(0), prop(0), var(PROJ), named(0), prop(1), var(PROJ)); case 2: return construct(Querytype.SELECT, var(PROJ), clazz(0), var(PROJ), clazz(1), named(0), prop(0), var(PROJ), named(1), prop(1), var(PROJ)); default: return NOT_DEFINED; } default: return NOT_DEFINED; } default: return NOT_DEFINED; } } private  PatternSparqlGenerator(); static PatternSparqlGenerator getInstance(); String nifToQuery(final Document doc); String nifStrigToQuery(final String nifString); String generateQuery(final List<String> classesIn, final List<String> propertiesIn, final List<String> namedEntitiesIn); int getLimit(); void setLimit(final int limit);  }
@Test public void notEmptyTest() { List<String> cStr = new ArrayList<>(Arrays.asList("class1", "class2", "dummy")); List<String> pStr = new ArrayList<>(Arrays.asList("property1", "property2", "dummy")); List<String> nStr = new ArrayList<>(Arrays.asList("namedEntity1", "namedEntity2", "dummy")); List<String> classes = new ArrayList<>(); List<String> properties = new ArrayList<>(); List<String> namedEntities = new ArrayList<>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { LOG.debug("Constructing for " + i + " Classes, " + j + " properties, " + k + " namedEntitites\n"); String query = gen.generateQuery(classes, properties, namedEntities); LOG.debug("Generated query:" + query); LOG.debug("\r\n"); if(i == 0 && j == 0 && k == 0) { Assert.assertTrue(EMPTY.equals(query)); } else { Assert.assertTrue(!EMPTY.equals(query)); } namedEntities.add(nStr.get(0)); nStr.remove(0); } nStr.addAll(namedEntities); namedEntities.clear(); properties.add(pStr.get(0)); pStr.remove(0); } pStr.addAll(properties); properties.clear(); classes.add(cStr.get(0)); cStr.remove(0); } }
@Test public void specificQuantitiesTest() { List<String> cStr = new ArrayList<>(Arrays.asList("class1", "class2")); List<String> pStr = new ArrayList<>(Arrays.asList("property1")); List<String> nStr = new ArrayList<>(Arrays.asList("namedEntity1")); LOG.debug("Constructing for " + cStr.size() + " Classes, " + pStr.size() + " properties, " + nStr.size() + " namedEntitites\n"); String query = gen.generateQuery(cStr, pStr, nStr); String correctAnswer = "SELECT * WHERE{ ?proj a dbo:class1 . ?proj a dbo:class2 . dbr:namedEntity1 dbo:property1 ?proj . }"; Assert.assertTrue(correctAnswer.equals(query)); LOG.debug("Generated query:" + query); cStr.clear(); LOG.debug("Constructing for " + cStr.size() + " Classes, " + pStr.size() + " properties, " + nStr.size() + " namedEntitites\n"); query = gen.generateQuery(cStr, pStr, nStr); correctAnswer = "SELECT * WHERE{ dbr:namedEntity1 dbo:property1 ?proj . }" ; Assert.assertTrue(correctAnswer.equals(query)); LOG.debug("Generated query:" + query); }