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); } }
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); } }
protected void checkRuleAtom(SWRLAtom atom) { if (atom instanceof SWRLObjectPropertyAtom) { m_containsObjectProperties=true; OWLObjectProperty objectProperty=((SWRLObjectPropertyAtom)atom).getPredicate().getNamedProperty(); boolean isGraphObjectProperty=m_graphObjectProperties.contains(objectProperty); boolean isNonGraphObjectProperty=m_objectPropertiesOccurringInOWLAxioms.contains(objectProperty); if (isGraphObjectProperty) m_containsGraphObjectProperties=true; if (isNonGraphObjectProperty) m_containsNonGraphObjectProperties=true; if (!isGraphObjectProperty && !isNonGraphObjectProperty) m_containsUndeterminedObjectProperties=true; } }
/** * @param body * rule body * @param head * rule head * @param annotations * annotations on the axiom */ public SWRLRuleImpl(@Nonnull Set<? extends SWRLAtom> body, @Nonnull Set<? extends SWRLAtom> head, @Nonnull Collection<? extends OWLAnnotation> annotations) { super(annotations); this.head = new LinkedHashSet<>(checkNotNull(head, "head cannot be null")); this.body = new LinkedHashSet<>(checkNotNull(body, "body cannot be null")); containsAnonymousClassExpressions = hasAnon(); }
protected void throwAnonIndError(SWRLAtom atom) { throw new IllegalArgumentException("A SWRL rule contains a fact ("+atom+") with an anonymous individual, which is not allowed. "); }
protected void throwVarError(SWRLAtom atom) { throw new IllegalArgumentException("A SWRL rule contains a head atom ("+atom+") with a variable that does not occur in the body. "); }
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 DisjunctiveRule(SWRLAtom[] body,SWRLAtom[] head) { m_body=body; m_head=head; }
protected void makeGraphObjectProperty(SWRLAtom atom) { if (atom instanceof SWRLObjectPropertyAtom) { OWLObjectProperty objectProperty=((SWRLObjectPropertyAtom)atom).getPredicate().getNamedProperty(); m_graphObjectProperties.add(objectProperty); } }
protected void makeNonGraphObjectProperty(SWRLAtom atom) { if (atom instanceof SWRLObjectPropertyAtom) { OWLObjectProperty objectProperty=((SWRLObjectPropertyAtom)atom).getPredicate().getNamedProperty(); m_objectPropertiesOccurringInOWLAxioms.add(objectProperty); } }
public void visit(SWRLObjectPropertyAtom at) { OWLObjectPropertyExpression ope=at.getPredicate().getSimplified(); OWLObjectProperty op=ope.getNamedProperty(); SWRLIArgument argument1; SWRLIArgument argument2; if (ope.isAnonymous()) { argument1=at.getSecondArgument(); argument2=at.getFirstArgument(); } else { argument1=at.getFirstArgument(); argument2=at.getSecondArgument(); } SWRLAtom newAtom=m_factory.getSWRLObjectPropertyAtom(op,argument1,argument2); /* we omitted the rule normalization from HermiT 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); } }
@Override public Set<SWRLAtom> getBody() { return CollectionFactory .getCopyOnRequestSetFromImmutableCollection(body); }
@Override public Set<SWRLAtom> getHead() { return CollectionFactory .getCopyOnRequestSetFromImmutableCollection(head); }
@Override protected SWRLRule readAxiom(BinaryOWLInputStream inputStream, Set<OWLAnnotation> annotations) throws IOException, BinaryOWLParseException { Set<SWRLAtom> body = inputStream.readOWLObjects(); Set<SWRLAtom> head = inputStream.readOWLObjects(); return inputStream.getDataFactory().getSWRLRule(body, head, annotations); }
/** * @param body * rule body * @param head * rule head */ public SWRLRuleImpl(@Nonnull Set<? extends SWRLAtom> body, @Nonnull Set<? extends SWRLAtom> head) { this(body, head, NO_ANNOTATIONS); }