Ejemplo n.º 1
0
 int[] getColumns(Resource test) {
   if (_model.contains(test, Vertere.column)) {
     NodeIterator listObjectsOfProperty = _model.listObjectsOfProperty(test, Vertere.column);
     List<RDFNode> toList = listObjectsOfProperty.toList();
     int[] columns = new int[toList.size()];
     for (int i = 0; i < toList.size(); i++) {
       columns[i] = toList.get(i).asLiteral().getInt();
     }
     return columns;
   } else {
     return new int[0];
   }
 }
  public static void main(String[] args) throws IOException {
    Model model = ModelFactory.createDefaultModel();
    try (InputStream is =
        new BZip2CompressorInputStream(new URL(DBPEDIA_SCHEMA_DOWNLOAD_URL).openStream())) {
      //			model = FileManager.get().loadModel("input/dbpedia_3.9.owl");
      model.read(is, null, "RDF/XML");
    }

    System.out.println(model.size() + " triples loaded.");

    Set<Resource> classes = subjects(model, RDF.type, OWL.Class);
    Set<Resource> objectProperties = subjects(model, RDF.type, OWL.ObjectProperty);
    Set<Resource> dataProperties = subjects(model, RDF.type, OWL.DatatypeProperty);

    Map<Set<Resource>, String> setToName = new HashMap<>();
    setToName.put(classes, "classes");
    setToName.put(objectProperties, "objectproperties");
    setToName.put(dataProperties, "dataproperties");

    FieldType stringType = new FieldType(StringField.TYPE_STORED);
    stringType.setStoreTermVectors(false);
    FieldType textType = new FieldType(TextField.TYPE_STORED);
    textType.setStoreTermVectors(false);

    for (Set<Resource> set : setToName.keySet()) {
      IndexWriter writer = createWriter(setToName.get(set));
      Set<Document> documents = new HashSet<>();

      for (Resource resource : set) {
        for (RDFNode object : model.listObjectsOfProperty(resource, RDFS.label).toSet()) {
          String label = object.asLiteral().getLexicalForm();

          Document luceneDocument = new Document();
          luceneDocument.add(new Field("uri", resource.getURI(), stringType));
          //					luceneDocument.add(new Field("dbpediaUri", indexDocument.getCanonicalDBpediaUri(),
          // stringType));
          luceneDocument.add(new Field("label", label, textType));
          //					documents.add(luceneDocument);
          writer.addDocument(luceneDocument);
        }
      }
      writer.addDocuments(documents);
      writer.commit();
      writer.close();
    }
  }
Ejemplo n.º 3
0
 public RDFNode lookup(Resource identity, String sourceValue) {
   if (!_model.contains(identity, Vertere.lookup)) {
     return null;
   }
   Resource lookupResource = _model.getProperty(identity, Vertere.lookup).getResource();
   NodeIterator listObjectsOfProperty =
       _model.listObjectsOfProperty(lookupResource, Vertere.lookup_entry);
   while (listObjectsOfProperty.hasNext()) {
     RDFNode entry = listObjectsOfProperty.next();
     Resource asResource = entry.asResource();
     String key = _model.getProperty(asResource, Vertere.lookup_key).getString();
     if (sourceValue.equals(key)) {
       return _model.getProperty(asResource, Vertere.lookup_value).getObject();
     }
   }
   return null;
 }
Ejemplo n.º 4
0
  @Override
  public void process(final Model model) {
    String identifier = null;
    try {
      identifier =
          model
              .listObjectsOfProperty(model.createProperty(filenameUtil.property))
              .next()
              .toString();
      LOG.debug("Going to store identifier=" + identifier);
    } catch (NoSuchElementException e) {
      LOG.warn("No identifier => cannot derive a filename for " + model.toString());
      return;
    }

    String directory = identifier;
    if (directory.length() >= filenameUtil.endIndex) {
      directory = directory.substring(filenameUtil.startIndex, filenameUtil.endIndex);
    }
    final String file =
        FilenameUtils.concat(
            filenameUtil.target,
            FilenameUtils.concat(
                directory + File.separator, identifier + "." + filenameUtil.fileSuffix));
    LOG.debug("Write to " + file);
    filenameUtil.ensurePathExists(file);

    try (final Writer writer =
        new OutputStreamWriter(new FileOutputStream(file), filenameUtil.encoding)) {
      final StringWriter tripleWriter = new StringWriter();
      RDFDataMgr.write(tripleWriter, model, this.serialization);
      IOUtils.write(tripleWriter.toString(), writer);
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
      throw new MetafactureException(e);
    }
  }
Ejemplo n.º 5
0
 @Override
 public List<Node> getObjects(Node subject, Node predicate) {
   Model graph = null;
   GraphConnection graphConnection = null;
   try {
     graphConnection = openGraph();
     graph = graphConnection.getGraph();
     graph.enterCriticalSection(Lock.READ);
     SimpleSelector selector = getJenaSelector(graph, new StatementImpl(subject, predicate, null));
     NodeIterator it = graph.listObjectsOfProperty(selector.getSubject(), selector.getPredicate());
     List<Node> res = new ArrayList<Node>();
     while (it.hasNext()) {
       res.add(getNXRelationsNode(it.nextNode().asNode()));
     }
     return res;
   } finally {
     if (graph != null) {
       graph.leaveCriticalSection();
     }
     if (graphConnection != null) {
       graphConnection.close();
     }
   }
 }
  /**
   * Get JSON map for a given resource by applying the river settings
   *
   * @param rs resource being processed
   * @param properties properties to be indexed
   * @param model model returned by the indexing query
   * @param getPropLabel if set to true all URI property values will be indexed as their label. The
   *     label is taken as the value of one of the properties set in {@link #uriDescriptionList}.
   * @return map of properties to be indexed for res
   */
  private Map<String, ArrayList<String>> getJsonMap(
      Resource rs, Set<Property> properties, Model model, boolean getPropLabel) {
    Map<String, ArrayList<String>> jsonMap = new HashMap<String, ArrayList<String>>();
    ArrayList<String> results = new ArrayList<String>();

    if (addUriForResource) {
      results.add("\"" + rs.toString() + "\"");
      jsonMap.put("http://www.w3.org/1999/02/22-rdf-syntax-ns#about", results);
    }

    Set<String> rdfLanguages = new HashSet<String>();

    for (Property prop : properties) {
      NodeIterator niter = model.listObjectsOfProperty(rs, prop);
      String property = prop.toString();
      results = new ArrayList<String>();

      String lang;
      String currValue;

      while (niter.hasNext()) {
        RDFNode node = niter.next();
        currValue = getStringForResult(node, getPropLabel);
        if (addLanguage) {
          if (node.isLiteral()) {
            lang = node.asLiteral().getLanguage();
            if (!lang.isEmpty()) {
              rdfLanguages.add("\"" + lang + "\"");
            }
          }
        }

        String shortValue = currValue;

        int currLen = currValue.length();
        // Unquote string
        if (currLen > 1) shortValue = currValue.substring(1, currLen - 1);

        // If either whiteMap does contains shortValue
        // or blackMap contains the value
        // skip adding it to the index
        boolean whiteMapCond =
            whiteMap.containsKey(property) && !whiteMap.get(property).contains(shortValue);
        boolean blackMapCond =
            blackMap.containsKey(property) && blackMap.get(property).contains(shortValue);

        if (whiteMapCond || blackMapCond) {
          continue;
        }

        if (normalizeObj.containsKey(shortValue)) {
          results.add("\"" + normalizeObj.get(shortValue) + "\"");
        } else {
          results.add(currValue);
        }
      }

      // Do not index empty properties
      if (results.isEmpty()) continue;

      if (normalizeProp.containsKey(property)) {
        property = normalizeProp.get(property);
        if (jsonMap.containsKey(property)) {
          jsonMap.get(property).addAll(results);
        } else {
          jsonMap.put(property, results);
        }
      } else {
        jsonMap.put(property, results);
      }
    }

    if (addLanguage) {
      if (rdfLanguages.isEmpty() && !language.isEmpty()) rdfLanguages.add(language);
      if (!rdfLanguages.isEmpty()) jsonMap.put("language", new ArrayList<String>(rdfLanguages));
    }

    for (Map.Entry<String, String> it : normalizeMissing.entrySet()) {
      if (!jsonMap.containsKey(it.getKey())) {
        ArrayList<String> res = new ArrayList<String>();
        res.add("\"" + it.getValue() + "\"");
        jsonMap.put(it.getKey(), res);
      }
    }

    return jsonMap;
  }
Ejemplo n.º 7
0
 public NodeIterator getResources() {
   NodeIterator listObjectsOfProperty =
       _model.listObjectsOfProperty(_specResource, Vertere.resource);
   return listObjectsOfProperty;
 }
Ejemplo n.º 8
0
 NodeIterator getOnlyIfs(Resource resource) {
   return _model.listObjectsOfProperty(resource, Vertere.only_if);
 }
Ejemplo n.º 9
0
 public NodeIterator getAttributes(Resource resourceSpec) {
   NodeIterator listObjectsOfProperty =
       _model.listObjectsOfProperty(resourceSpec, Vertere.attribute);
   return listObjectsOfProperty;
 }
Ejemplo n.º 10
0
 public NodeIterator getRelationships(Resource resourceSpec) {
   NodeIterator listObjectsOfProperty =
       _model.listObjectsOfProperty(resourceSpec, Vertere.relationship);
   return listObjectsOfProperty;
 }
Ejemplo n.º 11
0
 public NodeIterator getSpecifiedTypes(Resource resourceSpec) {
   NodeIterator listObjectsOfProperty = _model.listObjectsOfProperty(resourceSpec, Vertere.type);
   return listObjectsOfProperty;
 }