Exemple #1
0
 private static Set<ERule> convertNamesToRules(List<String> ruleNames) {
   if (ruleNames == null) {
     return Collections.emptySet();
   }
   Set<ERule> waivedRules = new LinkedHashSet<ERule>();
   for (String ruleName : ruleNames) {
     ERule rule = AD.getDefinition(ERule.class, ruleName);
     if (rule != null) {
       waivedRules.add(rule);
     } else {
       LogUtil.warn("unknown rule: " + rule);
     }
   }
   return waivedRules;
 }
 public static EActivityGroupDef getActivityGroupDef(EActivityGroup activityGroup) {
   EObject object = activityGroup.getData();
   if (object != null) {
     EClass eClass = object.eClass();
     if (eClass instanceof EActivityGroupDef) {
       return (EActivityGroupDef) eClass;
     }
     for (EClass eSupertype : eClass.getESuperTypes()) {
       if (eSupertype instanceof EActivityGroupDef) {
         return (EActivityGroupDef) eSupertype;
       }
     }
     return ActivityDictionary.getInstance().getActivityGroupDef();
   }
   return null;
 }
  /**
   * Add a reference to a named AD
   *
   * @param element
   * @param referenceURI
   * @param referenceID
   * @param attribute
   */
  @SuppressWarnings("unchecked")
  public static void addReference(
      EPlanElement element, URI referenceURI, String referenceID, String attribute) {
    if (referenceURI != null) {
      EObject data = element.getData();
      EStructuralFeature feature = data.eClass().getEStructuralFeature(attribute);
      EClass referenceClass = (EClass) feature.getEType();
      List groupReferences = (List) data.eGet(data.eClass().getEStructuralFeature(attribute));

      URI uri = referenceURI.appendFragment(referenceID);
      EObject groupObject =
          ActivityDictionary.getInstance().getEFactoryInstance().create(referenceClass);
      ((BasicEObjectImpl) groupObject).eSetProxyURI(uri);
      if (!groupReferences.contains(groupObject)) {
        groupReferences.add(groupObject);
      }
    }
  }
Exemple #4
0
public class RuleUtils {

  public static ActivityDictionary AD = ActivityDictionary.getInstance();

  /**
   * Returns whether or not the rule is waived for this plan
   *
   * @see isEnabled
   * @param rule
   * @return
   */
  public static boolean isWaived(EPlanElement element, ERule rule) {
    RuleAdvisorMember ruleMember = element.getMember(RuleAdvisorMember.class, true);
    List<String> ruleNames =
        WaiverUtils.getExistingWaivedViolations(ruleMember, RuleAdvisorMember.RULE_WAIVERS_KEY);
    if (ruleNames != null) {
      return ruleNames.contains(rule.getName());
    }
    return false;
  }

  /**
   * Returns whether or not the rule is enabled for this plan
   *
   * @see isWaived
   * @param rule
   * @return
   */
  public static boolean isEnabled(EPlanElement element, ERule rule) {
    return !isWaived(element, rule);
  }

  /** Returns a list of the names of all the rules that are currently waived on the element */
  public static EcoreEList<String> getWaivedRuleNames(EPlanElement element) {
    RuleAdvisorMember ruleMember = element.getMember(RuleAdvisorMember.class, true);
    List<String> ruleNames =
        WaiverUtils.getWaivedViolations(ruleMember, RuleAdvisorMember.RULE_WAIVERS_KEY);
    return (EcoreEList<String>) ruleNames;
  }

  /** Returns a list of all the rules that are currently waived on the element */
  public static Set<ERule> getWaivedRules(EPlanElement element) {
    List<String> ruleNames = getWaivedRuleNames(element);
    return convertNamesToRules(ruleNames);
  }

  /**
   * Set some particular rule to be waived or enabled
   *
   * @param rule
   * @param waived
   */
  public static void setWaived(EPlan element, ERule rule, boolean waived) {
    EcoreEList<String> ruleNames = getWaivedRuleNames(element);
    String name = rule.getName();
    if (waived) {
      FeatureTransactionAddOperation.execute("waive rule", ruleNames, name);
    } else if (!waived) {
      FeatureTransactionRemoveOperation.execute("unwaive rule", ruleNames, name);
    }
  }

  /**
   * Set these rules to be waived or enabled
   *
   * @param rules
   * @param waived
   */
  public static void setWaivedRules(EPlan element, Set<ERule> rules, boolean waived) {
    EcoreEList<String> oldRuleNames = getWaivedRuleNames(element);
    List<String> changedRuleNames = new ArrayList<String>();
    for (ERule rule : rules) {
      changedRuleNames.add(rule.getName());
    }
    if (waived) {
      FeatureTransactionAddAllOperation.execute("waive rule(s)", oldRuleNames, changedRuleNames);
    } else {
      FeatureTransactionRemoveAllOperation.execute(
          "unwaive rule(s)", oldRuleNames, changedRuleNames);
    }
  }

  private static Set<ERule> convertNamesToRules(List<String> ruleNames) {
    if (ruleNames == null) {
      return Collections.emptySet();
    }
    Set<ERule> waivedRules = new LinkedHashSet<ERule>();
    for (String ruleName : ruleNames) {
      ERule rule = AD.getDefinition(ERule.class, ruleName);
      if (rule != null) {
        waivedRules.add(rule);
      } else {
        LogUtil.warn("unknown rule: " + rule);
      }
    }
    return waivedRules;
  }

  /**
   * @param plan
   * @return a list of activities and other plan elements that have violations.
   */
  public static Set<EPlanElement> getPlanElementsWithViolations(EPlan plan) {
    Set<EPlanElement> result = new HashSet();
    if (noDomainFor(plan)) {
      return Collections.EMPTY_SET;
    }
    List<ViolationTracker> trackers = PlanAdvisorMember.get(plan).getViolationTrackers();
    for (ViolationTracker tracker : trackers) {
      Violation violation = tracker.getViolation();
      if (violation.isCurrentlyViolated()) {
        result.addAll(violation.getElements());
      }
    }
    return result;
  }

  private static boolean noDomainFor(EPlan plan) {
    EditingDomain domain = EMFUtils.getAnyDomain(plan);
    return domain == null || domain.getResourceSet() == null;
  }
}