public Iterator getInstancesOfClass(String className) {
    // get the instances of a class
    OntClass onto = model.getOntClass(className);

    if (onto == null) {
      System.out.println("ONT CLASS IS NULL");
      return (new ArrayList()).iterator();
    }

    Iterator instances = onto.listInstances();
    return instances;
  }
示例#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
  public Search(
      final java.awt.Frame parent,
      boolean modal,
      final OntClass ontClass,
      final OntProperty ontTarget,
      final JTextField txtField) {
    super(parent, modal);
    initComponents();
    this.parent = (SaveImage) parent;
    this.ontClass = ontClass;
    individuals = new ArrayList<>();
    all_individuals = new ArrayList<>();
    for (NodeIterator i =
            ontClass.listPropertyValues(
                Ontology.getOntModel().getProperty(Ontology.getNameSpace() + "lbl_netbeans"));
        i.hasNext(); ) {
      title_class.setText(i.next().toString());
    }
    this.txtField = txtField;
    model = new InstancesTable();
    tbl_individuals.setModel(model);
    for (ExtendedIterator<? extends OntResource> i = ontClass.listInstances(); i.hasNext(); ) {
      Individual inst = (Individual) i.next();
      if (inst.getNameSpace() != null) {
        individuals.add(inst);
        all_individuals.add(inst);
      }
    }

    tbl_individuals.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
              JTable target = (JTable) e.getSource();
              int row = target.getSelectedRow();
              temp = ((SaveImage) parent).getContentFields().get(ontTarget.toString());
              temp.setIndividual(individuals.get(row));
              temp.getField().setText(individuals.get(row).getLocalName());
            }
          }
        });
  }
示例#4
0
  public static void main(String args[]) {
    String filename = "example6.rdf";

    // Create an empty model
    OntModel model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM);

    // Use the FileManager to find the input file
    InputStream in = FileManager.get().open(filename);

    if (in == null) throw new IllegalArgumentException("File: " + filename + " not found");

    // Read the RDF/XML file
    model.read(in, null);
    // model.write(System.out,"Turtle");

    ExtendedIterator<? extends OntResource> iterator;
    // ** TASK 7.1: List all individuals of "Person" **
    OntClass person = model.getOntClass(ns + "Person");

    iterator = person.listInstances();
    System.out.println("Individuals");
    System.out.println("------------------------------------");
    while (iterator.hasNext()) {
      OntResource r = iterator.next();
      System.out.println(r.getURI());
    }
    System.out.println("------------------------------------");
    // ** TASK 7.2: List all subclasses of "Person" **
    iterator = person.listSubClasses();
    System.out.println("Sublcasses");
    System.out.println("------------------------------------");
    while (iterator.hasNext()) {
      OntResource r = iterator.next();
      System.out.println(r.getURI());
    }
    System.out.println("------------------------------------");
    // ** TASK 7.3: Make the necessary changes to get as well indirect instances and subclasses.
    // TIP: you need some inference... **

  }
示例#5
0
  public static void showShows() {
    OntModel model = PersistentOntology.getOntModel();
    model.write(System.out, "N-TRIPLES");
    OntClass show = model.getOntClass(PersistentOntology.NS + "TVShow");

    ExtendedIterator it = show.listInstances();

    while (it.hasNext()) {
      System.out.println("TVSHOW:");
      Individual ind = (Individual) it.next();
      Statement p = ind.getProperty(PersistentOntology.name);
      if (p != null)
        System.out.println(" - name: " + ind.getProperty(PersistentOntology.name).getString());

      p = ind.getProperty(PersistentOntology.status);
      if (p != null)
        System.out.println(" - status: " + ind.getProperty(PersistentOntology.status).getString());

      p = ind.getProperty(PersistentOntology.airDay);
      if (p != null) System.out.println(" - dates: " + p.getString());
      System.out.println();
    }
  }
示例#6
0
 public static Iterator<Thing> iterate(boolean direct, OntModel ontModel) {
   OntClass cls = ontModel.getOntClass("http://www.w3.org/2002/07/owl#Thing");
   ExtendedIterator<? extends RDFNode> it = cls.listInstances(direct);
   ExtendedIterator<RDFNode> nodeIt = new WrappedIterator<RDFNode>(it) {};
   return nodeIt.mapWith(nodeMapper).filterDrop(nullFilter);
 }
示例#7
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);
          }
        }
      }
    }
  }
示例#8
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);
      }
    }
  }