Ejemplo n.º 1
0
  public String getMap(String concept) {
    String TypeUri = ConceptEngine.skosPrefix + "#Concept";
    Element root = init();
    Element centerNode = buildCenterNode(concept, TypeUri);
    boolean includeConcept = false;

    String sparql =
        "PREFIX skos: <"
            + ConceptEngine.skosPrefix
            + "#> "
            + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
            + "SELECT distinct ?t "
            + "WHERE {	?x skos:prefLabel '"
            + concept
            + "'. "
            + "       	?x rdf:type	?t .} ";
    QueryResult r;
    try {
      r = QueryEngine.sparql(sparql);
      while (r.next()) {
        String typeUri = r.getString(0);
        if (typeUri.equals(TypeUri)) {
          includeConcept = true;
          continue;
        }
        Element node = nodeArray.addElement("Node").addAttribute("id", nodeID + "");
        node.addElement("Label").addText(concept);
        node.addElement("TypeUri").addText(typeUri);
        String typeLabel = model.getResLabel(typeUri);
        node.addElement("Type").addText(typeLabel);
        nodes.put(concept, node);
        Element edge = edgeArray.addElement("Edge").addAttribute("id", edgeID + "");
        edge.addElement("Label").addText("作为" + typeLabel);
        edge.addElement("TypeUri").addText("as");
        edge.addElement("FromID").addText("1");
        edge.addElement("ToID").addText(nodeID + "");
        nodeID++;
        edgeID++;
      }
    } catch (Exception e) {
      logger.error("Dart Grid: Query Error! --> " + concept + "(RDF.type)");
    }

    List<?> nodeList = nodeArray.elements();
    if (includeConcept) {
      buildMap(centerNode, true);
    }
    int j;
    for (j = 1; j < nodeList.size(); j++) {
      Element node = (Element) nodeList.get(j);
      buildMap(node, false);
    }
    nodeList = nodeArray.elements();
    for (; j < nodeList.size(); j++) {
      Element node = (Element) nodeList.get(j);
      buildMap(node, true);
    }
    return root.asXML();
  }
Ejemplo n.º 2
0
 private void addRelations(
     Element centerNode, String concept, List<?> relations, Boolean isOpposed) {
   if (!isOpposed)
     System.out.println("++++++++++++++" + concept + "++++++++++++++++++" + centerNode.asXML());
   for (int i = 0; i < relations.size(); i++) {
     Element relation = (Element) relations.get(i);
     String propertyUri = relation.attributeValue("propertyUri");
     if (propertyUri.equals(ConceptEngine.ontoPrefix + "#hasRela")) {
       buildRelaMap(centerNode);
       continue;
     }
     if (propertyUri.equals(ConceptEngine.ontoPrefix + "#relaConcept")) continue;
     String propertyLabel = relation.attributeValue("property");
     String objectUri = relation.attributeValue("objectUri");
     String objectLabel = relation.attributeValue("object");
     String sparql = ConceptEngine.getContentSparql(concept, propertyUri, false, isOpposed);
     QueryResult r;
     try {
       r = QueryEngine.sparql(sparql);
       if (r == null) continue;
       int ct = 0;
       while (r.next() && ct < count) {
         ct++;
         String content = r.getString(0);
         Element node = null;
         Boolean isNewNode = true;
         if (nodes.containsKey(content)) {
           Element e = (Element) nodes.get(content);
           if (e.elementText("TypeUri").equals(objectUri)) {
             isNewNode = false;
             node = e;
           }
         }
         if (isNewNode) {
           node = nodeArray.addElement("Node").addAttribute("id", nodeID++ + "");
           node.addElement("Label").addText(content);
           node.addElement("TypeUri").addText(objectUri);
           node.addElement("Type").addText(objectLabel);
           nodes.put(content, node);
         }
         Element edge = edgeArray.addElement("Edge").addAttribute("id", edgeID++ + "");
         edge.addElement("Label").addText(propertyLabel);
         edge.addElement("TypeUri").addText(propertyUri);
         edge.addElement("FromID")
             .addText(isOpposed ? node.attributeValue("id") : centerNode.attributeValue("id"));
         edge.addElement("ToID")
             .addText(isOpposed ? centerNode.attributeValue("id") : node.attributeValue("id"));
       }
     } catch (java.lang.NullPointerException e) {
       logger.error("Dart Grid: Query Error! --> " + concept + "(" + propertyLabel + ")");
       r = null;
       e.printStackTrace();
     }
   }
   // if(isOpposed)
   //	System.out.println("++++++++++++++++++++++++++++++++");
 }