public void visit(SWRLDataRangeAtom atom) { if (atom.getArgument() instanceof SWRLVariable) throwVarError(atom); // dr(literal) :- // convert to: ClassAssertion(DataSomeValuesFrom(freshDP DataOneOf(literal)) freshIndividual) // and top -> \forall freshDP.dr OWLLiteral lit=((SWRLLiteralArgument)atom.getArgument()).getLiteral(); OWLDataRange dr=atom.getPredicate(); OWLNamedIndividual freshIndividual=getFreshIndividual(); OWLDataProperty freshDP=getFreshDataProperty(); OWLDataSomeValuesFrom some=m_factory.getOWLDataSomeValuesFrom(freshDP,m_factory.getOWLDataOneOf(lit)); OWLClassExpression definition=getDefinitionFor(some,m_alreadyExists); if (!m_alreadyExists[0]) m_newInclusions.add(new OWLClassExpression[] { negative(definition),some }); addFact(m_factory.getOWLClassAssertionAxiom(definition,freshIndividual)); m_newInclusions.add(new OWLClassExpression[] { m_factory.getOWLDataAllValuesFrom(freshDP,dr) }); }
public void visit(SWRLDataRangeAtom at) { OWLDataRange dr=at.getPredicate(); SWRLDArgument argument=at.getArgument(); if (!(argument instanceof SWRLVariable)) throw new IllegalArgumentException("A SWRL rule contains a data range with an argument that is not a literal, and such rules are not supported."); if (!m_isPositive) dr=m_factory.getOWLDataComplementOf(dr); dr=m_expressionManager.getNNF(m_expressionManager.getSimplified(dr)); if (dr instanceof OWLDataIntersectionOf || dr instanceof OWLDataUnionOf) { OWLDatatype definition=getDefinitionFor(dr,m_alreadyExists); if (!m_alreadyExists[0]) m_dataRangeInclusions.add(new OWLDataRange[] { negative(definition),dr }); dr=definition; } SWRLAtom atom=m_factory.getSWRLDataRangeAtom(dr,argument); m_normalizedHeadAtoms.add(atom); m_headDataRangeVariables.add((SWRLVariable)argument); }
public void visit(SWRLObjectPropertyAtom at) { OWLObjectPropertyExpression ope=at.getPredicate().getSimplified(); OWLObjectProperty op=ope.getNamedProperty(); SWRLVariable variable1; SWRLVariable variable2; if (ope.isAnonymous()) { variable1=getVariableFor(at.getSecondArgument()); variable2=getVariableFor(at.getFirstArgument()); } else { variable1=getVariableFor(at.getFirstArgument()); variable2=getVariableFor(at.getSecondArgument()); } SWRLAtom newAtom=m_factory.getSWRLObjectPropertyAtom(op,variable1,variable2); if (m_isPositive) { // head m_normalizedHeadAtoms.add(newAtom); } else { // body m_normalizedBodyAtoms.add(newAtom); } }
@Nonnull @Override public Set<SWRLVariable> getVariables() { Set<SWRLVariable> toReturn = null; if (variables != null) { toReturn = new HashSet<>(variables); } if (toReturn != null) { return toReturn; } SWRLVariableExtractor extractor = new SWRLVariableExtractor(); accept(extractor); toReturn = extractor.getVariables(); variables = toReturn; return toReturn; }
public void visit(SWRLDataPropertyAtom at) { OWLDataProperty dp=at.getPredicate().asOWLDataProperty(); SWRLVariable variable1=getVariableFor(at.getFirstArgument()); SWRLDArgument argument2=at.getSecondArgument(); if (argument2 instanceof SWRLVariable) { SWRLVariable variable2=getVariableFor((SWRLVariable)argument2); if (m_isPositive) { m_normalizedHeadAtoms.add(m_factory.getSWRLDataPropertyAtom(dp,variable1,variable2)); m_headDataRangeVariables.add(variable2); } else { if (m_bodyDataRangeVariables.add(variable2)) m_normalizedBodyAtoms.add(m_factory.getSWRLDataPropertyAtom(dp,variable1,variable2)); else { SWRLVariable variable2Fresh=getFreshVariable(); m_normalizedBodyAtoms.add(m_factory.getSWRLDataPropertyAtom(dp,variable1,variable2Fresh)); m_normalizedHeadAtoms.add(m_factory.getSWRLDifferentIndividualsAtom(variable2,variable2Fresh)); } } } else { OWLLiteral literal=((SWRLLiteralArgument)argument2).getLiteral(); SWRLAtom newAtom=m_factory.getSWRLClassAtom(m_factory.getOWLDataHasValue(dp,literal),variable1); if (m_isPositive) m_headAtoms.add(newAtom); else m_bodyAtoms.add(newAtom); } }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof SWRLVariable)) { return false; } SWRLVariable other = (SWRLVariable) obj; return other.getIRI().equals(getIRI()); }
public void visit(SWRLVariable variable) { }
public void visit(SWRLRule rule) { // Process head one-by-one and thus break up the conjunction in the head. for (SWRLAtom headAtom : rule.getHead()) { m_individualsToVariables.clear(); m_bodyAtoms.clear(); m_headAtoms.clear(); m_variableRepresentative.clear(); m_normalizedBodyAtoms.clear(); m_normalizedHeadAtoms.clear(); m_bodyDataRangeVariables.clear(); m_headDataRangeVariables.clear(); // Initialize body with all atoms, and initialize head with just the atom we are processing. m_bodyAtoms.addAll(rule.getBody()); m_headAtoms.add(headAtom); // First process sameIndividual in the body to set up variable normalizations. for (SWRLAtom atom : rule.getBody()) { if (atom instanceof SWRLSameIndividualAtom) { m_bodyAtoms.remove(atom); SWRLSameIndividualAtom sameIndividualAtom=(SWRLSameIndividualAtom)atom; SWRLVariable variable1=getVariableFor(sameIndividualAtom.getFirstArgument()); SWRLIArgument argument2=sameIndividualAtom.getSecondArgument(); if (argument2 instanceof SWRLVariable) m_variableRepresentative.put((SWRLVariable)argument2,variable1); else { OWLIndividual individual=((SWRLIndividualArgument)argument2).getIndividual(); if (individual.isAnonymous()) throw new IllegalArgumentException("Internal error: Rules with anonymous individuals are not supported. "); m_individualsToVariables.put(individual.asOWLNamedIndividual(),variable1); m_bodyAtoms.add(m_factory.getSWRLClassAtom(m_factory.getOWLObjectOneOf(individual),variable1)); } } } // Now process head atoms; this might increase the number of body atoms. m_isPositive=true; while (!m_headAtoms.isEmpty()) m_headAtoms.remove(0).accept(this); // Now process body atoms. m_isPositive=false; while (!m_bodyAtoms.isEmpty()) m_bodyAtoms.remove(0).accept(this); // Do some checking and return the rule. if (!m_bodyDataRangeVariables.containsAll(m_headDataRangeVariables)) throw new IllegalArgumentException("A SWRL rule contains data range variables in the head, but not in the body, and this is not supported."); m_rules.add(new OWLAxioms.DisjunctiveRule(m_normalizedBodyAtoms.toArray(new SWRLAtom[m_normalizedBodyAtoms.size()]),m_normalizedHeadAtoms.toArray(new SWRLAtom[m_normalizedHeadAtoms.size()]))); } }
public void visit(SWRLVariable variable) { // nothing to do }
protected SWRLVariable getFreshVariable() { SWRLVariable variable=m_factory.getSWRLVariable(IRI.create("internal:swrl#"+m_newVariableIndex)); m_newVariableIndex++; return variable; }
public Atom visit(SWRLVariable node) { throw new IllegalStateException("Internal error: this part of the code is unused."); }
protected static Variable toVariable(SWRLIArgument argument) { if (argument instanceof SWRLVariable) return Variable.create(((SWRLVariable)argument).getIRI().toString()); else throw new IllegalStateException("Internal error: all arguments in a SWRL rule should have been normalized to variables."); }
protected static Variable toVariable(SWRLDArgument argument) { if (argument instanceof SWRLVariable) return Variable.create(((SWRLVariable)argument).getIRI().toString()); else throw new IllegalStateException("Internal error: all arguments in a SWRL rule should have been normalized to variables."); }
@Override public O visit(SWRLVariable node) { return doDefault(node); }
@Override public void visit(SWRLVariable node) { handleDefault(node); }
@Override public void visit(SWRLVariable node) { type = RULE_OBJECT_TYPE_INDEX_BASE + 6; }
/** @return the set of variables */ @Nonnull public LinkedHashSet<SWRLVariable> getVariables() { return variables; }
@Override public void visit(SWRLVariable node) { variables.add(node); }
@Override public void visit(SWRLVariable node) { hashCode = primes[67]; hashCode = hashCode * MULT + node.getIRI().hashCode(); }
@Override public SWRLVariable visit(SWRLVariable node) { return node; }
@Override protected int compareObjectOfSameType(@Nonnull OWLObject object) { return iri.compareTo(((SWRLVariable) object).getIRI()); }
@Override protected void writeObject(SWRLVariable object, BinaryOWLOutputStream outputStream) throws IOException { outputStream.writeIRI(object.getIRI()); }