@Test
 public void test_cg_implies_calls() throws ParserException {
   TestOCL ocl = createOCL();
   StandardLibrary standardLibrary = ocl.getStandardLibrary();
   ExpressionInOCL query = ocl.createQuery(standardLibrary.getOclVoidType(), "self->any(true)");
   String textQuery =
       "name = 'closure' implies\n" + "type.oclAsType(CollectionType).elementType = null";
   ocl.assertQueryTrue(query.getOwnedBody(), textQuery);
   ocl.dispose();
 }
 @Test
 public void test_cg_caught_if() throws ParserException {
   TestOCL ocl = createOCL();
   StandardLibrary standardLibrary = ocl.getStandardLibrary();
   ExpressionInOCL query = ocl.createQuery(standardLibrary.getOclVoidType(), "self->any(true)");
   String textQuery =
       "name = 'closure' implies\n"
           + "if self.ownedSource?.type.oclIsKindOf(SequenceType) or self.ownedSource?.type.oclIsKindOf(OrderedSetType)"
           + "then self.type.oclIsKindOf(OrderedSetType) else self.type.oclIsKindOf(SetType) endif";
   ocl.assertQueryTrue(query.getOwnedBody(), textQuery);
   ocl.dispose();
 }
예제 #3
0
 @Override
 public String toString() {
   OCLExpression bodyExpression = specification.getOwnedBody();
   if (bodyExpression != null) {
     return "<"
         + delegateDomain.getURI()
         + ":query> "
         + bodyExpression; //$NON-NLS-1$ //$NON-NLS-2$
   } else {
     return "<"
         + delegateDomain.getURI()
         + ":query> "
         + specification.getBody(); // $NON-NLS-1$ //$NON-NLS-2$
   }
 }
 @Test
 public void test_self_scope() {
   TestOCL ocl = createOCL();
   ExpressionInOCL query = ocl.assertQuery(null, "Sequence{1}");
   CollectionLiteralExp coll = (CollectionLiteralExp) query.getOwnedBody();
   CollectionItem item = (CollectionItem) coll.getOwnedParts().get(0);
   ocl.assertQueryTrue(item, "type = ownedItem.type");
   //		ocl.assertQueryInvalid(null, "type = item.type");		// A2.2 def'n of invalid = invalid
   ocl.assertQueryInvalid(
       null,
       "let item : CollectionItem = null in item.type = item"); // A2.2 def'n of invalid = invalid
   ocl.assertQueryInvalid(
       null,
       "let item : CollectionItem = invalid in item.type = item"); // A2.2 def'n of invalid =
                                                                   // invalid
   ocl.dispose();
 }
예제 #5
0
 /**
  * Executes the query for the specified <tt>target</tt> object. The result is the OCL evaluation
  * result which may be a Number, String, Collection or other object for normal returns or a
  * NullLiteralExp for null, or an InvalidLiteralExp for invalid.
  *
  * @param target the object on which to execute the query; this must be an instance of the context
  *     with which the delegate was created
  * @param arguments a map of variable names to values; these must correspond to the variables with
  *     which the delegate was created
  * @return the query's result
  * @throws InvocationTargetException in case of failure to prepare or execute the query, usually
  *     because of an exception
  */
 @Override
 public Object execute(@Nullable Object target, Map<String, ?> arguments)
     throws InvocationTargetException {
   @SuppressWarnings("null")
   @NonNull
   Map<String, ?> nonNullArguments =
       (arguments != null ? arguments : (Map<String, ?>) Collections.<String, Object>emptyMap());
   try {
     if (specification == null) {
       prepare();
     }
     @SuppressWarnings("null")
     @NonNull
     ExpressionInOCL nonNullSpecification = specification;
     OCL ocl = delegateDomain.getOCL();
     IdResolver idResolver = ocl.getIdResolver();
     Object targetValue = idResolver.boxedValueOf(target);
     org.eclipse.ocl.pivot.Class targetType = idResolver.getStaticTypeOf(targetValue);
     Type requiredType = nonNullSpecification.getOwnedContext().getType();
     if ((requiredType == null)
         || !targetType.conformsTo(ocl.getStandardLibrary(), requiredType)) {
       String message =
           StringUtil.bind(
               PivotMessagesInternal.WrongContextClassifier_ERROR_, targetType, requiredType);
       throw new OCLDelegateException(new SemanticException(message));
     }
     List<Variable> parameterVariables = nonNullSpecification.getOwnedParameters();
     int argCount = arguments != null ? arguments.size() : 0;
     if (parameterVariables.size() != argCount) {
       String message =
           StringUtil.bind(
               PivotMessagesInternal.MismatchedArgumentCount_ERROR_,
               argCount,
               parameterVariables.size());
       throw new OCLDelegateException(new SemanticException(message));
     }
     Query query = ocl.createQuery(nonNullSpecification);
     EvaluationEnvironment env = query.getEvaluationEnvironment(target);
     for (Variable parameterVariable : parameterVariables) {
       // bind arguments to parameter names
       String name = parameterVariable.getName();
       Object object = nonNullArguments.get(name);
       if ((object == null) && !nonNullArguments.containsKey(name)) {
         String message =
             StringUtil.bind(
                 PivotMessagesInternal.EvaluationResultIsInvalid_ERROR_,
                 nonNullSpecification.getBody());
         throw new OCLDelegateException(new SemanticException(message));
       }
       Object value = idResolver.boxedValueOf(object);
       targetType = idResolver.getStaticTypeOf(value);
       requiredType = ClassUtil.nonNullModel(parameterVariable.getType());
       if (!targetType.conformsTo(ocl.getStandardLibrary(), requiredType)) {
         String message =
             StringUtil.bind(
                 PivotMessagesInternal.MismatchedArgumentType_ERROR_,
                 name,
                 targetType,
                 requiredType);
         throw new OCLDelegateException(new SemanticException(message));
       }
       env.add(parameterVariable, value);
     }
     Object result = evaluate(query, target);
     //			if (result.isInvalid()) {
     //				String message = ClassUtil.bind(OCLMessages.EvaluationResultIsInvalid_ERROR_,
     // getOperationName());
     //				throw new OCLDelegateException(message);
     //			}
     //		if ((result == null) / * || ocl.isInvalid(result) * /) {
     //			String message = ClassUtil.bind(OCLMessages.EvaluationResultIsNull_ERROR_,
     // getOperationName());
     //			throw new OCLDelegateException(message);
     //		}
     //		return converter.convert(ocl, result);
     //			if (result == null) {
     //				String message = NLS.bind(OCLMessages.EvaluationResultIsInvalid_ERROR_,
     // PivotUtil.getBody(specification));
     //				throw new InvocationTargetException(new OCLDelegateException(message));
     //			}
     return idResolver.ecoreValueOf(null, result);
   } catch (InvocationTargetException e) {
     throw e;
   } catch (EvaluationException e) {
     String message =
         StringUtil.bind(
             PivotMessagesInternal.EvaluationResultIsInvalid_ERROR_, specification.getBody());
     throw new InvocationTargetException(new EvaluationException(message));
   } catch (WrappedException e) {
     throw new InvocationTargetException(e.getCause());
   } catch (Exception e) {
     throw new InvocationTargetException(e);
   }
 }