Пример #1
0
  private Model createModelFromDatabase() throws DataBaseNotAccessibleException {
    HashMap<String, Vector<String>> res = this.getMetaInfoFromDataBase();
    OntModel m = ModelFactory.createOntologyModel();

    String ns = "http://www.lirmm.fr/metaInfo#";
    m.setNsPrefix("metaInfos", ns);

    OntClass maDataBase = m.createClass(ns + "DATABASE");
    OntClass nameDataBase = m.createClass(ns + database.getDatabaseName());
    nameDataBase.addSuperClass(maDataBase);

    OntClass maSuperClasse = m.createClass(ns + "TABLE");

    for (String cle : res.keySet()) {
      OntClass c = m.createClass(ns + cle);
      c.addSuperClass(maSuperClasse);
      for (int i = 0; i < res.get(cle).size(); i++) {
        OntClass c2 = m.createClass(ns + res.get(cle).get(i));
        OntProperty prop =
            m.createOntProperty(ns + "COLONNE_" + c.getLocalName() + "_" + c2.getLocalName());
        prop.setRange(c2);
        prop.setDomain(c);
        c2.addSuperClass(maSuperClasse);
      }
    }
    return m;
  }
Пример #2
0
  /** Prints out the inferred classes of the aligned ontology */
  public void printInfClasses(Model model) {
    ExtendedIterator<OntClass> classes = ((OntModel) model).listClasses();
    while (classes.hasNext()) {
      OntClass thisClass = (OntClass) classes.next();
      if (thisClass.getLocalName() != null) {
        System.out.println("Found class: " + thisClass.getLocalName());

        ExtendedIterator inst = thisClass.listInstances();
        while (inst.hasNext()) {
          Individual thisInstance = (Individual) inst.next();
          System.out.println("\tFound instance: " + thisInstance.getLocalName());
        }
      }
    }
  } // printInfClasses
Пример #3
0
  private void addSubtypes(Frame frame) {

    Iterator<OntClass> sequence = frame.asOntClass().listSubClasses(true);
    while (sequence.hasNext()) {
      OntClass type = sequence.next();
      String subURI = type.getURI();
      if (subURI == null) continue;
      Frame subFrame = manager.getFrameByUri(subURI);
      if (subFrame == null) {
        subFrame = manager.getListTypeByListUri(subURI);
      }
      if (subFrame != null) {
        frame.getSubtypeList().add(subFrame);
      } else {

        Datatype datatype = manager.getDatatypeByUri(subURI);
        if (datatype != null) {
          frame.addSubdatatype(datatype);
          continue;
        }

        if (isStandard(subURI)) continue;
        logger.warn(
            "Ignoring supertype of " + type.getLocalName() + " because frame not found: " + subURI);
      }
    }
  }
Пример #4
0
  /**
   * Initialises the anchor, finding the node's compulsory and optional properties and their NL
   * representations and ordering it all in lists and maps, using the submenus.
   *
   * @param c OntClass corresponding to this anchor
   * @param query true if this is an anchor for a node in the QueryGraph
   * @throws BadAnchorException if the given SGNode does not need an anchor
   */
  public void init(OntClass c, boolean query) throws BadAnchorException {
    Map<String, Integer[]> map = reader.getCardinalities(c.getLocalName());
    List<OntProperty> list = reader.getDomainProperties(c.getLocalName());
    for (int i = 0; i < list.size(); i++) {
      OntProperty p = list.get(i);
      String name = p.getLocalName();
      int nr =
          node.getOutgoingEdges(name).size(); // number of times the node already has this property
      String inverse = reader.getInverse(name); // , n.getLabel(), null);
      if (inverse != null)
        nr +=
            node.getIncomingEdges(inverse)
                .size(); // plus the number of times the inverse has this node as range

      if (map.containsKey(name)) {
        int min =
            map.get(name)[0]; // if the minimum cardinality is not satisfied, add to 'compulsory'
        int max = map.get(name)[1];
        if (query) optional.add(p); // for the query, cardinality constraints do not matter
        else if ((min != 0)
            && (min > nr)) // if the minimum cardinality > 0, add the property to compulsory
        compulsory.add(p);
        else if ((max == 0)
            || (max > nr)) // if the maximum cardinality will not be violated, add to optional
        optional.add(p);
      } else optional.add(p); // no cardinality constraints, so add to optional
    }

    if (!compulsory.isEmpty()) // if there are compulsory properties, this is a red anchor
    redAnchor = true;
    else if (optional.isEmpty()) // compulsory and optional both empty - means this is not an anchor
    throw new BadAnchorException(
          "Node " + node.getLabel() + " is not an anchor anymore; all its relations are specified");

    sort(query);
  }
  /** Initialise the page with proper layout and all. */
  public void init() {
    content.removeAll();

    GridBagConstraints constraint = new GridBagConstraints();
    JTextArea area =
        new JTextArea(
            "Editing the Label of the class '"
                + ontClass.getLocalName()
                + "'. You need to define which property will be used to identify a particular instance of this class. "
                + "For example, to identify a paper you can use the title of that paper. "
                + "You can either directly use one of the property of this class or specify the property of a related class "
                + "(e.g. for an ourSpaces account, you might want to use the name of the Person that holds that account)");
    area.setEditable(false);
    area.setLineWrap(true);
    area.setWrapStyleWord(true);
    constraint.insets = new Insets(10, 10, 10, 10);
    constraint.fill = GridBagConstraints.HORIZONTAL;
    constraint.anchor = GridBagConstraints.CENTER;
    constraint.weightx = 1.0;
    constraint.gridx = 0;
    constraint.gridy = 0;
    constraint.gridwidth = 2;
    content.add(area, constraint);

    directPropertyTypeButton = new JRadioButton("Direct");
    directPropertyTypeButton.setActionCommand("direct");
    directPropertyTypeButton.setSelected(true);

    indirectPropertyTypeButton = new JRadioButton("Indirect");
    indirectPropertyTypeButton.setActionCommand("indirect");

    // Group the radio buttons.
    ButtonGroup group = new ButtonGroup();
    group.add(directPropertyTypeButton);
    group.add(indirectPropertyTypeButton);

    // Register a listener for the radio buttons.
    directPropertyTypeButton.addActionListener(this);
    indirectPropertyTypeButton.addActionListener(this);

    // Put the radio buttons in a column in a panel.
    JPanel radioPanel = new JPanel(new GridLayout(1, 0));
    JLabel propTypeLabel = new JLabel("Property Type: ");
    radioPanel.add(propTypeLabel);
    radioPanel.add(directPropertyTypeButton);
    radioPanel.add(indirectPropertyTypeButton);

    constraint.gridy = 1;
    content.add(radioPanel, constraint);

    // Initialise list of direct properties
    initDirectPropertyList();

    // Create Panel for direct properties
    constraint.weightx = 1;
    constraint.weighty = 0.7; // add extra space to the tree widget, so it can expand
    constraint.anchor = GridBagConstraints.FIRST_LINE_START;
    constraint.gridx = 1;
    constraint.gridy = 2;
    constraint.fill = GridBagConstraints.BOTH;
    constraint.gridwidth = 1;
    propertyList.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    directPropertyPane = new JScrollPane(propertyList);
    Border blackBorder = BorderFactory.createLineBorder(Color.black);
    directPropertyPane.setBorder(blackBorder);
    content.add(directPropertyPane, constraint);

    // Create Panel for indirect properties at the same place as the direct prop panel, so that only
    // one is displayed at a time
    indirectPropertyPanel = new JPanel(new GridLayout(0, 2));
    indirectPropertyPanel.setBorder(blackBorder);
    JLabel linkingPropLabel = new JLabel(" Property linking to another class: ");
    linkingPropField = new JTextField(20);
    classSubjectPropCheckBox = new JCheckBox("This class is the subject of the above property");
    JLabel relatedClassPropLabel =
        new JLabel(" Property of the related class that identify the original class: ");
    relatedClassPropField = new JTextField(20);

    indirectPropertyPanel.add(linkingPropLabel);
    indirectPropertyPanel.add(linkingPropField);
    indirectPropertyPanel.add(classSubjectPropCheckBox);
    indirectPropertyPanel.add(new JLabel());
    indirectPropertyPanel.add(relatedClassPropLabel);
    indirectPropertyPanel.add(relatedClassPropField);
    content.add(indirectPropertyPanel, constraint);

    /** TODO: Add a list to allow user to add more than one property used as label */
    // List of properties used as label
    //        constraint.gridy = 3;
    //        constraint.weighty = 0.3;
    //        String[] testList = {"test", "poi", "sdfs"};
    //        content.add(new JScrollPane(new JList(testList)), constraint);
    //        content.repaint();

    constraint.fill = GridBagConstraints.NONE;
    constraint.anchor = GridBagConstraints.PAGE_START;
    constraint.weighty = 0;
    constraint.weightx = 0;
    constraint.gridx = 0;
    constraint.gridy = 4;
    constraint.gridwidth = 2;
    backBtn.addActionListener(this);
    saveBtn.addActionListener(this);
    // 		exampleBtn.addActionListener(this);

    Container cont = new Container();
    cont.add(backBtn);
    cont.add(saveBtn);
    // 		cont.add(exampleBtn);
    cont.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
    content.add(cont, constraint);

    content.validate();
  }
Пример #6
0
  private void ImportRelationsInheritanceOrEquivalence(int typeRelation) throws Exception {

    List<OntClass> classes = this.jena.listClasses().toList();
    for (OntClass jenaClass : classes) {
      Class mobiClass = new Class(jenaClass.getLocalName());
      // this.mobi.getClass(jenaClass.getLocalName());

      if (mobiClass != null) {
        List<OntClass> subClasses = null;

        if (typeRelation == Relation.INHERITANCE)
          subClasses = this.OntClassInheritanceChain(jenaClass.listSubClasses().toList());
        else if (typeRelation == Relation.EQUIVALENCE)
          subClasses = jenaClass.listEquivalentClasses().toList();

        for (OntClass jenaSubClass : subClasses) {
          if (jenaSubClass != null
              && !jenaSubClass.getLocalName().equals(mobiClass.getUri())
              && jenaSubClass.listInstances().hasNext()) {

            // this.mobi.addConcept(mobiClass);
            Class mobiSubClass = new Class(jenaSubClass.getLocalName());

            Relation relation = null;
            if (typeRelation == Relation.INHERITANCE)
              relation = this.mobi.createInheritanceRelation("isSuper");
            else if (typeRelation == Relation.EQUIVALENCE)
              relation = this.mobi.createEquivalenceRelation("equals");

            relation.setClassA(mobiClass);
            relation.setClassB(mobiSubClass);

            List<? extends OntResource> individualsClassA = jenaClass.listInstances().toList();
            List<? extends OntResource> individualsClassB = jenaSubClass.listInstances().toList();

            for (OntResource resourceA : individualsClassA) {
              Individual individualA = resourceA.asIndividual();

              Instance instanceA = new Instance(individualA.getLocalName());
              // this.mobi.getInstance(individualA.getLocalName());

              for (OntResource resourceB : individualsClassB) {
                Individual individualB = resourceB.asIndividual();
                Instance instanceB = new Instance(individualB.getLocalName());
                // this.mobi.getInstance(individualB.getLocalName());

                if (instanceB != null && instanceA.getUri().equals(instanceB.getUri())) {

                  this.mobi.addConcept(mobiClass);
                  this.mobi.addConcept(mobiSubClass);
                  this.mobi.addConcept(instanceA);
                  this.mobi.addConcept(instanceB);

                  try {
                    this.mobi.isOneOf(instanceA, mobiClass);
                    this.mobi.isOneOf(instanceB, mobiSubClass);
                  } catch (ExceptionURI e) {
                  }

                  this.mobi.addConcept(instanceB);

                  relation.addInstanceRelation(instanceA, instanceB);
                }
              }
            }

            relation.processCardinality();
            if (relation.getInstanceRelationMapA().size() > 0) this.mobi.addConcept(relation);
          }
        }
      }
    }
  }
Пример #7
0
  private void ImportRelationsComposition() throws Exception {
    List<ObjectProperty> properties = this.jena.listObjectProperties().toList();

    for (ObjectProperty property : properties) {

      OntClass domain = property.listDomain().next().asClass();
      if (domain.isUnionClass()) domain = domain.asUnionClass().listOperands().toList().get(0);

      OntClass range = property.listRange().next().asClass();
      if (range.isUnionClass()) range = range.asUnionClass().listOperands().toList().get(0);

      Relation r = null;

      if (property.isSymmetricProperty())
        r =
            this.mobi.createSymmetricRelation(
                this.getNameObjectProperty(
                    property.getLocalName(),
                    domain.getLocalName().length(),
                    range.getLocalName().length()));
      else if (property.getInverse() == null)
        r =
            this.mobi.createUnidirecionalCompositionRelationship(
                this.getNameObjectProperty(
                    property.getLocalName(),
                    domain.getLocalName().length(),
                    range.getLocalName().length()));
      else if (property.getInverse() != null)
        r =
            this.mobi.createBidirecionalCompositionRelationship(
                this.getNameObjectProperty(
                    property.getLocalName(),
                    domain.getLocalName().length(),
                    range.getLocalName().length()),
                this.getNameObjectProperty(
                    property.getInverse().getLocalName(),
                    range.getLocalName().length(),
                    domain.getLocalName().length()));

      Class mobiDomain = new Class(domain.getLocalName());
      // this.mobi.getClass(domain.getLocalName());
      Class mobiRange = new Class(range.getLocalName());
      // this.mobi.getClass(range.getLocalName());

      if (mobiDomain != null && mobiRange != null) {
        r.setClassA(mobiDomain);
        r.setClassB(mobiRange);

        List<? extends OntResource> individuals = domain.listInstances().toList();

        for (OntResource resourceIndividual : individuals) {
          Individual individualDomain = resourceIndividual.asIndividual();

          NodeIterator propertyValues =
              this.jena.getIndividual(individualDomain.getURI()).listPropertyValues(property);

          while (propertyValues.hasNext()) {
            RDFNode node = propertyValues.next();
            Individual individualValue = node.as(Individual.class);

            this.mobi.addConcept(mobiDomain);
            this.mobi.addConcept(mobiRange);

            Instance instanceDomain = new Instance(individualDomain.getLocalName());
            Instance instanceRange = new Instance(individualValue.getLocalName());

            try {
              this.mobi.isOneOf(instanceDomain, mobiDomain);
              this.mobi.isOneOf(instanceRange, mobiRange);
            } catch (ExceptionURI e) {
            }

            r.addInstanceRelation(instanceDomain, instanceRange);
          }
        }

        r.processCardinality();

        if (r.getInstanceRelationMapA().size() > 0) this.mobi.addConcept(r);
      }
    }
  }
  @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;
  }