:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:



=== Action Semantics


The xref:refguide:applib:index/annotation/Action.adoc#semantics[semantics()] element describes whether the invocation modifies state of the system, and if so whether it does so idempotently.
If the action invocation does _not_ modify the state of the system, in other words is safe, then it also can beused to specify whether the results of the action can be cached automatically for the remainder of the request.

The `semantics` element was originally introduced for the xref:vro:ROOT:about.adoc[RestfulObjects viewer] in order that action invocations could be using the appropriate `HTTP` verb (`GET`, `PUT` and `POST`).

The table below summarizes the semantics:

[cols="2,1,3,1",options="header"]
|===
| Semantic
| Changes state
| Effect of multiple calls
| HTTP verb +
(Restful Objects)

| `SAFE_AND_REQUEST_CACHEABLE`
| No
| Will always return the same result each time invoked (within a given request scope)
| `GET`

| `SAFE`
| No
| Might result in different results each invocation
| `GET`

| `IDEMPOTENT` +
`IDEMPOTENT_ARE_YOU_SURE`
| Yes
| Will make no further changes if called multiple times (eg sets a property or adds to a `Set`). +
The "are you sure" variant requires that the user must explicitly confirm the action.
| `PUT`

| `NON_IDEMPOTENT` +
`NON_IDEMPOTENT_ARE_YOU_SURE`
| Yes
| Might change the state of the system each time called (eg increments a counter or adds to a `List`). +
The "are you sure" variant requires that the user must explicitly confirm the action.
| `POST`

|===

The actions' semantics are also used by the core runtime as part of the in-built concurrency checkng; invocation of a safe action (which includes request-cacheable) does _not_ perform a concurrency check, whereas non-safe actions _do_ perform a concurrency check.

For example:

[source,java]
----
public class Customer {

    @Action(semantics=SemanticsOf.SAFE_AND_REQUEST_CACHEABLE)
    public CreditRating checkCredit() {
        // ...
    }

    @Action(semantics=SemanticsOf.IDEMPOTENT)
    public void changeOfAddress(Address address) {
        // ...
    }

    @Action(semantics=SemanticsOf.NON_IDEMPOTENT)
    public Order placeNewOrder() {
        // ...
    }

    // ...
}
----

Actions that are safe and request-cacheable automatically use the xref:refguide:applib:index/services/queryresultscache/QueryResultsCache.adoc[QueryResultsCache] service to cache the result of the method.
Note though that the results of this caching will only be apparent if the action is invoked from another method using the xref:refguide:applib:index/services/wrapper/WrapperFactory.adoc[WrapperFactory] service.

