示例#1
0
  private Frame createFrame(OntClass type) {
    if (manager.isStandardDatatype(type.getNameSpace())) {
      manager.getDatatypeByUri(type.getURI());
      return null;
    }
    if (manager.isStandard(type.getNameSpace())) {
      return null;
    }
    String uri = type.getURI();
    if (uri == null) {
      throw new RuntimeException("URI of type is not defined");
    }
    Frame frame = manager.getFrameByUri(uri);
    if (frame != null) {
      return frame;
    }

    OntClass elemType = manager.getElementType(type);
    if (elemType != null) {
      String elemURI = elemType.getURI();
      ListType listType = manager.getListTypeByElementUri(elemURI);
      if (listType == null) {
        RdfType elemRdfType = manager.getTypeByURI(elemURI);
        if (elemRdfType == null) {
          elemRdfType = createFrame(elemType);
        }
        listType = new ListType(manager, type, elemRdfType);
        manager.add(listType);
      }
      return null;
    }

    List<OntResource> individuals = getEnumeratedIndividuals(type);

    if (individuals == null) {
      frame = new Frame(manager, type);
    } else {

      Enumeration enumFrame = new Enumeration(manager, type);
      frame = enumFrame;
      for (OntResource item : individuals) {
        NamedIndividual value = new NamedIndividual(item);
        enumFrame.add(value);
      }
    }

    manager.add(frame);
    return frame;
  }
  /**
   * Creates a XML file that will contain the specification encoding which property should be used
   * to identify a particular class
   */
  private void savePropertyFile() {
    // Creating XML file corresponding  to this class specification:
    Document doc = new Document();
    doc.addContent(new Comment("Specification for the class " + className));
    Element root = new Element(className);
    Element labelElmt = new Element(Lexicon.CLASS_LABEL);

    Collection<Element> labelPartElmtList = new ArrayList<Element>();

    // Create the "class name" part of the label (e.g. "the project" Policygrid)
    Element classNameLabelPartElmt = new Element(Lexicon.CLASS_LABEL_PART);
    classNameLabelPartElmt.setAttribute(Lexicon.LABEL_PART_TYPE, Lexicon.LITERAL_LABEL_PART_TYPE);
    classNameLabelPartElmt.addContent("the " + className.toLowerCase() + " ");
    labelPartElmtList.add(classNameLabelPartElmt);

    // Create the "label used as property" part of the label (e.g. the project "Policygrid")
    Element propLabelPartElmt = new Element(Lexicon.CLASS_LABEL_PART);
    labelPartElmtList.add(propLabelPartElmt);

    // Direct property:
    if (directPropertyTypeButton.isSelected()) {
      propLabelPartElmt.setAttribute(
          Lexicon.LABEL_PART_TYPE, Lexicon.DIRECT_PROPERTY_LABEL_PART_TYPE);

      if (propertyList.getSelectedValue() != null
          && propertyList.getSelectedValue() instanceof OntPropertyModel) {

        String selectedProp =
            ((OntPropertyModel) propertyList.getSelectedValue()).getOntProperty().toString();
        JOptionPane.showMessageDialog(
            parent,
            "The property used to identify this class is " + selectedProp,
            "Saving Class Spec",
            JOptionPane.INFORMATION_MESSAGE);
        propLabelPartElmt.addContent(selectedProp);

      } else {
        JOptionPane.showMessageDialog(
            parent,
            "Property selected is invalid. Please select another property.",
            "Missing Parameters",
            JOptionPane.ERROR_MESSAGE);
        return;
      }

      directPropertyPane.setVisible(true);
      indirectPropertyPanel.setVisible(false);

      // Indirect property:
    } else if (indirectPropertyTypeButton.isSelected()) {

      propLabelPartElmt.setAttribute(
          Lexicon.LABEL_PART_TYPE, Lexicon.INDIRECT_PROPERTY_LABEL_PART_TYPE);
      boolean classIsSubject = classSubjectPropCheckBox.isSelected();

      // Setting attributes:
      if (!"".equals(linkingPropField.getText()) && !"".equals(relatedClassPropField.getText())) {
        propLabelPartElmt.setAttribute(
            Lexicon.CLASS_SUBJECT, (new Boolean(classIsSubject).toString()));
        propLabelPartElmt.setAttribute(Lexicon.PROPERTY_LINK, (linkingPropField.getText()));
        propLabelPartElmt.addContent(relatedClassPropField.getText());
      } else {
        JOptionPane.showMessageDialog(
            parent, "Please fill in both fields.", "Missing Parameters", JOptionPane.ERROR_MESSAGE);
        return;
      }
      JOptionPane.showMessageDialog(
          parent,
          "Indirect property: \n linking Prop: "
              + linkingPropField.getText()
              + "\n This class is the Subject of this property: "
              + classIsSubject
              + "\n Related class property: "
              + relatedClassPropField.getText());
    }

    // Adding content of XML file:
    for (Element labelPartElmt : labelPartElmtList) {
      labelElmt.addContent(labelPartElmt);
    }
    root.addContent(labelElmt);
    doc.setRootElement(root);

    // Writing file:
    try {
      // TODO: need to use the URI in the xml file name (or in sub directory? --> one subdir per
      // namespace? --> need to encode URI special characters, or just use ontology file name)

      // Checking that the directory already exit, if not, create it:
      File directory =
          new File(
              Lexicon.CLASS_SPEC_PATH
                  + URLEncoder.encode(ontClass.getNameSpace(), "ISO-8859-1")
                  + "/");
      if (!directory.isDirectory()) {
        directory.mkdir();
      }

      FileWriter f = new FileWriter(directory.getPath() + "/" + className + ".xml");
      PrintWriter fw = new PrintWriter(f);
      XMLOutputter xml = new XMLOutputter(Format.getPrettyFormat());
      fw.print(xml.outputString(doc));
      fw.close();
      f.close();
    } catch (IOException e1) {
      e1.printStackTrace();
      System.out.println("Error when writting spec class XML file");
    }
  }
  @Override
  public UpdateContainer doIt(VWorkspace vWorkspace) throws CommandException {
    OntologyManager ontMgr = vWorkspace.getWorkspace().getOntologyManager();
    JSONArray classesList = new JSONArray();
    JSONArray classesMap = new JSONArray();
    JSONArray propertiesList = new JSONArray();
    JSONArray propertiesMap = new JSONArray();

    Map<String, String> prefixMap = vWorkspace.getWorkspace().getOntologyManager().getPrefixMap();

    ExtendedIterator<OntClass> iter = ontMgr.getOntModel().listNamedClasses();
    //		ExtendedIterator<DatatypeProperty> propsIter = ontMgr.getOntModel()
    //				.listDatatypeProperties();
    ExtendedIterator<OntProperty> propsIter = ontMgr.getOntModel().listAllOntProperties();
    final JSONObject outputObj = new JSONObject();

    try {
      while (iter.hasNext()) {
        OntClass cls = iter.next();

        String pr = prefixMap.get(cls.getNameSpace());
        String classLabel = cls.getLocalName();
        //				if (cls.getLabel(null) != null && !cls.getLabel(null).equals(""))
        //					classLabel = cls.getLabel(null);
        String clsStr = (pr != null && !pr.equals("")) ? pr + ":" + classLabel : classLabel;

        classesList.put(clsStr);
        JSONObject classKey = new JSONObject();
        classKey.put(clsStr, cls.getURI());
        classesMap.put(classKey);
      }

      while (propsIter.hasNext()) {
        //				DatatypeProperty prop = propsIter.next();
        OntProperty prop = propsIter.next();

        if (prop.isObjectProperty() && !prop.isDatatypeProperty()) continue;

        String pr = prefixMap.get(prop.getNameSpace());
        String propLabel = prop.getLocalName();
        //				if (prop.getLabel(null) != null && !prop.getLabel(null).equals(""))
        //					propLabel = prop.getLabel(null);
        String propStr = (pr != null && !pr.equals("")) ? pr + ":" + propLabel : propLabel;

        propertiesList.put(propStr);
        JSONObject propKey = new JSONObject();
        propKey.put(propStr, prop.getURI());
        propertiesMap.put(propKey);
      }

      // Populate the JSON object that will hold everything in output
      outputObj.put(JsonKeys.classList.name(), classesList);
      outputObj.put(JsonKeys.classMap.name(), classesMap);
      outputObj.put(JsonKeys.propertyList.name(), propertiesList);
      outputObj.put(JsonKeys.propertyMap.name(), propertiesMap);

    } catch (JSONException e) {
      logger.error("Error populating JSON!");
    }

    UpdateContainer upd =
        new UpdateContainer(
            new AbstractUpdate() {
              @Override
              public void generateJson(String prefix, PrintWriter pw, VWorkspace vWorkspace) {
                pw.print(outputObj.toString());
              }
            });
    return upd;
  }