示例#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;
  }
示例#2
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);
      }
    }
  }
 @Override
 public boolean accept(OntClass cls) {
   if (!cls.isAnon()) {
     return cls.getURI().startsWith(RDF.getURI());
   }
   return false;
 }
 /* (non-Javadoc)
  * @see org.sadiframework.client.Registry#findServicesByConnectedClass(com.hp.hpl.jena.rdf.model.Resource)
  */
 @Override
 public Collection<? extends Service> findServicesByConnectedClass(Resource clazz)
     throws SADIException {
   Set<String> classURIs = new HashSet<String>();
   if (clazz.isURIResource()) classURIs.add(clazz.getURI());
   if (clazz.canAs(OntClass.class)) {
     for (Iterator<? extends OntClass> i = clazz.as(OntClass.class).listSubClasses();
         i.hasNext(); ) {
       OntClass c = i.next();
       if (c.isURIResource()) classURIs.add(c.getURI());
     }
   }
   return findServicesByConnectedClass(classURIs);
   // TODO
   //		return findServices(RegistrySearchCriteria.findService().addConnectedClass(clazz));
 }
 protected Set<OWLClass> getAllClasses() {
   if (ks.isRemote()) {
     return new SPARQLTasks(((SparqlEndpointKS) ks).getEndpoint()).getAllClasses();
   } else {
     Set<OWLClass> classes = new TreeSet<OWLClass>();
     for (OntClass cls :
         ((LocalModelBasedSparqlEndpointKS) ks)
             .getModel()
             .listClasses()
             .filterDrop(new OWLFilter())
             .filterDrop(new RDFSFilter())
             .filterDrop(new RDFFilter())
             .toList()) {
       if (!cls.isAnon()) {
         classes.add(df.getOWLClass(IRI.create(cls.getURI())));
       }
     }
     return classes;
   }
 }
  @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;
  }
  // gets the URIs of the classes and their translation
  public HashMap<String, String> getClassesIdentifiers(
      float percentage,
      boolean activeRandomString,
      boolean activeTranslateString,
      boolean activeSynonym,
      int activeStringOperation) {
    HashMap<String, String> classesIdentifiers =
        new HashMap<String, String>(); // the HashMap of classes identifiers

    int nbClasses, toBeRenamed, renamedClasses;

    List<OntClass> notRenamedClasses = new ArrayList<OntClass>(); // the list of not renamed classes
    List<OntClass> classes = getOntologyClasses(); // the list of ontology classes
    List<OntClass> classesTo = new ArrayList<OntClass>(); // the list of classes to be renamed

    // alignment contains those classes which have already been renamed
    // builds the list of all unrenamed classes from the model
    for (OntClass c : classes) {
      String local = getLocalName(c.getURI());
      if (alignment.containsKey(local)) {
        if (alignment.getProperty(local).equals(local))
          notRenamedClasses.add(c); // add the class to not renamed classes
      }
    }

    nbClasses = classes.size();
    renamedClasses = nbClasses - notRenamedClasses.size(); // the number of renamed classes
    toBeRenamed =
        Math.round(percentage * nbClasses) - renamedClasses; // -renamedClasses -> for Benchmark

    // logger.trace( "NbClasses = {}; YetToRename = {}; I will rename = {};", nbClasses,
    // notRenamedClasses.size(), toBeRenamed );

    // toBeRenamed is negative when classes have been added to the model
    if (toBeRenamed < 0) toBeRenamed = 0;
    // build the list of classes to be renamed
    int[] n = randNumbers(notRenamedClasses.size(), toBeRenamed);
    for (int i = 0; i < toBeRenamed; i++) {
      OntClass cls = notRenamedClasses.get(n[i]);
      classesTo.add(cls);
    }

    for (OntClass cls : classesTo) {
      if (!cls.isRestriction()) {
        if (!cls.isAnon()) {
          String prefix = getNameSpace(cls.getURI());
          String localName = getLocalName(cls.getURI());

          // has the same Namespace as the Ontology Namespace
          if (prefix.equals(modifiedOntologyNS)) {
            if (!classesIdentifiers.containsKey(localName)) {
              if (activeTranslateString) { // replace the URI with the translated one
                String translateStrg = parseString(localName, true, false);
                classesIdentifiers.put(localName, translateStrg);
                replaceClassLabel(
                    cls.getURI(),
                    translateStrg,
                    activeRandomString,
                    activeTranslateString,
                    activeSynonym,
                    activeStringOperation);
                if (alignment.containsKey(localName)) { // alignment.remove( cls.getURI() );
                  alignment.put(localName, translateStrg); // the reference alignment
                }
              } else if (activeRandomString) { // replace the URI with a random string
                String newStrg = getRandomString();
                classesIdentifiers.put(localName, newStrg);
                replaceClassLabel(
                    cls.getURI(),
                    newStrg,
                    activeRandomString,
                    activeTranslateString,
                    activeSynonym,
                    activeStringOperation);
                if (alignment.containsKey(localName)) { // alignment.remove( cls.getURI() );
                  alignment.put(localName, newStrg); // the reference alignment
                }
              } else if (activeSynonym) { // replace the URI with a synonym
                String synonym = parseString(localName, false, true);
                classesIdentifiers.put(localName, synonym);
                replaceClassLabel(
                    cls.getURI(),
                    synonym,
                    activeRandomString,
                    activeTranslateString,
                    activeSynonym,
                    activeStringOperation);
                if (alignment.containsKey(localName)) { // alignment.remove( cls.getURI() );
                  alignment.put(localName, synonym); // the reference alignment
                }
              } else if (activeStringOperation == 1) { // replace the URI with the UpperCase URI
                classesIdentifiers.put(localName, localName.toUpperCase());
                replaceClassLabel(
                    cls.getURI(),
                    localName.toUpperCase(),
                    activeRandomString,
                    activeTranslateString,
                    activeSynonym,
                    activeStringOperation);
                if (alignment.containsKey(localName)) { // alignment.remove( cls.getURI() );
                  alignment.put(localName, localName.toUpperCase()); // the reference alignment
                }
              } else if (activeStringOperation == 2) { // replace the URI with the LowerCase URI
                classesIdentifiers.put(localName, localName.toLowerCase());
                replaceClassLabel(
                    cls.getURI(),
                    localName.toLowerCase(),
                    activeRandomString,
                    activeTranslateString,
                    activeSynonym,
                    activeStringOperation);
                if (alignment.containsKey(localName)) { // alignment.remove( cls.getURI() );
                  alignment.put(localName, localName.toLowerCase()); // the reference alignment
                }
              } else {
                classesIdentifiers.put(localName, localName + "CLASS");
                replaceClassLabel(
                    cls.getURI(),
                    localName + "CLASS",
                    activeRandomString,
                    activeTranslateString,
                    activeSynonym,
                    activeStringOperation);
                if (alignment.containsKey(localName)) { // alignment.remove( cls.getURI() );
                  alignment.put(localName, localName + "CLASS");
                }
              }
            }
          }
        }
      }
    }
    return classesIdentifiers;
  }