示例#1
0
  private void handleSubpropertyOf(OntProperty p, OntProperty ancestor) {

    List<RDFNode> list = ancestor.listPropertyValues(RDFS.subPropertyOf).toList();
    for (RDFNode node : list) {
      if (!node.canAs(OntProperty.class)) continue;

      OntProperty superProperty = node.as(OntProperty.class);
      if (superProperty.equals(ancestor)) continue;
      addFields(p, superProperty);
      handleSubpropertyOf(p, superProperty);
    }
  }
  public boolean doGet(
      MappedResource resource,
      Property property,
      boolean isInverse,
      HttpServletRequest request,
      HttpServletResponse response,
      Configuration config)
      throws IOException {
    Model descriptions = getAnonymousPropertyValues(resource, property, isInverse);
    if (descriptions.size() == 0) {
      return false;
    }

    Resource r = descriptions.getResource(resource.getWebURI());
    List resourceDescriptions = new ArrayList();
    StmtIterator it =
        isInverse ? descriptions.listStatements(null, property, r) : r.listProperties(property);
    while (it.hasNext()) {
      Statement stmt = it.nextStatement();
      RDFNode value = isInverse ? stmt.getSubject() : stmt.getObject();
      if (!value.isAnon()) {
        continue;
      }
      resourceDescriptions.add(
          new ResourceDescription((Resource) value.as(Resource.class), descriptions, config));
    }

    Model description = getResourceDescription(resource);
    ResourceDescription resourceDescription =
        new ResourceDescription(resource, description, config);

    String title =
        resourceDescription.getLabel()
            + (isInverse ? " ? " : " ? ")
            + config.getPrefixes().getNsURIPrefix(property.getNameSpace())
            + ":"
            + property.getLocalName();
    VelocityHelper template = new VelocityHelper(getServletContext(), response);
    Context context = template.getVelocityContext();
    context.put("project_name", config.getProjectName());
    context.put("project_link", config.getProjectLink());
    context.put("title", title);
    context.put("server_base", config.getWebApplicationBaseURI());
    context.put("sparql_endpoint", resource.getDataset().getDataSource().getEndpointURL());
    context.put("back_uri", resource.getWebURI());
    context.put("back_label", resourceDescription.getLabel());
    context.put(
        "rdf_link",
        isInverse ? resource.getInversePathDataURL(property) : resource.getPathDataURL(property));
    context.put("resources", resourceDescriptions);
    template.renderXHTML("pathpage.vm");
    return true;
  }
示例#3
0
 /**
  * Attempts to find the most plausible RDF type for a given property.
  *
  * @param property the property to get the type of
  * @return either owl:DatatypeProperty or owl:ObjectProperty
  */
 private Resource getPropertyType(Resource property) {
   StmtIterator it = model.listStatements(property, RDFS.range, (RDFNode) null);
   if (it.hasNext()) {
     while (it.hasNext()) {
       Statement s = it.nextStatement();
       RDFNode n = s.getObject();
       if (n.canAs(Resource.class)
           && model.contains((Resource) n.as(Resource.class), RDF.type, OWL.Class)) {
         return OWL.ObjectProperty;
       }
     }
   }
   return OWL.DatatypeProperty;
 }
 protected Set<ValueFactory> getValueFactory(RDFNode valueNode, OntModel displayOntModel) {
   // maybe use jenabean or owl2java for this?
   if (valueNode.isResource()) {
     Resource res = (Resource) valueNode.as(Resource.class);
     Statement stmt = res.getProperty(DisplayVocabulary.JAVA_CLASS_NAME);
     if (stmt == null || !stmt.getObject().isLiteral()) {
       log.debug("Cannot build value factory: java class was " + stmt.getObject());
       return Collections.emptySet();
     }
     String javaClassName = ((Literal) stmt.getObject().as(Literal.class)).getLexicalForm();
     if (javaClassName == null || javaClassName.length() == 0) {
       log.debug("Cannot build value factory: no java class was set.");
       return Collections.emptySet();
     }
     Class<?> clazz;
     Object newObj;
     try {
       clazz = Class.forName(javaClassName);
     } catch (ClassNotFoundException e) {
       log.debug("Cannot build value factory: no class found for " + javaClassName);
       return Collections.emptySet();
     }
     try {
       newObj = clazz.newInstance();
     } catch (Exception e) {
       log.debug(
           "Cannot build value factory: exception while creating object of java class "
               + javaClassName
               + " "
               + e.getMessage());
       return Collections.emptySet();
     }
     if (newObj instanceof ValueFactory) {
       ValueFactory valueFactory = (ValueFactory) newObj;
       return Collections.singleton(valueFactory);
     } else {
       log.debug(
           "Cannot build value factory: "
               + javaClassName
               + " does not implement "
               + ValueFactory.class.getName());
       return Collections.emptySet();
     }
   } else {
     log.debug("Cannot build value factory for " + valueNode);
     return Collections.emptySet();
   }
 }
示例#5
0
  private List<OntResource> getEnumeratedIndividuals(OntClass type) {
    Resource equivalentClass = type.getPropertyResourceValue(OWL.equivalentClass);
    if (equivalentClass == null) {
      return null;
    }
    Resource oneOf = equivalentClass.getPropertyResourceValue(OWL.oneOf);
    if (oneOf == null) return null;

    List<RDFNode> nodeList = oneOf.as(RDFList.class).asJavaList();

    List<OntResource> result = new ArrayList<OntResource>();

    for (RDFNode node : nodeList) {
      result.add(node.as(OntResource.class));
    }

    return result;
  }
示例#6
0
  private Field createListField(Frame frame, OntProperty p, OntResource range) {

    Resource intersection = range.getPropertyResourceValue(OWL.intersectionOf);
    if (intersection == null) return null;

    if (intersection.canAs(RDFList.class)) {
      List<RDFNode> intersectionList = intersection.as(RDFList.class).asJavaList();
      for (RDFNode node : intersectionList) {
        if (node.canAs(OntClass.class)) {
          OntClass intersectionMember = node.as(OntClass.class);

          if (RDF.first.equals(intersectionMember.getPropertyResourceValue(OWL.onProperty))) {
            // The intersectionMember has an owl:onProperty property whose value is rdf:first

            Resource elementRdfType =
                intersectionMember.getPropertyResourceValue(OWL.allValuesFrom);

            if (elementRdfType != null) {

              String elementTypeURI = elementRdfType.getURI();
              if (elementTypeURI != null) {
                RdfType elementType = manager.getTypeByURI(elementTypeURI);
                if (elementType != null) {

                  ListType listType = manager.getListTypeByElementUri(elementTypeURI);
                  if (listType == null) {
                    listType = new ListType(manager, intersectionMember, elementType);
                    manager.add(listType);
                  }

                  return new Field(frame, p, listType);
                }
              }
            }
          }
        }
      }
    }

    return null;
  }
  protected List<String> allIndividualsRelatedByObjectPropertyStmts(String uri) {
    List<String> additionalUris = new ArrayList<String>();

    QuerySolutionMap initialBinding = new QuerySolutionMap();
    Resource uriResource = ResourceFactory.createResource(uri);
    initialBinding.add("uri", uriResource);

    Query sparqlQuery = QueryFactory.create(QUERY_FOR_RELATED);
    model.getLock().enterCriticalSection(Lock.READ);
    try {
      QueryExecution qExec = QueryExecutionFactory.create(sparqlQuery, model, initialBinding);
      try {
        ResultSet results = qExec.execSelect();
        while (results.hasNext()) {
          QuerySolution soln = results.nextSolution();
          Iterator<String> iter = soln.varNames();
          while (iter.hasNext()) {
            String name = iter.next();
            RDFNode node = soln.get(name);
            if (node != null) {
              if (node.isURIResource()) {
                additionalUris.add(node.as(Resource.class).getURI());
              } else {
                log.warn(
                    "value from query for var " + name + "  was not a URIResource, it was " + node);
              }
            } else {
              log.warn("value for query for var " + name + " was null");
            }
          }
        }
      } catch (Throwable t) {
        log.error(t, t);
      } finally {
        qExec.close();
      }
    } finally {
      model.getLock().leaveCriticalSection();
    }
    return additionalUris;
  }
示例#8
0
 private void unifyRDFSVersion(String ns) {
   for (Iterator it = Jena.cloneIt(model.listStatements()); it.hasNext(); ) {
     Statement s = (Statement) it.next();
     Resource newSubject = s.getSubject();
     Property newPredicate = s.getPredicate();
     RDFNode newObject = s.getObject();
     boolean changed = false;
     if (ns.equals(newSubject.getNameSpace())) {
       changed = true;
       newSubject = model.getResource(RDFS.getURI() + newSubject.getLocalName());
     }
     if (ns.equals(newPredicate.getNameSpace())) {
       changed = true;
       newPredicate = model.getProperty(RDFS.getURI() + newPredicate.getLocalName());
     }
     if (newObject.canAs(Resource.class)) {
       Resource oldResource = (Resource) newObject.as(Resource.class);
       if (ns.equals(oldResource.getNameSpace())) {
         changed = true;
         newObject = model.getResource(RDFS.getURI() + oldResource.getLocalName());
       }
     }
     if (changed) {
       model.add(newSubject, newPredicate, newObject);
       if (log.isLoggable(Level.FINE)) {
         log.fine(
             "Replaced deprecated triple "
                 + s
                 + " with "
                 + newSubject
                 + ", "
                 + newPredicate
                 + ", "
                 + newObject);
       }
       it.remove();
     }
   }
 }
示例#9
0
 public static SHACLFunction asFunction(RDFNode resource) {
   return resource.as(SHACLFunction.class);
 }
示例#10
0
 public static SHACLArgument asArgument(RDFNode resource) {
   return resource.as(SHACLArgument.class);
 }
示例#11
0
 public static SHACLTemplateRule asTemplateRule(RDFNode node) {
   return node.as(SHACLTemplateRule.class);
 }
示例#12
0
 public static SHACLTemplateConstraint asTemplateConstraint(RDFNode node) {
   return node.as(SHACLTemplateConstraint.class);
 }
示例#13
0
 public static SHACLTemplateCall asTemplateCall(RDFNode resource) {
   return resource.as(SHACLTemplateCall.class);
 }
示例#14
0
 public static SHACLShape asShape(RDFNode node) {
   return node.as(SHACLShape.class);
 }
示例#15
0
 public static SHACLDerivedPropertyConstraint asDerivedPropertyConstraint(RDFNode node) {
   return node.as(SHACLDerivedPropertyConstraint.class);
 }
示例#16
0
 public static SHACLNativeScope asNativeScope(RDFNode node) {
   return node.as(SHACLNativeScope.class);
 }
示例#17
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);
      }
    }
  }
示例#18
0
 public static SHACLNativeConstraint asNativeConstraint(RDFNode node) {
   return node.as(SHACLNativeConstraint.class);
 }
示例#19
0
 /**
  * If _n_ is a ReifiedStatement, create a local copy of it, which will force the underlying
  * reifier to take note of the mapping.
  */
 private void noteIfReified(RDFNode n) {
   if (n.canAs(ReifiedStatement.class)) {
     ReifiedStatement rs = n.as(ReifiedStatement.class);
     createReifiedStatement(rs.getURI(), rs.getStatement());
   }
 }
  private void tryRestriction(
      OntClass theClass,
      VClassDao vcDao,
      ObjectPropertyDao opDao,
      IndividualDao iDao,
      ArrayList results,
      String vClassURI) {
    if (theClass.isRestriction()) {
      Restriction rest = (Restriction) theClass.as(Restriction.class);
      try {
        results.add("XX");
        Property onProperty = rest.getOnProperty();
        ObjectProperty op = opDao.getObjectPropertyByURI(onProperty.getURI());
        results.add(op.getLocalNameWithPrefix());
        if (rest.isAllValuesFromRestriction()) {
          results.add("all values from");
          AllValuesFromRestriction avfrest =
              (AllValuesFromRestriction) rest.as(AllValuesFromRestriction.class);
          Resource allValuesFrom = avfrest.getAllValuesFrom();
          results.add(printAsClass(vcDao, allValuesFrom));
        } else if (rest.isSomeValuesFromRestriction()) {
          results.add("some values from");
          SomeValuesFromRestriction svfrest =
              (SomeValuesFromRestriction) rest.as(SomeValuesFromRestriction.class);
          Resource someValuesFrom = svfrest.getSomeValuesFrom();
          results.add(printAsClass(vcDao, someValuesFrom));
        } else if (rest.isHasValueRestriction()) {
          results.add("has value");
          HasValueRestriction hvrest = (HasValueRestriction) rest.as(HasValueRestriction.class);
          RDFNode hasValue = hvrest.getHasValue();
          if (hasValue.isResource()) {
            Resource hasValueRes = (Resource) hasValue.as(Resource.class);
            try {
              if (hasValueRes.getURI() != null) {
                Individual ind = iDao.getIndividualByURI(hasValueRes.getURI());
                if (ind.getName() != null) {
                  results.add(ind.getName());
                }
              }
            } catch (Exception e) {
              results.add("???");
            }
          }

        } else if (rest.isMinCardinalityRestriction()) {
          MinCardinalityRestriction crest =
              (MinCardinalityRestriction) rest.as(MinCardinalityRestriction.class);
          results.add("at least " + crest.getMinCardinality());
          results.add(LAMBDA);
        } else if (rest.isMaxCardinalityRestriction()) {
          MaxCardinalityRestriction crest =
              (MaxCardinalityRestriction) rest.as(MaxCardinalityRestriction.class);
          results.add("at most " + crest.getMaxCardinality());
          results.add(LAMBDA);
        } else if (rest.isCardinalityRestriction()) {
          CardinalityRestriction crest =
              (CardinalityRestriction) rest.as(CardinalityRestriction.class);
          results.add("exactly " + crest.getCardinality());
          results.add(LAMBDA);
        }

        results.add(
            "<form action=\"addRestriction\" method=\"post\">"
                + "<input type=\"hidden\" name=\"_action\" value=\"delete\"/>"
                + "<input type=\"submit\" value=\"Delete\"/>"
                + "<input type=\"hidden\" name=\"_epoKey\" value=\""
                + epo.getKey()
                + "\"/>"
                + "<input type=\"hidden\" name=\"classUri\" value=\""
                + vClassURI
                + "\"/>"
                + "<input type=\"hidden\" name=\"restrictionId\" value=\""
                + ((rest.getId() != null) ? rest.getId() : rest.getURI())
                + "\"/>"
                + "</form>");

      } catch (Exception e) {
        e.printStackTrace(); // results.add("unknown property");
      }
    }
  }
示例#21
0
 public static SHACLNativeRule asNativeRule(RDFNode node) {
   return node.as(SHACLNativeRule.class);
 }
示例#22
0
文件: Node.java 项目: buaabarty/Scute
 /* (non-Javadoc)
  * @see com.hp.hpl.jena.rdf.model.RDFNode#as(java.lang.Class)
  */
 @Override
 public <T extends RDFNode> T as(Class<T> view) {
   return rdfNode.as(view);
 }