Set<RuleAtom> head = new LinkedHashSet<RuleAtom>();
Set<RuleAtom> body = new LinkedHashSet<RuleAtom>();
for( RuleAtom atom : rule.getHead() ) {
if( atom instanceof ClassAtom ) {
ClassAtom ca = (ClassAtom) atom;
AtomIObject arg = ca.getArgument();
ATermAppl c = ca.getPredicate();
ATermAppl normC = ATermUtils.normalize( c );
if( c != normC )
atom = new ClassAtom( normC, arg );
}
head.add( atom );
}
Map<AtomIObject, Set<ATermAppl>> types = new HashMap<AtomIObject, Set<ATermAppl>>();
for( RuleAtom atom : rule.getBody() ) {
if( atom instanceof IndividualPropertyAtom ) {
IndividualPropertyAtom propAtom = (IndividualPropertyAtom) atom;
ATermAppl prop = propAtom.getPredicate();
AtomIObject subj = propAtom.getArgument1();
if( subj instanceof AtomIVariable ) {
Set<ATermAppl> domains = getRole( prop ).getDomains();
if( domains != null )
MultiMapUtils.addAll( types, subj, domains );
}
AtomIObject obj = propAtom.getArgument2();
if( obj instanceof AtomIVariable ) {
Set<ATermAppl> ranges = getRole( prop ).getRanges();
if( ranges != null )
MultiMapUtils.addAll( types, obj, ranges );
}
}
}
for( RuleAtom atom : rule.getBody() ) {
if( atom instanceof ClassAtom ) {
ClassAtom ca = (ClassAtom) atom;
AtomIObject arg = ca.getArgument();
ATermAppl c = ca.getPredicate();
ATermAppl normC = ATermUtils.normalize( c );
if( MultiMapUtils.contains( types, arg, normC ) )
continue;
else if( c != normC )
atom = new ClassAtom( normC, ca.getArgument() );
}
body.add( atom );
}
return new Rule( rule.getName(), head, body );