public static void main(String[] args) { // create a graph with type inferencing SimpleSesameGraph g = new SimpleSesameGraph(true); // //load the film schema and the example data // g.addFile("../roadstead_ontologies/film-ontology.owl", SimpleSesameGraph.RDFXML); // try loading the geopolitical ontology g.addURI("http://aims.fao.org/aos/geopolitical.owl"); // List<HashMap<Object, Value>> solutions = g.runSPARQL("SELECT ?who WHERE { " + // "?who <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> // <http://roadstead.org/film#Person> ." + // "}"); /* Here we are looking for the names of all the things with a name */ List<HashMap<Object, Value>> solutions = g.runSPARQL( "SELECT ?name WHERE { " + "?what <http://aims.fao.org/aos/geopolitical.owl#territory> ?name ." + "?what <http://aims.fao.org/aos/geopolitical.owl#nameListEN> ?name ." + "}"); for (HashMap<Object, Value> object : solutions) { for (Value value : object.values()) { System.out.println("country: " + value.stringValue()); } } }
public void generate(String className, PrintWriter out) throws IOException, GraphUtilException, GenerationException { // be sure to have at least the uri constants generated if (stringGeneration == null && uriGeneration == null) { uriGeneration = GenerationSetting.createDefault(caseFormat, "", ""); // throw new GenerationException("no generation settings present, please set explicitly"); } log.trace("classname: {}", className); if (StringUtils.isBlank(name)) { name = className; } if (StringUtils.isBlank(prefix)) { throw new GenerationException("could not detect prefix, please set explicitly"); } else { log.debug("prefix: {}", prefix); } Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)"); ConcurrentMap<String, URI> splitUris = new ConcurrentHashMap<>(); for (Resource nextSubject : model.subjects()) { if (nextSubject instanceof URI) { Matcher matcher = pattern.matcher(nextSubject.stringValue()); if (matcher.find()) { String k = matcher.group(1); URI putIfAbsent = splitUris.putIfAbsent(k, (URI) nextSubject); if (putIfAbsent != null) { log.warn( "Conflicting keys found: uri={} key={} existing={}", nextSubject.stringValue(), k, putIfAbsent); } } } } // print // package is optional if (StringUtils.isNotBlank(packageName)) { out.printf("package %s;%n%n", getPackageName()); } // imports out.println("import org.openrdf.model.URI;"); out.println("import org.openrdf.model.ValueFactory;"); out.println("import org.openrdf.model.impl.ValueFactoryImpl;"); out.println(); final URI pfx = new URIImpl(prefix); Literal oTitle = getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), LABEL_PROPERTIES); Literal oDescr = getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), COMMENT_PROPERTIES); Set<Value> oSeeAlso = model.filter(pfx, RDFS.SEEALSO, null).objects(); // class JavaDoc out.println("/**"); if (oTitle != null) { out.printf( " * %s.%n", WordUtils.wrap(oTitle.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false)); out.println(" * <p>"); } if (oDescr != null) { out.printf( " * %s.%n", WordUtils.wrap(oDescr.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false)); out.println(" * <p>"); } out.printf(" * Namespace %s.%n", name); out.printf(" * Prefix: {@code <%s>}%n", prefix); if (!oSeeAlso.isEmpty()) { out.println(" *"); for (Value s : oSeeAlso) { if (s instanceof URI) { out.printf(" * @see <a href=\"%s\">%s</a>%n", s.stringValue(), s.stringValue()); } } } out.println(" */"); // class Definition out.printf("public class %s {%n", className); out.println(); // constants out.printf(getIndent(1) + "/** {@code %s} **/%n", prefix); out.printf(getIndent(1) + "public static final String NAMESPACE = \"%s\";%n", prefix); out.println(); out.printf(getIndent(1) + "/** {@code %s} **/%n", name.toLowerCase()); out.printf(getIndent(1) + "public static final String PREFIX = \"%s\";%n", name.toLowerCase()); out.println(); List<String> keys = new ArrayList<>(); keys.addAll(splitUris.keySet()); Collections.sort(keys, String.CASE_INSENSITIVE_ORDER); // do the string constant generation (if set) if (stringGeneration != null) { for (String key : keys) { final Literal comment = getFirstExistingObjectLiteral( model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES); final Literal label = getFirstExistingObjectLiteral( model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES); out.println(getIndent(1) + "/**"); if (label != null) { out.printf(getIndent(1) + " * %s%n", label.getLabel()); out.println(getIndent(1) + " * <p>"); } out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue()); if (comment != null) { out.println(getIndent(1) + " * <p>"); out.printf( getIndent(1) + " * %s%n", WordUtils.wrap( comment.getLabel().replaceAll("\\s+", " "), 70, "\n" + getIndent(1) + " * ", false)); } out.println(getIndent(1) + " *"); out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key); out.println(getIndent(1) + " */"); final String nextKey = cleanKey( // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting String.format( "%s%s%s", StringUtils.defaultString(stringGeneration.getConstantPrefix()), doCaseFormatting(key, stringGeneration.getCaseFormat()), StringUtils.defaultString(stringGeneration.getConstantSuffix()))); checkField(className, nextKey); out.printf( getIndent(1) + "public static final String %s = %s.NAMESPACE + \"%s\";%n", nextKey, className, key); out.println(); } } // do the entire uri constant generation if (uriGeneration != null) { for (String key : keys) { Literal comment = getFirstExistingObjectLiteral( model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES); Literal label = getFirstExistingObjectLiteral( model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES); out.println(getIndent(1) + "/**"); if (label != null) { out.printf(getIndent(1) + " * %s%n", label.getLabel()); out.println(getIndent(1) + " * <p>"); } out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue()); if (comment != null) { out.println(getIndent(1) + " * <p>"); out.printf( getIndent(1) + " * %s%n", WordUtils.wrap( comment.getLabel().replaceAll("\\s+", " "), 70, "\n" + getIndent(1) + " * ", false)); } out.println(getIndent(1) + " *"); out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key); out.println(getIndent(1) + " */"); final String nextKey = cleanKey( // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting String.format( "%s%s%s", StringUtils.defaultString(uriGeneration.getConstantPrefix()), doCaseFormatting(key, uriGeneration.getCaseFormat()), StringUtils.defaultString(uriGeneration.getConstantSuffix()))); // String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat())); checkField(className, nextKey); out.printf(getIndent(1) + "public static final URI %s;%n", nextKey); out.println(); } // static init out.println(getIndent(1) + "static {"); out.printf(getIndent(2) + "ValueFactory factory = ValueFactoryImpl.getInstance();%n"); out.println(); for (String key : keys) { final String nextKey = cleanKey( // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting String.format( "%s%s%s", StringUtils.defaultString(uriGeneration.getConstantPrefix()), doCaseFormatting(key, uriGeneration.getCaseFormat()), StringUtils.defaultString(uriGeneration.getConstantSuffix()))); out.printf( getIndent(2) + "%s = factory.createURI(%s.NAMESPACE, \"%s\");%n", nextKey, className, key); } out.println(getIndent(1) + "}"); out.println(); } // private contructor to avoid instances out.printf(getIndent(1) + "private %s() {%n", className); out.println(getIndent(2) + "//static access only"); out.println(getIndent(1) + "}"); out.println(); // class end out.println("}"); out.flush(); }