View Javadoc

1   /*
2     wsmo4j - a WSMO API and Reference Implementation
3    Copyright (c) 2005, University of Innsbruck, Austria
4    This library is free software; you can redistribute it and/or modify it under
5    the terms of the GNU Lesser General Public License as published by the Free
6    Software Foundation; either version 2.1 of the License, or (at your option)
7    any later version.
8    This library is distributed in the hope that it will be useful, but WITHOUT
9    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11   details.
12   You should have received a copy of the GNU Lesser General Public License along
13   with this library; if not, write to the Free Software Foundation, Inc.,
14   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15   */
16  package org.deri.wsmo4j.factory;
17  
18  
19  import java.util.*;
20  
21  import org.deri.wsmo4j.io.parser.wsml.*;
22  import org.deri.wsmo4j.logicalexpression.*;
23  import org.deri.wsmo4j.logicalexpression.terms.*;
24  import org.omwg.logicalexpression.*;
25  import org.omwg.logicalexpression.terms.*;
26  import org.omwg.ontology.*;
27  import org.wsmo.common.*;
28  import org.wsmo.factory.*;
29  import org.wsmo.wsml.*;
30  
31  import com.ontotext.wsmo4j.ontology.*;
32  
33  
34  /**
35   * <pre>
36   *
37   *  Created on Jun 20, 2005
38   *  Committed by $Author: morcen $
39   *
40   * </pre>
41   *
42   * @author reto.krummenacher@deri.org
43   * @author holger.lausen@deri.org
44   * @author thomas.haselwanter@deri.org
45   * @version $Revision: 1946 $ $Date: 2007-04-02 15:13:28 +0300 (Mon, 02 Apr 2007) $
46   * @see org.omwg.logexpression.LogicalExpressionFactory
47   */
48  public class LogicalExpressionFactoryImpl
49          extends AbstractLogicalExpressionFactoryImpl{
50  
51      private Map <String, Object> parserProperties = new HashMap <String, Object>();
52  
53      /**
54       * Creates a default LogicalExpressionFactory. The LogicalExpressionFactory used by default
55       * handles logical expressions as plain strings, instead of objects
56       * (variables, molecules, operators,...)
57       * @see org.wsmo.factory.Factory#createLogicalExpressionFactory(Map)
58       */
59  
60      public LogicalExpressionFactoryImpl() {
61          this(null);
62      }
63  
64      /**
65       * Creates a LogicalExpressionFactory. The LogicalExpressionFactory used by default
66       * handles logical expressions as plain strings, instead of objects
67       * (variables, molecules, operators,...)
68       * The LogicalExpressionFactory is initialised based on the supplied preferences.
69       * The properties map can contain the factories to be used as Strings or as
70       * instances. If a factory to use is only indicated as String, the constructor
71       * needs to create an instance of this given factory.
72       *
73       * @param map properties used to create the LogicalExpressionFactory, if properties==null,
74       * the default LogicalExpressionFactory is used
75       * @see org.wsmo.factory.Factory#createLogicalExpressionFactory(Map)
76       */
77      public LogicalExpressionFactoryImpl(Map properties) {
78          WsmoFactory factory = null;
79          if (properties != null && properties.containsKey(LEFACTORY_WSMO_FACTORY)) {
80              factory = (WsmoFactory) properties.get(LEFACTORY_WSMO_FACTORY);
81          }
82          //use default factory if none is given
83          if (factory == null) {
84              factory = Factory.createWsmoFactory(null);
85          }
86          this.parserProperties.put(Factory.WSMO_FACTORY, factory);
87  
88          DataFactory dataFactory = null;
89          if (properties != null && properties.containsKey(LEFACTORY_DATA_FACTORY)) {
90              dataFactory = (DataFactory) properties.get(LEFACTORY_DATA_FACTORY);
91          }
92          //use default factory if none is given
93          if (dataFactory == null || !(factory instanceof DataFactory)) {
94              dataFactory = Factory.createDataFactory(null);
95          }
96          this.parserProperties.put(Factory.DATA_FACTORY, dataFactory);
97  
98          this.parserProperties.put(Factory.LE_FACTORY, this);
99      }
100 
101     /**
102      * Creates a logical expression object model from a string. Note: only works
103      * if no sqname is in the logical expression if sqname is there namespace
104      * information are required
105      * @param expr String representation of a Logical Expression
106      * @return logical expression object model
107      * @throws ParserException provides information about where and why the parse process failed
108      */
109     public LogicalExpression createLogicalExpression(String expr)
110             throws ParserException {
111         return createLogicalExpression(expr, null);
112     }
113 
114     /**
115      * Creates a logical expression object model from a string.
116      * @param expr String representation of a Logical Expression
117      * @param nsHolder TopEntity
118      * @return logical expression object model
119      * @throws ParserException provides information about where and why the parse process failed
120      */
121     public LogicalExpression createLogicalExpression(String expr,
122             TopEntity nsHolder)
123             throws ParserException {
124         LogExprParserImpl leParser = new LogExprParserImpl(parserProperties, nsHolder);
125         return leParser.parse(expr);
126     }
127 
128     /**
129      * @param id identifier of Atom
130      * @param params list of parameters for that atom
131      * @return an atom
132      * @throws IllegalArgumentException in case the parameter id is a Value or the arguments of
133      * the list args aren't all of Type Term
134      * @see org.omwg.logexpression.LogicalExpressionFactory#createAtom(org.omwg.logexpression.terms.Identifier, java.util.List)
135      */
136     public Atom createAtom(Identifier id, List <Term> params)
137             throws IllegalArgumentException {
138         if (ConstantTransformer.getInstance().isBuiltInAtom(id.toString())) {
139             return new BuiltInAtomImpl(id, params);
140         }
141         else {
142             return new AtomImpl(id, params);
143         }
144 
145     }
146 
147     /**
148      * @see LogicalExpressionFactory#createCompoundMolecule(List)
149      */
150     public CompoundMolecule createCompoundMolecule(List <Molecule> molecules)
151             throws IllegalArgumentException {
152         return new CompoundMoleculeImpl(molecules);
153     }
154 
155     /**
156      * @see LogicalExpressionFactory#createSubConceptMolecule(List)
157      */
158     public SubConceptMolecule createSubConceptMolecule(Term identifier, Term superConcept){
159         return new SubConceptMoleculeImpl(identifier, superConcept);
160     }
161 
162     /**
163      * @see LogicalExpressionFactory#createMemberShipMolecule(Term, Term)
164      */
165     public MembershipMolecule createMemberShipMolecule(Term identifier, Term concept){
166         return new MemberShipMoleculeImpl(identifier, concept);
167     }
168 
169     /**
170      * @see LogicalExpressionFactory#createAttributeValue(Term, Term, Term)
171      */
172     public AttributeValueMolecule createAttributeValue(Term leftTerm, Term attributeName, Term rightTerm)
173             throws IllegalArgumentException {
174         return new AttributeValueMoleculeImpl(leftTerm, attributeName, rightTerm);
175     }
176 
177     /**
178      * @see LogicalExpressionFactory#createAttributeConstraint(Term, Term, Term)
179      */
180     public AttributeConstraintMolecule createAttributeConstraint(Term leftTerm, Term attributeName, Term rightTerm)
181             throws IllegalArgumentException {
182         return new AttributeConstraintMoleculeImpl(leftTerm, attributeName, rightTerm);
183     }
184 
185     /**
186      * @see LogicalExpressionFactory#createAttributeInference(Term, Term, Term)
187      */
188     public AttributeInferenceMolecule createAttributeInference(Term leftTerm, Term attributeName, Term rightTerm)
189             throws IllegalArgumentException {
190         return new AttributeInferenceMoleculeImpl(leftTerm, attributeName, rightTerm);
191     }
192 
193     /**
194      * @param functionSymbol identifier of the constructed term
195      * @param terms arguments of the constructed term
196      * @return a constructed term
197      * @throws IllegalArgumentException in case the functionSymbol is null or the arguments of
198      * the list aren't all of Type Term
199      * @see org.omwg.logexpression.LogicalExpressionFactory#createConstructedTerm(IRI, List)
200      */
201     public ConstructedTerm createConstructedTerm(IRI functionSymbol, List <Term> terms)
202             throws IllegalArgumentException {
203         if (ConstantTransformer.getInstance().isBuiltInFunctionSymbol(functionSymbol.toString())) {
204             return new BuiltInConstructedTermImpl(functionSymbol, terms);
205         }
206         else {
207             return new ConstructedTermImpl(functionSymbol, terms);
208         }
209 
210     }
211 
212     /**
213      * @param number the number of the anonymous id
214      * @return a numbered anonymous id
215      * @see org.omwg.logexpression.LogicalExpressionFactory#createAnonymousID(byte)
216      */
217     public NumberedAnonymousID createAnonymousID(byte number) {
218         return new NumberedAnonymousIDImpl(number);
219     }
220 
221     /**
222      * @param expr the expression that is affected by the operator
223      * @return unary expression
224      * @throws in case the logical expression contains a nested CONSTRAINT
225      * @see org.omwg.logexpression.LogicalExpressionFactory#createNegation(LogicalExpression)
226      */
227     public Negation createNegation(LogicalExpression expr)
228             throws IllegalArgumentException {
229         return new NegationImpl(expr);
230     }
231 
232     /**
233      * @param expr the expression that is affected by the operator
234      * @return unary expression
235      * @throws in case the logical expression contains a nested CONSTRAINT
236      * @see org.omwg.logexpression.LogicalExpressionFactory#createNegationAsFailure(LogicalExpression)
237      */
238     public NegationAsFailure createNegationAsFailure(LogicalExpression expr)
239             throws IllegalArgumentException {
240         return new NegationAsFailureImpl(expr);
241     }
242 
243     /**
244      * @param expr the expression that is affected by the operator
245      * @return unary expression
246      * @throws in case the logical expression contains a nested CONSTRAINT
247      * @see org.omwg.logexpression.LogicalExpressionFactory#createConstraint(LogicalExpression)
248      */
249     public Constraint createConstraint(LogicalExpression expr)
250             throws IllegalArgumentException {
251         return new ConstraintImpl(expr);
252     }
253 
254     /**
255      * @param exprLeft the left expression that is conected by the operator
256      * @param exprRight the right expression that is conected by the operator
257      * @return binary expression
258      * @throws IllegalArgumentException
259      * <p>in case one of the two logical expressions is null
260      * <p>in case one of the two logical expressions contains a nested CONSTRAINT</p>
261      * @see org.omwg.logexpression.LogicalExpressionFactory#createConjunction(LogicalExpression, LogicalExpression)
262      */
263     public Conjunction createConjunction(LogicalExpression exprLeft, LogicalExpression exprRight)
264             throws IllegalArgumentException {
265         return new ConjunctionImpl(exprLeft, exprRight);
266     }
267 
268     /**
269      * @param exprLeft the left expression that is conected by the operator
270      * @param exprRight the right expression that is conected by the operator
271      * @return binary expression
272      * @throws IllegalArgumentException
273      * <p>in case one of the two logical expressions is null
274      * <p>in case one of the two logical expressions contains a nested CONSTRAINT</p>
275      * @see org.omwg.logexpression.LogicalExpressionFactory#createDisjunction(LogicalExpression, LogicalExpression)
276      */
277     public Disjunction createDisjunction(LogicalExpression exprLeft, LogicalExpression exprRight)
278             throws IllegalArgumentException {
279         return new DisjunctionImpl(exprLeft, exprRight);
280     }
281 
282     /**
283      * @param exprLeft the left expression that is conected by the operator
284      * @param exprRight the right expression that is conected by the operator
285      * @return binary expression
286      * @throws IllegalArgumentException
287      * <p>in case one of the two logical expressions is null
288      * <p>in case one of the two logical expressions contains a nested CONSTRAINT</p>
289      * @see org.omwg.logexpression.LogicalExpressionFactory#createImplication(LogicalExpression, LogicalExpression)
290      */
291     public Implication createImplication(LogicalExpression exprLeft, LogicalExpression exprRight)
292             throws IllegalArgumentException {
293         return new ImplicationImpl(exprLeft, exprRight);
294     }
295 
296     /**
297      * @param exprLeft the left expression that is conected by the operator
298      * @param exprRight the right expression that is conected by the operator
299      * @return binary expression
300      * @throws IllegalArgumentException
301      * <p>in case one of the two logical expressions is null
302      * <p>in case one of the two logical expressions contains a nested CONSTRAINT</p>
303      * @see org.omwg.logexpression.LogicalExpressionFactory#createEquivalence(LogicalExpression, LogicalExpression)
304      */
305     public Equivalence createEquivalence(LogicalExpression exprLeft, LogicalExpression exprRight)
306             throws IllegalArgumentException {
307         return new EquationImpl(exprLeft, exprRight);
308     }
309 
310     /**
311      * @param exprLeft the left expression that is conected by the operator
312      * @param exprRight the right expression that is conected by the operator
313      * @return binary expression
314      * @throws IllegalArgumentException
315      * <p>in case one of the two logical expressions is null
316      * <p>in case one of the two logical expressions contains a nested CONSTRAINT</p>
317              * @see org.omwg.logexpression.LogicalExpressionFactory#createLogicProgrammingRule(LogicalExpression, LogicalExpression)
318      */
319     public LogicProgrammingRule createLogicProgrammingRule(LogicalExpression exprLeft, LogicalExpression exprRight)
320             throws IllegalArgumentException {
321         return new LogicProgrammingRuleImpl(exprLeft, exprRight);
322     }
323 
324     /**
325      * @param exprLeft the left expression that is conected by the operator
326      * @param exprRight the right expression that is conected by the operator
327      * @return binary expression
328      * @throws IllegalArgumentException
329      * <p>in case one of the two logical expressions is null
330      * <p>in case one of the two logical expressions contains a nested CONSTRAINT</p>
331              * @see org.omwg.logexpression.LogicalExpressionFactory#createInverseImplication(LogicalExpression, LogicalExpression)
332      */
333     public InverseImplication createInverseImplication(LogicalExpression exprLeft, LogicalExpression exprRight)
334             throws IllegalArgumentException {
335         return new InverseImplicationImpl(exprLeft, exprRight);
336     }
337 
338     /**
339      * @param variables Set of variables that are quantified (e.g. "?a")
340      * @param expr the expression that is quantified
341      * @return a universally quantified expression
342      * @throws IllegalArgumentException
343      * <p>in case the logical expression contains a nested CONSTRAINT</p>
344      * <p>in case the Set of variables is null</p>
345      * <p>in case the arguments of the list aren't all of Type Variable</p>
346      * @see org.omwg.logexpression.LogicalExpressionFactory#createUniversalQuantification(Set, LogicalExpression)
347      */
348     public UniversalQuantification createUniversalQuantification(Set <Variable> variables, LogicalExpression expr)
349             throws IllegalArgumentException {
350         return new UniversalQuantificationImpl(variables, expr);
351     }
352 
353     /**
354      * @param variable variable that is quantified (e.g. "?a")
355      * @param expr the expression that is quantified
356      * @return a universally quantified expression
357      * @throws IllegalArgumentException
358      * <p>in case the logical expression contains a nested CONSTRAINT</p>
359      * <p>in case the Set of variables is null</p>
360      * <p>in case the arguments of the list aren't all of Type Variable</p>
361      * @see org.omwg.logexpression.LogicalExpressionFactory#createUniversalQuantification(Variable, LogicalExpression)
362      */
363     public UniversalQuantification createUniversalQuantification(Variable variable, LogicalExpression expr)
364             throws IllegalArgumentException {
365         return new UniversalQuantificationImpl(variable, expr);
366     }
367 
368     /**
369      * @param variables Set of variables that are quantified (e.g. "?a")
370      * @param expr the expression that is quantified
371      * @return a existentially quantified expression
372      * @throws IllegalArgumentException
373      * <p>in case the logical expression contains a nested CONSTRAINT</p>
374      * <p>in case the Set of variables is null</p>
375      * <p>in case the arguments of the list aren't all of Type Variable</p>
376      * @see org.omwg.logexpression.LogicalExpressionFactory#createExistentialQuantification(Set, LogicalExpression)
377      */
378     public ExistentialQuantification createExistentialQuantification(Set <Variable> variables, LogicalExpression expr)
379             throws IllegalArgumentException {
380         return new ExistentialQuantificationImpl(variables, expr);
381     }
382 
383     /**
384      * @param variable variable that is quantified (e.g. "?a")
385      * @param expr the expression that is quantified
386      * @return a existentially quantified expression
387      * @throws IllegalArgumentException
388      * <p>in case the logical expression contains a nested CONSTRAINT</p>
389      * <p>in case the Set of variables is null</p>
390      * <p>in case the arguments of the list aren't all of Type Variable</p>
391              * @see org.omwg.logexpression.LogicalExpressionFactory#createExistentialQuantification(Variable, LogicalExpression)
392      */
393     public ExistentialQuantification createExistentialQuantification(Variable variable, LogicalExpression expr)
394             throws IllegalArgumentException {
395         return new ExistentialQuantificationImpl(variable, expr);
396     }
397     
398     /**
399      * Creates a new Variable instance
400      * @param varName The name of the new variable
401      * @return A newly created variable
402      */
403     public Variable createVariable(String varName) {
404         if (varName == null
405                 || varName.trim().length() == 0) {
406             throw new IllegalArgumentException();
407         }
408         if (varName.startsWith("?")) {
409             varName = varName.substring(1);
410         }
411         return new VariableImpl(varName);
412     }
413 }