View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.interpreter.expr;
36  
37  import java.util.Vector;
38  
39  import org.ogf.graap.wsag.interpreter.IExpression;
40  import org.ogf.graap.wsag.interpreter.InterpreterContext;
41  import org.ogf.graap.wsag.interpreter.InterpreterException;
42  import org.ogf.schemas.graap.wsAgreement.TermCompositorType;
43  import org.ogf.schemas.graap.wsAgreement.TermType;
44  
45  /**
46   * WS-Agreement allows to logically compose terms in an agreement using a simple expression language. Terms
47   * can be composed using logical AND (All), OR (OneOrMore), and XOR (ExacltyOne) operations in the agreement
48   * terms. This abstract expression implementation is the base class for the concrete implementations. It is
49   * used by the {@link org.ogf.graap.wsag.interpreter.AgreementInterpreter} that evaluates the validity of the
50   * agreement terms in an offer with respect to the expression language.
51   * 
52   * @see org.ogf.graap.wsag.interpreter.AgreementInterpreter
53   * 
54   * @author Oliver Waeldrich
55   * 
56   */
57  public abstract class AbstractExpression implements IExpression
58  {
59  
60      private TermCompositorType expression;
61  
62      private Vector<TermType> result;
63  
64      /**
65       * Creates an expression for a given term compositor.
66       * 
67       * @param term
68       *            the agreement term compositor
69       */
70      public AbstractExpression( TermCompositorType term )
71      {
72          this.expression = term;
73      }
74  
75      /**
76       * template method that implements a specific interpretation strategy. The strategy interprets a set of
77       * terms and returns a result, i.e. the selected term in an ExactlyOne statement.
78       * 
79       * @param terms
80       *            the terms to interpret
81       * @param context
82       *            the interpreter context
83       * @return the strategy result, i.e. a filtered term list
84       * 
85       * @throws InterpreterException
86       *             indicates an error during the term interpretation
87       */
88      protected abstract TermType[] interpret( TermType[] terms, InterpreterContext context )
89          throws InterpreterException;
90  
91      /**
92       * {@inheritDoc}
93       */
94      public TermType[] interpret( InterpreterContext context ) throws InterpreterException
95      {
96          result = new Vector<TermType>();
97  
98          //
99          // interpret the expressions
100         //
101         interpretAllExpression( expression.getAllArray(), context );
102         interpretExactlyOneExpression( expression.getExactlyOneArray(), context );
103         interpretOneOrMoreExpression( expression.getOneOrMoreArray(), context );
104 
105         //
106         // interpret the terms
107         //
108         interpretTerms( expression.getServiceDescriptionTermArray(), context );
109         interpretTerms( expression.getGuaranteeTermArray(), context );
110         interpretTerms( expression.getServicePropertiesArray(), context );
111         interpretTerms( expression.getServiceReferenceArray(), context );
112 
113         //
114         // now let the current expression handle the interpretation result
115         //
116         TermType[] interpretedTerms = (TermType[]) result.toArray( new TermType[result.size()] );
117 
118         return interpret( interpretedTerms, context );
119     }
120 
121     private void interpretAllExpression( TermCompositorType[] compositor, InterpreterContext context )
122         throws InterpreterException
123     {
124 
125         for ( int i = 0; i < compositor.length; i++ )
126         {
127             AllExpression allExpression = new AllExpression( compositor[i] );
128             TermType[] interpretionResult = allExpression.interpret( context );
129             for ( int j = 0; j < interpretionResult.length; j++ )
130             {
131                 result.add( interpretionResult[j] );
132             }
133         }
134 
135     }
136 
137     private void interpretExactlyOneExpression( TermCompositorType[] compositor, InterpreterContext context )
138         throws InterpreterException
139     {
140 
141         for ( int i = 0; i < compositor.length; i++ )
142         {
143             ExactlyOneExpression allExpression = new ExactlyOneExpression( compositor[i] );
144             TermType[] interpretionResult = allExpression.interpret( context );
145 
146             if ( interpretionResult.length != 1 )
147             {
148                 throw new InterpreterException(
149                                                 "ExaclyOne expression returned interpretation result unequal 1." );
150             }
151 
152             result.add( interpretionResult[0] );
153         }
154 
155     }
156 
157     private void interpretOneOrMoreExpression( TermCompositorType[] compositor, InterpreterContext context )
158         throws InterpreterException
159     {
160 
161         for ( int i = 0; i < compositor.length; i++ )
162         {
163             OneOrMoreExpression oneOrMoreExpression = new OneOrMoreExpression( compositor[i] );
164             TermType[] interpretionResult = oneOrMoreExpression.interpret( context );
165 
166             if ( interpretionResult.length == 0 )
167             {
168                 throw new InterpreterException( "OneOrMore expression returned no interpretation result." );
169             }
170 
171             for ( int j = 0; j < interpretionResult.length; j++ )
172             {
173                 result.add( interpretionResult[j] );
174             }
175         }
176 
177     }
178 
179     private void interpretTerms( TermType[] terms, InterpreterContext context ) throws InterpreterException
180     {
181 
182         for ( int i = 0; i < terms.length; i++ )
183         {
184             TermExpression oneOrMoreExpression = new TermExpression( terms[i] );
185             TermType[] interpretionResult = oneOrMoreExpression.interpret( context );
186 
187             for ( int j = 0; j < interpretionResult.length; j++ )
188             {
189                 result.add( interpretionResult[j] );
190             }
191         }
192 
193     }
194 }