body.add(new DatavaluedPropertyAtom(property, x, z)); body.add(new DatavaluedPropertyAtom(property, y, z));
body.add(new DatavaluedPropertyAtom(property, x, z)); body.add(new DatavaluedPropertyAtom(property, y, z));
@Override public void visit(final SWRLDataPropertyAtom atom) { if (atom.getPredicate().isAnonymous()) { _swrlAtom = null; return; } atom.getFirstArgument().accept(this); final AtomIObject subj = _swrlIObject; atom.getSecondArgument().accept(this); final AtomDObject obj = _swrlDObject; atom.getPredicate().accept(this); _swrlAtom = new DatavaluedPropertyAtom(_term, subj, obj); }
@Override public void visit(final SWRLDataPropertyAtom atom) { if (atom.getPredicate().isAnonymous()) { _swrlAtom = null; return; } atom.getFirstArgument().accept(this); final AtomIObject subj = _swrlIObject; atom.getSecondArgument().accept(this); final AtomDObject obj = _swrlDObject; atom.getPredicate().accept(this); _swrlAtom = new DatavaluedPropertyAtom(_term, subj, obj); }
@Override public void visit(final SWRLDataPropertyAtom atom) { if (atom.getPredicate().isAnonymous()) { _swrlAtom = null; return; } atom.getFirstArgument().accept(this); final AtomIObject subj = _swrlIObject; atom.getSecondArgument().accept(this); final AtomDObject obj = _swrlDObject; atom.getPredicate().accept(this); _swrlAtom = new DatavaluedPropertyAtom(_term, subj, obj); }
@Test public void testDatavalueBindingGeneratorObjects() { final DatavaluedPropertyAtom pattern = new DatavaluedPropertyAtom(dp2, new AtomIConstant(mary), _z); final BindingHelper genHelper = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Collections.singletonList(genHelper)); assertIteratorValues(new BindingToMapIterator(gen.iterator()), new Object[] { Collections.singletonMap(_z, data1), Collections.singletonMap(_z, data2), }); }
@Test public void testDatavalueBindingGeneratorSubjects() { final DatavaluedPropertyAtom pattern = new DatavaluedPropertyAtom(dp2, _x, new AtomDConstant(data2)); final BindingHelper genHelper = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Collections.singletonList(genHelper)); assertIteratorValues(new BindingToMapIterator(gen.iterator()), new Object[] { Collections.singletonMap(_x, mary), Collections.singletonMap(_x, robert), }); }
private void nonTrivialBuiltInTest() { final ATermAppl d1 = literal("1", Datatypes.INT), d2 = literal("2", Datatypes.INT), d12 = literal("3", Datatypes.INTEGER), i = term("i"), p = term("p"), q = term("q"), r = term("r"); _kb.addDatatypeProperty(p); _kb.addDatatypeProperty(q); _kb.addDatatypeProperty(r); _kb.addIndividual(i); _kb.addSubClass(TOP, hasValue(p, d1)); _kb.addSubClass(TOP, hasValue(q, d2)); final AtomIVariable x = new AtomIVariable("x"); final AtomDVariable z1 = new AtomDVariable("z1"); final AtomDVariable z2 = new AtomDVariable("z2"); final AtomDVariable z3 = new AtomDVariable("z3"); final List<RuleAtom> body = new ArrayList<>(); body.add(new DatavaluedPropertyAtom(p, x, z1)); body.add(new DatavaluedPropertyAtom(q, x, z2)); body.add(new BuiltInAtom(SWRLB + "add", z3, z1, z2)); final List<RuleAtom> head = new ArrayList<>(); head.add(new DatavaluedPropertyAtom(r, x, z3)); _kb.addRule(new Rule(head, body)); _kb.realize(); assertTrue(_kb.hasPropertyValue(i, r, d12)); }
@Test public void testNoVarPropertyAtom() { classes(_A); dataProperties(_p, _q); individuals(_a, _b); _kb.addType(_a, _A); _kb.addPropertyValue(_p, _b, TermFactory.literal(true)); final ATermAppl t = TermFactory.literal("t"); final ATermAppl f = TermFactory.literal("f"); final AtomIVariable x = new AtomIVariable("x"); List<RuleAtom> body = Arrays.<RuleAtom> asList(new ClassAtom(_A, x), new DatavaluedPropertyAtom(_p, new AtomIConstant(_b), new AtomDConstant(TermFactory.literal(true)))); List<RuleAtom> head = Arrays.<RuleAtom> asList(new DatavaluedPropertyAtom(_q, x, new AtomDConstant(t))); _kb.addRule(new Rule(head, body)); body = Arrays.<RuleAtom> asList(new ClassAtom(_A, x), new DatavaluedPropertyAtom(_p, new AtomIConstant(_b), new AtomDConstant(TermFactory.literal(false)))); head = Arrays.<RuleAtom> asList(new DatavaluedPropertyAtom(_q, x, new AtomDConstant(f))); _kb.addRule(new Rule(head, body)); assertIteratorValues(_kb.getDataPropertyValues(_q, _a).iterator(), t); assertIteratorValues(_kb.getDataPropertyValues(_q, _b).iterator()); }
@Test public void testEmptyRuleHead4() { final ATermAppl i = term("i"); final ATermAppl R = term("R"); final ATermAppl l = literal("l"); final AtomIVariable v = new AtomIVariable("v"); final AtomDConstant c = new AtomDConstant(l); _kb.addIndividual(i); _kb.addDatatypeProperty(R); _kb.addPropertyValue(R, i, l); final List<RuleAtom> body = new ArrayList<>(); final List<RuleAtom> head = new ArrayList<>(); body.add(new DatavaluedPropertyAtom(R, v, c)); _kb.addRule(new Rule(head, body)); assertFalse(_kb.isConsistent()); }
@Test public void testDatavalueBindingGeneratorChainedSubject() { final DatavaluedPropertyAtom pattern1 = new DatavaluedPropertyAtom(dp2, _x, new AtomDConstant(data2)); final DatavaluedPropertyAtom pattern2 = new DatavaluedPropertyAtom(dp2, _y, new AtomDConstant(data2)); final BindingHelper genHelper1 = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern1); final BindingHelper genHelper2 = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern2); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Arrays.asList(new BindingHelper[] { genHelper1, genHelper2 })); final List<VariableBinding> expected = new LinkedList<>(); VariableBinding binding; final ATermAppl[] names = new ATermAppl[] { mary, robert }; for (final ATermAppl xName : names) for (final ATermAppl yName : names) { final Individual xNode = _kb.getABox().getIndividual(xName); final Individual yNode = _kb.getABox().getIndividual(yName); binding = new VariableBinding(_kb.getABox()); binding.set(_x, xNode); binding.set(_y, yNode); expected.add(binding); } assertIteratorValues(gen.iterator(), expected.iterator()); }
@Test public void testNoSharedVar() { classes(_A); dataProperties(_p, _q); objectProperties(_r); individuals(_a, _b, _c); _kb.addType(_a, _A); _kb.addPropertyValue(_p, _b, TermFactory.literal(true)); _kb.addPropertyValue(_r, _b, _c); final ATermAppl t = TermFactory.literal("t"); final ATermAppl f = TermFactory.literal("f"); final AtomIVariable x = new AtomIVariable("x"); final AtomDVariable y = new AtomDVariable("y"); List<RuleAtom> body = Arrays.<RuleAtom> asList(new ClassAtom(_A, x), new DatavaluedPropertyAtom(_p, new AtomIConstant(_b), y), new BuiltInAtom(SWRLB + "equal", y, new AtomDConstant(TermFactory.literal(true)))); List<RuleAtom> head = Arrays.<RuleAtom> asList(new DatavaluedPropertyAtom(_q, x, new AtomDConstant(t))); _kb.addRule(new Rule(head, body)); body = Arrays.<RuleAtom> asList(new ClassAtom(_A, x), new DatavaluedPropertyAtom(_p, new AtomIConstant(_b), y), new BuiltInAtom(SWRLB + "equal", y, new AtomDConstant(TermFactory.literal(false)))); head = Arrays.<RuleAtom> asList(new DatavaluedPropertyAtom(_q, x, new AtomDConstant(f))); _kb.addRule(new Rule(head, body)); assertIteratorValues(_kb.getDataPropertyValues(_q, _a).iterator(), t); assertIteratorValues(_kb.getDataPropertyValues(_q, _b).iterator()); }
@Test public void testNoSharedVarFixedObject() { classes(_A); dataProperties(_p, _q); objectProperties(_r, _s); individuals(_a, _b, _c, _d); _kb.addType(_a, _A); _kb.addPropertyValue(_r, _c, _b); _kb.addPropertyValue(_p, _c, TermFactory.literal(true)); _kb.addPropertyValue(_s, _d, _b); _kb.addPropertyValue(_p, _d, TermFactory.literal(false)); final ATermAppl t = TermFactory.literal("t"); final ATermAppl f = TermFactory.literal("f"); final AtomIVariable x = new AtomIVariable("x"); final AtomIVariable y = new AtomIVariable("y"); List<RuleAtom> body = Arrays.<RuleAtom> asList(new ClassAtom(_A, x), new IndividualPropertyAtom(_r, y, new AtomIConstant(_b)), new DatavaluedPropertyAtom(_p, y, new AtomDConstant(TermFactory.literal(true)))); List<RuleAtom> head = Arrays.<RuleAtom> asList(new DatavaluedPropertyAtom(_q, x, new AtomDConstant(t))); _kb.addRule(new Rule(head, body)); body = Arrays.<RuleAtom> asList(new ClassAtom(_A, x), new IndividualPropertyAtom(_r, y, new AtomIConstant(_b)), new DatavaluedPropertyAtom(_p, y, new AtomDConstant(TermFactory.literal(false)))); head = Arrays.<RuleAtom> asList(new DatavaluedPropertyAtom(_q, x, new AtomDConstant(f))); _kb.addRule(new Rule(head, body)); assertIteratorValues(_kb.getDataPropertyValues(_q, _a).iterator(), t); assertIteratorValues(_kb.getDataPropertyValues(_q, _b).iterator()); }
@Test public void testNoVarTypeAtom() { classes(_A, _B, _C); dataProperties(_q); individuals(_a, _b); _kb.addType(_a, _A); _kb.addType(_b, _B); final ATermAppl t = TermFactory.literal("t"); final ATermAppl f = TermFactory.literal("f"); final AtomIVariable x = new AtomIVariable("x"); List<RuleAtom> body = Arrays.<RuleAtom> asList(new ClassAtom(_A, x), new ClassAtom(_B, new AtomIConstant(_b))); List<RuleAtom> head = Arrays.<RuleAtom> asList(new DatavaluedPropertyAtom(_q, x, new AtomDConstant(t))); _kb.addRule(new Rule(head, body)); body = Arrays.<RuleAtom> asList(new ClassAtom(_A, x), new ClassAtom(_A, new AtomIConstant(_b))); head = Arrays.<RuleAtom> asList(new DatavaluedPropertyAtom(_q, x, new AtomDConstant(f))); _kb.addRule(new Rule(head, body)); body = Arrays.<RuleAtom> asList(new ClassAtom(_B, x)); head = Arrays.<RuleAtom> asList(new ClassAtom(_C, x)); _kb.addRule(new Rule(head, body)); assertIteratorValues(_kb.getDataPropertyValues(_q, _a).iterator(), t); assertIteratorValues(_kb.getDataPropertyValues(_q, _b).iterator()); assertIteratorValues(_kb.getInstances(_C).iterator(), _b); }
@Test public void testDataRangeBindingHelper() { final DatavaluedPropertyAtom pattern = new DatavaluedPropertyAtom(dp2, _x, _z); final DataRangeAtom atom = new DataRangeAtom(ATermUtils.makeTermAppl(Namespaces.XSD + "integer"), _z); final BindingHelper patternHelper = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern); final BindingHelper rangeHelper = new DataRangeBindingHelper(_kb.getABox(), atom); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Arrays.asList(new BindingHelper[] { patternHelper, rangeHelper, })); final VariableBinding expectedBinding = new VariableBinding(_kb.getABox()); expectedBinding.set(_x, _kb.getABox().getIndividual(victor)); expectedBinding.set(_z, _kb.getABox().getLiteral(data4)); final List<VariableBinding> expected = new LinkedList<>(); expected.add(expectedBinding); assertIteratorValues(gen.iterator(), expected.iterator()); }
@Test public void testDatavalueBindingGeneratorChained() { final DatavaluedPropertyAtom pattern1 = new DatavaluedPropertyAtom(dp2, _x, _z); final DatavaluedPropertyAtom pattern2 = new DatavaluedPropertyAtom(dp2, _y, _z); final BindingHelper genHelper1 = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern1); final BindingHelper genHelper2 = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern2); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Arrays.asList(new BindingHelper[] { genHelper1, genHelper2 })); final List<VariableBinding> expected = new LinkedList<>(); VariableBinding binding; final ATermAppl[] names = new ATermAppl[] { mary, robert, victor }; final ATermAppl[] values = new ATermAppl[] { data1, data2, data3, data4 }; for (final ATermAppl xName : names) for (final ATermAppl yName : names) for (final ATermAppl zValue : values) { final Individual xNode = _kb.getABox().getIndividual(xName); final Individual yNode = _kb.getABox().getIndividual(yName); final Literal zNode = _kb.getABox().addLiteral(zValue); if (_kb.hasPropertyValue(xName, dp2, zValue) && _kb.hasPropertyValue(yName, dp2, zValue)) { binding = new VariableBinding(_kb.getABox()); binding.set(_x, xNode); binding.set(_y, yNode); binding.set(_z, zNode); expected.add(binding); } } assertIteratorValues(gen.iterator(), expected.iterator()); }
/** * Simple property chain test. Mostly tests the rete engine */ @Test public void dataPropertyChain1() { final ATermAppl d = literal("d"), i = term("i"), j = term("j"), k = term("k"), p = term("p"), r = term("r"); _kb.addDatatypeProperty(p); _kb.addObjectProperty(r); _kb.addIndividual(i); _kb.addIndividual(j); _kb.addIndividual(k); _kb.addPropertyValue(p, i, d); _kb.addPropertyValue(r, i, j); _kb.addPropertyValue(r, j, k); final AtomIVariable x = new AtomIVariable("x"), y = new AtomIVariable("y"); final AtomDVariable z = new AtomDVariable("z"); final RuleAtom body1 = new IndividualPropertyAtom(r, x, y); final RuleAtom body2 = new DatavaluedPropertyAtom(p, x, z), head = new DatavaluedPropertyAtom(p, y, z); final Rule rule = new Rule(Collections.singletonList(head), Arrays.asList(new RuleAtom[] { body1, body2 })); _kb.addRule(rule); _kb.realize(); assertTrue(_kb.hasPropertyValue(j, p, d)); assertTrue(_kb.hasPropertyValue(k, p, d)); }
/** * More complicated property chain test. Tests the rule _strategy */ @Test public void dataPropertyChain2() { final ATermAppl d = literal("d"), i = term("i"), j = term("j"), k = term("k"), p = term("p"), r = term("r"); _kb.addDatatypeProperty(p); _kb.addObjectProperty(r); _kb.addIndividual(i); _kb.addIndividual(j); _kb.addIndividual(k); _kb.addSubClass(TOP, oneOf(i, j, k)); _kb.addSubClass(TOP, min(r, 3, TOP)); _kb.addPropertyValue(p, i, d); final AtomIVariable x = new AtomIVariable("x"), y = new AtomIVariable("y"); final AtomDVariable z = new AtomDVariable("z"); final RuleAtom body1 = new IndividualPropertyAtom(r, x, y); final RuleAtom body2 = new DatavaluedPropertyAtom(p, x, z); final RuleAtom head = new DatavaluedPropertyAtom(p, y, z); final Rule rule = new Rule(Collections.singletonList(head), Arrays.asList(body1, body2)); _kb.addRule(rule); _kb.realize(); assertTrue(_kb.hasPropertyValue(j, p, d)); assertTrue(_kb.hasPropertyValue(k, p, d)); }
@Test public void testBindingBuiltins() { classes(_A); dataProperties(_p, _q); individuals(_a, _b, _c, _d); _kb.addPropertyValue(_p, _a, TermFactory.literal(1)); _kb.addPropertyValue(_p, _b, TermFactory.literal(5)); _kb.addPropertyValue(_p, _c, TermFactory.literal(10)); _kb.addPropertyValue(_p, _d, TermFactory.literal(15)); final AtomIVariable x = new AtomIVariable("x"); final AtomDVariable y = new AtomDVariable("y"); final AtomDVariable z = new AtomDVariable("z"); final List<RuleAtom> body = Arrays.<RuleAtom> asList(new DatavaluedPropertyAtom(_p, x, y), new BuiltInAtom(SWRLB + "pow", z, y, new AtomDConstant(TermFactory.literal(2))), new BuiltInAtom(SWRLB + "lessThan", z, new AtomDConstant(TermFactory.literal(100)))); final List<RuleAtom> head = Arrays.<RuleAtom> asList(new ClassAtom(_A, x)); _kb.addRule(new Rule(head, body)); assertIteratorValues(_kb.getInstances(_A).iterator(), _a, _b); }
@SuppressWarnings("unused") @Test public void inferredProperties() throws Exception { final ATermAppl d = literal("foo"), i = term("i"), j = term("j"), k = term("k"), p = term("p"), r = term("r"); _kb.addIndividual(i); _kb.addIndividual(j); _kb.addIndividual(k); _kb.addDatatypeProperty(p); _kb.addObjectProperty(r); _kb.addSubClass(TOP, min(r, 3, TOP)); _kb.addSubClass(TOP, or(value(i), value(j), value(k))); _kb.addPropertyValue(p, i, d); final AtomIVariable x = new AtomIVariable("x"), y = new AtomIVariable("y"); final AtomDVariable z = new AtomDVariable("z"); final RuleAtom head = new DatavaluedPropertyAtom(p, x, z); final RuleAtom body1 = new DatavaluedPropertyAtom(p, y, z); final RuleAtom body2 = new IndividualPropertyAtom(r, x, y); final Rule rule = new Rule(Collections.singletonList(head), Arrays.asList(body1, body2)); _kb.addRule(rule); _kb.ensureConsistency(); assertTrue(_kb.hasPropertyValue(j, r, i)); assertTrue(_kb.hasPropertyValue(k, r, i)); assertTrue(_kb.hasPropertyValue(j, p, d)); assertTrue(_kb.hasPropertyValue(k, p, d)); }