Added: incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/SubtractionAtom.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/SubtractionAtom.java?rev=1306351&view=auto ============================================================================== --- incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/SubtractionAtom.java (added) +++ incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/SubtractionAtom.java Wed Mar 28 14:09:58 2012 @@ -0,0 +1,102 @@ +/* + * 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. + */ +package org.apache.stanbol.rules.adapters.jena.atoms; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.stanbol.rules.adapters.AbstractAdaptableAtom; +import org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry; +import org.apache.stanbol.rules.adapters.jena.NodeClauseEntry; +import org.apache.stanbol.rules.base.api.RuleAtom; +import org.apache.stanbol.rules.base.api.RuleAtomCallExeption; +import org.apache.stanbol.rules.base.api.UnavailableRuleObjectException; +import org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException; +import org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom; + +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.reasoner.rulesys.BuiltinRegistry; +import com.hp.hpl.jena.reasoner.rulesys.ClauseEntry; +import com.hp.hpl.jena.reasoner.rulesys.Functor; +import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable; +import com.hp.hpl.jena.reasoner.rulesys.Rule; + +/** + * It adapts a SumAtom to the Jena difference functor. + * + * @author anuzzolese + * + */ +public class SubtractionAtom extends AbstractAdaptableAtom { + + @SuppressWarnings("unchecked") + @Override + public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, + UnavailableRuleObjectException, + UnsupportedTypeForExportException { + + String div_result = "subtraction_result" + System.currentTimeMillis(); + + Node arg1Node = null; + Node arg2Node = null; + Node arg3Node = Node_RuleVariable.createVariable(div_result); + + org.apache.stanbol.rules.manager.atoms.SubtractionAtom tmp = (org.apache.stanbol.rules.manager.atoms.SubtractionAtom) ruleAtom; + + NumericFunctionAtom numericFunctionAtom1 = tmp.getNumericFunctionAtom1(); + NumericFunctionAtom numericFunctionAtom2 = tmp.getNumericFunctionAtom2(); + + ClauseEntry clauseEntry1 = adapter.adaptTo(numericFunctionAtom1, Rule.class); + ClauseEntry clauseEntry2 = adapter.adaptTo(numericFunctionAtom2, Rule.class); + + List<ClauseEntry> clauseEntries = new ArrayList<ClauseEntry>(); + + if (clauseEntry1 instanceof HigherOrderClauseEntry) { + arg1Node = ((HigherOrderClauseEntry) clauseEntry1).getBindableNode(); + + clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry1).getClauseEntries()); + } else if (clauseEntry1 instanceof NodeClauseEntry) { + arg1Node = ((NodeClauseEntry) clauseEntry1).getNode(); + } else { + throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass()); + } + + if (clauseEntry2 instanceof HigherOrderClauseEntry) { + arg2Node = ((HigherOrderClauseEntry) clauseEntry2).getBindableNode(); + + clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry2).getClauseEntries()); + } else if (clauseEntry2 instanceof NodeClauseEntry) { + arg2Node = ((NodeClauseEntry) clauseEntry2).getNode(); + } else { + throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass()); + } + + java.util.List<Node> nodes = new ArrayList<Node>(); + + nodes.add(arg1Node); + nodes.add(arg2Node); + nodes.add(arg3Node); + + ClauseEntry clauseEntry = new Functor("difference", nodes, new BuiltinRegistry()); + + clauseEntries.add(clauseEntry); + + return (T) new HigherOrderClauseEntry(arg3Node, clauseEntries); + + } + +}
Added: incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/SumAtom.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/SumAtom.java?rev=1306351&view=auto ============================================================================== --- incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/SumAtom.java (added) +++ incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/SumAtom.java Wed Mar 28 14:09:58 2012 @@ -0,0 +1,102 @@ +/* + * 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. + */ +package org.apache.stanbol.rules.adapters.jena.atoms; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.stanbol.rules.adapters.AbstractAdaptableAtom; +import org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry; +import org.apache.stanbol.rules.adapters.jena.NodeClauseEntry; +import org.apache.stanbol.rules.base.api.RuleAtom; +import org.apache.stanbol.rules.base.api.RuleAtomCallExeption; +import org.apache.stanbol.rules.base.api.UnavailableRuleObjectException; +import org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException; +import org.apache.stanbol.rules.manager.atoms.NumericFunctionAtom; + +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.reasoner.rulesys.BuiltinRegistry; +import com.hp.hpl.jena.reasoner.rulesys.ClauseEntry; +import com.hp.hpl.jena.reasoner.rulesys.Functor; +import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable; +import com.hp.hpl.jena.reasoner.rulesys.Rule; + +/** + * It adapts a SumAtom to the Jena sum functor. + * + * @author anuzzolese + * + */ +public class SumAtom extends AbstractAdaptableAtom { + + @SuppressWarnings("unchecked") + @Override + public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, + UnavailableRuleObjectException, + UnsupportedTypeForExportException { + + String div_result = "sum_result" + System.currentTimeMillis(); + + Node arg1Node = null; + Node arg2Node = null; + Node arg3Node = Node_RuleVariable.createVariable(div_result); + + org.apache.stanbol.rules.manager.atoms.SumAtom tmp = (org.apache.stanbol.rules.manager.atoms.SumAtom) ruleAtom; + + NumericFunctionAtom numericFunctionAtom1 = tmp.getNumericFunctionAtom1(); + NumericFunctionAtom numericFunctionAtom2 = tmp.getNumericFunctionAtom2(); + + ClauseEntry clauseEntry1 = adapter.adaptTo(numericFunctionAtom1, Rule.class); + ClauseEntry clauseEntry2 = adapter.adaptTo(numericFunctionAtom2, Rule.class); + + List<ClauseEntry> clauseEntries = new ArrayList<ClauseEntry>(); + + if (clauseEntry1 instanceof HigherOrderClauseEntry) { + arg1Node = ((HigherOrderClauseEntry) clauseEntry1).getBindableNode(); + + clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry1).getClauseEntries()); + } else if (clauseEntry1 instanceof NodeClauseEntry) { + arg1Node = ((NodeClauseEntry) clauseEntry1).getNode(); + } else { + throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass()); + } + + if (clauseEntry2 instanceof HigherOrderClauseEntry) { + arg2Node = ((HigherOrderClauseEntry) clauseEntry2).getBindableNode(); + + clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry2).getClauseEntries()); + } else if (clauseEntry2 instanceof NodeClauseEntry) { + arg2Node = ((NodeClauseEntry) clauseEntry2).getNode(); + } else { + throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass()); + } + + java.util.List<Node> nodes = new ArrayList<Node>(); + + nodes.add(arg1Node); + nodes.add(arg2Node); + nodes.add(arg3Node); + + ClauseEntry clauseEntry = new Functor("sum", nodes, new BuiltinRegistry()); + + clauseEntries.add(clauseEntry); + + return (T) new HigherOrderClauseEntry(arg3Node, clauseEntries); + + } + +} Added: incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/TypedLiteralAtom.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/TypedLiteralAtom.java?rev=1306351&view=auto ============================================================================== --- incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/TypedLiteralAtom.java (added) +++ incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/TypedLiteralAtom.java Wed Mar 28 14:09:58 2012 @@ -0,0 +1,41 @@ +/* + * 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. + */ +package org.apache.stanbol.rules.adapters.jena.atoms; + +import org.apache.stanbol.rules.adapters.AbstractAdaptableAtom; +import org.apache.stanbol.rules.adapters.jena.NodeClauseEntry; +import org.apache.stanbol.rules.adapters.jena.NodeFactory; +import org.apache.stanbol.rules.base.api.RuleAtom; +import org.apache.stanbol.rules.base.api.RuleAtomCallExeption; + +/** + * It adapts a TypedLiteralAtom to a typed literal node in Jena. + * + * @author anuzzolese + * + */ +public class TypedLiteralAtom extends AbstractAdaptableAtom { + + @SuppressWarnings("unchecked") + @Override + public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption { + org.apache.stanbol.rules.manager.atoms.TypedLiteralAtom tmp = (org.apache.stanbol.rules.manager.atoms.TypedLiteralAtom) ruleAtom; + + return (T) new NodeClauseEntry(NodeFactory.getTypedLiteral(tmp)); + } + +} Added: incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UObjectAtom.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UObjectAtom.java?rev=1306351&view=auto ============================================================================== --- incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UObjectAtom.java (added) +++ incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UObjectAtom.java Wed Mar 28 14:09:58 2012 @@ -0,0 +1,36 @@ +/* + * 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. + */ +package org.apache.stanbol.rules.adapters.jena.atoms; + +import org.apache.stanbol.rules.adapters.AbstractAdaptableAtom; +import org.apache.stanbol.rules.base.api.RuleAtom; +import org.apache.stanbol.rules.base.api.RuleAtomCallExeption; + +/** + * TODO + * + * @author anuzzolese + * + */ +public class UObjectAtom extends AbstractAdaptableAtom { + + @Override + public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption { + throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass()); + } + +} Added: incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UnionAtom.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UnionAtom.java?rev=1306351&view=auto ============================================================================== --- incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UnionAtom.java (added) +++ incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UnionAtom.java Wed Mar 28 14:09:58 2012 @@ -0,0 +1,36 @@ +/* + * 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. + */ +package org.apache.stanbol.rules.adapters.jena.atoms; + +import org.apache.stanbol.rules.adapters.AbstractAdaptableAtom; +import org.apache.stanbol.rules.base.api.RuleAtom; +import org.apache.stanbol.rules.base.api.RuleAtomCallExeption; + +/** + * TODO + * + * @author anuzzolese + * + */ +public class UnionAtom extends AbstractAdaptableAtom { + + @Override + public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption { + throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass()); + } + +} Added: incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UpperCaseAtom.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UpperCaseAtom.java?rev=1306351&view=auto ============================================================================== --- incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UpperCaseAtom.java (added) +++ incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/UpperCaseAtom.java Wed Mar 28 14:09:58 2012 @@ -0,0 +1,36 @@ +/* + * 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. + */ +package org.apache.stanbol.rules.adapters.jena.atoms; + +import org.apache.stanbol.rules.adapters.AbstractAdaptableAtom; +import org.apache.stanbol.rules.base.api.RuleAtom; +import org.apache.stanbol.rules.base.api.RuleAtomCallExeption; + +/** + * TODO + * + * @author anuzzolese + * + */ +public class UpperCaseAtom extends AbstractAdaptableAtom { + + @Override + public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption { + throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass()); + } + +} Added: incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/VariableAtom.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/VariableAtom.java?rev=1306351&view=auto ============================================================================== --- incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/VariableAtom.java (added) +++ incubator/stanbol/trunk/rules/adapters/jena/src/main/java/org/apache/stanbol/rules/adapters/jena/atoms/VariableAtom.java Wed Mar 28 14:09:58 2012 @@ -0,0 +1,55 @@ +/* + * 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. + */ +package org.apache.stanbol.rules.adapters.jena.atoms; + +import java.net.URI; + +import org.apache.stanbol.rules.adapters.AbstractAdaptableAtom; +import org.apache.stanbol.rules.adapters.jena.NodeClauseEntry; +import org.apache.stanbol.rules.base.api.RuleAtom; +import org.apache.stanbol.rules.base.api.RuleAtomCallExeption; +import org.apache.stanbol.rules.base.api.Symbols; + +import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable; + +/** + * It adapts a VariableAtom to node variable in Jena. + * + * @author anuzzolese + * + */ +public class VariableAtom extends AbstractAdaptableAtom { + + @SuppressWarnings("unchecked") + @Override + public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption { + + org.apache.stanbol.rules.manager.atoms.VariableAtom tmp = (org.apache.stanbol.rules.manager.atoms.VariableAtom) ruleAtom; + + URI uri = tmp.getURI(); + + String variable = uri.toString(); + variable = variable.replace(Symbols.variablesPrefix, ""); + + if (variable.startsWith("?")) { + variable = variable.substring(1); + } + + return (T) new NodeClauseEntry(Node_RuleVariable.createVariable(variable)); + } + +} Added: incubator/stanbol/trunk/rules/adapters/jena/src/main/resources/OSGI-INF/metatype/metatype.properties URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/adapters/jena/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1306351&view=auto ============================================================================== --- incubator/stanbol/trunk/rules/adapters/jena/src/main/resources/OSGI-INF/metatype/metatype.properties (added) +++ incubator/stanbol/trunk/rules/adapters/jena/src/main/resources/OSGI-INF/metatype/metatype.properties Wed Mar 28 14:09:58 2012 @@ -0,0 +1,26 @@ +# 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. + +#=============================================================================== +#Properties defined by the Rule Store +#=============================================================================== +org.apache.stanbol.rules.manager.changes.RuleStoreImpl.name = Apache Stanbol Rule Store +org.apache.stanbol.rules.manager.changes.RuleStoreImpl.description = A storage facility for Apache Stanbol rules and recipes. + +org.apache.stanbol.rules.base.rule_namespace.name = Rule namespace +org.apache.stanbol.rules.base.rule_namespace.description = The default namespace of Stanbol rule instances in the rule ontology. + +org.apache.stanbol.rules.base.rule_ontology.name = Rule ontology location +org.apache.stanbol.rules.base.rule_ontology.description = The URL of the ontology containing the default rule set. Can be overridden programmatically. \ No newline at end of file Added: incubator/stanbol/trunk/rules/adapters/jena/src/test/java/org/apache/stanbol/rules/adapters/jena/JenaAdpterTest.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/adapters/jena/src/test/java/org/apache/stanbol/rules/adapters/jena/JenaAdpterTest.java?rev=1306351&view=auto ============================================================================== --- incubator/stanbol/trunk/rules/adapters/jena/src/test/java/org/apache/stanbol/rules/adapters/jena/JenaAdpterTest.java (added) +++ incubator/stanbol/trunk/rules/adapters/jena/src/test/java/org/apache/stanbol/rules/adapters/jena/JenaAdpterTest.java Wed Mar 28 14:09:58 2012 @@ -0,0 +1,167 @@ +/* + * 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. + */ + +package org.apache.stanbol.rules.adapters.jena; + +import java.util.List; + +import org.apache.clerezza.rdf.core.UriRef; +import org.apache.clerezza.rdf.core.sparql.query.ConstructQuery; +import org.apache.stanbol.rules.base.api.Recipe; +import org.apache.stanbol.rules.base.api.RuleAdapter; +import org.apache.stanbol.rules.base.api.RuleAtomCallExeption; +import org.apache.stanbol.rules.base.api.UnavailableRuleObjectException; +import org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException; +import org.apache.stanbol.rules.manager.KB; +import org.apache.stanbol.rules.manager.RecipeImpl; +import org.apache.stanbol.rules.manager.parse.RuleParserImpl; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.hp.hpl.jena.reasoner.rulesys.Rule; + +/** + * + * @author anuzzolese + * + */ +public class JenaAdpterTest { + + private static Logger log = LoggerFactory.getLogger(JenaAdpterTest.class); + + private Recipe recipeGood; + private Recipe recipeWrong; + private static RuleAdapter ruleAdapter; + + @BeforeClass + public static void setUpClass() { + ruleAdapter = new JenaAdapter(); + + } + + @AfterClass + public static void tearDownClass() { + ruleAdapter = null; + + } + + @Before + public void setUp() { + String separator = System.getProperty("line.separator"); + String recipeString = "kres = <http://kres.iks-project.eu/ontology.owl#> . " + + separator + + "foaf = <http://xmlns.com/foaf/0.1/> . " + + separator + + "rule1[ is(kres:Person, ?x) . has(kres:friend, ?x, ?y) -> is(foaf:Person, ?x) . has(foaf:knows, ?x, ?y) . is(foaf:Person, ?y)] . " + + "rule2[ is(kres:Person, ?x) . values(kres:age, ?x, ?age) . endsWith(?t, \"string\") . gt(?age, sum(sub(70, ?k), ?z)) -> is(kres:OldPerson, ?x)]"; + + KB kb = RuleParserImpl.parse("http://incubator.apache.com/stanbol/rules/adapters/jena/test/", + recipeString); + + recipeGood = new RecipeImpl( + new UriRef("http://incubator.apache.com/stanbol/rules/adapters/jena/test"), "A recipe.", + kb.getRuleList()); + + recipeString = "kres = <http://kres.iks-project.eu/ontology.owl#> . " + + separator + + "foaf = <http://xmlns.com/foaf/0.1/> . " + + separator + + "rule1[ is(kres:Person, ?x) . has(kres:friend, ?x, ?y) -> is(foaf:Person, ?x) . has(foaf:knows, ?x, ?y) . is(foaf:Person, ?y)] . " + + "rule2[ is(kres:Person, ?x) . same(\"Andrea\", localname(?x)) -> is(kres:OldPerson, ?x)]"; + + kb = RuleParserImpl.parse("http://incubator.apache.com/stanbol/rules/adapters/jena/test/", + recipeString); + + recipeWrong = new RecipeImpl(new UriRef( + "http://incubator.apache.com/stanbol/rules/adapters/jena/test"), "A recipe.", + kb.getRuleList()); + } + + @After + public void tearDown() { + recipeGood = null; + } + + @SuppressWarnings("unchecked") + @Test + public void test() { + try { + + List<Rule> rules = (List<Rule>) ruleAdapter.adaptTo(recipeGood, Rule.class); + + StringBuilder sb = new StringBuilder(); + for (Rule rule : rules) { + sb.append(rule.toString()); + } + + Assert.assertNotSame(sb.toString(), ""); + } catch (UnavailableRuleObjectException e) { + Assert.fail(e.getMessage()); + } catch (UnsupportedTypeForExportException e) { + Assert.fail(e.getMessage()); + } catch (RuleAtomCallExeption e) { + Assert.fail(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void wrongAdaptabeClassTest() { + try { + + List<ConstructQuery> constructQueries = (List<ConstructQuery>) ruleAdapter.adaptTo(recipeGood, + ConstructQuery.class); + for (ConstructQuery constructQuery : constructQueries) { + log.debug(constructQuery.toString()); + Assert.fail("The adapter for Jena should not accept " + + ConstructQuery.class.getCanonicalName() + " objects."); + } + } catch (UnavailableRuleObjectException e) { + Assert.fail(e.getMessage()); + } catch (UnsupportedTypeForExportException e) { + log.debug(e.getMessage()); + } catch (RuleAtomCallExeption e) { + Assert.fail(e.getMessage()); + } + + } + + @SuppressWarnings("unchecked") + @Test + public void unavailableRuleObjectTest() { + try { + + List<Rule> rules = (List<Rule>) ruleAdapter.adaptTo(recipeWrong, Rule.class); + for (Rule rule : rules) { + log.debug(rule.toString()); + } + } catch (UnavailableRuleObjectException e) { + Assert.fail(e.getMessage()); + } catch (UnsupportedTypeForExportException e) { + Assert.fail(e.getMessage()); + } catch (RuleAtomCallExeption e) { + Assert.assertTrue(e.getMessage(), true); + } + + } +}
