1 19 20 package com.hp.hpl.jena.reasoner.dig; 23 24 25 26 import java.util.*; 29 30 import com.hp.hpl.jena.datatypes.*; 31 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; 32 import com.hp.hpl.jena.graph.Graph; 33 import com.hp.hpl.jena.ontology.*; 34 import com.hp.hpl.jena.rdf.model.*; 35 import com.hp.hpl.jena.reasoner.TriplePattern; 36 import com.hp.hpl.jena.util.iterator.*; 37 import com.hp.hpl.jena.util.iterator.ExtendedIterator; 38 import com.hp.hpl.jena.util.xml.SimpleXMLPath; 39 import com.hp.hpl.jena.vocabulary.*; 40 41 import org.apache.commons.logging.LogFactory; 42 import org.w3c.dom.*; 43 44 45 54 public class DIGAdapter 55 { 56 59 60 public static final DIGProfile RACER_17_PROFILE = new DIGProfile() { 61 public String getDIGNamespace() {return "http://dl.kr.org/dig/lang"; } 62 public String getSchemaLocation() {return "http://potato.cs.man.ac.uk/dig/level0/dig.xsd"; } 63 public String getContentType() {return "application/x-www-form-urlencoded";} 64 }; 65 66 private static final int UNION = 1; 68 private static final int INTERSECTION = 2; 69 private static final int COMPLEMENT = 3; 70 private static final int ENUMERATED = 4; 71 private static final int RESTRICTION = 5; 72 73 74 public static final String ANON_MARKER = "anon:"; 75 76 77 public static final List KNOWN_CONCEPTS = Arrays.asList( new Object [] {OWL.Thing.getURI(), OWL.Nothing.getURI(), 78 DAML_OIL.Thing.getURI(), DAML_OIL.Thing.getURI() } ); 79 80 81 public static final List XSD_INT_TYPES = Arrays.asList( new Object [] { 82 XSDDatatype.XSDint.getURI(), 83 XSDDatatype.XSDinteger.getURI(), 84 XSDDatatype.XSDnonNegativeInteger.getURI(), 85 XSDDatatype.XSDbyte.getURI(), 86 XSDDatatype.XSDshort.getURI(), 87 XSDDatatype.XSDlong.getURI(), 88 XSDDatatype.XSDunsignedByte.getURI(), 89 XSDDatatype.XSDunsignedLong.getURI(), 90 XSDDatatype.XSDunsignedInt.getURI(), 91 XSDDatatype.XSDunsignedShort.getURI(), 92 } ); 93 94 95 98 99 private static int s_queryID = 0; 100 101 102 protected static DIGQueryTranslator[] s_queryTable = { 103 new DIGQuerySubsumesTranslator( RDFS.subClassOf.getURI() ), 105 new DIGQuerySubsumesTranslator( DAML_OIL.subClassOf.getURI() ), 106 107 new DIGQueryDisjointTranslator( OWL.disjointWith.getURI() ), 109 new DIGQueryDisjointTranslator( DAML_OIL.disjointWith.getURI() ), 110 111 new DIGQueryAncestorsTranslator( RDFS.subClassOf.getURI(), true ), 113 new DIGQueryAncestorsTranslator( RDFS.subClassOf.getURI(), false ), 114 new DIGQueryAncestorsTranslator( DAML_OIL.subClassOf.getURI(), true ), 115 new DIGQueryAncestorsTranslator( DAML_OIL.subClassOf.getURI(), false ), 116 117 new DIGQueryParentsTranslator( ReasonerVocabulary.directSubClassOf.getURI(), true ), 119 new DIGQueryParentsTranslator( ReasonerVocabulary.directSubClassOf.getURI(), false ), 120 121 new DIGQueryParentsTranslator( null, ReasonerVocabulary.directSubClassOf.getURI(), null, true ), 123 new DIGQueryParentsTranslator( null, ReasonerVocabulary.directSubClassOf.getURI(), null, false ), 124 125 new DIGQueryClassHierarchyTranslator( RDFS.subClassOf.getURI() ), 127 new DIGQueryClassHierarchyTranslator( DAML_OIL.subClassOf.getURI() ), 128 129 new DIGQueryEquivalentsTranslator( OWL.equivalentClass.getURI(), true ), 131 new DIGQueryEquivalentsTranslator( OWL.equivalentClass.getURI(), false ), 132 new DIGQueryEquivalentsTranslator( DAML_OIL.sameClassAs.getURI(), true ), 133 new DIGQueryEquivalentsTranslator( DAML_OIL.sameClassAs.getURI(), false ), 134 135 new DIGQueryIsEquivalentTranslator( OWL.equivalentClass.getURI() ), 136 new DIGQueryIsEquivalentTranslator( DAML_OIL.sameClassAs.getURI() ), 137 138 new DIGQueryRoleAncestorsTranslator( RDFS.subPropertyOf.getURI(), true ), 140 new DIGQueryRoleAncestorsTranslator( RDFS.subPropertyOf.getURI(), false ), 141 new DIGQueryRoleAncestorsTranslator( DAML_OIL.subPropertyOf.getURI(), true ), 142 new DIGQueryRoleAncestorsTranslator( DAML_OIL.subPropertyOf.getURI(), false ), 143 144 new DIGQueryRoleParentsTranslator( ReasonerVocabulary.directSubPropertyOf.getURI(), true ), 145 new DIGQueryRoleParentsTranslator( ReasonerVocabulary.directSubPropertyOf.getURI(), false ), 146 147 new DIGQueryRoleHierarchyTranslator( RDFS.subPropertyOf.getURI() ), 149 new DIGQueryRoleHierarchyTranslator( DAML_OIL.subPropertyOf.getURI() ), 150 151 new DIGQueryAllConceptsTranslator( RDF.type.getURI(), RDFS.Class.getURI() ), 153 new DIGQueryAllConceptsTranslator( RDF.type.getURI(), OWL.Class.getURI() ), 154 new DIGQueryAllConceptsTranslator( RDF.type.getURI(), DAML_OIL.Class.getURI() ), 155 156 new DIGQueryInstancesTranslator( RDF.type.getURI() ), 158 new DIGQueryInstancesTranslator( DAML_OIL.type.getURI() ), 159 new DIGQueryTypesTranslator( RDF.type.getURI() ), 160 new DIGQueryTypesTranslator( DAML_OIL.type.getURI() ), 161 new DIGQueryInstanceTranslator( RDF.type.getURI() ), 162 new DIGQueryInstanceTranslator( DAML_OIL.type.getURI() ), 163 new DIGQueryDifferentFromTranslator( OWL.differentFrom.getURI() ), 164 new DIGQueryDifferentFromTranslator( DAML_OIL.differentIndividualFrom.getURI() ), 165 new DIGQueryRoleFillersTranslator(), 166 new DIGQueryRoleFillerTranslator(), 167 168 new DIGQueryIsConceptTranslator(), 170 new DIGQueryIsRoleTranslator(), 171 new DIGQueryIsIndividualTranslator(), 172 }; 173 174 175 178 179 protected DIGProfile m_profile = RACER_17_PROFILE; 180 181 182 protected OntModel m_sourceData; 183 184 185 private int m_skolemCounter = 0; 186 187 188 private DIGConnection m_connection; 189 190 191 protected Set m_indNames = new HashSet(); 192 193 194 protected boolean m_indNamesAsked = false; 195 196 197 protected Set m_conceptNames = new HashSet(); 198 199 200 protected boolean m_conceptNamesAsked = false; 201 202 203 protected Set m_roleNames = new HashSet(); 204 205 206 protected boolean m_roleNamesAsked = false; 207 208 209 protected Model m_axioms = null; 210 211 212 215 223 public DIGAdapter( OntModelSpec spec, Graph source ) { 224 this( spec, source, DIGConnectionPool.getInstance().allocate(), null ); 225 } 226 227 228 239 public DIGAdapter( OntModelSpec spec, Graph source, DIGConnection connection, Model axioms ) { 240 m_connection = connection; 241 m_axioms = axioms; 242 243 m_sourceData = ModelFactory.createOntologyModel( spec, ModelFactory.createModelForGraph( source ) ); 245 246 m_sourceData.setStrictMode( false ); 248 } 249 250 251 254 258 public DIGProfile getProfile() { 259 return m_profile; 260 } 261 262 263 268 public void setProfile( DIGProfile profile ) { 269 m_profile = profile; 270 } 271 272 273 277 public Profile getOntLanguage() { 278 return m_sourceData.getProfile(); 279 } 280 281 282 287 public DIGIdentifier getDigIdentifier() { 288 Document getIDVerb = getConnection().createDigVerb( DIGProfile.GET_IDENTIFIER, getProfile() ); 289 return new DigIdentifierImpl( getConnection().sendDigVerb( getIDVerb, getProfile() ) ); 290 } 291 292 293 301 public boolean uploadKB() { 302 getConnection().bindKB( false, getProfile() ); 304 305 Document kbDIG = translateKbToDig(); 307 308 Document response = getConnection().sendDigVerb( kbDIG, getProfile() ); 309 return !getConnection().warningCheck( response ); 310 } 311 312 313 314 319 public Iterator getRecentWarnings() { 320 return getConnection().getWarnings(); 321 } 322 323 324 329 public Document translateKbToDig() { 330 Document tell = getConnection().createDigVerb( DIGProfile.TELLS, getProfile() ); 331 Element root = tell.getDocumentElement(); 332 333 addNamedEntities( root ); 334 translateClasses( root ); 335 translateRoles( root ); 336 translateAttributes( root ); 337 translateIndividuals( root ); 338 translateAllDifferentAxioms( root ); 339 340 return tell; 341 } 342 343 344 347 public void resetKB() { 348 getConnection().bindKB( true, getProfile() ); 349 350 m_indNames.clear(); 352 m_indNamesAsked = false; 353 m_conceptNames.clear(); 354 m_conceptNamesAsked = false; 355 m_roleNames.clear(); 356 m_roleNamesAsked = false; 357 } 358 359 360 365 public DIGConnection getConnection() { 366 return m_connection; 367 } 368 369 370 373 public void close() { 374 getConnection().release(); 375 m_connection = null; 376 } 377 378 379 388 public ExtendedIterator find( TriplePattern pattern ) { 389 DIGQueryTranslator tr = getQueryTranslator( pattern, null ); 390 391 ExtendedIterator remote = (tr == null) ? null : tr.find( pattern, this ); 392 393 com.hp.hpl.jena.graph.Node pSubj = normaliseNode( pattern.getSubject() ); 394 com.hp.hpl.jena.graph.Node pPred = normaliseNode( pattern.getPredicate() ); 395 com.hp.hpl.jena.graph.Node pObj = normaliseNode( pattern.getObject() ); 396 ExtendedIterator local = m_sourceData.getGraph().find( pSubj, pPred, pObj ); 397 398 ExtendedIterator i = (remote == null) ? local : remote.andThen( local ); 400 401 i = (m_axioms == null) ? i : i.andThen( m_axioms.getGraph().find( pSubj, pPred, pObj ) ); 403 404 return UniqueExtendedIterator.create( i ); 406 } 407 408 409 422 public ExtendedIterator find( TriplePattern pattern, Model premises ) { 423 DIGQueryTranslator tr = getQueryTranslator( pattern, premises ); 424 425 if (tr == null) { 426 LogFactory.getLog( getClass() ).debug( "Could not find DIG query translator for " + pattern ); 427 } 428 429 ExtendedIterator remote = (tr == null) ? null : tr.find( pattern, this, premises ); 430 431 com.hp.hpl.jena.graph.Node pSubj = normaliseNode( pattern.getSubject() ); 432 com.hp.hpl.jena.graph.Node pPred = normaliseNode( pattern.getPredicate() ); 433 com.hp.hpl.jena.graph.Node pObj = normaliseNode( pattern.getObject() ); 434 ExtendedIterator local = m_sourceData.getGraph().find( pSubj, pPred, pObj ); 435 436 ExtendedIterator i = (remote == null) ? local : remote.andThen( local ); 438 439 i = (m_axioms == null) ? i : i.andThen( m_axioms.getGraph().find( pSubj, pPred, pObj ) ); 441 442 return UniqueExtendedIterator.create( i ); 444 } 445 446 447 454 public DIGQueryTranslator getQueryTranslator( TriplePattern pattern, Model premises ) { 455 for (int i = 0; i < s_queryTable.length; i++) { 456 DIGQueryTranslator dqt = s_queryTable[i]; 457 458 if (s_queryTable[i].trigger( pattern, this, premises )) { 459 return s_queryTable[i]; 460 } 461 } 462 463 return null; 464 } 465 466 467 471 public Graph getGraph() { 472 return m_sourceData.getGraph(); 473 } 474 475 476 482 public String getResourceID( Resource r ) { 483 return getNodeID( r.asNode() ); 484 } 485 486 487 493 public String getNodeID( com.hp.hpl.jena.graph.Node n ) { 494 if (n.isBlank()) { 495 return ANON_MARKER + n.getBlankNodeId().toString(); 496 } 497 else { 498 return n.getURI(); 499 } 500 } 501 502 503 513 public void addClassDescription( Element elem, com.hp.hpl.jena.graph.Node node ) { 514 addClassDescription( elem, node, m_sourceData ); 515 } 516 517 518 528 public void addClassDescription( Element elem, com.hp.hpl.jena.graph.Node node, Model sourceData ) { 529 Model m = (sourceData == null) ? m_sourceData : sourceData; 530 addClassDescription( elem, (Resource) m.getRDFNode( node ), m ); 531 } 532 533 534 543 public void addClassDescription( Element elem, Resource res, Model sourceData ) { 544 Resource cls = (res.getModel() != sourceData) ? sourceData.getResource( res.getURI() ) : res; 546 547 if (!cls.isAnon() || m_conceptNames.contains( getNodeID( cls.getNode() ))) { 548 translateClassIdentifier( elem, cls ); 550 } 551 else { 552 translateClassDescription( elem, (OntClass) cls.as( OntClass.class ), sourceData ); 554 } 555 } 556 557 558 564 public boolean isIndividual( com.hp.hpl.jena.graph.Node node ) { 565 return node.isConcrete() && !node.isLiteral() && getKnownIndividuals().contains( getNodeID( node ) ); 566 } 567 568 569 577 public boolean isRole( com.hp.hpl.jena.graph.Node node, Model premises ) { 578 return node.isConcrete() && 579 (getKnownRoles().contains( getNodeID( node ) ) || 580 ((premises != null) && 581 isPremisesRole( node, premises )) ); 582 } 583 584 585 593 public boolean isConcept( com.hp.hpl.jena.graph.Node node, Model premises ) { 594 return node.isConcrete() && !node.isLiteral() && 595 (getKnownConcepts().contains( getNodeID( node ) ) || 596 ((premises != null) && isPremisesClass( node, premises )) || 597 KNOWN_CONCEPTS.contains( getNodeID( node ) )); 598 } 599 600 601 606 public OntModelSpec getSourceSpecification() { 607 return m_sourceData.getSpecification(); 608 } 609 610 611 618 public Element createQueryElement( Document query, String elemName ) { 619 Element qElem = addElement( query.getDocumentElement(), elemName ); 620 qElem.setAttribute( DIGProfile.ID, "q" + s_queryID++ ); 621 return qElem; 622 } 623 624 625 628 629 636 protected void addNamedEntities( Element tell ) { 637 HashSet roles = new HashSet(); 639 HashSet attributes = new HashSet(); 640 HashSet concepts = new HashSet(); 641 HashSet individuals = new HashSet(); 642 643 addAll( m_sourceData.listClasses(), concepts ); 644 addAll( m_sourceData.listDatatypeProperties(), attributes ); 645 addAll( m_sourceData.listIndividuals(), individuals ); 646 647 collectRoleProperties( roles ); 648 649 addNamedDefs( tell, concepts.iterator(), DIGProfile.DEFCONCEPT, m_conceptNames ); 651 addNamedDefs( tell, roles.iterator(), DIGProfile.DEFROLE, m_roleNames ); 652 addNamedDefs( tell, attributes.iterator(), DIGProfile.DEFATTRIBUTE, null); 653 addNamedDefs( tell, individuals.iterator(), DIGProfile.DEFINDIVIDUAL, m_indNames ); 654 } 655 656 657 658 protected void collectRoleProperties( Collection roles ) { 659 addAll( m_sourceData.listObjectProperties(), roles ); 660 addAll( m_sourceData.listInverseFunctionalProperties(), roles ); 661 addAll( m_sourceData.listTransitiveProperties(), roles ); 662 663 if (m_sourceData.getProfile().SYMMETRIC_PROPERTY() != null) { 665 addAll( m_sourceData.listSymmetricProperties(), roles ); 666 } 667 } 668 669 670 677 protected void addNamedDefs( Element tell, Iterator i, String defType, Set nameCollection ) { 678 while (i.hasNext()) { 679 RDFNode n = (Resource) i.next(); 680 if (n instanceof Resource) { 681 String id = getNodeID( n.asNode() ); 682 addNamedElement( tell, defType, getNodeID( n.asNode() ) ); 683 684 if (nameCollection != null) { 686 nameCollection.add( id ); 687 } 688 } 689 } 690 } 691 692 693 701 protected Element addNamedElement( Element parent, String elemName, String uri ) { 702 Element elem = addElement( parent, elemName ); 703 elem.setAttribute( DIGProfile.NAME, uri ); 704 705 return elem; 706 } 707 708 709 710 protected Element addElement( Element parent, String childName ) { 711 Element child = parent.getOwnerDocument().createElement( childName ); 712 return (Element) parent.appendChild( child ); 713 } 714 715 716 717 private void addAll( Iterator i, Collection c ) { 718 for (; i.hasNext(); c.add( i.next() ) ); 719 } 720 721 722 729 protected void translateClasses( Element tell ) { 730 translateSubClassAxioms( tell ); 731 translateClassEquivalences( tell ); 732 translateClassDisjointAxioms( tell ); 733 734 translateRestrictions( tell ); 735 736 translateClassExpressions( tell, getOntLanguage().INTERSECTION_OF(), INTERSECTION ); 738 translateClassExpressions( tell, getOntLanguage().UNION_OF(), UNION ); 739 translateClassExpressions( tell, getOntLanguage().COMPLEMENT_OF(), COMPLEMENT ); 740 translateClassExpressions( tell, getOntLanguage().ONE_OF(), ENUMERATED ); 741 } 742 743 748 protected void translateSubClassAxioms( Element tell ) { 749 StmtIterator i = m_sourceData.listStatements( null, getOntLanguage().SUB_CLASS_OF(), (RDFNode) null ); 750 while (i.hasNext()) { 751 Statement sc = i.nextStatement(); 752 Element impliesc = addElement( tell, DIGProfile.IMPLIESC ); 753 addClassDescription( impliesc, sc.getSubject(), m_sourceData ); 754 addClassDescription( impliesc, sc.getResource(), m_sourceData ); 755 } 756 } 757 758 759 764 protected void translateClassEquivalences( Element tell ) { 765 StmtIterator i = m_sourceData.listStatements( null, getOntLanguage().EQUIVALENT_CLASS(), (RDFNode) null ); 767 while (i.hasNext()) { 768 Statement sc = i.nextStatement(); 769 Element equalc = addElement( tell, DIGProfile.EQUALC ); 770 addClassDescription( equalc, sc.getSubject(), m_sourceData ); 771 addClassDescription( equalc, sc.getResource(), m_sourceData ); 772 } 773 } 774 775 776 784 protected void translateClassExpressions( Element tell, Property p, int classExprType ) { 785 translateClassExpressions( tell, m_sourceData.listStatements( null, p, (RDFNode) null ), classExprType, m_sourceData ); 786 } 787 788 789 793 protected void translateRestrictions( Element tell ) { 794 translateClassExpressions( tell, 795 m_sourceData.listStatements( null, RDF.type, getOntLanguage().RESTRICTION() ), 796 RESTRICTION, m_sourceData ); 797 } 798 799 800 807 protected void translateClassExpressions( Element tell, StmtIterator i, int classExprType, Model source ) { 808 while (i.hasNext()) { 809 OntClass cls = (OntClass) i.nextStatement().getSubject().as( OntClass.class ); 810 811 Element equalc = addElement( tell, DIGProfile.EQUALC ); 812 addClassDescription( equalc, cls, source ); 813 814 switch (classExprType) { 815 case UNION: translateUnionClass( equalc, cls, source ); break; 816 case INTERSECTION: translateIntersectionClass( equalc, cls, source ); break; 817 case COMPLEMENT: translateComplementClass( equalc, cls, source ); break; 818 case ENUMERATED: translateEnumeratedClass( equalc, cls, source ); break; 819 case RESTRICTION: translateRestrictionClass( equalc, cls, source ); break; 820 } 821 } 822 } 823 824 825 831 protected void translateClassDescription( Element parent, OntClass classDescr, Model source ) { 832 if (classDescr.isUnionClass()) { 833 translateUnionClass( parent, classDescr, source ); 834 } 835 else if (classDescr.isIntersectionClass()) { 836 translateIntersectionClass( parent, classDescr, source ); 837 } 838 else if (classDescr.isComplementClass()) { 839 translateComplementClass( parent, classDescr, source ); 840 } 841 else if (classDescr.isEnumeratedClass()) { 842 translateEnumeratedClass( parent, classDescr, source ); 843 } 844 else if (classDescr.isRestriction()) { 845 translateRestrictionClass( parent, classDescr, source ); 846 } 847 } 848 849 850 856 protected void translateClassDisjointAxioms( Element tell ) { 857 StmtIterator i = m_sourceData.listStatements( null, getOntLanguage().DISJOINT_WITH(), (RDFNode) null ); 858 while (i.hasNext()) { 859 Statement sc = i.nextStatement(); 860 Element impliesc = addElement( tell, DIGProfile.DISJOINT ); 861 addClassDescription( impliesc, sc.getSubject(), m_sourceData ); 862 addClassDescription( impliesc, sc.getResource(), m_sourceData ); 863 } 864 } 865 866 867 873 protected void translateClassIdentifier( Element expr, Resource c ) { 874 if (c.equals( getOntLanguage().THING())) { 875 addElement( expr, DIGProfile.TOP ); 877 return; 878 } 879 else if (c.equals( getOntLanguage().NOTHING())) { 880 addElement( expr, DIGProfile.BOTTOM ); 882 return; 883 } 884 else { 885 Element catom = addElement( expr, DIGProfile.CATOM ); 887 String digConceptName = getNodeID( c.asNode() ); 888 catom.setAttribute( DIGProfile.NAME, digConceptName ); 889 } 890 } 891 892 893 899 protected void translateRestrictionClass( Element expr, Resource c, Model source ) { 900 Restriction r = (Restriction) c.as( Restriction.class ); 901 902 if (r.isAllValuesFromRestriction()) { 903 Element all = addElement( expr, DIGProfile.ALL ); 905 addNamedElement( all, DIGProfile.RATOM, r.getOnProperty().getURI() ); 906 addClassDescription( all, r.asAllValuesFromRestriction().getAllValuesFrom(), source ); 907 } 908 else if (r.isSomeValuesFromRestriction()) { 909 Element some = addElement( expr, DIGProfile.SOME ); 911 addNamedElement( some, DIGProfile.RATOM, r.getOnProperty().getURI() ); 912 addClassDescription( some, r.asSomeValuesFromRestriction().getSomeValuesFrom(), source ); 913 } 914 else if (r.isHasValueRestriction()) { 915 translateHasValueRestriction( expr, r.asHasValueRestriction() ); 917 } 918 else if (r.isMinCardinalityRestriction()) { 919 translateCardinalityRestriction( expr, r.asMinCardinalityRestriction().getMinCardinality(), r, 921 DIGProfile.ATLEAST, getOntLanguage().THING(), source ); 922 } 923 else if (r.isMaxCardinalityRestriction()) { 924 translateCardinalityRestriction( expr, r.asMaxCardinalityRestriction().getMaxCardinality(), r, 926 DIGProfile.ATMOST, getOntLanguage().THING(), source ); 927 } 928 else if (r.isCardinalityRestriction()) { 929 Element and = addElement( expr, DIGProfile.AND ); 931 932 translateCardinalityRestriction( and, r.asCardinalityRestriction().getCardinality(), r, 934 DIGProfile.ATMOST, getOntLanguage().THING(), source ); 935 translateCardinalityRestriction( and, r.asCardinalityRestriction().getCardinality(), r, 936 DIGProfile.ATLEAST, getOntLanguage().THING(), source ); 937 } 938 } 940 941 942 943 protected void translateEnumeratedClass(Element expr, OntClass cls, Model source ) { 944 Element iset = addElement( expr, DIGProfile.ISET ); 946 for (Iterator i = cls.asEnumeratedClass().listOneOf(); i.hasNext(); ) { 947 RDFNode n = (RDFNode) i.next(); 948 949 if (n instanceof Resource) { 950 addNamedElement( iset, DIGProfile.INDIVIDUAL, ((Resource) n).getURI() ); 951 } 952 else { 953 LogFactory.getLog( getClass() ).warn( "DIG language cannot yet represent enumerations of concrete literals: " + ((Literal) n).getLexicalForm() ); 954 } 956 } 957 } 958 959 960 961 protected void translateComplementClass(Element expr, OntClass cls, Model source ) { 962 Element not = addElement( expr, DIGProfile.NOT ); 964 addClassDescription( not, cls.asComplementClass().getOperand(), source ); 965 } 966 967 968 969 protected void translateIntersectionClass(Element expr, OntClass cls, Model source) { 970 Element or = addElement( expr, DIGProfile.AND ); 972 translateClassList( or, cls.asIntersectionClass().getOperands(), source ); 973 } 974 975 976 977 protected void translateUnionClass(Element expr, OntClass cls, Model source) { 978 Element or = addElement( expr, DIGProfile.OR ); 980 translateClassList( or, cls.asUnionClass().getOperands(), source ); 981 } 982 983 984 992 private void translateCardinalityRestriction( Element parent, int card, Restriction r, String exprName, Resource qualType, Model source ) { 993 Element restrict = addElement( parent, exprName ); 994 restrict.setAttribute( DIGProfile.NUM, Integer.toString( card ) ); 995 addNamedElement( restrict, DIGProfile.RATOM, r.getOnProperty().getURI() ); 996 addClassDescription( restrict, qualType, source ); 997 } 998 999 1000 1009 protected void translateHasValueRestriction( Element expr, HasValueRestriction r ) { 1010 RDFNode value = r.getHasValue(); 1011 Property p = r.getOnProperty(); 1012 1013 if (value instanceof Literal) { 1015 Literal lit = (Literal) value; 1017 boolean intDomain = isIntegerType( lit.getDatatype() ); 1018 1019 Element eq = addElement( expr, (intDomain ? DIGProfile.INTEQUALS : DIGProfile.STRINGEQUALS ) ); 1021 eq.setAttribute( DIGProfile.VAL, lit.getLexicalForm() ); 1022 1023 addNamedElement( eq, DIGProfile.ATTRIBUTE, p.getURI() ); 1024 } 1025 else { 1026 Element some = addElement( expr, DIGProfile.SOME ); 1028 addNamedElement( some, DIGProfile.RATOM, p.getURI() ); 1029 1030 Element iset = addElement( some, DIGProfile.ISET ); 1032 addNamedElement( iset, DIGProfile.INDIVIDUAL, ((Resource) value).getURI() ); 1033 } 1034 } 1035 1036 1039 protected void translateClassList( Element expr, RDFList operands, Model source ) { 1040 for (Iterator i = operands.iterator(); i.hasNext(); ) { 1041 addClassDescription( expr, (Resource) i.next(), source ); 1042 } 1043 } 1044 1045 1046 1047 protected void translateIndividuals( Element expr ) { 1048 for (Iterator i = m_sourceData.listIndividuals(); i.hasNext(); ) { 1049 translateIndividual( expr, (Resource) i.next() ); 1050 } 1051 } 1052 1053 1054 protected void translateIndividual( Element expr, Resource r ) { 1055 Individual ind = (Individual) r.as( Individual.class ); 1056 translateInstanceTypes( expr, ind ); 1057 1058 for (StmtIterator i = ind.listProperties(); i.hasNext(); ) { 1059 Statement s = i.nextStatement(); 1060 OntProperty p = (OntProperty) s.getPredicate().as( OntProperty.class ); 1061 1062 if (p.equals( getOntLanguage().DIFFERENT_FROM())) { 1063 translateDifferentIndividuals( expr, ind, (Individual) s.getResource().as( Individual.class ) ); 1064 } 1065 else if (p.equals( getOntLanguage().SAME_AS())) { 1066 translateSameIndividuals( expr, ind, (Individual) s.getResource().as( Individual.class ) ); 1067 } 1068 else if (p.isObjectProperty() || 1069 p.isTransitiveProperty() || 1070 p.isSymmetricProperty() || 1071 p.isInverseFunctionalProperty()) { 1072 translateInstanceRole( expr, ind, p, (Individual) s.getResource().as( Individual.class ) ); 1073 } 1074 else if (p.isDatatypeProperty()) { 1075 translateInstanceAttrib( expr, ind, p, s.getLiteral() ); 1076 } 1077 } 1078 } 1079 1080 1081 1082 protected void translateInstanceTypes( Element expr, Individual ind ) { 1083 for (Iterator i = ind.listRDFTypes( true ); i.hasNext(); ) { 1084 Resource type = (Resource) i.next(); 1085 Element inst = addElement( expr, DIGProfile.INSTANCEOF ); 1086 addNamedElement( inst, DIGProfile.INDIVIDUAL, getResourceID( ind ) ); 1087 addClassDescription( inst, (OntClass) type.as( OntClass.class ), m_sourceData ); 1088 } 1089 } 1090 1091 1092 1093 protected void translateInstanceRole( Element expr, Individual ind, OntProperty p, Individual obj) { 1094 Element related = addElement( expr, DIGProfile.RELATED ); 1095 addNamedElement( related, DIGProfile.INDIVIDUAL, getResourceID( ind ) ); 1096 addNamedElement( related, DIGProfile.RATOM, p.getURI() ); 1097 addNamedElement( related, DIGProfile.INDIVIDUAL, getResourceID( obj ) ); 1098 } 1099 1100 1101 1102 protected void translateInstanceAttrib( Element expr, Individual ind, OntProperty p, Literal obj ) { 1103 Element related = addElement( expr, DIGProfile.VALUE ); 1104 addNamedElement( related, DIGProfile.INDIVIDUAL, getResourceID( ind ) ); 1105 addNamedElement( related, DIGProfile.ATTRIBUTE, p.getURI() ); 1106 1107 translateLiteral( obj, related); 1108 } 1109 1110 1111 protected void translateLiteral( Literal lit, Element parent ) { 1112 if (isIntegerType( lit.getDatatype() )) { 1113 Element ival = addElement( parent, DIGProfile.IVAL ); 1114 ival.appendChild( parent.getOwnerDocument().createTextNode( lit.getLexicalForm() ) ); 1115 } 1116 else { 1117 Element sval = addElement( parent, DIGProfile.SVAL ); 1118 sval.appendChild( parent.getOwnerDocument().createTextNode( lit.getLexicalForm() ) ); 1119 } 1120 } 1121 1122 1123 1124 protected void translateDifferentIndividuals( Element expr, Individual ind, Individual other ) { 1125 Element disjoint = addElement( expr, DIGProfile.DISJOINT ); 1126 Element iset0 = addElement( disjoint, DIGProfile.ISET ); 1127 addNamedElement( iset0, DIGProfile.INDIVIDUAL, getResourceID( ind ) ); 1128 Element iset1 = addElement( disjoint, DIGProfile.ISET ); 1129 addNamedElement( iset1, DIGProfile.INDIVIDUAL, getResourceID( other ) ); 1130 } 1131 1132 1133 1134 protected void translateSameIndividuals( Element expr, Individual ind, Individual other ) { 1135 Element disjoint = addElement( expr, DIGProfile.EQUALC ); 1136 Element iset0 = addElement( disjoint, DIGProfile.ISET ); 1137 addNamedElement( iset0, DIGProfile.INDIVIDUAL, getResourceID( ind ) ); 1138 Element iset1 = addElement( disjoint, DIGProfile.ISET ); 1139 addNamedElement( iset1, DIGProfile.INDIVIDUAL, getResourceID( other ) ); 1140 } 1141 1142 1143 1144 protected void translateRoles( Element expr ) { 1145 Set roles = new HashSet(); 1146 collectRoleProperties( roles ); 1147 1148 for (Iterator i = roles.iterator(); i.hasNext(); ) { 1149 translateRole( expr, (ObjectProperty) ((Property) i.next()).as( ObjectProperty.class ), m_sourceData ); 1150 } 1151 } 1152 1153 1154 protected void translateRole( Element expr, ObjectProperty role, Model source ) { 1155 translateBinaryPropertyAxioms( expr, role.getURI(), DIGProfile.IMPLIESR, role.listSuperProperties(), DIGProfile.RATOM ); 1156 translateBinaryPropertyAxioms( expr, role.getURI(), DIGProfile.EQUALR, role.listEquivalentProperties(), DIGProfile.RATOM ); 1157 translateDomainRangeAxioms( expr, role.getURI(), DIGProfile.DOMAIN, role.listDomain(), DIGProfile.RATOM, source ); 1158 translateDomainRangeAxioms( expr, role.getURI(), DIGProfile.RANGE, role.listRange(), DIGProfile.RATOM, source ); 1159 translateInverseAxioms( expr, role, DIGProfile.RATOM ); 1160 1161 if (role.isTransitiveProperty()) { 1162 translateUnaryPropertyAxiom( expr, role.getURI(), DIGProfile.TRANSITIVE, DIGProfile.RATOM ); 1163 } 1164 if (role.isFunctionalProperty()) { 1165 translateUnaryPropertyAxiom( expr, role.getURI(), DIGProfile.FUNCTIONAL, DIGProfile.RATOM ); 1166 } 1167 if (role.isInverseFunctionalProperty()) { 1168 translateInverseFunctionalAxiom( expr, role, DIGProfile.RATOM ); 1169 } 1170 if (role.isSymmetricProperty()) { 1171 translateInverseAxiom( expr, role, DIGProfile.RATOM, role ); 1172 } 1173 } 1174 1175 1176 protected void translateAttributes( Element expr ) { 1177 for (Iterator i = m_sourceData.listDatatypeProperties(); i.hasNext(); ) { 1178 translateAttribute( expr, (DatatypeProperty) ((Property) i.next()).as( DatatypeProperty.class ), m_sourceData ); 1179 } 1180 } 1181 1182 1183 protected void translateAttribute( Element expr, DatatypeProperty attrib, Model source ) { 1184 translateBinaryPropertyAxioms( expr, attrib.getURI(), DIGProfile.IMPLIESR, attrib.listSuperProperties(), DIGProfile.ATTRIBUTE); 1185 translateBinaryPropertyAxioms( expr, attrib.getURI(), DIGProfile.EQUALR, attrib.listEquivalentProperties(), DIGProfile.ATTRIBUTE ); 1186 translateDomainRangeAxioms( expr, attrib.getURI(), DIGProfile.DOMAIN, attrib.listDomain(), DIGProfile.ATTRIBUTE, source ); 1187 translateAttribRangeAxioms( expr, attrib.getURI(), attrib.listRange(), DIGProfile.ATTRIBUTE ); 1188 1189 if (attrib.isFunctionalProperty()) { 1190 translateUnaryPropertyAxiom( expr, attrib.getURI(), DIGProfile.FUNCTIONAL, DIGProfile.ATTRIBUTE ); 1191 } 1192 } 1193 1194 1195 protected void translateBinaryPropertyAxioms( Element expr, String propURI, String axiomType, Iterator i, String propType ) { 1196 while (i.hasNext()) { 1197 Property prop = (Property) i.next(); 1198 Element binaryAxiom = addElement( expr, axiomType ); 1199 addNamedElement( binaryAxiom, propType, propURI ); 1200 addNamedElement( binaryAxiom, propType, prop.getURI() ); 1201 } 1202 } 1203 1204 1205 protected void translateUnaryPropertyAxiom( Element expr, String propURI, String axiomType, String propType ) { 1206 Element unaryAxiom = addElement( expr, axiomType ); 1207 addNamedElement( unaryAxiom, propType, propURI ); 1208 } 1209 1210 1211 protected void translateDomainRangeAxioms( Element expr, String propURI, String axiomType, Iterator i, String propType, Model source ) { 1212 while (i.hasNext()) { 1213 Element drAxiom = addElement( expr, axiomType ); 1214 addNamedElement( drAxiom, propType, propURI ); 1215 addClassDescription( drAxiom, (Resource) i.next(), source ); 1216 } 1217 } 1218 1219 1220 protected void translateAttribRangeAxioms( Element expr, String propURI, Iterator i, String propType ) { 1221 while (i.hasNext()) { 1222 Resource type = (Resource) i.next(); 1223 RDFDatatype dt = TypeMapper.getInstance().getTypeByName( type.getURI() ); 1224 1225 Element drAxiom = addElement( expr, isIntegerType( dt ) ? DIGProfile.RANGEINT : DIGProfile.RANGESTRING ); 1226 addNamedElement( drAxiom, propType, propURI ); 1227 } 1228 } 1229 1230 1231 protected void translateInverseAxioms( Element expr, ObjectProperty p, String propType ) { 1232 for (Iterator i = p.listInverse(); i.hasNext(); ) { 1233 translateInverseAxiom(expr, p, propType, (Property) i.next() ); 1234 } 1235 } 1236 1237 1238 protected void translateInverseAxiom( Element expr, Property p, String propType, Property inv ) { 1239 Element equalr = addElement( expr, DIGProfile.EQUALR ); 1240 addNamedElement( equalr, propType, p.getURI() ); 1241 Element inverse = addElement( equalr, DIGProfile.INVERSE ); 1242 addNamedElement( inverse, propType, inv.getURI() ); 1243 } 1244 1245 1246 1247 protected void translateInverseFunctionalAxiom( Element expr, ObjectProperty role, String propType ) { 1248 String skolemName = getSkolemName( role.getLocalName() ); 1250 1251 addNamedElement( expr, DIGProfile.DEFROLE, skolemName ); 1253 Element functional = addElement( expr, DIGProfile.FUNCTIONAL ); 1254 addNamedElement( functional, propType, skolemName ); 1255 1256 Element equalr = addElement( expr, DIGProfile.EQUALR ); 1258 addNamedElement( equalr, propType, role.getURI() ); 1259 Element inverse = addElement( equalr, DIGProfile.INVERSE ); 1260 addNamedElement( inverse, propType, skolemName ); 1261 } 1262 1263 1264 1265 protected void translateAllDifferentAxioms( Element expr ) { 1266 if (m_sourceData.getProfile().ALL_DIFFERENT() != null) { 1267 for (Iterator i = m_sourceData.listAllDifferent(); i.hasNext(); ) { 1268 AllDifferent ad = (AllDifferent) ((Resource) i.next()).as( AllDifferent.class ); 1269 translateAllDifferent( expr, ad.getDistinctMembers() ); 1270 } 1271 } 1272 } 1273 1274 1275 1276 protected void translateAllDifferent( Element expr, RDFList diffMembers ) { 1277 List dm = diffMembers.asJavaList(); 1278 1279 for (int i = 0; i < dm.size(); i++) { 1280 Individual ind0 = (Individual) ((Resource) dm.get(i)).as( Individual.class ); 1281 1282 for (int j = i+1; j < dm.size(); j++) { 1283 Individual ind1 = (Individual) ((Resource) dm.get(j)).as( Individual.class ); 1284 translateDifferentIndividuals( expr, ind0, ind1 ); 1285 } 1286 } 1287 } 1288 1289 1290 1293 private boolean isIntegerType( RDFDatatype type ) { 1294 String typeURI = (type != null) ? type.getURI() : null; 1295 return typeURI != null && XSD_INT_TYPES.contains( typeURI ); 1296 } 1297 1298 1299 1300 private String getSkolemName( String root ) { 1301 return "skolem(" + root + "," + m_skolemCounter++ + ")"; 1302 1303 } 1304 1305 1306 1307 1311 protected Set getKnownIndividuals() { 1312 if (!m_indNamesAsked) { 1313 m_indNames.addAll( collectNamedTerms( DIGProfile.ALL_INDIVIDUALS, 1314 new String [] {DIGProfile.INDIVIDUAL_SET, DIGProfile.INDIVIDUAL} ) ); 1315 m_indNamesAsked = true; 1316 } 1317 1318 return m_indNames; 1319 } 1320 1321 1322 1326 protected Set getKnownConcepts() { 1327 if (!m_conceptNamesAsked) { 1328 m_conceptNames.addAll( collectNamedTerms( DIGProfile.ALL_CONCEPT_NAMES, 1329 new String [] {DIGProfile.CONCEPT_SET, DIGProfile.SYNONYMS, DIGProfile.CATOM} ) ); 1330 m_conceptNamesAsked = true; 1331 } 1332 1333 return m_conceptNames; 1334 } 1335 1336 1337 1341 protected Set getKnownRoles() { 1342 if (!m_roleNamesAsked) { 1343 m_roleNames.addAll( collectNamedTerms( DIGProfile.ALL_ROLE_NAMES, 1344 new String [] {DIGProfile.ROLE_SET, DIGProfile.SYNONYMS, DIGProfile.RATOM} ) ); 1345 m_roleNamesAsked = true; 1346 } 1347 1348 return m_roleNames; 1349 } 1350 1351 1352 1358 protected Set collectNamedTerms( String queryType, String [] path ) { 1359 Set names = new HashSet(); 1360 1361 Document query = getConnection().createDigVerb( DIGProfile.ASKS, getProfile() ); 1363 createQueryElement( query, queryType ); 1364 Document response = getConnection().sendDigVerb( query, getProfile() ); 1365 1366 SimpleXMLPath p = new SimpleXMLPath( true ); 1368 for (int j = 0; j < path.length; j++) { 1369 p.appendElementPath( path[j] ); 1370 } 1371 p.appendAttrPath( DIGProfile.NAME ); 1372 1373 addAll( p.getAll( response ), names ); 1375 1376 return names; 1377 } 1378 1379 1380 1381 private boolean isPremisesClass( com.hp.hpl.jena.graph.Node node, Model premises ) { 1382 RDFNode rdfNode = premises.getRDFNode( node ); 1383 Profile oProf = getOntLanguage(); 1384 1385 if (rdfNode instanceof Resource) { 1386 Resource r = (Resource) rdfNode; 1387 Resource any = null; 1388 1389 return 1390 ((oProf.CLASS() != null) && premises.contains( r, RDF.type, oProf.CLASS()) ) || 1391 ((oProf.RESTRICTION() != null) && premises.contains( r, RDF.type, oProf.RESTRICTION()) ) || 1392 ((oProf.SUB_CLASS_OF() != null) && premises.contains( r, oProf.SUB_CLASS_OF(), any ) ) || 1393 ((oProf.SUB_CLASS_OF() != null) && premises.contains( any, oProf.SUB_CLASS_OF(), r ) ) || 1394 ((oProf.UNION_OF() != null) && premises.contains( r, oProf.SUB_CLASS_OF(), any ) ) || 1395 ((oProf.INTERSECTION_OF() != null) && premises.contains( r, oProf.SUB_CLASS_OF(), any ) ) || 1396 ((oProf.COMPLEMENT_OF() != null) && premises.contains( r, oProf.SUB_CLASS_OF(), any ) ) || 1397 ((oProf.DISJOINT_WITH() != null) && premises.contains( r, oProf.DISJOINT_WITH(), any ) ) || 1398 ((oProf.EQUIVALENT_CLASS() != null) && premises.contains( r, oProf.EQUIVALENT_CLASS(), any )); 1399 } 1400 1401 return false; 1403 } 1404 1405 1406 private boolean isPremisesRole( com.hp.hpl.jena.graph.Node node, Model premises ) { 1407 RDFNode rdfNode = premises.getRDFNode( node ); 1408 Profile oProf = getOntLanguage(); 1409 1410 if (rdfNode instanceof Resource) { 1411 Resource r = (Resource) rdfNode; 1412 Resource any = null; 1413 1414 return 1415 ((oProf.PROPERTY() != null) && premises.contains( r, RDF.type, oProf.PROPERTY()) ) || 1416 ((oProf.OBJECT_PROPERTY() != null) && premises.contains( r, RDF.type, oProf.OBJECT_PROPERTY()) ) || 1417 ((oProf.DATATYPE_PROPERTY() != null) && premises.contains( r, RDF.type, oProf.DATATYPE_PROPERTY()) ) || 1418 ((oProf.TRANSITIVE_PROPERTY() != null) && premises.contains( r, RDF.type, oProf.TRANSITIVE_PROPERTY()) ) || 1419 ((oProf.FUNCTIONAL_PROPERTY() != null) && premises.contains( r, RDF.type, oProf.FUNCTIONAL_PROPERTY()) ) || 1420 ((oProf.INVERSE_FUNCTIONAL_PROPERTY() != null) && premises.contains( r, RDF.type, oProf.INVERSE_FUNCTIONAL_PROPERTY())) || 1421 ((oProf.SYMMETRIC_PROPERTY() != null) && premises.contains( r, RDF.type, oProf.SYMMETRIC_PROPERTY()) ) || 1422 ((oProf.SUB_PROPERTY_OF() != null) && premises.contains( r, oProf.SUB_PROPERTY_OF(), any ) ) || 1423 ((oProf.SUB_PROPERTY_OF() != null) && premises.contains( any, oProf.SUB_PROPERTY_OF(), r ) ) || 1424 ((oProf.INVERSE_OF() != null) && premises.contains( r, oProf.INVERSE_OF (), any ) ) || 1425 ((oProf.INVERSE_OF() != null) && premises.contains( any, oProf.INVERSE_OF (), r ) ); 1426 } 1427 1428 return false; 1430 } 1431 1432 1433 private com.hp.hpl.jena.graph.Node normaliseNode( com.hp.hpl.jena.graph.Node n ) { 1434 return n.isConcrete() ? n : com.hp.hpl.jena.graph.Node.ANY; 1435 } 1436 1437 1438 1442 1443 1444 1445 private class DigIdentifierImpl 1446 implements DIGIdentifier 1447 { 1448 private Document m_id; 1449 1450 private DigIdentifierImpl( Document id ) { 1451 m_id = id; 1452 } 1453 1454 public String getName() {return m_id.getDocumentElement().getAttribute( DIGProfile.NAME ); } 1455 public String getVersion() {return m_id.getDocumentElement().getAttribute( DIGProfile.VERSION ); } 1456 public String getMessage() {return m_id.getDocumentElement().getAttribute( DIGProfile.MESSAGE ); } 1457 1458 public Iterator supportsLanguage() {return supports( DIGProfile.LANGUAGE ); } 1459 public Iterator supportsTell() {return supports( DIGProfile.TELL ); } 1460 public Iterator supportsAsk() {return supports( DIGProfile.ASK ); } 1461 1462 private Iterator supports( String support ) { 1463 Element supports = getChild( m_id.getDocumentElement(), DIGProfile.SUPPORTS ); 1464 return childElementNames( getChild( supports, support ) ); 1465 } 1466 1467 1468 private Iterator childElementNames( Element node ) { 1469 ArrayList l = new ArrayList(); 1470 NodeList nl = node.getChildNodes(); 1471 1472 for (int i = 0; i < nl.getLength(); i++) { 1473 org.w3c.dom.Node n = nl.item(i); 1474 1475 if (n instanceof Element) { 1477 l.add( n.getNodeName() ); 1478 } 1479 } 1480 1481 return l.iterator(); 1482 } 1483 1484 1485 private Element getChild( Element node, String name ) { 1486 return (Element) node.getElementsByTagName( name ).item( 0 ); 1487 } 1488 } 1489} 1490 1491 1492 1518 | Popular Tags |