|
From: <tom...@us...> - 2008-04-18 18:39:45
|
Revision: 1227
http://jason.svn.sourceforge.net/jason/?rev=1227&view=rev
Author: tomklapiscak
Date: 2008-04-18 11:39:28 -0700 (Fri, 18 Apr 2008)
Log Message:
-----------
Correcting commit errors...
Added Paths:
-----------
trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/
trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/IncomingProtocolProcessingStrategy.java
trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/OutgoingProtocolProcessingStrategy.java
trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/ProtocolProcessingStrategy.java
trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/ProtocolProcessor.java
trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/package.html
Added: trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/IncomingProtocolProcessingStrategy.java
===================================================================
--- trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/IncomingProtocolProcessingStrategy.java (rev 0)
+++ trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/IncomingProtocolProcessingStrategy.java 2008-04-18 18:39:28 UTC (rev 1227)
@@ -0,0 +1,304 @@
+/*
+ * Copyright (C) 2008 Thomas Klapiscak (t.g...@du...)
+ *
+ * This file is part of JASDL.
+ *
+ * JASDL is free software: you can redistribute it and/or modify
+ * it under the terms of the Lesser GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * JASDL is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Lesser GNU General Public License for more details.
+ *
+ * You should have received a copy of the Lesser GNU General Public License
+ * along with JASDL. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package jasdl.bridge.protocol;
+
+import static jasdl.util.Common.strip;
+import jasdl.JASDLParams;
+import jasdl.bridge.JASDLOntologyManager;
+import jasdl.bridge.mapping.aliasing.Alias;
+import jasdl.bridge.mapping.aliasing.MappingStrategy;
+import jasdl.bridge.seliteral.SELiteral;
+import jasdl.util.exception.JASDLException;
+import jasdl.util.exception.JASDLMessageContentException;
+import jasdl.util.exception.JASDLNotEnrichedException;
+import jasdl.util.exception.JASDLUnknownMappingException;
+import jason.asSyntax.Atom;
+import jason.asSyntax.ListTerm;
+import jason.asSyntax.ListTermImpl;
+import jason.asSyntax.Literal;
+import jason.asSyntax.StringTerm;
+import jason.asSyntax.Structure;
+import jason.asSyntax.Term;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+
+import org.semanticweb.owl.model.OWLEntity;
+import org.semanticweb.owl.model.OWLException;
+import org.semanticweb.owl.model.OWLOntology;
+
+public class IncomingProtocolProcessingStrategy implements ProtocolProcessingStrategy {
+
+ private List<MappingStrategy> mappingStrategies;
+
+ public IncomingProtocolProcessingStrategy(List<MappingStrategy> mappingStrategies) {
+ this.mappingStrategies = mappingStrategies;
+ }
+
+ public Literal process(Literal l, JASDLOntologyManager jasdlOntologyManager) throws JASDLException {
+ try {
+ jasdlOntologyManager.getLogger().fine("Processing incoming " + l);
+
+ SELiteral sl = new SELiteral(l, jasdlOntologyManager); // <- NOTE: might not actually be an SE-Literal!
+ processAllDifferent(sl, jasdlOntologyManager);
+ processNamed(sl, jasdlOntologyManager);
+ processAnon(sl, jasdlOntologyManager);
+ processIndividuals(sl, jasdlOntologyManager); // <- MUST be performed last (since we need o annotation to be added)
+
+ return sl.getLiteral();
+ } catch (JASDLNotEnrichedException e) {
+ // do nothing
+ }
+ return l;
+
+ }
+
+ private void processAllDifferent(SELiteral sl, JASDLOntologyManager jasdlOntologyManager) throws JASDLMessageContentException {
+ List<Term> allDifferentAnnots = sl.getLiteral().getAnnots(JASDLParams.ALL_DIFFERENT_ANNOTATION_FUNCTOR).getAsList();
+ if (allDifferentAnnots.size() > 1) {
+ throw new JASDLMessageContentException("Multiple " + JASDLParams.ALL_DIFFERENT_ANNOTATION_FUNCTOR + " annotations preset");
+ } else if (allDifferentAnnots.size() == 1) {
+ Term _allDifferentAnnot = allDifferentAnnots.get(0);
+ if (!_allDifferentAnnot.isStructure())
+ throw new JASDLMessageContentException(_allDifferentAnnot + " must be a structure");
+ Structure allDifferentAnnot = (Structure) _allDifferentAnnot;
+
+ // get ontology
+ Term ontologyTerm = allDifferentAnnot.getTerm(0);
+ if (!(ontologyTerm instanceof StringTerm))
+ throw new JASDLMessageContentException("second term of " + JASDLParams.NAMED_ANNOTATION_FUNCTOR + " must be a string");
+ String _uri = ontologyTerm.toString();
+
+ // try loading the ontology, if fails create it (we don't know whether this ontology is personal or not here!)
+ OWLOntology ontology;
+ try {
+ ontology = jasdlOntologyManager.loadOntology(_uri, mappingStrategies);
+ } catch (JASDLException e) {
+ try {
+ ontology = jasdlOntologyManager.createOntology(_uri, true);
+ } catch (JASDLException e1) {
+ throw new JASDLMessageContentException("Unable to create ontology", e);
+ }
+ }
+ // add "o" annotation with label (may be anonymous if ontology was novel or personal)
+ Structure o = new Structure(JASDLParams.ONTOLOGY_ANNOTATION_FUNCTOR);
+ try {
+ o.addTerm(jasdlOntologyManager.getLabelManager().getLeft(ontology));
+ } catch (JASDLUnknownMappingException e) {
+ throw new JASDLMessageContentException("Ontology creation failed", e);
+ }
+ sl.getLiteral().addAnnot(o);
+
+ sl.getLiteral().delAnnot(allDifferentAnnot);
+
+ }
+ }
+
+ private void processIndividuals(SELiteral sl, JASDLOntologyManager jasdlOntologyManager) throws JASDLMessageContentException {
+ List<Term> iAnnots = sl.getLiteral().getAnnots(JASDLParams.I_ANNOTATION_FUNCTOR).getAsList();
+ if (iAnnots.size() > 1) {
+ throw new JASDLMessageContentException("Multiple " + JASDLParams.I_ANNOTATION_FUNCTOR + " annotations present");
+ } else if (iAnnots.size() == 1) {
+ try {
+ Term _iAnnot = iAnnots.get(0);
+ if (!_iAnnot.isStructure())
+ throw new JASDLMessageContentException(_iAnnot + " must be a structure");
+ Structure iAnnot = (Structure) _iAnnot;
+
+ if (sl.getLiteral().getFunctor().equals(JASDLParams.OWL_ALL_DIFFERENT_FUNCTOR.toString())) { // for all_different assertions
+ ListTerm uris = (ListTerm) iAnnot.getTerm(0);
+ ListTerm originals = (ListTerm) sl.getLiteral().getTerm(0); // must be a list term
+ ListTerm translations = new ListTermImpl();
+ int i = 0;
+ for (Term uriTerm : uris) {
+ try {
+ translations.add(translateIndividualURIStringTerm(uriTerm, jasdlOntologyManager));
+ } catch (JASDLUnknownMappingException e) {
+ // individual is not known, leave it as it is
+ translations.add(originals.get(i));
+ }
+ i++;
+ }
+ sl.getLiteral().setTerm(0, translations);
+ } else { // for class and property assertions
+ for (int i = 0; i < sl.getLiteral().getArity(); i++) { // for each ground atomic term - handles classes and object properies. terms of i annotation are relative to literal terms
+ Term term = sl.getLiteral().getTerm(i);
+ if (term.isGround() && term.isAtom()) {
+ // get the URI relative to this term
+ if (iAnnot.getArity() - 1 < i)
+ throw new JASDLMessageContentException("Insufficient arity");
+ try {
+ sl.getLiteral().setTerm(i, translateIndividualURIStringTerm(iAnnot.getTerm(i), jasdlOntologyManager));
+ } catch (JASDLUnknownMappingException e) {
+ // individual is not known, leave it as it is
+ }
+ }
+ }
+ }
+ sl.getLiteral().delAnnot(iAnnot); // drop i, no longer needed
+ } catch (JASDLMessageContentException e) {
+ throw new JASDLMessageContentException("Unable to translate individuals for " + sl, e);
+ }
+ }
+ }
+
+ private Atom translateIndividualURIStringTerm(Term _uriTerm, JASDLOntologyManager jasdlOntologyManager) throws JASDLMessageContentException, JASDLUnknownMappingException {
+ if (!_uriTerm.isString())
+ throw new JASDLMessageContentException(_uriTerm + " does not have a single string term");
+ StringTerm uriTerm = (StringTerm) _uriTerm;
+ URI uri;
+ try {
+ uri = new URI(strip(uriTerm.toString(), "\"")); // Quotes stripped
+ } catch (URISyntaxException e) {
+ throw new JASDLMessageContentException("Invalid URI supplied", e);
+ }
+
+ // if this agent is aware of an individual known by this uri, translate atom to local mapping (otherwise UnknownMappingException will be thrown)
+ OWLEntity entity = jasdlOntologyManager.toEntity(uri);
+ if (!entity.isOWLIndividual())
+ throw new JASDLMessageContentException(uri + " is not an individual");
+ Alias alias = jasdlOntologyManager.getAliasManager().getLeft(entity);
+ // replace the atom functor with that of local alias
+ return alias.getFunctor();
+ }
+
+ /**
+ * Applies processing to incoming SE-Literals possessing the "named" annotation, i.e. they refer to a class defined in the ontology schema.
+ * @param sl
+ * @param agent
+ * @return
+ * @throws JASDLException
+ */
+ private void processNamed(SELiteral sl, JASDLOntologyManager jasdlOntologyManager) throws JASDLException {
+ ListTerm nameds = sl.getLiteral().getAnnots(JASDLParams.NAMED_ANNOTATION_FUNCTOR);
+ if (nameds.size() == 1) {
+
+ // get named annotation
+ Term _named = nameds.get(0);
+ if (!(_named instanceof Structure)) {
+ throw new JASDLException("Invalid " + JASDLParams.NAMED_ANNOTATION_FUNCTOR + " annotation: " + _named);
+ }
+ Structure named = (Structure) _named;
+ if (named.getArity() != 2) {
+ throw new JASDLException("Invalid " + JASDLParams.NAMED_ANNOTATION_FUNCTOR + " annotation arity: " + _named);
+ }
+
+ // get ontology
+ Term ontologyTerm = named.getTerm(1);
+ if (!(ontologyTerm instanceof StringTerm))
+ throw new JASDLMessageContentException("second term of " + JASDLParams.NAMED_ANNOTATION_FUNCTOR + " must be a string");
+ String _uri = named.getTerm(1).toString();
+ OWLOntology ontology = jasdlOntologyManager.loadOntology(_uri, mappingStrategies); // may instantiate a new ontology with anonymous label
+
+ // add "o" annotation with label (may be anonymous if ontology was novel)
+ Structure o = new Structure(JASDLParams.ONTOLOGY_ANNOTATION_FUNCTOR);
+ o.addTerm(jasdlOntologyManager.getLabelManager().getLeft(ontology));
+ sl.getLiteral().addAnnot(o);
+
+ // get entity URI
+ Term expressionTerm = named.getTerm(0);
+ if (!(expressionTerm instanceof StringTerm)) {
+ throw new JASDLException("Invalid " + JASDLParams.NAMED_ANNOTATION_FUNCTOR + " annotation term: " + expressionTerm);
+ }
+ String expression = strip(expressionTerm.toString(), "\""); // quotes stripped
+
+ URI uri;
+ try {
+ uri = new URI(expression);
+ } catch (URISyntaxException e) {
+ throw new JASDLException("Invalid entity URI in " + expression);
+ }
+ Alias local = jasdlOntologyManager.getAliasManager().getLeft(jasdlOntologyManager.toEntity(uri)); // will already be present by definition
+
+ // create new (SE) Literal
+ sl.mutateFunctor(local.getFunctor().toString());
+
+ sl.getLiteral().delAnnot(named); // drop named, no longer needed
+ }
+ }
+
+ private void processAnon(SELiteral sl, JASDLOntologyManager jasdlOntologyManager) throws JASDLException {
+ ListTerm anons = sl.getLiteral().getAnnots(JASDLParams.ANON_ANNOTATION_FUNCTOR);
+ if (anons.size() == 1) {
+ // Get Anon annotation
+ Term _anon = anons.get(0);
+ if (!(_anon instanceof Structure)) {
+ throw new JASDLException("Invalid " + JASDLParams.ANON_ANNOTATION_FUNCTOR + " annotation: " + _anon);
+ }
+ Structure anon = (Structure) _anon;
+ if (anon.getArity() != 3) {
+ throw new JASDLException("Invalid " + JASDLParams.ANON_ANNOTATION_FUNCTOR + " annotation arity: " + _anon);
+ }
+
+ // Get expression
+ Term expressionTerm = anon.getTerm(0);
+ if (!(expressionTerm instanceof StringTerm)) {
+ throw new JASDLException("Invalid " + JASDLParams.ANON_ANNOTATION_FUNCTOR + " annotation expression term: " + expressionTerm);
+ }
+ String expression = strip(expressionTerm.toString(), "\""); // quotes stripped
+
+ // Parse prequisite ontology URIs
+ Term prereqsTerm = anon.getTerm(1);
+ if (!(prereqsTerm instanceof ListTerm)) {
+ throw new JASDLException("Invalid " + JASDLParams.ANON_ANNOTATION_FUNCTOR + " annotation prereqs term: " + expressionTerm);
+ }
+ ListTerm prereqs = (ListTerm) prereqsTerm;
+ for (Term _prereqTerm : prereqs) {
+ if (!(_prereqTerm instanceof StringTerm)) {
+ throw new JASDLException("Invalid " + JASDLParams.ANON_ANNOTATION_FUNCTOR + " prereq: " + _prereqTerm);
+ }
+ StringTerm prereqTerm = (StringTerm) _prereqTerm;
+ String prereq = strip(prereqTerm.toString(), "\""); // quotes stripped
+ jasdlOntologyManager.loadOntology(prereq, mappingStrategies); // <- may instantiate a new ontology
+ }
+
+ // Get (possibly instantiate) a personal ontology for this agent
+ Term personalURITerm = anon.getTerm(2);
+ if (!personalURITerm.isString())
+ throw new JASDLMessageContentException("third term of " + JASDLParams.ANON_ANNOTATION_FUNCTOR + " must be a string");
+ OWLOntology ontology;
+ try {
+ ontology = jasdlOntologyManager.getOntology(personalURITerm.toString());
+ } catch (JASDLUnknownMappingException e) {
+ // we have not recieved an anonymous class definition from this agent before, instantiate a personal placeholder ontology for them
+ ontology = jasdlOntologyManager.createOntology(personalURITerm.toString(), true);
+ }
+ // add "o" annotation
+ Structure o = new Structure(JASDLParams.ONTOLOGY_ANNOTATION_FUNCTOR);
+ o.addTerm(jasdlOntologyManager.getLabelManager().getLeft(ontology));
+ sl.getLiteral().addAnnot(o);
+
+ // do we already know this expression?
+ Atom functor = new Atom(sl.getLiteral().getFunctor());
+ try {
+ jasdlOntologyManager.defineClass(functor, sl.getOntologyLabel(), expression, jasdlOntologyManager.getManchesterURIDescriptionParser()); // Instantiate defined expression
+ } catch (OWLException e) {
+ // do nothing
+ throw new JASDLException("Error defining new class " + functor, e);
+ }
+
+ // drop anon annotation, no longer needed
+ sl.getLiteral().delAnnot(anon);
+ }
+
+ }
+
+}
Added: trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/OutgoingProtocolProcessingStrategy.java
===================================================================
--- trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/OutgoingProtocolProcessingStrategy.java (rev 0)
+++ trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/OutgoingProtocolProcessingStrategy.java 2008-04-18 18:39:28 UTC (rev 1227)
@@ -0,0 +1,206 @@
+/*
+ * Copyright (C) 2008 Thomas Klapiscak (t.g...@du...)
+ *
+ * This file is part of JASDL.
+ *
+ * JASDL is free software: you can redistribute it and/or modify
+ * it under the terms of the Lesser GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * JASDL is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Lesser GNU General Public License for more details.
+ *
+ * You should have received a copy of the Lesser GNU General Public License
+ * along with JASDL. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package jasdl.bridge.protocol;
+
+import jasdl.JASDLParams;
+import jasdl.bridge.JASDLOntologyManager;
+import jasdl.bridge.factory.SELiteralFactory;
+import jasdl.bridge.seliteral.SELiteral;
+import jasdl.bridge.seliteral.SELiteralAllDifferentAssertion;
+import jasdl.util.exception.JASDLException;
+import jasdl.util.exception.JASDLNotEnrichedException;
+import jasdl.util.exception.JASDLUnknownMappingException;
+import jason.asSyntax.ListTerm;
+import jason.asSyntax.ListTermImpl;
+import jason.asSyntax.Literal;
+import jason.asSyntax.StringTerm;
+import jason.asSyntax.StringTermImpl;
+import jason.asSyntax.Structure;
+import jason.asSyntax.Term;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.semanticweb.owl.model.OWLDescription;
+import org.semanticweb.owl.model.OWLEntity;
+import org.semanticweb.owl.model.OWLIndividual;
+import org.semanticweb.owl.model.OWLOntology;
+
+public class OutgoingProtocolProcessingStrategy implements ProtocolProcessingStrategy {
+
+ private SELiteralFactory SELiteralFactory;
+
+ public OutgoingProtocolProcessingStrategy(SELiteralFactory SELiteralFactory) {
+ this.SELiteralFactory = SELiteralFactory;
+ }
+
+ /**
+ * Only applies processing if l is SE-Literal, otherwise l is returned as is
+ * Replaces ontology label with physical uri
+ * Adds expr annotation unambiguously describing what is meant by alias
+ * Adds origin annotation if literal refers to defined class
+ * @param l
+ * @throws JASDLException
+ */
+ public Literal process(Literal l, JASDLOntologyManager jom) throws JASDLException {
+ jom.getLogger().fine("Processing outgoing " + l);
+
+ Literal result = l;
+ try {
+ SELiteral sl = SELiteralFactory.construct(l);
+
+ OWLEntity entity = (OWLEntity) sl.toOWLObject();
+ String expression = normaliseExpression(jom.getManchesterURIOWLObjectRenderer().render(entity), jom);
+
+ StringTerm expressionTerm = new StringTermImpl(expression);
+
+ if (entity.isOWLClass() && jom.getDefinitionManager().isKnownLeft(entity.asOWLClass())) { // we have an anonymous run-time defined class
+
+ // construct anon annotation
+ Structure anon = new Structure(JASDLParams.ANON_ANNOTATION_FUNCTOR);
+
+ // add the class expression
+ anon.addTerm(expressionTerm);
+
+ // add set of prerequisite ontologies
+ Set<OWLOntology> prereqs = new HashSet<OWLOntology>();
+ String[] tokens = expression.toString().split("[ |\n]");
+ for (String token : tokens) {
+ try {
+ URI entityURI = new URI(token);
+ URI ontologyURI = new URI(entityURI.getScheme(), entityURI.getSchemeSpecificPart(), null);
+ prereqs.add(jom.getLogicalURIManager().getLeft(ontologyURI));
+ } catch (URISyntaxException e) {
+ // do nothing, probably a keyword
+ } catch (JASDLUnknownMappingException e) {
+ // do nothing, probably a keyword
+ }
+ }
+ ListTerm list = new ListTermImpl();
+ for (OWLOntology prereq : prereqs) {
+ list.add(new StringTermImpl(jom.getPhysicalURIManager().getRight(prereq).toString()));
+ }
+ anon.addTerm(list);
+
+ // add the (personal) ontology this is from
+ StringTerm personalURI = new StringTermImpl(jom.getPhysicalURIManager().getRight(sl.getOntology()).toString());
+ anon.addTerm(personalURI);
+
+ sl.getLiteral().addAnnot(anon);
+
+ jom.getLogger().fine("Sending anonymous "+sl);
+
+ } else {
+
+ // unambiguously refer to named entity
+ StringTerm ontologyURITerm = new StringTermImpl(jom.getPhysicalURIManager().getRight(sl.getOntology()).toString());
+ if ((sl instanceof SELiteralAllDifferentAssertion)) {// special case for all_different
+ Structure all_different = new Structure(JASDLParams.ALL_DIFFERENT_ANNOTATION_FUNCTOR);
+ all_different.addTerm(ontologyURITerm);
+ sl.getLiteral().addAnnot(all_different);
+ } else {
+ Structure named = new Structure(JASDLParams.NAMED_ANNOTATION_FUNCTOR);
+ named.addTerm(expressionTerm);
+ // add ontology this naming is from
+ named.addTerm(ontologyURITerm);
+ sl.getLiteral().addAnnot(named);
+ }
+
+ }
+
+ // unambiguously refer to any nested individuals ("i" annotation). terms of "i" are relative to terms of literal
+ Structure iAnnot = new Structure(JASDLParams.I_ANNOTATION_FUNCTOR);
+ if (sl instanceof SELiteralAllDifferentAssertion) { // for all_different assertions
+ ListTerm translations = new ListTermImpl();
+ SELiteralAllDifferentAssertion ad = sl.asAllDifferentAssertion();
+ //Set<OWLIndividual> is = ad.getOWLIndividuals(); <- can't use this! We need to impose an ordering
+ ListTerm originals = (ListTerm) ad.getLiteral().getTerm(0);
+ for (Term original : originals) {
+ OWLIndividual i = sl.getOWLIndividual(original);
+ URI uri = i.getURI();
+ translations.add(new StringTermImpl(uri.toString()));
+ }
+ iAnnot.addTerm(translations);
+ } else { // for class and property assertions
+ for (int i = 0; i < sl.getLiteral().getArity(); i++) { // for each ground atomic term of literal
+ Term term = sl.getLiteral().getTerm(i);
+ if (term.isGround() && term.isAtom()) {
+ URI uri = sl.getOWLIndividual(i).getURI();
+ iAnnot.addTerm(new StringTermImpl(uri.toString()));
+ } else {
+ iAnnot.addTerm(JASDLParams.NO_INDIVIDUAL_MAPPING); // <- added so subject / objects mappings can be easily distinguished by maintenance of relative positions for object properties
+ }
+ }
+ }
+ sl.getLiteral().addAnnot(iAnnot);
+
+ sl.getLiteral().delAnnot(sl.getOntologyAnnotation()); // drop "o" before sending
+
+ result = sl.getLiteral();
+ } catch (JASDLNotEnrichedException e) {
+ // do nothing
+ //e.printStackTrace();
+ }
+ return result;
+ }
+
+ /**
+ * Returns an expression in which all references to run-time defined classes have been (recursuvely) replaced
+ * with the rendering of their anonymous descriptions, thus ensuring this rendering only refers to predefined classes.
+ * @param expression expression to normalise
+ * @param agent
+ * @return normalised form of expression
+ * @throws JASDLException
+ */
+ private String normaliseExpression(String expression, JASDLOntologyManager jasdlOntologyManager) throws JASDLException {
+ String[] tokens = expression.toString().split("[ |\n]");
+ String newExpression = "";
+ for (String token : tokens) {
+ try {
+ URI entityURI = new URI(token);
+ OWLEntity entity = jasdlOntologyManager.toEntity(entityURI);
+ if (entity.isOWLClass()) {
+ try {
+ OWLDescription desc = jasdlOntologyManager.getDefinitionManager().getRight(entity.asOWLClass());
+ String rendering = jasdlOntologyManager.getManchesterURIOWLObjectRenderer().render(desc);
+ newExpression += "(" + normaliseExpression(rendering, jasdlOntologyManager) + ")";
+ } catch (JASDLUnknownMappingException e1) {
+ // this is a predefined class
+ newExpression += token;
+ }
+ } else {
+ // this is a predefined non-class entity (property, individual, etc)
+ newExpression += token;
+ }
+ } catch (URISyntaxException e) {
+ // this is (probably) a keyword
+ newExpression += " " + token + " ";
+ } catch (JASDLUnknownMappingException e2) {
+ // this is (probably) a keyword
+ newExpression += " " + token + " ";
+ }
+
+ }
+ return newExpression;
+ }
+
+}
Added: trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/ProtocolProcessingStrategy.java
===================================================================
--- trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/ProtocolProcessingStrategy.java (rev 0)
+++ trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/ProtocolProcessingStrategy.java 2008-04-18 18:39:28 UTC (rev 1227)
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2008 Thomas Klapiscak (t.g...@du...)
+ *
+ * This file is part of JASDL.
+ *
+ * JASDL is free software: you can redistribute it and/or modify
+ * it under the terms of the Lesser GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * JASDL is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Lesser GNU General Public License for more details.
+ *
+ * You should have received a copy of the Lesser GNU General Public License
+ * along with JASDL. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package jasdl.bridge.protocol;
+
+import jasdl.bridge.JASDLOntologyManager;
+import jasdl.util.exception.JASDLException;
+import jason.asSyntax.Literal;
+
+public interface ProtocolProcessingStrategy {
+ public Literal process(Literal propcont, JASDLOntologyManager jasdlOntologyManager) throws JASDLException;
+}
Added: trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/ProtocolProcessor.java
===================================================================
--- trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/ProtocolProcessor.java (rev 0)
+++ trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/ProtocolProcessor.java 2008-04-18 18:39:28 UTC (rev 1227)
@@ -0,0 +1,92 @@
+package jasdl.bridge.protocol;
+
+import jasdl.bridge.JASDLOntologyManager;
+import jasdl.bridge.factory.SELiteralFactory;
+import jasdl.bridge.mapping.aliasing.MappingStrategy;
+import jasdl.util.exception.JASDLException;
+import jasdl.util.exception.JASDLMessageContentException;
+import jason.asSyntax.ListTerm;
+import jason.asSyntax.ListTermImpl;
+import jason.asSyntax.Literal;
+import jason.asSyntax.Structure;
+import jason.asSyntax.Term;
+import jason.asSyntax.VarTerm;
+
+import java.util.List;
+
+public class ProtocolProcessor {
+ private JASDLOntologyManager jom;
+
+ private ProtocolProcessingStrategy incomingStrategy;
+
+ private ProtocolProcessingStrategy outgoingStrategy;
+
+ /**
+ * Sets up using default processing strategies
+ *
+ */
+ public ProtocolProcessor(JASDLOntologyManager jom, List<MappingStrategy> mappingStrategies, SELiteralFactory seLiteralFactory) {
+ this.jom = jom;
+ this.incomingStrategy = new IncomingProtocolProcessingStrategy(mappingStrategies);
+ this.outgoingStrategy = new OutgoingProtocolProcessingStrategy(seLiteralFactory);
+ }
+
+ public ProtocolProcessor(JASDLOntologyManager jom, ProtocolProcessingStrategy incomingStrategy, ProtocolProcessingStrategy outgoingStrategy) {
+ super();
+ this.jom = jom;
+ this.incomingStrategy = incomingStrategy;
+ this.outgoingStrategy = outgoingStrategy;
+ }
+
+ public Structure processIncomingStructure(Structure struct) throws JASDLMessageContentException {
+ return processStructure(struct, incomingStrategy);
+ }
+
+ public Structure processOutgoingStructure(Structure struct) throws JASDLMessageContentException {
+ return processStructure(struct, outgoingStrategy);
+ }
+
+ private Structure processStructure(Structure struct, ProtocolProcessingStrategy strategy) throws JASDLMessageContentException {
+
+ // recurse down terms and lists (for "bundled" SE-enriched content)
+ if (struct.getArity() > 0) { // if we have any terms to process!
+ // Process outer SE-enriched content
+ if (struct.isLiteral()) { // processing strategies only apply to Literals
+ Literal l = (Literal) struct;
+ try {
+ struct = strategy.process(l, jom);
+ } catch (JASDLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ //int i=0;
+ //for(Term _term : struct.getTerms()){ - mustn't use in case struct is a list
+ if (struct.isVar()) {
+ struct = (Structure) ((VarTerm) struct).getValue(); // resolve struct if var
+ }
+ if (struct.isList()) { // special processing for lists (since we can't (or rather shouldn't)) use getTerm or setTerm)
+ ListTermImpl newList = new ListTermImpl(); // a "clone"
+ for (Term e : ((ListTerm) struct).getAsList()) {
+ jom.getLogger().finest("Processing " + e);
+ if (e instanceof Structure) {
+ Structure alteredTerm = (Structure) processStructure((Structure) e, strategy);
+ newList.append(alteredTerm);
+ }
+ }
+ return newList;
+ } else { // all other structures
+ for (int i = 0; i < struct.getArity(); i++) {
+ Term _term = struct.getTerm(i);
+ if (_term instanceof Structure) {
+ Structure term = (Structure) _term;
+ Structure alteredTerm = (Structure) processStructure(term, strategy);
+ struct.setTerm(i, alteredTerm); // modifies original struct
+ }
+ }
+ }
+ }
+
+ return struct;
+ }
+}
Added: trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/package.html
===================================================================
--- trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/package.html (rev 0)
+++ trunk/applications/jasdl-owlapi/src/jasdl/architecture/protocol/package.html 2008-04-18 18:39:28 UTC (rev 1227)
@@ -0,0 +1,5 @@
+<html>
+ <body>
+ Contains classes concerned with implementing JASDL's syntactic translation protocol. Currently used for SE-Message and SE-Percept translation.
+ </body>
+</html>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|