List<QuerySelectable> select = new ArrayList<QuerySelectable>(); for (String view : pathQuery.getView()) { Path path = new Path(model, view, subclasses); String parentPath = path.getPrefix().getNoConstraintsString(); String outerJoinGroup = outerJoinGroups.get(parentPath); if (root.equals(outerJoinGroup)) { QueryClass qc = (QueryClass) queryBits.get(parentPath); QueryField qf = new QueryField(qc, path.getLastElement()); queryBits.put(view, qf); if (!select.contains(qc)) { while ((!path.isRootPath()) && (!root.equals(outerJoinGroups.get(path.getPrefix() .getNoConstraintsString())))) { path = path.getPrefix(); if (!path.isRootPath()) { view = path.getNoConstraintsString(); if (!pathExpressionsDone.contains(view)) { QueryPathExpressionWithSelect pe = pathExpressions.get(view);
/** * If the last element in the path is a reference or collection return the ClassDescriptor that * the reference or collection references. If the path has one element (eg. "Employee"), * return its ClassDescriptor. If the last element in the path is an attribute, return null. * @return the ClassDescriptor */ public ClassDescriptor getEndClassDescriptor() { if (getEndFieldDescriptor() == null) { return getStartClassDescriptor(); } if (!getEndFieldDescriptor().isAttribute()) { if (getEndFieldDescriptor().isCollection()) { CollectionDescriptor collDesc = (CollectionDescriptor) getEndFieldDescriptor(); return collDesc.getReferencedClassDescriptor(); } if (getEndFieldDescriptor().isReference()) { ReferenceDescriptor refDesc = (ReferenceDescriptor) getEndFieldDescriptor(); return refDesc.getReferencedClassDescriptor(); } } return null; }
/** * Returns the last ClassDescriptor in the path. If the last element is an attribute, then the * class before it in the path is returned. Otherwise, class of the last element is returned. * The class of an element is the referenced type of the FieldDescriptor (modified by the class * constraint), or simply the class if it is the first element in the path. For example, if the * path is "Department.manager.name" then this method will return Manager. If the path is * "Department.manager[CEO].name" then this method will return CEO. * * @return the ClassDescriptor */ public ClassDescriptor getLastClassDescriptor() { List<ClassDescriptor> l = getElementClassDescriptors(); return l.get(l.size() - 1); }
/** * Return new Path that has this Path as its prefix and has fieldName as the last element. * * @param fieldName the field name * @return the new Path * @throws PathException if the resulting Path is not valid */ public Path append(String fieldName) throws PathException { return new Path(model, toString() + "." + fieldName); }
/** * Adds all the parts of a Path to a Set. Call this with only a non-attribute Path. * * @param validMainPaths a Set of Strings to add to * @param path a Path object */ private static void addValidPaths(Set<String> validMainPaths, Path path) { Path pathToAdd = path; while (!pathToAdd.isRootPath()) { validMainPaths.add(pathToAdd.toStringNoConstraints()); pathToAdd = pathToAdd.getPrefix(); } validMainPaths.add(pathToAdd.toStringNoConstraints()); }
private static Path getPathOfClass(TemplateQuery template, String pathStr) throws PathException { Path path = template.makePath(pathStr); if (path.endIsAttribute()) { path = path.getPrefix(); } return path; }
Path path = new Path(model, stringPath, subclasses); String outerJoinGroup = outerJoinGroups.get(stringPath); if (path.isRootPath()) { QueryClass qc = new QueryClass(path.getEndType()); ((Query) q).addFrom(qc); queryBits.put(stringPath, qc); String parent = path.getPrefix().getNoConstraintsString(); QueryClass parentQc = (QueryClass) ((queryBits.get(parent) instanceof QueryClass) ? queryBits.get(parent) : null); qc = new QueryClass(path.getEndType()); if (!pathConstraintNullOnly.containsKey(path.toString())) { if (q instanceof Query) { ((Query) q).addFrom(qc); if (path.endIsReference()) { andCs.addConstraint(new ContainsConstraint( new QueryObjectReference(parentQc, path.getLastElement()), ConstraintOp.CONTAINS, qc)); } else { andCs.addConstraint(new ContainsConstraint( new QueryCollectionReference(parentQc, path.getLastElement()), ConstraintOp.CONTAINS, qc)); boolean isCollection = path.endIsCollection();
if (path.endIsAttribute()) { throw new IllegalArgumentException("stringPath \"" + stringPath + "\" is an attribute, not a class"); String rootOfStringPath = path.getStartClassDescriptor().getUnqualifiedName(); if ((lRootClass != null) && (!lRootClass.equals(rootOfStringPath))) { throw new NoSuchElementException("Path " + stringPath + " is not in the query"); Path groupPath = path; Set<String> toAdd = new HashSet<String>(); while (!(groups.containsKey(groupPath.getNoConstraintsString()))) { toAdd.add(groupPath.toStringNoConstraints()); if (groupPath.isRootPath()) { break; groupPath = groupPath.getPrefix(); String group = groups.get(groupPath.getNoConstraintsString()); for (String toAddElement : toAdd) { groups.put(toAddElement, group); Class<?> type = path.getEndType(); Set<String> lExistingLoops = getExistingLoops(); Set<String> retval = new HashSet<String>(); if (!entry.getKey().equals(stringPath)) { Path entryPath = makePath(entry.getKey()); if (type.isAssignableFrom(entryPath.getEndType()) || entryPath.getEndType().isAssignableFrom(type)) {
Path path = new Path(model, constraint.getPath(), subclasses); if (rootClass == null) { rootClass = path.getStartClassDescriptor().getUnqualifiedName(); } else { String newRootClass = path.getStartClassDescriptor().getUnqualifiedName(); if (!rootClass.equals(newRootClass)) { problems.add("Multiple root classes in query: " + rootClass + " and " if (path.endIsAttribute()) { addValidPaths(validMainPaths, path.getPrefix()); } else { addValidPaths(validMainPaths, path); if (!path.endIsAttribute()) { problems.add("Constraint " + constraint + " must be on an attribute"); continue; Class<?> valueType = path.getEndType(); try { TypeUtil.stringToObject(valueType, if (path.isRootPath()) { problems.add("Constraint " + constraint + " cannot be applied to the root path"); if (path.endIsAttribute()) { problems.add("Constraint " + constraint + " must not be on an attribute");
PathConstraint constraint = entry.getKey(); String stringPath = constraint.getPath(); Path path = new Path(model, stringPath, subclasses); QuerySelectable field = queryBits.get(constraint.getPath()); if (field == null) { QueryClass qc = (QueryClass) queryBits.get(path.getPrefix() .getNoConstraintsString()); field = new QueryField(qc, path.getLastElement()); queryBits.put(stringPath, field); Class<?> fieldType = path.getEndType(); if (String.class.equals(fieldType)) { codeToConstraint.put(code, makeQueryStringConstraint( if (path.endIsAttribute()) { codeToConstraint.put(code, new SimpleConstraint((QueryField) field, constraint.getOp())); } else { String parent = path.getPrefix().getNoConstraintsString(); QueryClass parentQc = (QueryClass) ((queryBits.get(parent) instanceof QueryClass) ? queryBits.get(parent) : null); if (path.endIsReference()) { QueryObjectReference qr = new QueryObjectReference(parentQc, path.getLastElement()); codeToConstraint.put(code, new ContainsConstraint(qr, constraint.getOp())); } else { // collection QueryCollectionReference qr = new QueryCollectionReference(parentQc,
public static Set<Object> resolveCollectionPath(Path path, Object o) throws PathException { if (!path.containsCollections()) { Model model = path.getModel(); if (path.getStartClassDescriptor() != null) { Set<ClassDescriptor> clds = model.getClassDescriptorsForClass(o.getClass()); if (!clds.contains(path.getStartClassDescriptor())) { throw new PathException("ClassDescriptor from the start of path: " + path + " is not a superclass of the class: " + Util.getFriendlyName(o.getClass()) + " while resolving object: " + o, path.toString()); List<String> pathStrings = path.getElements(); for (int i = 0; i < path.getElements().size(); i++) { path = new Path(model, objectClass + pathString);
private void validateOuterJoins(List<String> problems, Set<String> validMainPaths) { for (String joinPath : outerJoinStatus.keySet()) { try { Path path = new Path(model, joinPath, subclasses); if (path.endIsAttribute()) { problems.add("Outer join status on path " + joinPath + " must not be on an attribute"); continue; if (path.isRootPath()) { problems.add("Outer join status cannot be set on root path " + joinPath); continue; for (String validPath : validMainPaths) { try { Path path = new Path(model, validPath, subclasses); while (isInner(path)) { path = path.getPrefix(); outerJoinGroups.put(validPath, path.getNoConstraintsString()); } catch (PathException e) {
public void testValid() throws Exception { String stringPath = "Department.company"; Path path = new Path(model, stringPath); ClassDescriptor cld = model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.Department"); assertEquals(cld, path.getStartClassDescriptor()); FieldDescriptor fld = cld.getFieldDescriptorByName("company"); assertEquals(fld, path.getEndFieldDescriptor()); ClassDescriptor compDesc = model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.Company"); assertEquals(compDesc, path.getEndClassDescriptor()); assertFalse(path.containsCollections()); assertEquals(stringPath, path.toString()); }
private Set<String> validateView(List<String> problems, Set<String> validMainPaths) { if (view.isEmpty()) { problems.add(NO_VIEW_ERROR); } else { for (String viewPath : view) { try { Path path = new Path(model, viewPath, subclasses); if (!path.endIsAttribute()) { problems.add("Path " + viewPath + " in view list must be an attribute"); continue; } if (rootClass == null) { rootClass = path.getStartClassDescriptor().getUnqualifiedName(); } else { String newRootClass = path.getStartClassDescriptor().getUnqualifiedName(); if (!rootClass.equals(newRootClass)) { problems.add("Multiple root classes in query: " + rootClass + " and " + newRootClass); continue; } } addValidPaths(validMainPaths, path.getPrefix()); } catch (PathException e) { problems.add("Path " + viewPath + " in view list is not in the model"); } } } return validMainPaths; }
Path p = new Path(oldModel, path); String pathUpdated = path; for (String key : renamedFields.keySet()) { String cls = key.substring(0, dotIndex); String prevField = key.substring(dotIndex + 1); List<Integer> elementContainingField = p.getElementsContainingField(cls, prevField); if (!elementContainingField.isEmpty()) { for (int index : elementContainingField) { p.getElements().set(index, renamedFields.get(key)); pathUpdated = p.toStringNoConstraints(); isUpdated = true; String cls = entry.getKey(); String newClass = entry.getValue(); if (p.startContainsClass(cls)) { pathUpdated = pathUpdated.replace(cls, newClass); isUpdated = true;
public void testShortPath() throws Exception { String stringPath = "Department"; Path path = new Path(model, stringPath); ClassDescriptor cld = model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.Department"); assertEquals(cld, path.getStartClassDescriptor()); assertEquals(cld, path.getEndClassDescriptor()); assertNull(path.getEndFieldDescriptor()); //assertEquals(Department.class, path.getEndType()); }
private void checkConstrainedPath(Path path) { assertEquals(model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.Department"), path.getStartClassDescriptor()); assertEquals(model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.Department"), path.getElementClassDescriptors().get(0)); assertEquals("manager", path.getElements().get(0)); assertEquals(model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.CEO"), path.getElementClassDescriptors().get(1)); assertEquals("company", path.getElements().get(1)); assertEquals(model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.Company"), path.getElementClassDescriptors().get(2)); assertEquals("departments", path.getElements().get(2)); assertEquals(model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.Department"), path.getElementClassDescriptors().get(3)); assertEquals("employees", path.getElements().get(3)); assertEquals(model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.Manager"), path.getElementClassDescriptors().get(4)); assertEquals("age", path.getElements().get(4)); assertEquals(Integer.class, path.getEndType()); }
public void testValid2() throws Exception { String stringPath = "Department.company.name"; Path path = new Path(model, stringPath); ClassDescriptor cld = model.getClassDescriptorByName("org.intermine.pathquery.model.testmodel.Department"); assertEquals(String.class, path.getEndType()); assertEquals(cld, path.getStartClassDescriptor()); }
/** * Return a Path object that represents the prefix of this path, ie this Path without the * last element. If the Path contains only the root class, an exception is thrown. * * @return the prefix Path */ public Path getPrefix() { if (isRootPath()) { throw new RuntimeException("path (" + this + ") has only one element"); } String pathString = toString(); int lastDotIndex = pathString.lastIndexOf('.'); int lastIndex = pathString.lastIndexOf(':'); if (lastDotIndex > lastIndex) { lastIndex = lastDotIndex; } try { return new Path(model, pathString.substring(0, lastIndex)); } catch (PathException e) { // Should not happen throw new Error("There must be a bug", e); } }
public void testGetPrefix() throws Exception { Map<String, String> constraintMap = new HashMap<String, String>(); constraintMap.put("Department.manager", "CEO"); constraintMap.put("Department.manager.company.departments.employees", "Manager"); String stringPath = "Department.manager.company.departments.employees.age"; Path path = new Path(model, stringPath, constraintMap); Path prefix = path.getPrefix(); assertEquals("Department.manager[CEO].company.departments.employees[Manager]", prefix.toString()); prefix = prefix.getPrefix(); assertEquals("Department.manager[CEO].company.departments", prefix.toString()); prefix = prefix.getPrefix(); assertEquals("Department.manager[CEO].company", prefix.toString()); prefix = prefix.getPrefix(); assertEquals("Department.manager[CEO]", prefix.toString()); prefix = prefix.getPrefix(); assertEquals("Department", prefix.toString()); try { prefix = prefix.getPrefix(); fail("expected RuntimeException"); } catch (RuntimeException e) { // expected } }