示例#1
0
  /**
   * Initialises the TextFrame. Finds the equivalent Ontology class, creates the graph with it's
   * root, and then expands the Graph with information about the user, the date of deposit, and the
   * project.
   *
   * @param type The type of resource we want to deposit
   * @param user User ID
   * @param projectID Project ID, may be null
   * @param userInfo SemanticGraphTransformer containing all information about this user in the
   *     archive
   * @param r Ontology
   * @param query True if this is the querying module, and we need a QueryGraph
   * @throws OntologyInputException if there was an error extracting information from the archive
   */
  public TextFrame(
      String type,
      String user,
      String projectID,
      SemanticGraphTransformer userInfo,
      OntologyReader r,
      boolean query)
      throws OntologyInputException {
    super(userInfo, r);
    if (query) // if it's a query, we don't need to include the user info
    setGraph(new QueryGraph(user));
    reader = r;
    doctype = type;
    OntClass c = r.getClass(type);
    if (c == null) throw new OntologyInputException("Class " + type + " not in ontology");

    SGNode userNode =
        getGraph().getRoot(); // store old root, because we need to pass it to AutomaticGenerator
    SGNode root = new SGNode(type);
    if (query) root = new QueryNode(type);
    root.setRemovable(false);
    addNode(root);
    getGraph().setRoot(root);

    if (!query) AutomaticGenerator.expandGraph(this, userNode, projectID);
    getGraph().stopFlashing();
  }
 /**
  * Checks whether adding p to this node would violate cardinality constraints
  *
  * @param p OntProperty
  * @return true if adding the property would violate a cardinality constraint
  */
 public boolean violatesCardinality(OntProperty p) {
   if (p.isInverseFunctionalProperty()) {
     if (node.getIncomingEdges(p.getLocalName()).size() > 0) return true;
   }
   OntProperty inverse = reader.getInverse(p);
   if (inverse == null)
     return false; // if the inverse property does not exist there are no cardinal constraints on
   // it...
   if (optional.contains(inverse) || compulsory.contains(inverse)) return false;
   if (addShowOrHideOption() == SGNode.INCOMPLETE) return false;
   return true;
 }
 /** Divides the list of optional properties into submenus. */
 private Map<String, List<OntProperty>> getMenus() {
   Map<String, List<OntProperty>> result = new HashMap<String, List<OntProperty>>();
   for (int i = 0; i < optional.size(); i++) {
     String menu = reader.getSubmenu(optional.get(i), node.getLabel());
     if (menu == null) continue;
     List<OntProperty> list = new ArrayList();
     if (result.containsKey(menu)) list = result.get(menu);
     list.add(optional.get(i));
     result.put(menu, list);
   }
   return result;
 }
  /**
   * 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);
  }
示例#5
0
  /**
   * Initialises the TextFrame of a query. Finds the equivalent Ontology class, creates the graph
   * with it's root, and then expands the Graph with information about the user and the date of
   * deposit.
   *
   * @param type Te type of resource we want to find.
   * @param user User ID
   * @param r Ontology
   * @throws OntologyInputException if there was an error extracting information from the archive
   */
  public TextFrame(String type, String user, OntologyReader r) throws OntologyInputException {
    super(new QueryGraph(user), r);
    reader = r;
    doctype = type;
    OntClass c = r.getClass(type);
    if (c == null) throw new OntologyInputException("Class " + type + " not in ontology");

    SGNode userNode =
        getGraph().getRoot(); // store old root, because we need to pass it to AutomaticGenerator
    SGNode root = new QueryNode(type);
    root.setRemovable(false);
    addNode(root);
    getGraph().setRoot(root);
    getGraph().stopFlashing();
  }
 /**
  * Returns the natural language representation of the given resource
  *
  * @param r Ontology resource
  * @return String NL-representation
  */
 public String getNLExpression(OntResource r) {
   String str = reader.getNLExpression(r);
   if (str != null) // if the nl-expr. has been defined in the ontology, use that
   return str;
   return getNLExpression(r.getLocalName());
 }
  /**
   * If this is a resource that is available, and this user has access to it, this method sets the
   * URI.
   */
  private void setURI(String userID) {
    if ((userID == null) || (!(node instanceof SGNode))) return; // only do this for SGNodes

    SGNode n = (SGNode) node;
    List<Edge> list = n.getOutgoingEdges("HasURI");
    if (list.size() > 0) {
      SGStringNode target = (SGStringNode) list.get(0).getTarget();
      uri = (String) target.getValue();
    }
    if (uri == null) return; // if there is no URI, we are done

    list = n.getOutgoingEdges("AccessConditions");
    if (list.size() == 0) {
      System.out.println("Could not find access rights to node " + n.getNLLabel(reader));
      uri = null;
      return; // assume that access must be public, if unspecified
    }
    String access = (String) ((SGStringNode) list.get(0).getTarget()).getValue();
    if (access.equals("public")) return; // everyone has access to a public resource

    SGNode depositor = n.getPropertyTarget("DepositedBy", reader);
    if ((depositor != null) && userID.equals(depositor.getUniqueID()))
      return; // depositor has access to all his resources

    if (access.equals("restricted to project members")) // access restricted to project members
    { // find out which project the resource is associated with
      SGNode project = n.getPropertyTarget("ProducedInProject", reader);
      if (project != null) { // find out if this user is a member
        List<String> subprops = reader.getSubProperties("HasMember");
        for (Iterator it = project.getOutgoingEdges(); it.hasNext(); ) {
          SGEdge edge = (SGEdge) it.next();
          if (subprops.contains(edge.getLabel()) && userID.equals(edge.getTarget().getUniqueID())) {
            System.out.println(
                "FOUND MEMBER OF PROJECT "
                    + project.getNLLabel(reader)
                    + ": "
                    + edge.getTarget().getNLLabel(reader));
            return;
          }
        }

        subprops = reader.getSubProperties("MemberOf");
        for (Iterator it = project.getIncomingEdges(); it.hasNext(); ) {
          SGEdge edge = (SGEdge) it.next();
          if (subprops.contains(edge.getLabel()) && userID.equals(edge.getSource().getUniqueID())) {
            System.out.println(
                "FOUND MEMBER OF PROJECT "
                    + project.getNLLabel(reader)
                    + ": "
                    + edge.getSource().getNLLabel(reader));
            return;
          }
        }
      }
    } else if (access.equals("restricted to authors")) {
      for (Edge edge : n.getOutgoingEdges("HasAuthor")) {
        if (userID.equals(((SGNode) edge.getTarget()).getUniqueID())) return;
      }
      for (Edge edge : n.getIncomingEdges("AuthorOf")) {
        if (userID.equals(((SGNode) edge.getSource()).getUniqueID())) return;
      }
    }

    uri = null; // in all other cases, the user should not have access to the resource
  }